117 lines
4.2 KiB
PHP
Raw Normal View History

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
{
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);
2021-01-10 14:28:22 +03:00
$type = config('type.' . $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;
$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
$rules = [
2020-12-24 01:28:38 +03:00
'type' => 'required|string',
'document_number' => 'required|string|unique:documents,NULL,' . $id . ',id,type,' . $type . ',company_id,' . $company_id . ',deleted_at,NULL',
2020-01-11 16:57:32 +03:00
'status' => 'required|string',
2021-06-20 15:23:12 +03:00
'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',
2018-08-30 16:06:41 +03:00
'amount' => 'required',
2019-11-16 10:21:14 +03:00
'items.*.name' => 'required|string',
'items.*.price' => 'required|amount',
'currency_code' => 'required|string|currency',
'currency_rate' => 'required|gt:0',
2019-11-16 10:21:14 +03:00
'contact_id' => 'required|integer',
'contact_name' => 'required|string',
2018-04-23 22:17:20 +03:00
'category_id' => 'required|integer',
2021-01-29 11:04:15 +03:00
'company_logo' => $company_logo,
2021-01-22 12:38:17 +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
];
$items = $this->request->get('items');
if ($items) {
foreach ($items as $key => $item) {
$size = 5;
if (Str::contains($item['quantity'], ['.', ','])) {
$size = 7;
}
$rules['items.' . $key . '.quantity'] = 'required|max:' . $size;
$this->items_quantity_size[$key] = $size;
}
}
return $rules;
2017-09-14 22:21:00 +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');
$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);
$this->request->set('due_at', $due_at);
}
}
public function messages()
{
$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'),
];
if ($this->items_quantity_size) {
foreach ($this->items_quantity_size as $key => $quantity_size) {
$messages['items.' . $key . '.quantity.max'] = trans('validation.size', ['attribute' => Str::lower(trans('invoices.quantity')), 'size' => $quantity_size]);
}
}
return $messages;
}
2017-09-14 22:21:00 +03:00
}