70 lines
2 KiB
PHP
70 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Codes;
|
|
|
|
use App\Models\Code;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
class ExportCodesRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Gate::check('export', Code::class) and $this->json();
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$codesAvailableToExport = Code::query()->where('code_exported', false);
|
|
/** @var integer|null */
|
|
$lastExportedId = optional(Code::query()->where('code_exported', true)
|
|
->orderBy('id', 'desc')->first())->id ?? null;
|
|
if ($lastExportedId) {
|
|
$codesAvailableToExport = $codesAvailableToExport
|
|
->where('id', '>', $lastExportedId);
|
|
}
|
|
$this->merge([
|
|
'available_export' => $codesAvailableToExport->count()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string,string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'nb_code_tot' => 'required|integer|min:1|lte:available_export',
|
|
'nb_code_fic' => 'required|integer|min:1',
|
|
'nb_code_ligne' => 'required|integer|min:1',
|
|
'available_export' => 'required|integer'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array<string,string>
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'nb_code_fic' => trans('nombre total de codes à exporter'),
|
|
'nb_code_tot' => trans('nombre de codes par fichier'),
|
|
'nb_code_ligne' => trans('nombre de codes par ligne'),
|
|
'available_export' => trans('nombre de codes restant à exporté(s)')
|
|
];
|
|
}
|
|
}
|