Add Booking module: model, create/read/cancel API, Filament resource (T4.1-T4.7)
- Booking model with booking_vehicle_options line items (supports mixing vehicle options like front_seat + back_seat in one booking), price snapshot, status machine, and driver/car assignment fields - BookingService: front-seat max, disabled-option toggles, duplicate-option and whole-vehicle-exclusivity guards - CreateBookingAction, CancelBookingAction, AssignDriverAction - BookingRefGenerator: sequential EVB-AAAAA1-style refs via row lock - POST/GET/cancel booking API endpoints (Sanctum, ownership + admin policy) - BookingPlugin + Filament BookingResource: list, detail view, Cancel and Assign Driver actions (shared between table and detail page) - domain.md updated for multi-vehicle-option bookings (§2) and driver/ vehicle assignment (§5a)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Booking\Services;
|
||||
|
||||
use Modules\Booking\Models\Booking;
|
||||
|
||||
class BookingRefGenerator
|
||||
{
|
||||
private const PREFIX = 'EVB';
|
||||
|
||||
private const CHARS = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
/**
|
||||
* Must be called inside the same DB::transaction() as the booking insert
|
||||
* — the row lock on the latest booking is what keeps concurrent callers
|
||||
* from generating the same ref, and it only holds for the transaction's
|
||||
* lifetime.
|
||||
*/
|
||||
public function generate(): string
|
||||
{
|
||||
// Lock the latest row so concurrent transactions can't read the same ref.
|
||||
$latest = Booking::lockForUpdate()->orderByDesc('id')->value('booking_ref');
|
||||
|
||||
// If the latest ref doesn't match the expected format, start the sequence fresh.
|
||||
if ($latest && preg_match('/^[A-Z]+-[A-Z0-9]+$/', $latest)) {
|
||||
return $this->incrementRef($latest);
|
||||
}
|
||||
|
||||
return self::PREFIX.'-AAAAA1';
|
||||
}
|
||||
|
||||
private function incrementRef(string $ref): string
|
||||
{
|
||||
preg_match('/^(.*)-([A-Z0-9]+)$/', $ref, $matches);
|
||||
|
||||
$prefix = $matches[1];
|
||||
$suffix = str_split($matches[2]);
|
||||
$base = strlen(self::CHARS);
|
||||
$i = count($suffix) - 1;
|
||||
$carry = true;
|
||||
|
||||
while ($i >= 0 && $carry) {
|
||||
$idx = strpos(self::CHARS, $suffix[$i]);
|
||||
|
||||
if ($idx + 1 < $base) {
|
||||
$suffix[$i] = self::CHARS[$idx + 1];
|
||||
$carry = false;
|
||||
} else {
|
||||
$suffix[$i] = self::CHARS[0];
|
||||
}
|
||||
$i--;
|
||||
}
|
||||
|
||||
if ($carry) {
|
||||
array_unshift($suffix, self::CHARS[0]);
|
||||
}
|
||||
|
||||
return $prefix.'-'.implode('', $suffix);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Booking\Services;
|
||||
|
||||
use Modules\Booking\Data\VehicleSelectionData;
|
||||
use Modules\Booking\Exceptions\InvalidVehicleSelectionException;
|
||||
use Modules\Shared\Enums\VehicleOption;
|
||||
|
||||
class BookingService
|
||||
{
|
||||
/**
|
||||
* Enforces the only v1 inventory rule (max Front Seats per booking), the
|
||||
* blunt config toggles for Back Seat / Whole Vehicle availability, and
|
||||
* shape rules around combining options in one booking (no duplicate
|
||||
* option lines, Whole Vehicle can't be mixed with anything else since it
|
||||
* already covers the whole car).
|
||||
*
|
||||
* Deliberately does not check real capacity/availability — that's an
|
||||
* explicitly deferred future phase (domain.md §2, §7).
|
||||
*
|
||||
* @param list<VehicleSelectionData> $selections
|
||||
*
|
||||
* @throws InvalidVehicleSelectionException
|
||||
*/
|
||||
public function validateSelections(array $selections): void
|
||||
{
|
||||
$seen = [];
|
||||
|
||||
foreach ($selections as $selection) {
|
||||
if (isset($seen[$selection->vehicleOption->value])) {
|
||||
throw InvalidVehicleSelectionException::duplicateOption($selection->vehicleOption);
|
||||
}
|
||||
|
||||
$seen[$selection->vehicleOption->value] = true;
|
||||
|
||||
$this->validateOption($selection->vehicleOption, $selection->passengerCount);
|
||||
}
|
||||
|
||||
if (isset($seen[VehicleOption::WholeVehicle->value]) && count($seen) > 1) {
|
||||
throw InvalidVehicleSelectionException::wholeVehicleCannotBeCombined();
|
||||
}
|
||||
}
|
||||
|
||||
private function validateOption(VehicleOption $vehicleOption, int $passengerCount): void
|
||||
{
|
||||
match ($vehicleOption) {
|
||||
VehicleOption::FrontSeat => $this->validateFrontSeat($passengerCount),
|
||||
VehicleOption::BackSeat => $this->validateEnabled($vehicleOption, 'booking.back_seat_enabled'),
|
||||
VehicleOption::WholeVehicle => $this->validateEnabled($vehicleOption, 'booking.whole_vehicle_enabled'),
|
||||
};
|
||||
}
|
||||
|
||||
private function validateFrontSeat(int $passengerCount): void
|
||||
{
|
||||
$max = config('booking.front_seat_max_per_booking');
|
||||
|
||||
if ($passengerCount > $max) {
|
||||
throw InvalidVehicleSelectionException::frontSeatLimitExceeded($passengerCount, $max);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateEnabled(VehicleOption $vehicleOption, string $configKey): void
|
||||
{
|
||||
if (! config($configKey)) {
|
||||
throw InvalidVehicleSelectionException::optionDisabled($vehicleOption);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user