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
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
2020-12-24 01:28:38 +03:00
|
|
|
$type = $this->request->get('type', Model::INVOICE_TYPE);
|
|
|
|
|
2021-01-05 23:22:38 +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') {
|
2020-12-24 01:28:38 +03:00
|
|
|
$id = is_numeric($this->$type) ? $this->$type : $this->{$type}->getAttribute('id');
|
2017-09-14 22:21:00 +03:00
|
|
|
} else {
|
|
|
|
$id = null;
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$attachment = 'nullable';
|
|
|
|
|
|
|
|
if ($this->request->get('attachment', null)) {
|
|
|
|
$attachment = 'mimes:' . config('filesystems.mimes') . '|between:0,' . config('filesystems.max_size') * 1024;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
// Get company id
|
2020-01-13 17:15:20 +03:00
|
|
|
$company_id = $this->request->get('company_id');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
return [
|
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',
|
2020-12-24 01:28:38 +03:00
|
|
|
'issued_at' => 'required|date_format:Y-m-d H:i:s',
|
2018-09-05 16:43:50 +03:00
|
|
|
'due_at' => 'required|date_format:Y-m-d H:i:s',
|
2018-08-30 16:06:41 +03:00
|
|
|
'amount' => 'required',
|
2019-11-16 10:21:14 +03:00
|
|
|
'items.*.name' => 'required|string',
|
|
|
|
'items.*.quantity' => 'required',
|
|
|
|
'items.*.price' => 'required|amount',
|
2018-06-27 19:28:56 +03:00
|
|
|
'currency_code' => 'required|string|currency',
|
2018-06-23 15:59:13 +03:00
|
|
|
'currency_rate' => 'required',
|
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',
|
2019-11-16 10:21:14 +03:00
|
|
|
'attachment' => $attachment,
|
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()
|
|
|
|
{
|
|
|
|
return [
|
2019-11-16 10:21:14 +03:00
|
|
|
'items.*.name.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('general.name'))]),
|
|
|
|
'items.*.quantity.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('invoices.quantity'))]),
|
|
|
|
'items.*.price.required' => trans('validation.required', ['attribute' => mb_strtolower(trans('invoices.price'))]),
|
|
|
|
'items.*.currency.required' => trans('validation.custom.invalid_currency'),
|
|
|
|
'items.*.currency.string' => trans('validation.custom.invalid_currency'),
|
2018-09-03 13:24:27 +03:00
|
|
|
];
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|