#26 - Loading animation + success message fix
This commit was merged in pull request #31.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?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 {
|
||||
$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()));
|
||||
}
|
||||
|
||||
$discount_detail = (new RsvMembershipService())->discount_detail_for($definition, $data);
|
||||
$discount_pct = $discount_detail['percent'];
|
||||
$final_price = $calculator->calculate($definition, $data);
|
||||
$subtotal = $price_before_discount;
|
||||
$discount_amount = $subtotal - $final_price;
|
||||
|
||||
return [
|
||||
'price' => $final_price,
|
||||
'price_before_discount' => $price_before_discount,
|
||||
'discount_percent' => $discount_pct,
|
||||
'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,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The names these values expose, so template validation accepts {{ price }}.
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function names(): array {
|
||||
return ['price', 'price_before_discount', 'discount_percent', 'pricing'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user