feat: finalize voucher handling and discount display in summary and overview

This commit is contained in:
Björn Fromme
2025-11-26 17:29:15 +01:00
parent 32bef9a4e9
commit f4691ec67e
15 changed files with 762 additions and 46 deletions
@@ -6,19 +6,27 @@ namespace App\Form\Service;
use App\Form\Model\BookingDto;
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
use App\Service\BookingPriceCalculatorService;
use App\Service\VoucherValidationService;
/**
* Handles promotional voucher code field processing.
*
* Validation is performed by the PromoVoucher constraint on ParticipantEditDto.
* This handler maintains dependency order to ensure validation occurs after
* all price-affecting fields have been processed.
* Validates promo codes via API and stores the validated voucher data
* for early discount display. Validation is also performed by the
* PromoVoucher constraint on ParticipantEditDto for form error feedback.
*
* Dependencies: ALL price-affecting fields (insurance, services, ski pass, rentals, etc.)
* since promo validation requires accurate participant pricing.
*/
class ParticipantPromoVoucherFieldHandler extends AbstractParticipantFieldHandler
{
public function __construct(
private readonly VoucherValidationService $voucherValidationService,
private readonly BookingPriceCalculatorService $priceCalculatorService,
) {
}
public function getFieldName(): string
{
return 'promoVoucherCode';
@@ -44,12 +52,42 @@ class ParticipantPromoVoucherFieldHandler extends AbstractParticipantFieldHandle
public function shouldProcess(array $submittedData, string $mode, int $participantIndex): bool
{
// No processing needed - validation handled by PromoVoucher constraint
return false;
$promoCode = $this->getFieldValue($submittedData, $this->getFieldName());
return null !== $promoCode && '' !== trim($promoCode);
}
public function processField(array $submittedData, BookingDto $bookingDto, int $participantIndex): void
{
// No processing needed - validation handled by PromoVoucher constraint
$participant = $this->getParticipant($bookingDto, $participantIndex);
if (null === $participant) {
return;
}
$promoCode = $this->getFieldValue($submittedData, $this->getFieldName());
// Clear validated voucher if code is empty
if (null === $promoCode || '' === trim($promoCode)) {
$participant->validatedPromoVoucher = null;
return;
}
// Calculate participant price for validation
$participantPrice = $this->priceCalculatorService->calculateIndividualParticipantPrice(
$bookingDto,
$participantIndex
);
// Validate promo voucher and store result
$result = $this->voucherValidationService->validatePromoVoucher(
trim($promoCode),
$bookingDto->travel->id,
$participantPrice
);
// Store validated voucher (null if invalid)
$participant->validatedPromoVoucher = $result->isValid ? $result->voucher : null;
}
}
@@ -52,21 +52,17 @@ class ParticipantPurchaseVoucherFieldHandler extends AbstractParticipantFieldHan
$voucherCode = $this->getFieldValue($submittedData, $this->getFieldName());
// Clear validated voucher if code is empty
if (null === $voucherCode || '' === trim($voucherCode)) {
$participant->validatedPurchaseVoucher = null;
return;
}
// Validate voucher to check if it's goodwill type
// Validate voucher and store result
$result = $this->voucherValidationService->validatePurchaseVoucher($voucherCode);
// Only process valid vouchers
if (false === $result->isValid || null === $result->voucher) {
return;
}
// Check if this is a goodwill voucher
if (true === $result->voucher->isGoodwill()) {
$participant->hasGoodwillVoucher = true;
}
// Store validated voucher (null if invalid)
$participant->validatedPurchaseVoucher = $result->isValid ? $result->voucher : null;
}
}