2021-11-03 11:18:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
2023-10-06 15:00:42 +02:00
|
|
|
namespace App\Http\Requests\Bo;
|
2021-11-03 11:18:19 +01:00
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
|
|
|
|
|
class StatsByBrandRequest extends FormRequest
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return boolean
|
2021-11-03 11:18:19 +01:00
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function authorize(): bool
|
2021-11-03 11:18:19 +01:00
|
|
|
{
|
2023-10-06 15:00:42 +02:00
|
|
|
return auth('backend')->check();
|
2021-11-03 11:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
2023-09-06 17:24:53 +02:00
|
|
|
* @throws \Illuminate\Validation\ValidationException If given dates are unparsable of period
|
|
|
|
|
* is bigger than 1 year.
|
2021-12-19 15:57:24 +01:00
|
|
|
* @throws \Exception If carbon date processing failed.
|
2021-11-03 11:18:19 +01:00
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function rules(): array
|
2021-11-03 11:18:19 +01:00
|
|
|
{
|
|
|
|
|
if ($this->post()) {
|
|
|
|
|
try {
|
|
|
|
|
if ((Carbon::parse($this->end)->diffInDays(Carbon::parse($this->start)) + 1) > 365) {
|
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
|
'Vous devez sélectionner une période de maximum 1 ans'
|
|
|
|
|
]);
|
|
|
|
|
}
|
2023-09-06 17:24:53 +02:00
|
|
|
} catch (\Exception $e) {
|
2021-11-03 11:18:19 +01:00
|
|
|
if (\get_class($e) !== ValidationException::class) {
|
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
|
'Les dates séléectionnées ne sembles pas être valides'
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'brand' => 'required|exists:brands,id',
|
|
|
|
|
'start' => 'required|date|date_format:Y-m-d',
|
2023-09-04 13:08:34 +02:00
|
|
|
'end' => 'required|date|date_format:Y-m-d|after_or_equal:start',
|
2021-11-03 11:18:19 +01:00
|
|
|
];
|
2023-06-22 17:25:52 +02:00
|
|
|
} //end if
|
2021-11-03 11:18:19 +01:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|