Files
Reservair/includes/Services/Membership/RsvMembershipService.php
T

51 lines
1.4 KiB
PHP
Raw Normal View History

2026-06-17 11:15:09 +02:00
<?php
class RsvMembershipService {
/**
* Total membership discount for a submission.
*
* Each binding names a form field whose submitted value must match a key
* in the bound program. Matching bindings' discounts are combined per the
* definition's combine mode: the best single discount, or all summed and
* capped at 100%.
*/
public function discount_for(RsvFormDefinition $def, RsvFormData $data): float {
$repo = new RsvMembershipProgramRepository();
$matched_discounts = [];
foreach ($def->getMembershipBindings() as $binding) {
$program_id = intval($binding['program_id'] ?? 0);
$discount = floatval($binding['discount'] ?? 0.0);
$field = strval($binding['field'] ?? '');
if ($program_id <= 0 || $field === '') {
continue;
}
$raw = $data->getValue($field, '');
$value = is_scalar($raw) ? trim((string) $raw) : '';
if ($value === '') {
continue;
}
if ($repo->key_exists($program_id, $value)) {
$matched_discounts[] = $discount;
}
}
if (empty($matched_discounts)) {
return 0.0;
}
if ($def->getMembershipCombine() === 'sum') {
return min(100.0, array_sum($matched_discounts));
}
return max($matched_discounts);
}
}