wip: skipass-duration based rentals filtering

This commit is contained in:
Björn Fromme
2025-09-10 14:26:18 +02:00
parent 5469c834a6
commit b6f991b291
8 changed files with 288 additions and 17 deletions
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace App\Form\Service\Condition;
use App\BusProNet\Model\Service;
use App\Form\Model\BookingDtoInterface;
use App\Form\Service\Contract\FieldConditionInterface;
/**
* Condition that evaluates whether a skipass is selected for a participant.
*
* This condition checks if a participant has selected a skipass from the
* available travel services. When a skipass is selected, rental equipment
* fields should become visible and filtered by the skipass duration.
*
* The condition examines the participant's skiPass property to determine if
* a skipass service has been selected, triggering rental field visibility
* and duration-based filtering.
*/
class SkiPassSelectionCondition implements FieldConditionInterface
{
/**
* Evaluates whether the participant has selected a skipass.
*
* Checks if the participant's skiPass property contains a Service object,
* which would indicate a skipass has been selected and rental services
* should be made available with duration filtering applied.
*
* @param BookingDtoInterface $bookingDto The current booking data (create or edit)
* @param int $participantIndex The index of the participant being evaluated
* @param array<string, mixed> $formData Current form data for condition evaluation
*
* @return bool True if a skipass is selected, false otherwise
*/
public function evaluate(BookingDtoInterface $bookingDto, int $participantIndex, array $formData): bool
{
// First check submitted form data for skipass selection
if (isset($formData['participants'][$participantIndex]['skiPass'])) {
$selectedSkiPass = $formData['participants'][$participantIndex]['skiPass'];
if (null !== $selectedSkiPass && '' !== $selectedSkiPass) {
return true;
}
}
// Then check participant DTO data for existing skipass selection
$participant = $bookingDto->getParticipant($participantIndex);
if (null !== $participant && null !== $participant->skiPass) {
// Check if skipass is actually a Service object
if ($participant->skiPass instanceof Service) {
return true;
}
}
return false;
}
/**
* Returns field names that trigger re-evaluation of this condition.
*
* This condition depends on the skiPass field, so any changes to skipass
* selections should trigger re-evaluation of rental field states.
*
* @return string[] Array containing the field names that affect this condition
*/
public function getDependentFields(): array
{
return ['skiPass'];
}
/**
* Returns a human-readable description of this condition.
*
* Provides a clear description of the condition logic for debugging,
* logging, and developer documentation purposes.
*
* @return string A brief description of the condition logic
*/
public function getDescription(): string
{
return 'Rentals visible and filtered by duration when skipass is selected';
}
}
@@ -12,6 +12,7 @@ use App\Form\Service\Condition\FieldValueCondition;
use App\Form\Service\Condition\RentalSelectionCondition;
use App\Form\Service\Condition\RoomSelectionCondition;
use App\Form\Service\Condition\ServiceSubTypeCondition;
use App\Form\Service\Condition\SkiPassSelectionCondition;
/**
* Field state provider for the booking create workflow.
@@ -55,6 +56,7 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
protected function registerFieldStateConditions(): void
{
$rentalCondition = new RentalSelectionCondition();
$skiPassCondition = new SkiPassSelectionCondition();
// Hide body dimensions section unless rental services are selected
$this->fieldStateConditions['bodyDimensions'] = [
@@ -73,8 +75,12 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
'hidden' => CompositeCondition::not($dateOfBirthProvidedCondition),
];
// Show rentals only when both date of birth is provided AND skipass is selected
$this->fieldStateConditions['rentals'] = [
'hidden' => CompositeCondition::not($dateOfBirthProvidedCondition),
'hidden' => CompositeCondition::or(
CompositeCondition::not($dateOfBirthProvidedCondition),
CompositeCondition::not($skiPassCondition)
),
];
$this->fieldStateConditions['board'] = [
@@ -183,7 +183,7 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
'choice_label' => fn (?Service $service) => $this->formatServiceLabelWithPrice($service),
];
// Rentals field provider - provides age-appropriate rental options from travel data filtered by date range
// Rentals field provider - provides age-appropriate rental options filtered by selected skipass duration
$this->fieldOptionProviders['rentals'] = fn (BookingDtoInterface $bookingDto, int $participantIndex, array $options = []) => [
'label' => 'Leihmaterial',
'multiple' => true,
@@ -191,7 +191,11 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
'required' => false,
'choices' => $this->filterServicesByAvailability(
$this->filterServicesByAgeConstraints(
$bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_RENTALS, true, true),
$this->filterRentalsBySkiPassDuration(
$bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_RENTALS, true, true),
$bookingDto,
$participantIndex
),
$bookingDto,
$participantIndex
),
@@ -364,6 +368,45 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
// ];
}
/**
* Filters rental services by selected skipass duration.
*
* Only returns rentals that have exactly the same dateFrom and dateTo
* as the participant's selected skipass. This ensures rental equipment
* is only available for the exact duration of the skipass.
*
* @param array $rentals Array of rental Service objects to filter
* @param BookingDtoInterface $bookingDto The booking DTO containing participant data
* @param int $participantIndex Index of the participant to evaluate
*
* @return array Filtered array of rentals matching skipass duration
*/
private function filterRentalsBySkiPassDuration(array $rentals, BookingDtoInterface $bookingDto, int $participantIndex): array
{
$participant = $bookingDto->getParticipant($participantIndex);
if (null === $participant || null === $participant->skiPass) {
return []; // No skipass selected = no rentals available
}
$selectedSkiPass = $participant->skiPass;
// If skipass has no valid dates, return empty rentals
if (null === $selectedSkiPass->dateFrom || null === $selectedSkiPass->dateTo) {
return [];
}
return array_filter($rentals, function (Service $rental) use ($selectedSkiPass) {
// Rental must have valid dates to be considered
if (null === $rental->dateFrom || null === $rental->dateTo) {
return false;
}
// Exact date matching: rental dates must match skipass dates exactly
return $rental->dateFrom->format('Y-m-d') === $selectedSkiPass->dateFrom->format('Y-m-d')
&& $rental->dateTo->format('Y-m-d') === $selectedSkiPass->dateTo->format('Y-m-d');
});
}
/**
* Formats service label with pricing information.
*
@@ -16,11 +16,11 @@ use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
* creation process. It processes the rentalInsurance field from form submissions,
* validates the selection, and updates the participant DTO with the valid selection.
*
* The rental insurance field is only shown when the participant has selected
* rental services, creating a dependency chain where rental insurance depends
* on rental selections.
* The rental insurance field is only shown when the participant has selected both
* a skipass and rental services, creating a dependency chain where rental insurance
* depends on skipass and rental selections.
*
* Dependencies: dateOfBirth (for age evaluation) and rentals (for field visibility)
* Dependencies: dateOfBirth (for age evaluation), skiPass (for visibility), and rentals (for field visibility)
*/
class ParticipantRentalInsuranceFieldHandler extends AbstractParticipantFieldHandler
{
@@ -37,14 +37,14 @@ class ParticipantRentalInsuranceFieldHandler extends AbstractParticipantFieldHan
/**
* Returns the field dependencies for proper processing order.
*
* This handler depends on both dateOfBirth (for age evaluation) and rentals
* (because rental insurance is only relevant when rentals are selected).
* This handler depends on dateOfBirth (for age evaluation), skiPass (for field visibility),
* and rentals (because rental insurance is only relevant when rentals are selected).
*
* @return string[] Array containing 'dateOfBirth' and 'rentals' dependencies
* @return string[] Array containing 'dateOfBirth', 'skiPass', and 'rentals' dependencies
*/
public function getDependencies(): array
{
return ['dateOfBirth', 'rentals'];
return ['dateOfBirth', 'skiPass', 'rentals'];
}
/**
@@ -85,11 +85,12 @@ class ParticipantRentalInsuranceFieldHandler extends AbstractParticipantFieldHan
return;
}
// Check if rental insurance should be visible based on rental selections
// Check if rental insurance should be visible based on skipass and rental selections
$hasSkiPass = null !== $participant->skiPass;
$hasRentals = false === empty($participant->rentals);
if (false === $hasRentals) {
// If no rentals are selected, clear rental insurance data
if (false === $hasSkiPass || false === $hasRentals) {
// If no skipass or no rentals are selected, clear rental insurance data
$participant->rentalInsuranceSelected = false;
$participant->rentalInsurance = null;
@@ -14,10 +14,10 @@ use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
*
* This handler manages rental equipment selections for participants in the booking
* creation process. It processes the rentals field from form submissions,
* filters out age-inappropriate options, and updates the participant DTO with only
* valid selections.
* filters out age-inappropriate options and duration-inappropriate options, and
* updates the participant DTO with only valid selections.
*
* Dependencies: dateOfBirth (must be processed first for age evaluation)
* Dependencies: dateOfBirth (for age evaluation) and skiPass (for duration filtering)
*/
class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
{
@@ -26,6 +26,19 @@ class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
return 'rentals';
}
/**
* Returns the field dependencies for proper processing order.
*
* This handler depends on both dateOfBirth (for age evaluation) and skiPass
* (for duration filtering and field visibility logic).
*
* @return string[] Array containing 'dateOfBirth' and 'skiPass' dependencies
*/
public function getDependencies(): array
{
return ['dateOfBirth', 'skiPass'];
}
/**
* Determines if this handler should process the field based on submitted data.
*
@@ -51,6 +64,14 @@ class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
return;
}
// Check if rentals should be available based on skipass selection
if (null === $participant->skiPass) {
// No skipass selected = clear all rental selections
$participant->rentals = [];
return;
}
$selectedRentals = $this->getFieldValue($submittedData, $this->getFieldName()) ?? [];
$availableRentals = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_RENTALS, true, true);