feat: cap rooms sharing a contingent per booking
This commit is contained in:
1 parent
5942ecf340
commit
3d94505e8d
18 files changed
+671
-19
No files matched your search
@@ -15,6 +15,7 @@ use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\ParticipantDto;
|
||||
use App\Form\Model\RoomSelectionDto;
|
||||
use App\Form\Service\ServiceAgeEvaluator;
|
||||
use App\Model\RoomContingentPool;
|
||||
use App\Model\RoomSelectionAdjustment;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -28,6 +29,7 @@ class BookingConfigurator
|
||||
private readonly ParticipantEligibilityChecker $participantEligibilityService,
|
||||
private readonly BookingStatusRuleRegistry $bookingStatusRuleRegistry,
|
||||
private readonly AgencyLoader $agencyLoader,
|
||||
private readonly RoomContingentPoolResolver $poolResolver,
|
||||
#[Autowire(param: 'default_booking_status')]
|
||||
private readonly string $defaultBookingStatus,
|
||||
) {
|
||||
@@ -102,16 +104,19 @@ class BookingConfigurator
|
||||
* exists; one that only loses part of its contingent leaves a quantity nothing else re-checks
|
||||
* before the booking is submitted; and one that becomes bookable again has no selection at all.
|
||||
* Rebuilding the selections from the current rooms, with the chosen quantities carried over and
|
||||
* capped at what is left, keeps both sides in step.
|
||||
* capped at what is left, keeps both sides in step. Rooms sharing a contingent are capped
|
||||
* together as well, in selection order, since BusPro reports the shared remainder on each.
|
||||
*
|
||||
* @return list<RoomSelectionAdjustment> The selections that had to give up quantity, in selection order
|
||||
*/
|
||||
public function reconcileRoomSelections(BookingDto $bookingDto): array
|
||||
{
|
||||
$availableRooms = $bookingDto->travel->getAvailableRooms();
|
||||
$pools = $this->poolResolver->findSharedPools($bookingDto->travel);
|
||||
|
||||
$adjustments = [];
|
||||
$quantitiesByRoomId = [];
|
||||
$poolRemainders = [];
|
||||
|
||||
foreach ($bookingDto->roomSelections as $roomSelection) {
|
||||
if (null === $roomSelection->id) {
|
||||
@@ -121,6 +126,13 @@ class BookingConfigurator
|
||||
$requestedQuantity = $roomSelection->quantity ?? 0;
|
||||
$bookableQuantity = min($requestedQuantity, $availableRooms[$roomSelection->id]->available ?? 0);
|
||||
|
||||
$poolIndex = $this->findPoolIndex($pools, $roomSelection->id);
|
||||
if (null !== $poolIndex) {
|
||||
$poolRemainders[$poolIndex] ??= $pools[$poolIndex]->getAvailable();
|
||||
$bookableQuantity = min($bookableQuantity, $poolRemainders[$poolIndex]);
|
||||
$poolRemainders[$poolIndex] -= $bookableQuantity;
|
||||
}
|
||||
|
||||
$quantitiesByRoomId[$roomSelection->id] = $bookableQuantity;
|
||||
|
||||
if (0 < $requestedQuantity && $bookableQuantity < $requestedQuantity) {
|
||||
@@ -141,6 +153,20 @@ class BookingConfigurator
|
||||
return $adjustments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<RoomContingentPool> $pools
|
||||
*/
|
||||
private function findPoolIndex(array $pools, int $roomId): ?int
|
||||
{
|
||||
foreach ($pools as $index => $pool) {
|
||||
if ($pool->contains($roomId)) {
|
||||
return $index;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a room selection DTO from room data and quantities.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user