wip: booking process, room assignment

This commit is contained in:
Björn Fromme
2025-07-18 15:40:23 +02:00
parent eecea0abd1
commit 6feb8178b5
12 changed files with 454 additions and 201 deletions
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Controller\Booking;
use App\Form\Model\BookingCreateDto;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Provides common functionality for booking creation controllers.
*
* This trait contains shared validation and redirect logic used across
* all booking creation steps to ensure consistent behavior and reduce
* code duplication.
*/
trait BookingCreateTrait
{
/**
* Validates step access and redirects if necessary.
*
* @param BookingCreateDto $bookingCreateDto
* @param int $expectedStep
*/
private function validateStepAccess(BookingCreateDto $bookingCreateDto, int $expectedStep): void
{
// Allow access to current step or any previous step
if ($expectedStep > $bookingCreateDto->currentStep) {
$this->addFlash('error', 'Bitte erst die vorherigen Schritte abschließen.');
$this->redirectToCurrentStep($bookingCreateDto);
}
}
/**
* Redirects to the current step based on the DTO's currentStep.
*
* @param BookingCreateDto $bookingCreateDto
*/
private function redirectToCurrentStep(BookingCreateDto $bookingCreateDto): RedirectResponse
{
$routeParams = [
'date_id' => $bookingCreateDto->travelData->id,
'hotel_id' => $bookingCreateDto->travelData->hotelId,
];
$route = match ($bookingCreateDto->currentStep) {
1 => 'app_booking_create_step_1',
2 => 'app_booking_create_step_2',
3 => 'app_booking_create_step_3',
default => 'app_booking_create_step_1',
};
return $this->redirectToRoute($route, $routeParams);
}
}
-132
View File
@@ -1,132 +0,0 @@
<?php
namespace App\Controller\Booking;
use App\Form\BookingCreateStep1Type;
use App\Form\BookingCreateStep2Type;
use App\Form\Model\ParticipantDto;
use App\Service\BookingService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class CreateController extends AbstractController
{
public function __construct(
private readonly BookingService $bookingCreateService,
) {
}
#[Route('/bookings/create', name: 'app_booking_create_step_1')]
public function index(Request $request): Response
{
$bookingCreateDto = $this->bookingCreateService->getOrCreateBookingCreateDto($request);
// Validate step access - allow step 1 or redirect to current step
$this->validateStepAccess($bookingCreateDto, 1);
$form = $this->createForm(BookingCreateStep1Type::class, $bookingCreateDto, [
'validation_groups' => ['booking_create_step_1'],
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$bookingCreateDto->currentStep = 2;
$this->bookingCreateService->saveBookingCreateDto($request, $bookingCreateDto);
return $this->redirectToRoute('app_booking_create_step_2');
}
return $this->render('booking/create_step_1.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'form' => $form->createView(),
]);
}
#[Route('/bookings/create/participants', name: 'app_booking_create_step_2')]
public function participants(Request $request): Response
{
$bookingCreateDto = $this->bookingCreateService->getOrCreateBookingCreateDto($request);
// Validate step access
$this->validateStepAccess($bookingCreateDto, 2);
// Ensure correct number of participants
if ($bookingCreateDto->getParticipantsCount() !== count($bookingCreateDto->participants)) {
$participants = $bookingCreateDto->participants;
$bookingCreateDto->participants = [];
for ($i = 0; $i < $bookingCreateDto->getParticipantsCount(); ++$i) {
$bookingCreateDto->participants[] = $participants[$i] ?? new ParticipantDto();
}
}
$form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto, [
'attr' => ['novalidate' => 'novalidate'],
'validation_groups' => ['booking_create_step_2'],
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$bookingCreateDto->currentStep = 3;
$this->bookingCreateService->saveBookingCreateDto($request, $bookingCreateDto);
return $this->redirectToRoute('app_booking_create_step_3');
}
return $this->render('booking/create_step_2.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'form' => $form->createView(),
]);
}
#[Route('/bookings/create/confirm', name: 'app_booking_create_step_3')]
public function confirm(Request $request): Response
{
$bookingCreateDto = $this->bookingCreateService->getOrCreateBookingCreateDto($request);
// Validate step access
$this->validateStepAccess($bookingCreateDto, 3);
return $this->render('booking/confirm.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
]);
}
/**
* Validates step access and redirects if necessary.
*
* @param \App\Form\Model\BookingCreateDto $bookingCreateDto
*/
private function validateStepAccess($bookingCreateDto, int $expectedStep): void
{
// Allow access to current step or any previous step
if ($expectedStep > $bookingCreateDto->currentStep) {
$this->addFlash('error', 'Bitte erst die vorherigen Schritte abschließen.');
$this->redirectToCurrentStep($bookingCreateDto);
}
}
/**
* Redirects to the current step based on the DTO's currentStep.
*/
private function redirectToCurrentStep($bookingCreateDto): void
{
$routeParams = [
'date_id' => $bookingCreateDto->travelData->id,
'hotel_id' => $bookingCreateDto->travelData->hotelId,
];
$route = match ($bookingCreateDto->currentStep) {
1 => 'app_booking_create_step_1',
2 => 'app_booking_create_step_2',
3 => 'app_booking_create_step_3',
default => 'app_booking_create_step_1',
};
$this->redirectToRoute($route, $routeParams);
}
}
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Controller\Booking;
use App\Form\BookingCreateStep1Type;
use App\Service\BookingService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
/**
* Handles the first step of the booking creation process.
*
* This controller manages room selection functionality where users
* choose the types and quantities of rooms for their booking.
*/
class CreateStep1Controller extends AbstractController
{
use BookingCreateTrait;
public function __construct(
private readonly BookingService $bookingService,
) {
}
/**
* Displays and processes the room selection form.
*/
#[Route('/bookings/create', name: 'app_booking_create_step_1')]
public function index(Request $request): Response
{
$bookingCreateDto = $this->bookingService->getOrCreateBookingCreateDto($request);
$participantsCount = $this->bookingService->getParticipantsCount($bookingCreateDto->roomSelections, $bookingCreateDto->travelData);
// Validate step access - allow step 1 or redirect to current step
$this->validateStepAccess($bookingCreateDto, 1);
$form = $this->createForm(BookingCreateStep1Type::class, $bookingCreateDto, [
'validation_groups' => ['booking_create_step_1'],
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$bookingCreateDto->currentStep = 2;
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
return $this->redirectToRoute('app_booking_create_step_2');
}
return $this->render('booking/create_step_1.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'participantsCount' => $participantsCount,
'form' => $form->createView(),
]);
}
}
@@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
namespace App\Controller\Booking;
use App\Form\BookingCreateStep2Type;
use App\Form\Model\BookingCreateDto;
use App\Form\Model\ParticipantDto;
use App\Service\BookingService;
use App\Validator\Constraints\Participant;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
/**
* Handles the second step of the booking creation process.
*
* This controller manages participant information collection including
* dynamic room assignment functionality with HTMX-based form updates.
*/
class CreateStep2Controller extends AbstractController
{
use BookingCreateTrait;
public function __construct(
private readonly BookingService $bookingService,
) {
}
/**
* Displays and processes the participant information form.
*/
#[Route('/bookings/create/participants', name: 'app_booking_create_step_2')]
public function participants(Request $request): Response
{
$bookingCreateDto = $this
->bookingService
->getOrCreateBookingCreateDto($request);
// Validate step access
$this->validateStepAccess($bookingCreateDto, 2);
// Ensure correct number of participants
$this->ensureCorrectNumberOfParticipants($bookingCreateDto);
$participantsCount = $this->getParticipantsCount($bookingCreateDto);
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
$form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto, [
'attr' => ['novalidate' => 'novalidate'],
'validation_groups' => ['booking_create_step_2'],
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$bookingCreateDto->currentStep = 3;
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
return $this->redirectToRoute('app_booking_create_step_3');
}
return $this->render('booking/create_step_2.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'participantsCount' => $participantsCount,
'form' => $form->createView(),
]);
}
/**
* Handles HTMX requests for dynamic form updates when room selections change.
*/
#[Route('/bookings/create/participants/refresh', name: 'app_booking_create_step_2_refresh', methods: ['POST'])]
public function refreshParticipantForm(Request $request): Response
{
$bookingCreateDto = $this->bookingService->getOrCreateBookingCreateDto($request);
// Process form data without validation to capture current state
$form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto, [
'attr' => ['novalidate' => 'novalidate'],
'validation_groups' => false,
]);
$form->handleRequest($request);
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
return $this->renderBlock('booking/create_step_2.html.twig', 'participants_form', [
'form' => $form->createView(),
]);
}
private function getParticipantsCount(BookingCreateDto $bookingCreateDto): int
{
return $this
->bookingService
->getParticipantsCount($bookingCreateDto->roomSelections, $bookingCreateDto->travelData);
}
private function ensureCorrectNumberOfParticipants(BookingCreateDto $bookingCreateDto): void
{
$participantsCount = $this->getParticipantsCount($bookingCreateDto);
$participants = $bookingCreateDto->participants;
$bookingCreateDto->participants = [];
for ($i = 0; $i < $participantsCount; ++$i) {
$participant = $participants[$i] ?? new ParticipantDto();
$participant->index = $i;
$bookingCreateDto->participants[$i] = $participant;
}
}
}
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Controller\Booking;
use App\Service\BookingService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
/**
* Handles the third step of the booking creation process.
*
* This controller manages the booking confirmation and final review
* before completing the booking process.
*/
class CreateStep3Controller extends AbstractController
{
use BookingCreateTrait;
public function __construct(
private readonly BookingService $bookingCreateService,
) {
}
/**
* Displays the booking confirmation page.
*/
#[Route('/bookings/create/confirm', name: 'app_booking_create_step_3')]
public function confirm(Request $request): Response
{
$bookingCreateDto = $this->bookingCreateService->getOrCreateBookingCreateDto($request);
// Validate step access
$this->validateStepAccess($bookingCreateDto, 3);
return $this->render('booking/confirm.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
]);
}
}
+13
View File
@@ -10,6 +10,8 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BookingCreateParticipantType extends AbstractType
@@ -52,6 +54,16 @@ class BookingCreateParticipantType extends AbstractType
'label' => 'Telefon (mobil)',
'required' => false,
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
$data = $event->getData();
$form = $event->getForm();
$form->add('assignedRoomId', ChoiceType::class, [
'label' => 'Zimmer',
'placeholder' => 'Bitte wählen',
'choices' => $options['room_choices'][$data->index],
]);
})
;
}
@@ -59,6 +71,7 @@ class BookingCreateParticipantType extends AbstractType
{
$resolver->setDefaults([
'data_class' => ParticipantDto::class,
'room_choices' => [],
]);
}
}
+111 -7
View File
@@ -6,19 +6,123 @@ use App\Form\Model\BookingCreateDto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BookingCreateStep2Type extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('participants', CollectionType::class, [
'entry_type' => BookingCreateParticipantType::class,
'allow_add' => false,
'allow_delete' => false,
'by_reference' => false,
]);
$builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
$builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'onPreSubmit']);
}
public function onPreSetData(FormEvent $event): void
{
/** @var BookingCreateDto $bookingCreateDto */
$bookingCreateDto = $event->getData();
$form = $event->getForm();
if (null === $bookingCreateDto) {
return;
}
// Count participants assigned to each room
$roomOccupancy = [];
foreach ($bookingCreateDto->participants as $participant) {
if (null !== $participant->assignedRoomId) {
$roomOccupancy[$participant->assignedRoomId] = ($roomOccupancy[$participant->assignedRoomId] ?? 0) + 1;
}
}
// Generate per-participant room choices
$roomChoices = [];
foreach ($bookingCreateDto->participants as $index => $participant) {
$participantRoomChoices = [];
foreach ($bookingCreateDto->getSelectedRooms() as $roomSelection) {
$currentOccupancy = $roomOccupancy[$roomSelection->roomId] ?? 0;
// If this participant is already assigned to this room, exclude them from occupancy count
$adjustedOccupancy = $currentOccupancy;
if ($participant->assignedRoomId === $roomSelection->roomId) {
--$adjustedOccupancy;
}
$remainingCapacity = $roomSelection->minPax - $adjustedOccupancy;
// Include room if it has capacity OR if it's the participant's current assignment
if ($remainingCapacity > 0 || $participant->assignedRoomId === $roomSelection->roomId) {
$participantRoomChoices[$roomSelection->roomLabel] = $roomSelection->roomId;
}
}
$roomChoices[$index] = $participantRoomChoices;
}
// Create a custom form type that handles per-participant room choices
$form->add('participants', CollectionType::class, [
'entry_type' => BookingCreateParticipantType::class,
'allow_add' => false,
'allow_delete' => false,
'by_reference' => false,
'entry_options' => [
'room_choices' => $roomChoices,
],
]);
}
public function onPreSubmit(FormEvent $event): void
{
$data = $event->getData();
$form = $event->getForm();
/** @var BookingCreateDto $bookingCreateDto */
$bookingCreateDto = $form->getData();
$roomOccupancy = [];
foreach ($data['participants'] as $participant) {
if (null !== $participant['assignedRoomId']) {
$assignedRoomId = (int) $participant['assignedRoomId'];
$roomOccupancy[$assignedRoomId] = ($roomOccupancy[$assignedRoomId] ?? 0) + 1;
}
}
$roomChoices = [];
foreach ($data['participants'] as $index => $participant) {
$participantRoomChoices = [];
$assignedRoomId = (int) $participant['assignedRoomId'];
foreach ($bookingCreateDto->getSelectedRooms() as $roomSelection) {
$currentOccupancy = $roomOccupancy[$roomSelection->roomId] ?? 0;
// If this participant is already assigned to this room, exclude them from occupancy count
$adjustedOccupancy = $currentOccupancy;
if ($assignedRoomId === $roomSelection->roomId) {
--$adjustedOccupancy;
}
$remainingCapacity = $roomSelection->minPax - $adjustedOccupancy;
// Include room if it has capacity OR if it's the participant's current assignment
if ($remainingCapacity > 0 || $assignedRoomId === $roomSelection->roomId) {
$participantRoomChoices[$roomSelection->roomLabel] = $roomSelection->roomId;
}
}
$roomChoices[$index] = $participantRoomChoices;
}
$form->remove('participants');
$form->add('participants', CollectionType::class, [
'entry_type' => BookingCreateParticipantType::class,
'allow_add' => false,
'allow_delete' => false,
'by_reference' => false,
'entry_options' => [
'room_choices' => $roomChoices,
],
]);
}
public function configureOptions(OptionsResolver $resolver): void
+4 -38
View File
@@ -4,16 +4,15 @@ namespace App\Form\Model;
use App\BusProNet\Model\Travel;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class BookingCreateDto
{
public int $currentStep = 1;
#[Assert\Valid]
/**
* @var array<int, RoomSelectionDto>
*/
#[Assert\Valid]
public array $roomSelections = [];
/**
@@ -26,42 +25,9 @@ class BookingCreateDto
{
}
#[Assert\Callback(groups: ['booking_create_step_1'])]
public function assertValidRoomSelections(ExecutionContextInterface $context): void
{
$participantsCount = $this->getParticipantsCount();
$selectedContingent = 0;
foreach ($this->roomSelections as $roomSelection) {
$selectedContingent += $roomSelection->quantity * $roomSelection->minPax;
}
if (0 === $selectedContingent) {
$context->buildViolation('Bitte mindestens ein Zimmer auswählen.')
->atPath('roomSelections')
->addViolation();
}
if ($selectedContingent !== $participantsCount) {
$context->buildViolation('Die ausgewählten Zimmer passen nicht zur Teilnehmerzahl.')
->atPath('roomSelections')
->addViolation();
}
}
public function getParticipantsCount(): int
{
$participantsCount = 0;
$rooms = $this->travelData->getAvailableRooms();
foreach ($this->roomSelections as $roomSelection) {
$room = $rooms[$roomSelection->roomId];
$participantsCount += $room->minPax * $roomSelection->quantity;
}
return $participantsCount;
}
/**
* @return array<int, RoomSelectionDto>
*/
public function getSelectedRooms(): array
{
return array_filter($this->roomSelections, function (RoomSelectionDto $roomSelection) {
+4
View File
@@ -37,6 +37,10 @@ class ParticipantDto
public ?string $email = null;
public ?string $mobile = null;
#[Assert\NotNull(message: 'Bitte ein Zimmer auswählen', groups: ['booking_create_step_2'])]
public ?int $assignedRoomId = null;
public array $courses = [];
public array $additionalServices = [];
public array $skiPass = [];
+14
View File
@@ -3,6 +3,7 @@
namespace App\Service;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Travel;
use App\Form\Model\BookingCreateDto;
use App\Form\Model\RoomSelectionDto;
use App\Service\TravelDataService;
@@ -88,4 +89,17 @@ class BookingService
return array_map('intval', array_filter($rooms, 'strlen'));
}
public function getParticipantsCount(array $roomSelections, Travel $travelData): int
{
$participantsCount = 0;
$rooms = $travelData->getAvailableRooms();
foreach ($roomSelections as $roomSelection) {
$room = $rooms[$roomSelection->roomId];
$participantsCount += $room->minPax * $roomSelection->quantity;
}
return $participantsCount;
}
}