67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Credit;
|
|
|
|
use App\Models\Credit\Account;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class CreateEstimateRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
if (!auth()->check()) {
|
|
return false;
|
|
}
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
return $user->isAdmin() and $user->can('estimate', $this->ticket);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
/** @var \App\Models\Ticket */
|
|
$ticket = $this->ticket;
|
|
return [
|
|
'amount' => [
|
|
'required', 'numeric', 'gt:0', function ($attribute, $value, $fail) {
|
|
if ((\floatval($value) * 1000) % 125 !== 0) {
|
|
$fail("$attribute doit être un multiple de 125 (soit 0.125)");
|
|
}
|
|
}
|
|
],
|
|
'credit_account_id' => [
|
|
'required', 'exists:credit_accounts,id',
|
|
function ($attribute, $value, $fail) use ($ticket) {
|
|
/** @var \App\Models\Credit\Account|null */
|
|
$account = Account::whereId($value)->first();
|
|
if ($account and !$ticket->accountIsValidForThisTicket($account)) {
|
|
$fail("$attribute does not belong to the ticket brand");
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'amount' => 'Montant',
|
|
'credit_account_id' => 'Compte'
|
|
];
|
|
}
|
|
}
|