diff --git a/src/Form/Service/ParticipantTransportationDiscountReplacementFieldHandler.php b/src/Form/Service/ParticipantTransportationDiscountReplacementFieldHandler.php index 5781b23..bbd2898 100644 --- a/src/Form/Service/ParticipantTransportationDiscountReplacementFieldHandler.php +++ b/src/Form/Service/ParticipantTransportationDiscountReplacementFieldHandler.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Form\Service; use App\BusProNet\Constants; +use App\BusProNet\Model\Service; use App\BusProNet\Utility\DirectionMapper; use App\Form\Model\BookingDto; use App\Form\Service\Abstract\AbstractParticipantFieldHandler; @@ -122,6 +123,8 @@ class ParticipantTransportationDiscountReplacementFieldHandler extends AbstractP $regularPkw->label ) ); + + $this->syncBookingTransportationService($bookingDto, $participantIndex, $discountedPkw, $regularPkw); } return; // Done processing for BUS inbound scenario @@ -168,6 +171,46 @@ class ParticipantTransportationDiscountReplacementFieldHandler extends AbstractP $discountedPkw->label ) ); + + $this->syncBookingTransportationService($bookingDto, $participantIndex, $regularPkw, $discountedPkw); + } + } + + /** + * Keeps $bookingDto->booking->transportationServices consistent with a participant's + * updated outbound transportation selection. Only runs in edit mode. + * + * Clones the new service before inserting it to avoid mutating the shared travel-data + * object when resetServiceMappings() clears mappings at submit time. + */ + private function syncBookingTransportationService( + BookingDto $bookingDto, + int $participantIndex, + Service $oldService, + Service $newService, + ): void { + if (null === $bookingDto->booking) { + return; + } + + $services = &$bookingDto->booking->transportationServices; + + if (isset($services[$oldService->id])) { + $services[$oldService->id]->mapping = array_values( + array_filter( + $services[$oldService->id]->mapping, + fn(int $idx): bool => $idx !== $participantIndex, + ) + ); + } + + if (false === isset($services[$newService->id])) { + $services[$newService->id] = clone $newService; + $services[$newService->id]->mapping = []; + } + + if (false === in_array($participantIndex, $services[$newService->id]->mapping, true)) { + $services[$newService->id]->mapping[] = $participantIndex; } } }