akaunting/app/Http/Requests/Banking/Transaction.php

102 lines
3.6 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-11-16 10:21:14 +03:00
namespace App\Http\Requests\Banking;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\FormRequest;
use App\Models\Banking\Transaction as Model;
use App\Utilities\Date;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
class Transaction extends FormRequest
2017-09-14 22:21:00 +03:00
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$type = $this->request->get('type', Model::INCOME_TYPE);
2019-11-16 10:21:14 +03:00
$type = config('type.transaction.' . $type . '.route.parameter');
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
// Check if store or update
if ($this->getMethod() == 'PATCH') {
2022-06-04 20:20:31 +03:00
$model = $this->isApi() ? 'transaction' : $type;
$id = is_numeric($this->$model) ? $this->$model : $this->{$model}->getAttribute('id');
2022-06-01 10:15:55 +03:00
} else {
$id = null;
}
// Get company id
2023-03-07 15:18:14 +03:00
$company_id = (int) $this->request->get('company_id', company_id());
2022-06-01 10:15:55 +03:00
$attachment = 'nullable';
if ($this->files->get('attachment')) {
$attachment = 'mimes:' . config('filesystems.mimes') . '|between:0,' . config('filesystems.max_size') * 1024;
}
$rules = [
2019-11-16 10:21:14 +03:00
'type' => 'required|string',
2022-06-01 10:15:55 +03:00
'number' => 'required|string|unique:transactions,NULL,' . $id . ',id,company_id,' . $company_id . ',deleted_at,NULL',
2017-09-14 22:21:00 +03:00
'account_id' => 'required|integer',
2018-09-05 16:43:50 +03:00
'paid_at' => 'required|date_format:Y-m-d H:i:s',
2022-12-20 16:33:32 +03:00
'amount' => 'required|amount:0',
'currency_code' => 'required|string|currency',
'currency_rate' => 'required|gt:0',
2019-11-16 10:21:14 +03:00
'document_id' => 'nullable|integer',
'contact_id' => 'nullable|integer',
2017-09-14 22:21:00 +03:00
'category_id' => 'required|integer',
'payment_method' => 'required|string',
2021-02-10 12:08:16 +03:00
'attachment.*' => $attachment,
'recurring_count' => 'gte:0',
'recurring_interval' => 'exclude_unless:recurring_frequency,custom|gt:0',
2017-09-14 22:21:00 +03:00
];
// Is Recurring
if ($this->request->has('recurring_frequency')) {
// first line of the recurring rule
if ($this->request->get('recurring_frequency') == 'custom') {
$rules['recurring_interval'] = 'required|gte:1';
$rules['recurring_custom_frequency'] = 'required|string|in:daily,weekly,monthly,yearly';
}
// second line of the recurring rule
$rules['recurring_started_at'] = 'required|date_format:Y-m-d H:i:s';
switch($this->request->get('recurring_limit')) {
case 'date':
$rules['recurring_limit_date'] = 'required|date_format:Y-m-d H:i:s|after_or_equal:recurring_started_at';
break;
case 'count':
$rules['recurring_limit_count'] = 'required|gte:0';
break;
}
}
return $rules;
2017-09-14 22:21:00 +03:00
}
public function withValidator($validator)
{
if ($validator->errors()->count()) {
$paid_at = Date::parse($this->request->get('paid_at'))->format('Y-m-d');
$this->request->set('paid_at', $paid_at);
if ($this->request->get('recurring_started_at')) {
$recurring_started_at = Date::parse($this->request->get('recurring_started_at'))->format('Y-m-d');
$this->request->set('recurring_started_at', $recurring_started_at);
}
if ($this->request->get('recurring_limit_date')) {
$recurring_limit_date = Date::parse($this->request->get('recurring_limit_date'))->format('Y-m-d');
$this->request->set('recurring_limit_date', $recurring_limit_date);
}
}
}
2017-09-14 22:21:00 +03:00
}