From ac23baf625b0013414c9ab310abc9d098a690b34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Wed, 11 Nov 2026 17:16:42 +0100 Subject: [PATCH] feat: lock pooled rooms once the pool is used up --- src/Form/BookingCreateStep1Type.php | 35 ++++++ src/Model/RoomContingentPool.php | 18 ++++ .../Constraints/RoomSelectionValidator.php | 11 +- templates/booking/create/step_1.html.twig | 3 +- tests/Form/BookingCreateStep1TypeTest.php | 101 ++++++++++++++++++ 5 files changed, 157 insertions(+), 11 deletions(-) create mode 100644 tests/Form/BookingCreateStep1TypeTest.php diff --git a/src/Form/BookingCreateStep1Type.php b/src/Form/BookingCreateStep1Type.php index 8ad7c3c9..16991ca1 100644 --- a/src/Form/BookingCreateStep1Type.php +++ b/src/Form/BookingCreateStep1Type.php @@ -3,14 +3,22 @@ namespace App\Form; use App\Form\Model\BookingDto; +use App\Service\RoomContingentPoolResolver; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; /** @extends AbstractType */ class BookingCreateStep1Type extends AbstractType { + public function __construct( + private readonly RoomContingentPoolResolver $poolResolver, + ) { + } + public function buildForm(FormBuilderInterface $builder, array $options): void { $builder @@ -28,6 +36,33 @@ class BookingCreateStep1Type extends AbstractType ; } + /** + * Caps the step buttons of rooms sharing a contingent at what the other rooms leave of it. + * + * Runs on every render, including the htmx refresh after each change, so selecting one room + * of a pool locks the others once the pool is used up. Only the view changes: the choices + * keep the room's own count, and RoomSelectionValidator stays the check on submit. + */ + public function finishView(FormView $view, FormInterface $form, array $options): void + { + $bookingDto = $form->getData(); + $selectionViews = $view['roomSelections']; + + foreach ($this->poolResolver->findSharedPools($bookingDto->travel) as $pool) { + $remaining = max(0, $pool->getAvailable() - $pool->getSelectedQuantity($bookingDto->roomSelections)); + + foreach (array_keys($pool->rooms) as $roomId) { + if (!isset($selectionViews[$roomId]) || !isset($selectionViews[$roomId]['quantity'])) { + continue; + } + + $quantityView = $selectionViews[$roomId]['quantity']; + $quantity = $bookingDto->roomSelections[$roomId]->quantity ?? 0; + $quantityView->vars['max_value'] = min($quantityView->vars['max_value'], $quantity + $remaining); + } + } + } + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ diff --git a/src/Model/RoomContingentPool.php b/src/Model/RoomContingentPool.php index 4ab02baf..cdd9340d 100644 --- a/src/Model/RoomContingentPool.php +++ b/src/Model/RoomContingentPool.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Model; use App\BusProNet\Model\Room; +use App\Form\Model\RoomSelectionDto; /** * Offered rooms of one travel that draw on the same BusPro contingent. @@ -33,6 +34,23 @@ final readonly class RoomContingentPool return min(array_map(static fn (Room $room): int => $room->available ?? 0, $this->rooms)); } + /** + * Rooms the given selections take from the pool. + * + * @param iterable $roomSelections + */ + public function getSelectedQuantity(iterable $roomSelections): int + { + $quantity = 0; + foreach ($roomSelections as $roomSelection) { + if (null !== $roomSelection->id && $this->contains($roomSelection->id)) { + $quantity += $roomSelection->quantity ?? 0; + } + } + + return $quantity; + } + public function contains(int $roomId): bool { return isset($this->rooms[$roomId]); diff --git a/src/Validator/Constraints/RoomSelectionValidator.php b/src/Validator/Constraints/RoomSelectionValidator.php index bf259406..dbcbb182 100644 --- a/src/Validator/Constraints/RoomSelectionValidator.php +++ b/src/Validator/Constraints/RoomSelectionValidator.php @@ -57,17 +57,8 @@ class RoomSelectionValidator extends ConstraintValidator */ private function assertSharedContingentsNotExceeded(BookingDto $bookingDto, RoomSelection $constraint): void { - $selectedRooms = $bookingDto->getSelectedRooms(); - foreach ($this->poolResolver->findSharedPools($bookingDto->travel) as $pool) { - $selectedQuantity = 0; - foreach ($selectedRooms as $roomSelection) { - if (null !== $roomSelection->id && $pool->contains($roomSelection->id)) { - $selectedQuantity += $roomSelection->quantity ?? 0; - } - } - - if ($selectedQuantity > $pool->getAvailable()) { + if ($pool->getSelectedQuantity($bookingDto->getSelectedRooms()) > $pool->getAvailable()) { $this->context->buildViolation($constraint->sharedContingentExceededMessage) ->setParameter('{{ available }}', (string) $pool->getAvailable()) ->setParameter('{{ rooms }}', implode(', ', $pool->getLabels())) diff --git a/templates/booking/create/step_1.html.twig b/templates/booking/create/step_1.html.twig index 2c01b6d1..e9ac05be 100644 --- a/templates/booking/create/step_1.html.twig +++ b/templates/booking/create/step_1.html.twig @@ -3,7 +3,8 @@ {% set htmx_change_trigger = 'change delay:300ms' %} {% macro stepFormField(form, roomId, pool = null) %} -
+ {# A room whose shared contingent the other selected rooms have used up #} +
{{ form.vars.label_room }} diff --git a/tests/Form/BookingCreateStep1TypeTest.php b/tests/Form/BookingCreateStep1TypeTest.php new file mode 100644 index 00000000..ad7df0f9 --- /dev/null +++ b/tests/Form/BookingCreateStep1TypeTest.php @@ -0,0 +1,101 @@ +createView(poolAvailable: 1, quantities: [2366 => 1, 2367 => 0, 27 => 0]); + + self::assertSame(1, $this->maxValue($view, 2366), 'the selected room keeps its quantity'); + self::assertSame(0, $this->maxValue($view, 2367)); + self::assertSame(3, $this->maxValue($view, 27), 'a room outside the pool is untouched'); + } + + public function testRoomsOfAPoolWithRoomLeftShareWhatRemains(): void + { + $view = $this->createView(poolAvailable: 2, quantities: [2366 => 1, 2367 => 0, 27 => 0]); + + self::assertSame(2, $this->maxValue($view, 2366)); + self::assertSame(1, $this->maxValue($view, 2367)); + } + + public function testNothingSelectedLeavesEachRoomAtThePoolCount(): void + { + $view = $this->createView(poolAvailable: 1, quantities: [2366 => 0, 2367 => 0, 27 => 0]); + + self::assertSame(1, $this->maxValue($view, 2366)); + self::assertSame(1, $this->maxValue($view, 2367)); + } + + public function testAnOverSelectedPoolNeverCapsBelowTheSelection(): void + { + $view = $this->createView(poolAvailable: 1, quantities: [2366 => 1, 2367 => 1, 27 => 0]); + + self::assertSame(1, $this->maxValue($view, 2366)); + self::assertSame(1, $this->maxValue($view, 2367)); + } + + /** + * @param array $quantities + */ + private function createView(int $poolAvailable, array $quantities): FormView + { + $eight = $this->createRoom(2366, $poolAvailable); + $seven = $this->createRoom(2367, $poolAvailable); + $double = $this->createRoom(27, 3); + + $travel = new Travel(); + $travel->rooms = [$eight, $seven, $double]; + + $bookingDto = new BookingDto($travel, 163113); + foreach ([$eight, $seven, $double] as $room) { + $selection = new RoomSelectionDto(); + $selection->id = $room->id; + $selection->maxQuantity = (int) $room->available; + $selection->quantity = $quantities[$room->id]; + $bookingDto->roomSelections[$room->id] = $selection; + } + + $poolResolver = $this->createStub(RoomContingentPoolResolver::class); + $poolResolver->method('findSharedPools')->willReturn([new RoomContingentPool([2366 => $eight, 2367 => $seven])]); + + return Forms::createFormFactoryBuilder() + ->addExtension(new PreloadedExtension([new BookingCreateStep1Type($poolResolver)], [])) + ->getFormFactory() + ->create(BookingCreateStep1Type::class, $bookingDto) + ->createView(); + } + + private function maxValue(FormView $view, int $roomId): int + { + return $view['roomSelections'][$roomId]['quantity']->vars['max_value']; + } + + private function createRoom(int $id, int $available): Room + { + $room = new Room(); + $room->id = $id; + $room->label = 'Zimmer '.$id; + $room->available = $available; + $room->status = Constants::STATUS_AVAILABLE; + + return $room; + } +}