feat: auto-booking services

addresses #869cfcptv
This commit is contained in:
Björn Fromme
2026-03-16 20:24:58 +01:00
parent 6d9265de0f
commit c510cbf590
19 changed files with 1829 additions and 72 deletions
+3
View File
@@ -29,6 +29,9 @@ class Service
#[Groups(['api:single', 'api:list'])]
public bool $mandatory = false;
#[Groups(['api:single', 'api:list'])]
public bool $autoBook = false;
#[Groups(['api:single', 'api:list'])]
public ?string $label = null;
+3
View File
@@ -148,6 +148,9 @@ class TravelParser extends AbstractParser
$service->id = $serviceId;
$service->subType = $serviceNode->attr('unterart');
$service->mandatory = $this->stringToBool($serviceNode->attr('pflicht'));
$autoBookValue = $serviceNode->attr('automatisch_buchen');
$service->autoBook = $this->stringToBool($autoBookValue)
|| 'true' === strtolower((string) $autoBookValue);
$service->dateFrom = $this->stringToDate($serviceNode->attr('termin'));
$service->dateTo = $this->stringToDate($serviceNode->attr('bis'));
$service->label = $this->getStringOrNullValue($serviceNode->filterXPath('//text'));
@@ -85,7 +85,7 @@ class Step2Controller extends AbstractController
$this->roomAssignmentService->assignRoomsIfNeeded($bookingCreateDto);
// Preselect mandatory services
$this->bookingService->preselectMandatoryServices($bookingCreateDto);
$this->bookingService->preselectDefaultServices($bookingCreateDto);
// Save BookingDto to session
$this->bookingService->saveBookingDto($request, $bookingCreateDto, BookingDto::MODE_CREATE);
@@ -149,18 +149,19 @@ class Step2Controller extends AbstractController
$form = $this->createParticipantForm($bookingDto, $index);
$form->handleRequest($request);
$isSubmitted = $form->isSubmitted();
// Detect dummy data fill token — render pre-filled form immediately, skipping validation
$isDummyDataFill = $this
->dummyDataFillService
->isTokenMatch($bookingDto->participants[$index], $bookingDto->getMode())
;
if (true === $form->isSubmitted() && true === $isDummyDataFill) {
if (true === $isSubmitted && true === $isDummyDataFill) {
$this->dummyDataFillService->fill($bookingDto->participants[$index], $index);
// Keep behavior consistent with cards view: newly filled participant data
// (especially dateOfBirth) must immediately trigger mandatory service preselection.
$this->bookingService->preselectMandatoryServices($bookingDto);
$this->bookingService->preselectDefaultServices($bookingDto);
$bookingDto->bookingStatus = Constants::BOOKING_STATUS_OPEN;
$this->bookingService->saveBookingDto($request, $bookingDto, BookingDto::MODE_CREATE);
@@ -171,10 +172,16 @@ class Step2Controller extends AbstractController
return $this->renderParticipantForm($form, $index, $bookingDto);
}
if (true === $isSubmitted) {
// Re-run default preselection after participant form input changes (e.g. DOB).
// This ensures auto-book defaults are applied as soon as eligibility becomes known.
$this->bookingService->preselectDefaultServices($bookingDto);
}
// Collect notifications from field handlers (run during PRE_SUBMIT)
$notifications = $this->collectAndClearNotifications($bookingDto);
if (true === $form->isSubmitted() && true === $form->isValid()) {
if (true === $isSubmitted && true === $form->isValid()) {
// Save BookingDto to session
$this->bookingService->saveBookingDto($request, $bookingDto, BookingDto::MODE_CREATE);
@@ -122,13 +122,19 @@ trait ParticipantCardFlowTrait
int $index,
string $refreshRouteName,
): Response {
$refreshFormOptions = ['validation_groups' => false];
// Create form with validation disabled
$form = $this->createParticipantForm($bookingDto, $index, [
'validation_groups' => false,
]);
$form = $this->createParticipantForm($bookingDto, $index, $refreshFormOptions);
$form->handleRequest($request);
// Refresh endpoint is POST-only and always processes submitted participant data.
$this->bookingService->preselectDefaultServices($bookingDto);
// Recreate form so the rendered state reflects any new auto-preselections.
$form = $this->createParticipantForm($bookingDto, $index, $refreshFormOptions);
// Collect notifications from field handlers
$notifications = $this->collectAndClearNotifications($bookingDto);
+4
View File
@@ -86,6 +86,10 @@ class ParticipantDto
public array $courses = [];
public array $additionalServices = [];
public array $autoBookOptOutServiceIds = [];
public array $autoBookOptOutSkiPassIds = [];
public array $autoBookOptOutBoardIds = [];
public array $autoBookOptOutRentalIds = [];
// Note: Ski pass validation is conditional - see ParticipantEditDto::validateSkiPassRequired()
// Babies (0-2 years) are exempt from ski pass requirement
@@ -7,6 +7,7 @@ namespace App\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
@@ -96,6 +97,10 @@ class ParticipantAdditionalServicesFieldHandler extends AbstractParticipantField
return;
}
if (false === $participant instanceof ParticipantDto) {
return;
}
// Extract current service selections from submitted data
$selectedServices = $this->getFieldValue($submittedData, $this->getFieldName()) ?? [];
@@ -114,10 +119,66 @@ class ParticipantAdditionalServicesFieldHandler extends AbstractParticipantField
$participantIndex
);
$this->updateAutoBookOptOutServices(
$participant,
$availableServices,
$validSelections,
$bookingDto,
$participantIndex
);
// Update participant with validated selections
$participant->additionalServices = $validSelections;
}
private function updateAutoBookOptOutServices(
ParticipantDto $participant,
array $availableServices,
array $validSelections,
BookingDto $bookingDto,
int $participantIndex,
): void {
$currentlySelectedAutoBookIds = [];
foreach ($participant->additionalServices as $selectedService) {
if (false === $selectedService instanceof Service) {
continue;
}
if (null === $selectedService->id || true === $selectedService->mandatory || false === $selectedService->autoBook) {
continue;
}
if (false === $this->isServiceValidForParticipant($selectedService, $availableServices, $bookingDto, $participantIndex)) {
continue;
}
$currentlySelectedAutoBookIds[] = $selectedService->id;
}
$newlySelectedAutoBookIds = [];
foreach ($validSelections as $selectedService) {
if (null === $selectedService->id || true === $selectedService->mandatory || false === $selectedService->autoBook) {
continue;
}
$newlySelectedAutoBookIds[] = $selectedService->id;
}
// Add explicit user deselections to opt-out list
foreach ($currentlySelectedAutoBookIds as $serviceId) {
if (false === in_array($serviceId, $newlySelectedAutoBookIds, true)
&& false === in_array($serviceId, $participant->autoBookOptOutServiceIds, true)) {
$participant->autoBookOptOutServiceIds[] = $serviceId;
}
}
// Remove opt-out when user manually re-selects the service
$participant->autoBookOptOutServiceIds = array_values(array_filter(
$participant->autoBookOptOutServiceIds,
static fn (int $serviceId): bool => false === in_array($serviceId, $newlySelectedAutoBookIds, true)
));
}
/**
* Filters service selections to keep only those valid for the participant's age.
*
@@ -7,6 +7,7 @@ namespace App\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
@@ -57,6 +58,10 @@ class ParticipantBoardFieldHandler extends AbstractParticipantFieldHandler
return;
}
if (false === $participant instanceof ParticipantDto) {
return;
}
$selectedBoard = $this->getFieldValue($submittedData, $this->getFieldName()) ?? [];
// Get available board options from travel data (includes booked options in edit mode)
@@ -73,9 +78,63 @@ class ParticipantBoardFieldHandler extends AbstractParticipantFieldHandler
$participantIndex
);
$this->updateAutoBookOptOutBoard(
$participant,
$availableBoard,
$validSelections,
$bookingDto,
$participantIndex
);
$participant->board = $validSelections;
}
private function updateAutoBookOptOutBoard(
ParticipantDto $participant,
array $availableBoard,
array $validSelections,
BookingDto $bookingDto,
int $participantIndex,
): void {
$currentlySelectedAutoBookIds = [];
foreach ($participant->board as $selectedService) {
if (false === $selectedService instanceof Service) {
continue;
}
if (null === $selectedService->id || true === $selectedService->mandatory || false === $selectedService->autoBook) {
continue;
}
if (false === $this->isServiceValidForParticipant($selectedService, $availableBoard, $bookingDto, $participantIndex)) {
continue;
}
$currentlySelectedAutoBookIds[] = $selectedService->id;
}
$newlySelectedAutoBookIds = [];
foreach ($validSelections as $selectedService) {
if (null === $selectedService->id || true === $selectedService->mandatory || false === $selectedService->autoBook) {
continue;
}
$newlySelectedAutoBookIds[] = $selectedService->id;
}
foreach ($currentlySelectedAutoBookIds as $serviceId) {
if (false === in_array($serviceId, $newlySelectedAutoBookIds, true)
&& false === in_array($serviceId, $participant->autoBookOptOutBoardIds, true)) {
$participant->autoBookOptOutBoardIds[] = $serviceId;
}
}
$participant->autoBookOptOutBoardIds = array_values(array_filter(
$participant->autoBookOptOutBoardIds,
static fn (int $serviceId): bool => false === in_array($serviceId, $newlySelectedAutoBookIds, true)
));
}
private function filterValidServiceSelections(
array $selectedServices,
array $availableServices,
@@ -249,13 +249,26 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
$attributes = [];
// Make mandatory services readonly only when actually selected by participant.
if (true === $service->mandatory) {
$participant = $bookingDto->getParticipant($participantIndex);
$isSelected = null !== $participant
&& $this->hasServiceById($participant->board, $service->id);
if (true === $isSelected) {
$attributes['readonly'] = true;
$attributes['data-tooltip'] = 'Diese Leistung ist nicht abwählbar';
}
}
// Add service description as data attribute for frontend use
if (null !== $service->description && '' !== trim($service->description)) {
$attributes['data-description'] = $service->description;
}
// Make readonly if service is unavailable (intelligently handles edit mode)
if ($this->shouldMakeServiceReadonly($service, $bookingDto, $participantIndex, 'board')) {
// Make readonly if service is unavailable (only if not already mandatory)
if (false === $service->mandatory
&& $this->shouldMakeServiceReadonly($service, $bookingDto, $participantIndex, 'board')) {
$attributes['readonly'] = true;
$attributes['data-tooltip'] = 'ausgebucht';
}
@@ -361,13 +374,26 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
$attributes = [];
// Make mandatory services readonly only when actually selected by participant.
if (true === $service->mandatory) {
$participant = $bookingDto->getParticipant($participantIndex);
$isSelected = null !== $participant
&& $this->hasServiceById($participant->rentals, $service->id);
if (true === $isSelected) {
$attributes['readonly'] = true;
$attributes['data-tooltip'] = 'Diese Leistung ist nicht abwählbar';
}
}
// Add service description as data attribute for frontend use
if (null !== $service->description && '' !== trim($service->description)) {
$attributes['data-description'] = $service->description;
}
// Make readonly if service is unavailable (intelligently handles edit mode)
if ($this->shouldMakeServiceReadonly($service, $bookingDto, $participantIndex, 'rentals')) {
// Make readonly if service is unavailable (only if not already mandatory)
if (false === $service->mandatory
&& $this->shouldMakeServiceReadonly($service, $bookingDto, $participantIndex, 'rentals')) {
$attributes['readonly'] = true;
$attributes['data-tooltip'] = 'ausgebucht';
}
@@ -7,6 +7,7 @@ namespace App\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
@@ -65,6 +66,10 @@ class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
return;
}
if (false === $participant instanceof ParticipantDto) {
return;
}
// Store previous rental state BEFORE checking skipass
$previousRentals = $participant->rentals;
@@ -102,6 +107,14 @@ class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
$participantIndex
);
$this->updateAutoBookOptOutRentals(
$participant,
$durationFilteredRentals,
$validSelections,
$bookingDto,
$participantIndex
);
// Notify if rentals were cleared due to skipass duration change
// Only show notification if user had submitted rentals that got filtered out,
// not if the user intentionally cleared the selection (empty submission)
@@ -115,6 +128,52 @@ class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
$participant->rentals = $validSelections;
}
private function updateAutoBookOptOutRentals(
ParticipantDto $participant,
array $availableRentals,
array $validSelections,
BookingDto $bookingDto,
int $participantIndex,
): void {
$currentlySelectedAutoBookIds = [];
foreach ($participant->rentals as $selectedService) {
if (false === $selectedService instanceof Service) {
continue;
}
if (null === $selectedService->id || true === $selectedService->mandatory || false === $selectedService->autoBook) {
continue;
}
if (false === $this->isServiceValidForParticipant($selectedService, $availableRentals, $bookingDto, $participantIndex)) {
continue;
}
$currentlySelectedAutoBookIds[] = $selectedService->id;
}
$newlySelectedAutoBookIds = [];
foreach ($validSelections as $selectedService) {
if (null === $selectedService->id || true === $selectedService->mandatory || false === $selectedService->autoBook) {
continue;
}
$newlySelectedAutoBookIds[] = $selectedService->id;
}
foreach ($currentlySelectedAutoBookIds as $serviceId) {
if (false === in_array($serviceId, $newlySelectedAutoBookIds, true)
&& false === in_array($serviceId, $participant->autoBookOptOutRentalIds, true)) {
$participant->autoBookOptOutRentalIds[] = $serviceId;
}
}
$participant->autoBookOptOutRentalIds = array_values(array_filter(
$participant->autoBookOptOutRentalIds,
static fn (int $serviceId): bool => false === in_array($serviceId, $newlySelectedAutoBookIds, true)
));
}
private function filterValidServiceSelections(
array $selectedServices,
array $availableServices,
@@ -7,6 +7,7 @@ namespace App\Form\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
/**
@@ -91,6 +92,10 @@ class ParticipantSkiPassFieldHandler extends AbstractParticipantFieldHandler
return;
}
if (false === $participant instanceof ParticipantDto) {
return;
}
// Extract current skipass selection from submitted data
$selectedSkiPass = $this->getFieldValue($submittedData, $this->getFieldName());
@@ -102,6 +107,13 @@ class ParticipantSkiPassFieldHandler extends AbstractParticipantFieldHandler
true
);
$currentlySelectedAutoBookSkiPassId = $this->getSelectedAutoBookSkiPassId(
$participant,
$availableSkipasses,
$bookingDto,
$participantIndex
);
// For single selection, validate the selected skipass and convert ID to Service object
$validSelection = null;
if (null !== $selectedSkiPass) {
@@ -111,10 +123,61 @@ class ParticipantSkiPassFieldHandler extends AbstractParticipantFieldHandler
}
}
$this->updateAutoBookOptOutSkiPasses($participant, $currentlySelectedAutoBookSkiPassId, $validSelection);
// Update participant with validated selection
$participant->skiPass = $validSelection;
}
private function getSelectedAutoBookSkiPassId(
ParticipantDto $participant,
array $availableSkipasses,
BookingDto $bookingDto,
int $participantIndex,
): ?int {
$currentSkiPass = $participant->skiPass;
if (null === $currentSkiPass || null === $currentSkiPass->id) {
return null;
}
if (true === $currentSkiPass->mandatory || false === $currentSkiPass->autoBook) {
return null;
}
if (false === $this->isServiceValidForParticipant($currentSkiPass, $availableSkipasses, $bookingDto, $participantIndex)) {
return null;
}
return $currentSkiPass->id;
}
private function updateAutoBookOptOutSkiPasses(
ParticipantDto $participant,
?int $currentlySelectedAutoBookSkiPassId,
?Service $validSelection,
): void {
$newlySelectedAutoBookSkiPassId = null;
if (null !== $validSelection
&& null !== $validSelection->id
&& false === $validSelection->mandatory
&& true === $validSelection->autoBook) {
$newlySelectedAutoBookSkiPassId = $validSelection->id;
}
if (null !== $currentlySelectedAutoBookSkiPassId
&& $currentlySelectedAutoBookSkiPassId !== $newlySelectedAutoBookSkiPassId
&& false === in_array($currentlySelectedAutoBookSkiPassId, $participant->autoBookOptOutSkiPassIds, true)) {
$participant->autoBookOptOutSkiPassIds[] = $currentlySelectedAutoBookSkiPassId;
}
if (null !== $newlySelectedAutoBookSkiPassId) {
$participant->autoBookOptOutSkiPassIds = array_values(array_filter(
$participant->autoBookOptOutSkiPassIds,
static fn (int $serviceId): bool => $serviceId !== $newlySelectedAutoBookSkiPassId
));
}
}
/**
* Validates if a selected skipass is still valid for the participant.
*
+435 -60
View File
@@ -6,12 +6,14 @@ namespace App\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\BusProNet\XmlLoader\AgencyLoader;
use App\Exception\BookingSessionNotFoundException;
use App\Exception\NoRoomsAvailableException;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Service\ServiceAgeEvaluator;
use App\Form\Model\RoomSelectionDto;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
@@ -460,72 +462,445 @@ class BookingService
}
/**
* Pre-selects mandatory services for all participants in the booking DTO.
* Pre-selects default services for all participants.
*
* This method ensures that mandatory services are selected before form rendering
* and pricing calculations, resolving timing issues where mandatory services
* were only selected during form rendering via choice_attr callbacks.
*
* Mandatory services are only preselected for eligible participants - those who
* have at least one skipass available for their age. Ineligible participants are
* skipped to prevent their mandatory services from being included in pricing.
*
* @param BookingDto $bookingDto The booking DTO to update with mandatory services
* Mandatory and auto-book rules are handled in dedicated methods:
* mandatory first, auto-book second.
*/
public function preselectDefaultServices(BookingDto $bookingDto): void
{
$additionalServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_ADDITIONAL);
$mandatoryAdditionalServices = array_filter(
$additionalServices,
static fn (Service $service): bool => true === $service->mandatory
);
$autoBookAdditionalServices = array_filter(
$additionalServices,
// Services that are both mandatory and auto-book belong to mandatory bucket only.
static fn (Service $service): bool => true === $service->autoBook && false === $service->mandatory
);
$skiPassServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true);
$mandatorySkiPassServices = array_filter(
$skiPassServices,
static fn (Service $service): bool => true === $service->mandatory
);
$autoBookSkiPassServices = array_filter(
$skiPassServices,
// Services that are both mandatory and auto-book belong to mandatory bucket only.
static fn (Service $service): bool => true === $service->autoBook && false === $service->mandatory
);
$boardServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_BOARD, true);
$mandatoryBoardServices = array_filter(
$boardServices,
static fn (Service $service): bool => true === $service->mandatory
);
$autoBookBoardServices = array_filter(
$boardServices,
// Services that are both mandatory and auto-book belong to mandatory bucket only.
static fn (Service $service): bool => true === $service->autoBook && false === $service->mandatory
);
$rentalServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_RENTALS, true);
$mandatoryRentalServices = array_filter(
$rentalServices,
static fn (Service $service): bool => true === $service->mandatory
);
$autoBookRentalServices = array_filter(
$rentalServices,
// Services that are both mandatory and auto-book belong to mandatory bucket only.
static fn (Service $service): bool => true === $service->autoBook && false === $service->mandatory
);
$ageEvaluator = new ServiceAgeEvaluator();
foreach ($bookingDto->participants as $participantIndex => $participant) {
if (false === $this->canPreselectServicesForParticipant($bookingDto, $participantIndex, $participant)) {
continue;
}
$this->preselectMandatoryAdditionalServices(
$participant,
$mandatoryAdditionalServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->preselectAutoBookAdditionalServices(
$participant,
$autoBookAdditionalServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->preselectMandatorySkiPass(
$participant,
$mandatorySkiPassServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->preselectAutoBookSkiPass(
$participant,
$autoBookSkiPassServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->preselectMandatoryBoardServices(
$participant,
$mandatoryBoardServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->preselectAutoBookBoardServices(
$participant,
$autoBookBoardServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->preselectMandatoryRentals(
$participant,
$mandatoryRentalServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->preselectAutoBookRentals(
$participant,
$autoBookRentalServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
}
}
/**
* @deprecated Use preselectDefaultServices() instead.
*/
public function preselectMandatoryServices(BookingDto $bookingDto): void
{
$additionalServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_ADDITIONAL);
$mandatoryServices = array_filter($additionalServices, fn ($service) => true === $service->mandatory);
$this->preselectDefaultServices($bookingDto);
}
// Pre-select mandatory services for each participant
foreach ($bookingDto->participants as $participantIndex => $participant) {
if (null === $participant->dateOfBirth) {
continue; // Skip participants without age information
}
// Skip babies - they only get services with explicit age ranges (handled by field filtering)
$age = $participant->getAge($bookingDto->travel->dateFrom);
if (null !== $age && $age <= Constants::BABY_MAX_AGE) {
continue;
}
// Skip ineligible participants (no skipasses available for their age)
if (false === $this->participantEligibilityService->isParticipantEligible($bookingDto, $participantIndex)) {
continue;
}
// Get age-appropriate mandatory services for this participant
$ageAppropriateServices = array_filter($mandatoryServices, function ($service) use ($bookingDto, $participant) {
if (null === $service->ageFrom && null === $service->ageTo) {
return true; // No age restrictions
}
// participant's age at travel date is relevant
$age = $participant->getAge($bookingDto->travel->dateFrom);
if (null !== $service->ageFrom && $age < $service->ageFrom) {
return false;
}
if (null !== $service->ageTo && $age > $service->ageTo) {
return false;
}
return true;
});
// Add mandatory services to current selections
$currentSelections = $participant->additionalServices ?? [];
$currentServiceIds = array_map(fn ($service) => $service->id, $currentSelections);
foreach ($ageAppropriateServices as $mandatoryService) {
if (false === in_array($mandatoryService->id, $currentServiceIds, true)) {
$currentSelections[] = $mandatoryService;
}
}
$participant->additionalServices = $currentSelections;
private function canPreselectServicesForParticipant(
BookingDto $bookingDto,
int $participantIndex,
ParticipantDto $participant,
): bool {
if (null === $participant->dateOfBirth) {
return false;
}
$age = $participant->getAge($bookingDto->travel->dateFrom);
if (null !== $age && $age <= Constants::BABY_MAX_AGE) {
return false;
}
return $this->participantEligibilityService->isParticipantEligible($bookingDto, $participantIndex);
}
private function preselectMandatoryAdditionalServices(
ParticipantDto $participant,
array $mandatoryAdditionalServices,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): void {
$eligibleServices = $this->getEligibleServicesForParticipant(
$mandatoryAdditionalServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->appendAdditionalServices($participant, $eligibleServices, false);
}
private function preselectAutoBookAdditionalServices(
ParticipantDto $participant,
array $autoBookAdditionalServices,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): void {
$eligibleServices = $this->getEligibleServicesForParticipant(
$autoBookAdditionalServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->appendAdditionalServices($participant, $eligibleServices, true);
}
private function preselectMandatorySkiPass(
ParticipantDto $participant,
array $mandatorySkiPassServices,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): void {
if (null !== $participant->skiPass) {
return;
}
$eligibleServices = $this->getEligibleServicesForParticipant(
$mandatorySkiPassServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
foreach ($eligibleServices as $service) {
$participant->skiPass = $service;
return;
}
}
private function preselectAutoBookSkiPass(
ParticipantDto $participant,
array $autoBookSkiPassServices,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): void {
if (null !== $participant->skiPass) {
return;
}
$eligibleServices = $this->getEligibleServicesForParticipant(
$autoBookSkiPassServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
foreach ($eligibleServices as $service) {
if (null !== $service->id
&& true === in_array($service->id, $participant->autoBookOptOutSkiPassIds, true)) {
continue;
}
$participant->skiPass = $service;
return;
}
}
private function preselectMandatoryBoardServices(
ParticipantDto $participant,
array $mandatoryBoardServices,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): void {
$eligibleServices = $this->getEligibleServicesForParticipant(
$mandatoryBoardServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->appendBoardServices($participant, $eligibleServices, false);
}
private function preselectAutoBookBoardServices(
ParticipantDto $participant,
array $autoBookBoardServices,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): void {
$eligibleServices = $this->getEligibleServicesForParticipant(
$autoBookBoardServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$this->appendBoardServices($participant, $eligibleServices, true);
}
private function preselectMandatoryRentals(
ParticipantDto $participant,
array $mandatoryRentalServices,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): void {
if (null === $participant->skiPass) {
return;
}
$eligibleServices = $this->getEligibleServicesForParticipant(
$mandatoryRentalServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$matchingDurationServices = $this->getRentalsMatchingSkiPassDuration($eligibleServices, $participant);
$this->appendRentalServices($participant, $matchingDurationServices, false);
}
private function preselectAutoBookRentals(
ParticipantDto $participant,
array $autoBookRentalServices,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): void {
if (null === $participant->skiPass) {
return;
}
$eligibleServices = $this->getEligibleServicesForParticipant(
$autoBookRentalServices,
$bookingDto,
$participantIndex,
$ageEvaluator
);
$matchingDurationServices = $this->getRentalsMatchingSkiPassDuration($eligibleServices, $participant);
$this->appendRentalServices($participant, $matchingDurationServices, true);
}
/**
* @param Service[] $services
*
* @return Service[]
*/
private function getEligibleServicesForParticipant(
array $services,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): array {
return array_filter(
$services,
fn (Service $service): bool => $this->isServiceAvailableForParticipant(
$service,
$bookingDto,
$participantIndex,
$ageEvaluator
)
);
}
/**
* @param Service[] $services
*/
private function appendAdditionalServices(ParticipantDto $participant, array $services, bool $respectOptOut): void
{
$currentSelections = $participant->additionalServices ?? [];
$currentServiceIds = array_map(static fn (Service $service): ?int => $service->id, $currentSelections);
foreach ($services as $service) {
if (null === $service->id || true === in_array($service->id, $currentServiceIds, true)) {
continue;
}
if (true === $respectOptOut
&& true === in_array($service->id, $participant->autoBookOptOutServiceIds, true)) {
continue;
}
$currentSelections[] = $service;
}
$participant->additionalServices = $currentSelections;
}
/**
* @param Service[] $services
*/
private function appendBoardServices(ParticipantDto $participant, array $services, bool $respectOptOut): void
{
$currentSelections = $participant->board ?? [];
$currentServiceIds = array_map(static fn (Service $service): ?int => $service->id, $currentSelections);
foreach ($services as $service) {
if (null === $service->id || true === in_array($service->id, $currentServiceIds, true)) {
continue;
}
if (true === $respectOptOut
&& true === in_array($service->id, $participant->autoBookOptOutBoardIds, true)) {
continue;
}
$currentSelections[] = $service;
}
$participant->board = $currentSelections;
}
/**
* @param Service[] $services
*/
private function appendRentalServices(ParticipantDto $participant, array $services, bool $respectOptOut): void
{
$currentSelections = $participant->rentals ?? [];
$currentServiceIds = array_map(static fn (Service $service): ?int => $service->id, $currentSelections);
foreach ($services as $service) {
if (null === $service->id || true === in_array($service->id, $currentServiceIds, true)) {
continue;
}
if (true === $respectOptOut
&& true === in_array($service->id, $participant->autoBookOptOutRentalIds, true)) {
continue;
}
$currentSelections[] = $service;
}
$participant->rentals = $currentSelections;
}
/**
* @param Service[] $rentals
*
* @return Service[]
*/
private function getRentalsMatchingSkiPassDuration(array $rentals, ParticipantDto $participant): array
{
$selectedSkiPass = $participant->skiPass;
if (null === $selectedSkiPass || null === $selectedSkiPass->dateFrom || null === $selectedSkiPass->dateTo) {
return [];
}
return array_filter($rentals, static function (Service $rental) use ($selectedSkiPass): bool {
if (null === $rental->dateFrom || null === $rental->dateTo) {
return false;
}
return $rental->dateFrom == $selectedSkiPass->dateFrom
&& $rental->dateTo == $selectedSkiPass->dateTo;
});
}
private function isServiceAvailableForParticipant(
Service $service,
BookingDto $bookingDto,
int $participantIndex,
ServiceAgeEvaluator $ageEvaluator,
): bool
{
if (false === $ageEvaluator->canEvaluate($service)) {
return true;
}
return $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex);
}
/**