wip: rentals insurance field
This commit is contained in:
@@ -21,7 +21,7 @@ use App\Form\Service\Condition\ServiceSubTypeCondition;
|
||||
* state functionality provided by AbstractFieldStateProvider.
|
||||
*
|
||||
* Current field state conditions:
|
||||
* - Body dimension fields become required when rental services are selected
|
||||
* - Body dimension fields are hidden unless rental services are selected
|
||||
* - Age-dependent service fields are hidden until birth date is provided
|
||||
* - Transportation pickup fields are hidden by default, shown only when transportation type is BUS
|
||||
* - Parking field is hidden by default, shown only when outbound transportation is PKW
|
||||
@@ -56,17 +56,9 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
|
||||
{
|
||||
$rentalCondition = new RentalSelectionCondition();
|
||||
|
||||
// Make body dimension fields required when rental services are selected
|
||||
$this->fieldStateConditions['height'] = [
|
||||
'required' => $rentalCondition,
|
||||
];
|
||||
|
||||
$this->fieldStateConditions['weight'] = [
|
||||
'required' => $rentalCondition,
|
||||
];
|
||||
|
||||
$this->fieldStateConditions['shoeSize'] = [
|
||||
'required' => $rentalCondition,
|
||||
// Hide body dimensions section unless rental services are selected
|
||||
$this->fieldStateConditions['bodyDimensions'] = [
|
||||
'hidden' => CompositeCondition::not($rentalCondition),
|
||||
];
|
||||
|
||||
// Hide age-dependent fields when no date of birth is provided
|
||||
@@ -121,6 +113,11 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
|
||||
),
|
||||
];
|
||||
|
||||
// Show rental insurance only when rental services are selected (hidden by default)
|
||||
$this->fieldStateConditions['rentalInsurance'] = [
|
||||
'hidden' => CompositeCondition::not($rentalCondition),
|
||||
];
|
||||
|
||||
// Example field state conditions would be registered here
|
||||
// For demonstration purposes, here are some example patterns:
|
||||
|
||||
|
||||
@@ -183,6 +183,13 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
|
||||
'choice_label' => fn (?Service $service) => $this->formatServiceLabelWithPrice($service),
|
||||
];
|
||||
|
||||
// Rental insurance field provider - provides rental insurance options when rental services are selected
|
||||
$this->fieldOptionProviders['rentalInsurance'] = fn (BookingDtoInterface $bookingDto, int $participantIndex, array $options = []) => [
|
||||
'label' => $this->getRentalInsuranceCheckboxLabel($bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_RENTAL_INSURANCE, true, true)),
|
||||
'required' => false,
|
||||
'property_path' => 'rentalInsuranceSelected',
|
||||
];
|
||||
|
||||
// Skipass field provider - provides age-appropriate skipass options from travel data filtered by date range
|
||||
$this->fieldOptionProviders['skiPass'] = fn (BookingDtoInterface $bookingDto, int $participantIndex, array $options = []) => [
|
||||
'label' => 'Skipass',
|
||||
@@ -477,4 +484,16 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
|
||||
return $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates label for rental insurance checkbox including pricing information.
|
||||
*/
|
||||
private function getRentalInsuranceCheckboxLabel(array $rentalInsuranceServices): string
|
||||
{
|
||||
if (empty($rentalInsuranceServices)) {
|
||||
return 'Leihmaterial-Versicherung';
|
||||
}
|
||||
$rentalInsuranceService = reset($rentalInsuranceServices); // Get the first (and only) rental insurance service
|
||||
return $this->formatServiceLabelWithPrice($rentalInsuranceService);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Service;
|
||||
|
||||
use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\Form\Model\BookingDtoInterface;
|
||||
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
|
||||
|
||||
/**
|
||||
* Handles processing of the rentalInsurance field for booking participants.
|
||||
*
|
||||
* This handler manages rental insurance selections for participants in the booking
|
||||
* 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.
|
||||
*
|
||||
* Dependencies: dateOfBirth (for age evaluation) and rentals (for field visibility)
|
||||
*/
|
||||
class ParticipantRentalInsuranceFieldHandler extends AbstractParticipantFieldHandler
|
||||
{
|
||||
/**
|
||||
* Returns the form field name this handler processes.
|
||||
*
|
||||
* @return string The field name 'rentalInsurance'
|
||||
*/
|
||||
public function getFieldName(): string
|
||||
{
|
||||
return 'rentalInsurance';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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).
|
||||
*
|
||||
* @return string[] Array containing 'dateOfBirth' and 'rentals' dependencies
|
||||
*/
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return ['dateOfBirth', 'rentals'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if this handler should process the field based on submitted data.
|
||||
*
|
||||
* For service selection fields like rental insurance, we always need to process
|
||||
* to handle cases where the selection is cleared (field not present in data).
|
||||
* This ensures the participant DTO is updated with null when no
|
||||
* rental insurance is selected.
|
||||
*
|
||||
* @param array<string, mixed> $submittedData The submitted participant form data
|
||||
* @param int $participantIndex The index of the participant being processed
|
||||
*
|
||||
* @return bool Always returns true for service selection fields
|
||||
*/
|
||||
public function shouldProcess(array $submittedData, int $participantIndex): bool
|
||||
{
|
||||
return true; // Always process to handle deselection cases
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the rentalInsurance field for a specific participant.
|
||||
*
|
||||
* This method extracts the rental insurance selection from submitted form data,
|
||||
* validates the selection against the participant's age constraints, and
|
||||
* updates the participant DTO with the valid selection. If the rental insurance
|
||||
* is no longer appropriate for the participant's age, it is automatically cleared.
|
||||
*
|
||||
* @param array<string, mixed> $submittedData The submitted participant form data
|
||||
* @param BookingDtoInterface $bookingDto The booking DTO to update (create or edit)
|
||||
* @param int $participantIndex The index of the participant being processed
|
||||
*/
|
||||
public function processField(array $submittedData, BookingDtoInterface $bookingDto, int $participantIndex): void
|
||||
{
|
||||
// Safely get the participant object, returning early if not found
|
||||
$participant = $this->getParticipant($bookingDto, $participantIndex);
|
||||
if (null === $participant) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if rental insurance should be visible based on rental selections
|
||||
$hasRentals = false === empty($participant->rentals);
|
||||
|
||||
if (false === $hasRentals) {
|
||||
// If no rentals are selected, clear rental insurance data
|
||||
$participant->rentalInsuranceSelected = false;
|
||||
$participant->rentalInsurance = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract checkbox value from submitted data (this comes from the rentalInsuranceSelected property)
|
||||
$rentalInsuranceSelected = $this->getFieldValue($submittedData, $this->getFieldName());
|
||||
$isRentalInsuranceSelected = (bool) $rentalInsuranceSelected;
|
||||
|
||||
// Store boolean value
|
||||
$participant->rentalInsuranceSelected = $isRentalInsuranceSelected;
|
||||
|
||||
// Store service object based on checkbox state for pricing calculations
|
||||
if (true === $isRentalInsuranceSelected) {
|
||||
$participant->rentalInsurance = $this->findRentalInsuranceService($bookingDto);
|
||||
} else {
|
||||
$participant->rentalInsurance = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the rental insurance service from available services.
|
||||
*
|
||||
* Gets the first (and typically only) rental insurance service.
|
||||
* Returns null if no rental insurance services are available.
|
||||
*
|
||||
* @param BookingDtoInterface $bookingDto The booking DTO containing travel data
|
||||
*
|
||||
* @return Service|null The rental insurance service object, or null if not found
|
||||
*/
|
||||
private function findRentalInsuranceService(BookingDtoInterface $bookingDto): ?Service
|
||||
{
|
||||
$rentalInsuranceServices = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_RENTAL_INSURANCE, true, true);
|
||||
|
||||
if (empty($rentalInsuranceServices)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return reset($rentalInsuranceServices); // Get the first (and typically only) rental insurance service
|
||||
}
|
||||
}
|
||||
@@ -26,11 +26,6 @@ class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
|
||||
return 'rentals';
|
||||
}
|
||||
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return ['dateOfBirth'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if this handler should process the field based on submitted data.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user