feat: auto-assign rooms

This commit is contained in:
Björn Fromme
2026-03-16 11:59:10 +01:00
parent 4489169553
commit 08d534e39b
2 changed files with 87 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Form\Model\BookingCreateDto;
/**
* Handles automatic room assignment for booking participants.
*
* This service automatically assigns participants to selected rooms based on room capacity
* and quantity, eliminating the need for manual room selection in the booking flow.
*/
class RoomAssignmentService
{
/**
* Automatically assigns participants to rooms based on selected room quantities and capacities.
*
* Assignment algorithm:
* - Iterates through selected rooms in order
* - For each room quantity, assigns minPax participants to that room ID
* - Ensures sequential assignment (participant 0, 1, 2, etc.)
*
* Example:
* - 2x "Doppelzimmer" (capacity 2) = participants 0-1 → room A, participants 2-3 → room A
* - 1x "3-Bett-Zimmer" (capacity 3) = participants 4-6 → room B
*
* @param BookingCreateDto $dto The booking DTO containing room selections and participants
*/
public function assignParticipantsToRooms(BookingCreateDto $dto): void
{
$participantIndex = 0;
$selectedRooms = $dto->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;
}
}
}
}
}
}