143 lines
4.5 KiB
PHP
143 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Bo\Orders;
|
|
|
|
use App\Lib\Helpers\RequestHelper;
|
|
use App\Queries\Orders\GetBoPaginatedOrdersQueryInput;
|
|
use Carbon\CarbonInterface;
|
|
use Domain\Enums\OrderStatus;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Validation\Rules\Enum;
|
|
use Infrastructure\Adapters\Services\AppAdminAuthService;
|
|
|
|
class IndexOrdersRequest extends FormRequest
|
|
{
|
|
public function __construct(
|
|
private readonly AppAdminAuthService $authService
|
|
) {
|
|
}
|
|
|
|
public function toGetBoPaginatedOrdersQueryInput(): GetBoPaginatedOrdersQueryInput
|
|
{
|
|
$session = $this->hasSession() ? $this->session() : null;
|
|
$status = $this->getStatus();
|
|
|
|
if ($session !== null) {
|
|
if ($this->isNotFilled('status')) {
|
|
$session->forget('orders.filter.status');
|
|
}
|
|
|
|
if ($this->has('status')) {
|
|
$session->forget('orders.filter.status');
|
|
$session->put('orders.filter.status', $status);
|
|
}
|
|
}
|
|
|
|
$search = $this->filled('search') ? $this->string('search')->value() : null;
|
|
|
|
return new GetBoPaginatedOrdersQueryInput(
|
|
pagination: RequestHelper::pagination($this),
|
|
search: $search,
|
|
status: $status,
|
|
orderDateStart: $this->getOrderDateStart(),
|
|
orderDateEnd: $this->getOrderDateEnd(),
|
|
orderPickupStart: $this->getOrderPickupStart(),
|
|
orderPickupEnd: $this->getOrderPickupEnd(),
|
|
);
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return $this->authService->check();
|
|
}
|
|
|
|
#[\Override]
|
|
protected function prepareForValidation(): void
|
|
{
|
|
// * Remove empty filter values to prevent date and enum validation failures on empty strings.
|
|
foreach (
|
|
[
|
|
'order_date_start',
|
|
'order_date_end',
|
|
'order_pickup_start',
|
|
'order_pickup_end',
|
|
'status',
|
|
] as $inputName
|
|
) {
|
|
if ($this->has($inputName) && empty($this->input($inputName))) {
|
|
$this->request->remove($inputName);
|
|
$this->query->remove($inputName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getStatus(): ?OrderStatus
|
|
{
|
|
if ($this->isNotFilled('status')) {
|
|
return null;
|
|
}
|
|
|
|
$value = $this->input('status');
|
|
|
|
if (is_numeric($value)) {
|
|
return OrderStatus::tryFrom((int) $value);
|
|
}
|
|
|
|
return $this->enum('status', OrderStatus::class);
|
|
}
|
|
|
|
public function getOrderDateStart(): ?CarbonInterface
|
|
{
|
|
return $this->has('order_date_start') ?
|
|
Date::parse($this->string('order_date_start')->value()) :
|
|
null;
|
|
}
|
|
|
|
public function getOrderDateEnd(): ?CarbonInterface
|
|
{
|
|
return $this->has('order_date_end') ?
|
|
Date::parse($this->string('order_date_end')->value()) :
|
|
null;
|
|
}
|
|
|
|
public function getOrderPickupStart(): ?CarbonInterface
|
|
{
|
|
return $this->has('order_pickup_start') ?
|
|
Date::parse($this->string('order_pickup_start')->value()) :
|
|
null;
|
|
}
|
|
|
|
public function getOrderPickupEnd(): ?CarbonInterface
|
|
{
|
|
return $this->has('order_pickup_end') ?
|
|
Date::parse($this->string('order_pickup_end')->value()) :
|
|
null;
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'status' => ['sometimes', 'nullable', new Enum(OrderStatus::class)],
|
|
'order_date_start' => ['required_with:order_date_end', 'sometimes', 'date'],
|
|
'order_date_end' => ['sometimes', 'date', 'after:order_date_start'],
|
|
'order_pickup_start' => ['required_with:order_pickup_end', 'sometimes', 'date'],
|
|
'order_pickup_end' => ['sometimes', 'date', 'after:order_pickup_start'],
|
|
];
|
|
}
|
|
|
|
#[\Override]
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'status' => trans('validation.custom.attributes.order_status'),
|
|
'order_date_start' => trans('validation.custom.attributes.order_date_start'),
|
|
'order_date_end' => trans('validation.custom.attributes.order_date_end'),
|
|
'order_pickup_start' => trans('validation.custom.attributes.order_pickup_start'),
|
|
'order_pickup_end' => trans('validation.custom.attributes.order_pickup_end'),
|
|
];
|
|
}
|
|
}
|