diff --git a/src/Controller/Booking/CreateStep2Controller.php b/src/Controller/Booking/CreateStep2Controller.php index 7d1ef6e..0bae619 100644 --- a/src/Controller/Booking/CreateStep2Controller.php +++ b/src/Controller/Booking/CreateStep2Controller.php @@ -10,6 +10,7 @@ use App\Form\Model\BookingCreateDto; use App\Form\Model\ParticipantDto; use App\Service\BookingPriceCalculatorService; use App\Service\BookingService; +use App\Service\RoomAssignmentService; use App\Service\TravelDataService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -32,6 +33,7 @@ class CreateStep2Controller extends AbstractController private readonly BookingService $bookingService, private readonly BookingPriceCalculatorService $priceCalculator, private readonly TravelDataService $travelDataService, + private readonly RoomAssignmentService $roomAssignmentService, ) { } @@ -59,6 +61,9 @@ class CreateStep2Controller extends AbstractController $this->ensureCorrectNumberOfParticipants($bookingCreateDto); $participantsCount = $this->getParticipantsCount($bookingCreateDto); + // Auto-assign participants to rooms if not already assigned + $this->autoAssignRoomsIfNeeded($bookingCreateDto); + // Pre-select mandatory services for participants with birth dates $this->bookingService->preselectMandatoryServices($bookingCreateDto); @@ -204,4 +209,28 @@ class CreateStep2Controller extends AbstractController $this->travelDataService->patchAvailabilities($bookingCreateDto->travel, $availabilities); } } + + /** + * Automatically assigns participants to rooms if they don't have room assignments yet. + * + * This is called when entering Step 2 to ensure all participants have room assignments + * based on the selected rooms from Step 1. Only assigns if participants are unassigned. + * + * @param BookingCreateDto $bookingCreateDto The booking DTO with participants and room selections + */ + private function autoAssignRoomsIfNeeded(BookingCreateDto $bookingCreateDto): void + { + // Check if any participants need room assignment + $needsAssignment = false; + foreach ($bookingCreateDto->participants as $participant) { + if (null === $participant->assignedRoomId) { + $needsAssignment = true; + break; + } + } + + if ($needsAssignment) { + $this->roomAssignmentService->assignParticipantsToRooms($bookingCreateDto); + } + } } diff --git a/src/Service/RoomAssignmentService.php b/src/Service/RoomAssignmentService.php new file mode 100644 index 0000000..35021b3 --- /dev/null +++ b/src/Service/RoomAssignmentService.php @@ -0,0 +1,58 @@ +getSelectedRooms(); + $availableRooms = $dto->travel->getAvailableRooms(); + + foreach ($selectedRooms as $roomSelection) { + $room = $availableRooms[$roomSelection->roomId] ?? null; + + if (null === $room) { + continue; // Skip if room not found + } + + $capacityPerRoom = $room->minPax ?? 1; + + // Assign participants for each room quantity + for ($i = 0; $i < $roomSelection->quantity; ++$i) { + // Assign participants according to room capacity + for ($j = 0; $j < $capacityPerRoom; ++$j) { + if (isset($dto->participants[$participantIndex])) { + $dto->participants[$participantIndex]->assignedRoomId = $room->id; + ++$participantIndex; + } + } + } + } + } +}