Added default sorting by date in descending order for order queries. Updated related views and tests to reflect this change.
96 lines
3 KiB
PHP
96 lines
3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Queries\Orders;
|
|
|
|
use App\Contracts\Query;
|
|
use App\Contracts\QueryInput;
|
|
use App\Dto\OrderDto;
|
|
use App\Models\Order;
|
|
use App\Services\DataTableService;
|
|
use Domain\Enums\OrderStatus;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Query<\App\Queries\Orders\GetBoPaginatedOrdersQueryInput, \App\Queries\Orders\GetBoPaginatedOrdersQueryOutput>
|
|
*/
|
|
final readonly class GetBoPaginatedOrdersQuery implements Query
|
|
{
|
|
public function __construct(
|
|
private DataTableService $dataTable,
|
|
) {
|
|
}
|
|
|
|
#[\Override]
|
|
public function __invoke(QueryInput $input): GetBoPaginatedOrdersQueryOutput
|
|
{
|
|
Assert::isInstanceOf($input, GetBoPaginatedOrdersQueryInput::class);
|
|
|
|
$query = Order::query()->with(['shop']);
|
|
|
|
if (! empty($input->search)) {
|
|
$this->dataTable->searchQuery(
|
|
Order::class,
|
|
$query,
|
|
$input->search,
|
|
fn ($q) => $q->orWhereHas(
|
|
'shop',
|
|
fn ($q) => $q->whereLike('name', "%{$input->search}%")
|
|
),
|
|
'reference',
|
|
);
|
|
}
|
|
|
|
// * Date Order filter
|
|
if ($input->orderDateStart !== null) {
|
|
if ($input->orderDateEnd !== null) {
|
|
$query->whereCreatedAtBetween($input->orderDateStart, $input->orderDateEnd);
|
|
} else {
|
|
$query->whereCreatedAtBetween($input->orderDateStart, $input->orderDateStart);
|
|
}
|
|
}
|
|
|
|
// * Date Pickup filter
|
|
if ($input->orderPickupStart !== null) {
|
|
if ($input->orderPickupEnd !== null) {
|
|
$query->whereBetween('pickup_day', [
|
|
$input->orderPickupStart,
|
|
$input->orderPickupEnd,
|
|
]);
|
|
} else {
|
|
$query->where('pickup_day', $input->orderPickupStart);
|
|
}
|
|
}
|
|
|
|
if ($input->status !== null) {
|
|
$query->where('status', $input->status);
|
|
}
|
|
|
|
$this->dataTable->sortQuery(
|
|
Order::class,
|
|
$query,
|
|
defaultSortCol: 'date',
|
|
defaultSortWay: \SortDirection::Descending,
|
|
columnMap: ['date' => 'id'],
|
|
);
|
|
|
|
$paginator = $this->dataTable->paginateQuery($query, $input->pagination);
|
|
/** @var LengthAwarePaginator<int, OrderDto> $ordersDtoPaginator */
|
|
$ordersDtoPaginator = $paginator->through(fn (Order $order): OrderDto => OrderDto::fromModel($order));
|
|
|
|
$searchFields = str(collect([
|
|
trans('validation.custom.attributes.reference'),
|
|
trans_choice('models.classes.shops', 1),
|
|
])->implode(', '))->title()->value();
|
|
|
|
$orderStatusList = OrderStatus::cases();
|
|
|
|
return new GetBoPaginatedOrdersQueryOutput(
|
|
orders: $ordersDtoPaginator,
|
|
searchFields: $searchFields,
|
|
orderStatusList: $orderStatusList,
|
|
);
|
|
}
|
|
}
|