ldc-supportweb/app/Http/Requests/Bo/Credit/CreateEstimateRequest.php

72 lines
1.9 KiB
PHP
Raw Normal View History

2022-05-31 14:06:07 +02:00
<?php
namespace App\Http\Requests\Bo\Credit;
2022-05-31 14:06:07 +02:00
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 (fmod((\floatval($value) * 1000), 62.5) !== 0.0) {
2024-07-22 11:32:03 +02:00
$fail("$attribute doit être un multiple de 62.5 (soit 0.0625)");
2022-05-31 14:06:07 +02:00
}
}
],
'credit_account_id' => [
'required',
'exists:credit_accounts,id',
2022-05-31 14:06:07 +02:00
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 attributes(): array
2022-05-31 14:06:07 +02:00
{
return [
'amount' => 'Montant',
'credit_account_id' => 'Compte à débiter'
2022-05-31 14:06:07 +02:00
];
}
}