*/ 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 */ public static function names(): array { return ['price', 'price_before_discount', 'discount_percent', 'pricing']; } }