2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
namespace App\Http\Requests\Document;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Http\FormRequest;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Models\Document\Document as Model;
|
|
|
|
use App\Utilities\Date;
|
|
|
|
use Illuminate\Support\Str;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
class Document extends FormRequest
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2021-11-08 13:41:15 +03:00
|
|
|
protected $items_quantity_size = [];
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
2021-05-14 18:29:24 +03:00
|
|
|
$company_logo = 'nullable';
|
|
|
|
$attachment = 'nullable';
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
$type = $this->request->get('type', Model::INVOICE_TYPE);
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
$type = config('type.document.' . $type . '.route.parameter');
|
2020-12-24 01:28:38 +03:00
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
// Check if store or update
|
|
|
|
if ($this->getMethod() == 'PATCH') {
|
2021-02-17 22:38:11 +03:00
|
|
|
$model = $this->isApi() ? 'document' : $type;
|
2021-02-17 21:01:41 +03:00
|
|
|
|
|
|
|
$id = is_numeric($this->$model) ? $this->$model : $this->{$model}->getAttribute('id');
|
2017-09-14 22:21:00 +03:00
|
|
|
} else {
|
|
|
|
$id = null;
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:29:24 +03:00
|
|
|
if ($this->files->get('company_logo')) {
|
|
|
|
$company_logo = 'mimes:' . config('filesystems.mimes') . '|between:0,' . config('filesystems.max_size') * 1024 . '|dimensions:max_width=1000,max_height=1000';
|
2021-01-29 11:04:15 +03:00
|
|
|
}
|
|
|
|
|
2021-05-14 18:29:24 +03:00
|
|
|
if ($this->files->get('attachment')) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$attachment = 'mimes:' . config('filesystems.mimes') . '|between:0,' . config('filesystems.max_size') * 1024;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
// Get company id
|
2021-07-18 17:12:37 +03:00
|
|
|
$company_id = (int) $this->request->get('company_id');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2021-11-08 13:41:15 +03:00
|
|
|
$rules = [
|
2022-07-24 00:36:20 +03:00
|
|
|
'type' => 'required|string',
|
|
|
|
'document_number' => 'required|string|unique:documents,NULL,' . $id . ',id,type,' . $type . ',company_id,' . $company_id . ',deleted_at,NULL',
|
|
|
|
'status' => 'required|string',
|
|
|
|
'issued_at' => 'required|date_format:Y-m-d H:i:s|before_or_equal:due_at',
|
|
|
|
'due_at' => 'required|date_format:Y-m-d H:i:s|after_or_equal:issued_at',
|
|
|
|
'amount' => 'required',
|
|
|
|
'items.*.name' => 'required|string',
|
|
|
|
'items.*.price' => 'required|amount',
|
|
|
|
'currency_code' => 'required|string|currency',
|
|
|
|
'currency_rate' => 'required|gt:0',
|
|
|
|
'contact_id' => 'required|integer',
|
|
|
|
'contact_name' => 'required|string',
|
|
|
|
'category_id' => 'required|integer',
|
|
|
|
'company_logo' => $company_logo,
|
|
|
|
'attachment.*' => $attachment,
|
|
|
|
'recurring_count' => 'gte:0',
|
|
|
|
'recurring_interval' => 'exclude_unless:recurring_frequency,custom|gt:0',
|
2017-09-14 22:21:00 +03:00
|
|
|
];
|
2021-11-08 13:41:15 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
$items = $this->request->all('items');
|
2021-11-08 13:41:15 +03:00
|
|
|
|
|
|
|
if ($items) {
|
|
|
|
foreach ($items as $key => $item) {
|
2022-07-24 00:36:20 +03:00
|
|
|
$size = 5;
|
2021-11-08 13:41:15 +03:00
|
|
|
|
|
|
|
if (Str::contains($item['quantity'], ['.', ','])) {
|
|
|
|
$size = 7;
|
|
|
|
}
|
|
|
|
|
2022-01-17 17:49:00 +03:00
|
|
|
$rules['items.' . $key . '.quantity'] = 'required|max:' . $size;
|
2022-06-01 10:15:55 +03:00
|
|
|
|
2021-11-08 13:41:15 +03:00
|
|
|
$this->items_quantity_size[$key] = $size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rules;
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
2018-08-06 18:41:58 +03:00
|
|
|
|
|
|
|
public function withValidator($validator)
|
|
|
|
{
|
|
|
|
if ($validator->errors()->count()) {
|
|
|
|
// Set date
|
2020-12-24 01:28:38 +03:00
|
|
|
$issued_at = Date::parse($this->request->get('issued_at'))->format('Y-m-d');
|
2018-08-06 18:41:58 +03:00
|
|
|
$due_at = Date::parse($this->request->get('due_at'))->format('Y-m-d');
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
$this->request->set('issued_at', $issued_at);
|
2018-08-06 18:41:58 +03:00
|
|
|
$this->request->set('due_at', $due_at);
|
|
|
|
}
|
|
|
|
}
|
2018-09-03 13:24:27 +03:00
|
|
|
|
|
|
|
public function messages()
|
|
|
|
{
|
2021-11-08 13:41:15 +03:00
|
|
|
$messages = [
|
2021-02-17 22:40:35 +03:00
|
|
|
'items.*.name.required' => trans('validation.required', ['attribute' => Str::lower(trans('general.name'))]),
|
|
|
|
'items.*.quantity.required' => trans('validation.required', ['attribute' => Str::lower(trans('invoices.quantity'))]),
|
|
|
|
'items.*.price.required' => trans('validation.required', ['attribute' => Str::lower(trans('invoices.price'))]),
|
2019-11-16 10:21:14 +03:00
|
|
|
'items.*.currency.required' => trans('validation.custom.invalid_currency'),
|
|
|
|
'items.*.currency.string' => trans('validation.custom.invalid_currency'),
|
2018-09-03 13:24:27 +03:00
|
|
|
];
|
2021-11-08 13:41:15 +03:00
|
|
|
|
|
|
|
if ($this->items_quantity_size) {
|
|
|
|
foreach ($this->items_quantity_size as $key => $quantity_size) {
|
2022-01-17 17:49:00 +03:00
|
|
|
$messages['items.' . $key . '.quantity.max'] = trans('validation.size', ['attribute' => Str::lower(trans('invoices.quantity')), 'size' => $quantity_size]);
|
2021-11-08 13:41:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 00:36:20 +03:00
|
|
|
$messages['company_logo.dimensions'] = trans('validation.custom.invalid_dimension', [
|
|
|
|
'attribute' => Str::lower(trans('settings.company.logo')),
|
2022-07-24 00:40:51 +03:00
|
|
|
'width' => config('filesystems.max_width'),
|
|
|
|
'height' => config('filesystems.max_height'),
|
2022-07-24 00:36:20 +03:00
|
|
|
]);
|
|
|
|
|
2021-11-08 13:41:15 +03:00
|
|
|
return $messages;
|
2018-09-03 13:24:27 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|