getParticipant(0); if (null === $applicant || null === $applicant->insurance) { return; } $isFamilyInsurance = true === $applicant->insurance->familyInsurance; // Check if bulk insurance is enabled OR the applicant's selection is a family // insurance (which always applies to dependents, independent of the bulk checkbox) if (false === $applicant->bulkInsuranceBooking && false === $isFamilyInsurance) { return; } // Get selectable (non-complementary) insurances with caching $selectableInsurances = $this->insuranceService->getSelectableInsurances($bookingDto->travel); // Calculate travel prices for all participants (excluding insurance) $participantPrices = []; foreach ($bookingDto->getParticipants() as $index => $participant) { $participantPrices[$index] = $this->priceCalculatorService->calculateIndividualParticipantPriceExcludingInsurance($bookingDto, $index); } // Use InsuranceManager for proper type-based assignment with price tier matching $assignments = $this->insuranceService->batchAssignInsuranceToParticipants( $selectableInsurances, $applicant->insurance, $bookingDto, $participantPrices ); // Apply assignments to all participants; for non-family insurance skip the applicant // (they already hold the insurance they selected themselves) foreach ($assignments as $index => $insurance) { if (0 === $index && false === $isFamilyInsurance) { continue; } $participant = $bookingDto->getParticipant($index); if (null === $participant) { continue; } // In edit mode: non-family bulk assignment must not overwrite an individually // locked insurance. Family insurance always overrides, since dependents can // never legitimately keep their own policy alongside it. if (BookingDto::MODE_EDIT === $bookingDto->getMode() && false === $isFamilyInsurance && null !== $participant->insurance) { continue; // Skip participants with existing insurance assignment } $participant->insurance = $insurance; } } }