2026-06-16 19:33:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/** Values derived from a submission (not entered by the visitor), exposed to templates. */
|
|
|
|
|
final class RsvFormCalculatedValues {
|
|
|
|
|
/** @return array<string, mixed> */
|
|
|
|
|
public function for(RsvFormDefinition $definition, RsvFormData $data): array {
|
2026-06-17 11:15:09 +02:00
|
|
|
$calculator = new RsvFormPriceCalculator();
|
|
|
|
|
global $rsv_form_price_registry;
|
|
|
|
|
|
|
|
|
|
$price_before_discount = 0.0;
|
|
|
|
|
foreach ($definition->getElements() as $element) {
|
|
|
|
|
$element_calculator = $rsv_form_price_registry->get($element->getType());
|
|
|
|
|
if ($element_calculator === null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$price_before_discount += (float) $element_calculator($element, $data->getValue($element->getName()));
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 11:20:28 +02:00
|
|
|
$discount_detail = (new RsvMembershipService())->discount_detail_for($definition, $data);
|
|
|
|
|
$discount_pct = $discount_detail['percent'];
|
2026-06-17 11:15:09 +02:00
|
|
|
$final_price = $calculator->calculate($definition, $data);
|
2026-06-22 11:20:28 +02:00
|
|
|
$subtotal = $price_before_discount;
|
|
|
|
|
$discount_amount = $subtotal - $final_price;
|
2026-06-17 11:15:09 +02:00
|
|
|
|
2026-06-16 19:33:55 +02:00
|
|
|
return [
|
2026-06-17 11:15:09 +02:00
|
|
|
'price' => $final_price,
|
|
|
|
|
'price_before_discount' => $price_before_discount,
|
|
|
|
|
'discount_percent' => $discount_pct,
|
2026-06-22 11:20:28 +02:00
|
|
|
'pricing' => [
|
|
|
|
|
'currency' => 'CZK',
|
|
|
|
|
'subtotal' => $subtotal,
|
|
|
|
|
'discount' => $discount_pct > 0.0 ? [
|
|
|
|
|
'percent' => $discount_pct,
|
|
|
|
|
'amount' => round($discount_amount, 2),
|
|
|
|
|
'reason' => $discount_detail['reason'],
|
|
|
|
|
] : null,
|
|
|
|
|
'total' => $final_price,
|
|
|
|
|
],
|
2026-06-16 19:33:55 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The names these values expose, so template validation accepts {{ price }}.
|
|
|
|
|
* @return list<string>
|
|
|
|
|
*/
|
|
|
|
|
public static function names(): array {
|
2026-06-22 11:20:28 +02:00
|
|
|
return ['price', 'price_before_discount', 'discount_percent', 'pricing'];
|
2026-06-16 19:33:55 +02:00
|
|
|
}
|
|
|
|
|
}
|