Files
Reservair/includes/Services/Forms/Pricing/RsvFormPriceCalculator.php
T

23 lines
818 B
PHP
Raw Normal View History

2026-06-16 19:33:55 +02:00
<?php
2026-06-17 11:15:09 +02:00
/** Computes a form's total price as the sum of its elements' prices, reduced by membership discount. */
2026-06-16 19:33:55 +02:00
class RsvFormPriceCalculator {
public function calculate(RsvFormDefinition $definition, RsvFormData $data): float {
global $rsv_form_price_registry;
$total = 0.0;
foreach ($definition->getElements() as $element) {
$calculator = $rsv_form_price_registry->get($element->getType());
if ($calculator === null) {
continue; // Unpriced element type contributes nothing.
}
$total += (float) $calculator($element, $data->getValue($element->getName()));
}
2026-06-17 11:15:09 +02:00
$pct = (new RsvMembershipService())->discount_for($definition, $data);
return $total * (1.0 - max(0.0, min(100.0, $pct)) / 100.0);
2026-06-16 19:33:55 +02:00
}
}