wip: check eligibility based on ski pass availability

This commit is contained in:
Björn Fromme
2026-03-16 11:59:10 +01:00
parent f5913eb947
commit 16c239716b
9 changed files with 471 additions and 184 deletions
+21 -3
View File
@@ -21,6 +21,10 @@ use App\Form\Model\ParticipantDto;
*/
class BookingPriceCalculatorService
{
public function __construct(
private readonly ParticipantEligibilityService $participantEligibilityService
) {
}
/**
* Calculates comprehensive pricing breakdown for a booking.
*
@@ -89,6 +93,8 @@ class BookingPriceCalculatorService
/**
* Calculates pricing for all selected services across all participants, grouped by subtype.
*
* Only includes services from eligible participants (those with available skipasses for their age).
*
* @param BookingDtoInterface $bookingDto The booking data containing participants and their service selections
*
* @return array Array of service groups with each group containing services of the same subtype
@@ -101,10 +107,15 @@ class BookingPriceCalculatorService
return [];
}
// Aggregate service selections across all participants
// Aggregate service selections across all eligible participants
$serviceAggregation = [];
foreach ($participants as $participant) {
foreach ($participants as $participantIndex => $participant) {
// Skip ineligible participants (no skipasses available for their age)
if (false === $this->participantEligibilityService->isParticipantEligible($bookingDto, $participantIndex)) {
continue;
}
$this->aggregateParticipantServices($participant, $serviceAggregation);
}
@@ -285,6 +296,8 @@ class BookingPriceCalculatorService
* - Zustieg: Sum of all pickup prices and base transportation costs (positive and negative)
* - Parkplatz: Sum of all parking service prices
*
* Only includes transportation costs from eligible participants.
*
* Note: Transportation discounts will be handled generically by groupServicesBySubtype as "Beförderung - Rabatt"
*
* @param BookingDtoInterface $bookingDto The booking data containing participants
@@ -301,7 +314,12 @@ class BookingPriceCalculatorService
$pickupParticipants = 0;
$parkingParticipants = 0;
foreach ($participants as $participant) {
foreach ($participants as $participantIndex => $participant) {
// Skip ineligible participants (no skipasses available for their age)
if (false === $this->participantEligibilityService->isParticipantEligible($bookingDto, $participantIndex)) {
continue;
}
$participantPickupCost = 0.0;
$participantParkingCost = 0.0;
+12 -2
View File
@@ -19,6 +19,7 @@ class BookingService
public function __construct(
private readonly TravelDataService $travelDataService,
private readonly BookingPriceCalculatorService $priceCalculator,
private readonly ParticipantEligibilityService $participantEligibilityService,
) {
}
@@ -149,7 +150,7 @@ class BookingService
* Converts a Room model into a RoomSelectionDto with the specified quantity
* selection. Used during booking initialization to create selectable room options.
*
* @param Room $room The room model to convert
* @param Room $room The room model to convert
* @param array $roomsIdsAndQuantities Array of room ID to quantity mappings
*
* @return RoomSelectionDto The room selection DTO
@@ -329,6 +330,10 @@ class BookingService
* 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 BookingCreateDto $bookingDto The booking DTO to update with mandatory services
*/
public function preselectMandatoryServices(BookingCreateDto $bookingDto): void
@@ -337,11 +342,16 @@ class BookingService
$mandatoryServices = array_filter($additionalServices, fn ($service) => true === $service->mandatory);
// Pre-select mandatory services for each participant
foreach ($bookingDto->participants as $participant) {
foreach ($bookingDto->participants as $participantIndex => $participant) {
if (null === $participant->dateOfBirth) {
continue; // Skip participants without age information
}
// 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) {
@@ -0,0 +1,117 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\BusProNet\Constants;
use App\BusProNet\Model\Service;
use App\Form\Model\BookingDtoInterface;
use Carbon\CarbonImmutable;
use Spatie\Blink\Blink;
/**
* Service for evaluating participant eligibility for booking.
*
* A participant is considered eligible when at least one skipass is available
* for their age at the travel date. This service provides the business logic
* for eligibility checks used by both the conditional field state system and
* the view layer via Twig extension.
*
* Results are cached per request using Blink to avoid redundant calculations
* when checking the same participant multiple times.
*/
class ParticipantEligibilityService
{
/**
* Checks if a participant is eligible for booking.
*
* Returns true when the participant can book services (at least one skipass available).
* Returns false when the participant cannot book (no skipasses available for their age).
*
* Results are cached per request to avoid redundant calculations.
*
* @param BookingDtoInterface $bookingDto The current booking data
* @param int $participantIndex The index of the participant being evaluated
*
* @return bool True if participant is eligible (has available skipasses)
*/
public function isParticipantEligible(BookingDtoInterface $bookingDto, int $participantIndex): bool
{
$participant = $bookingDto->getParticipant($participantIndex);
if (null === $participant || null === $participant->dateOfBirth) {
return false;
}
// Create cache key based on participant's birth date, travel date, and index
$cacheKey = sprintf(
'participant_eligibility_%s_%s_%d',
$participant->dateOfBirth->format('Y-m-d'),
$bookingDto->travel->dateFrom->format('Y-m-d'),
$participantIndex
);
return Blink::global()->once($cacheKey, function () use ($bookingDto, $participantIndex) {
$allSkiPasses = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true, true);
$availableSkiPasses = array_filter(
$allSkiPasses,
fn (Service $service) => $this->isSkiPassAvailableForParticipant($service, $bookingDto, $participantIndex)
);
return !empty($availableSkiPasses);
});
}
/**
* Checks if a skipass service is available for the given participant based on age constraints.
*/
private function isSkiPassAvailableForParticipant(Service $service, BookingDtoInterface $bookingDto, int $participantIndex): bool
{
$participant = $bookingDto->getParticipant($participantIndex);
if (null === $participant || null === $participant->dateOfBirth) {
return false;
}
// No age constraints = available to all
if (null === $service->ageConstraintType) {
return true;
}
$travelStartDate = CarbonImmutable::instance($bookingDto->travel->dateFrom);
$birthDate = CarbonImmutable::instance($participant->dateOfBirth);
$ageAtTravelStart = $travelStartDate->diffInYears($birthDate);
$birthYear = (int) $birthDate->format('Y');
$constraintType = $service->ageConstraintType ?? 'absolute_age';
if ('absolute_age' === $constraintType || 'mixed' === $constraintType) {
$minAge = $service->ageFrom;
$maxAge = $service->ageTo;
if (null !== $minAge && $ageAtTravelStart < $minAge) {
return false;
}
if (null !== $maxAge && $ageAtTravelStart > $maxAge) {
return false;
}
}
if ('birth_year' === $constraintType || 'mixed' === $constraintType) {
$minBirthYear = $service->birthYearFrom;
$maxBirthYear = $service->birthYearTo;
if (null !== $minBirthYear && $birthYear < $minBirthYear) {
return false;
}
if (null !== $maxBirthYear && $birthYear > $maxBirthYear) {
return false;
}
}
return true;
}
}