100 lines
4.0 KiB
PHP
100 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Supervisor;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Supervisor\Concerns\ScopesSupervisorResources;
|
|
use App\Models\Booking;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class BookingPageController extends Controller
|
|
{
|
|
use ScopesSupervisorResources;
|
|
|
|
public function index(Request $request): Response
|
|
{
|
|
$query = Booking::query()
|
|
->with(['user:id,first_name,last_name,email', 'machine:id,uuid,name,establishment_id', 'machine.establishment:id,name']);
|
|
|
|
$this->scopeMachineRelationQuery($query);
|
|
$this->applyEstablishmentFilter($request, $query, 'machine');
|
|
$this->applyUserSearchFilter($request, $query);
|
|
|
|
if ($request->filled('status')) {
|
|
$query->where('status', $request->string('status'));
|
|
}
|
|
|
|
if ($request->filled('date')) {
|
|
$query->whereDate('slot_start', $request->string('date'));
|
|
}
|
|
|
|
$bookings = $query
|
|
->tap(fn ($builder) => $this->applySort($request, $builder, [
|
|
'slot_start' => 'bookings.slot_start',
|
|
'user' => fn ($builder, string $direction) => $this->sortByRelatedUser($builder, 'bookings', 'user_id', $direction),
|
|
'machine' => fn ($builder, string $direction) => $this->sortByRelatedMachine($builder, 'bookings', $direction),
|
|
'status' => 'bookings.status',
|
|
'booking_fee' => 'bookings.booking_fee',
|
|
], 'slot_start', 'desc'))
|
|
->paginate($this->supervisorListPerPage($request))
|
|
->withQueryString()
|
|
->through(fn (Booking $booking) => [
|
|
'uuid' => $booking->uuid,
|
|
'status' => $booking->status,
|
|
'status_label' => $this->statusLabel($booking->status),
|
|
'slot_start' => $booking->slot_start?->toIso8601String(),
|
|
'slot_end' => $booking->slot_end?->toIso8601String(),
|
|
'booking_fee' => (float) $booking->booking_fee,
|
|
'reserved_amount' => (float) $booking->reserved_amount,
|
|
'penalty_amount' => (float) $booking->penalty_amount,
|
|
'user' => $booking->user ? [
|
|
'name' => trim($booking->user->first_name.' '.$booking->user->last_name),
|
|
'email' => $booking->user->email,
|
|
] : null,
|
|
'machine' => $booking->machine ? [
|
|
'uuid' => $booking->machine->uuid,
|
|
'name' => $booking->machine->name,
|
|
'establishment_name' => $booking->machine->establishment?->name,
|
|
] : null,
|
|
]);
|
|
|
|
return Inertia::render('Supervisor/Bookings/Index', [
|
|
'bookings' => $bookings,
|
|
'establishments' => $this->establishmentOptions(),
|
|
'filters' => [
|
|
'establishment_id' => $request->filled('establishment_id') ? (int) $request->input('establishment_id') : null,
|
|
'user' => $request->string('user')->toString() ?: null,
|
|
'status' => $request->string('status')->toString() ?: null,
|
|
'date' => $request->string('date')->toString() ?: null,
|
|
'sort' => $request->string('sort')->toString() ?: 'slot_start',
|
|
'direction' => $request->string('direction')->toString() === 'asc' ? 'asc' : 'desc',
|
|
'per_page' => $this->supervisorListPerPage($request),
|
|
],
|
|
'statusOptions' => $this->statusOptions(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
private function statusOptions(): array
|
|
{
|
|
return [
|
|
'pending' => 'En attente',
|
|
'confirmed' => 'Confirmée',
|
|
'cancelled' => 'Annulée',
|
|
'expired' => 'Expirée',
|
|
'active' => 'Active',
|
|
'completed' => 'Terminée',
|
|
'no_show' => 'Absent',
|
|
];
|
|
}
|
|
|
|
private function statusLabel(string $status): string
|
|
{
|
|
return $this->statusOptions()[$status] ?? $status;
|
|
}
|
|
}
|