Commit cd3c2724 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix discount calculation: مصاريف تنمية excluded from discountable base

Business rule: the 35 EGP development fee is a flat non-discountable surcharge
added AFTER all discounts are applied. Discount applies only to base_amount.

Correct formula: discount = base × pct, total = (base - discount) + dev_fee
Previous (wrong): discount = (base + dev_fee) × pct

For member 51 (2023/2024): 410×50% = 205 discount, total = 205 + 35 = 240
(was incorrectly: 445×50% = 222.50, total = 222.50)
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 2e65dd2e
......@@ -73,10 +73,9 @@ final class SubscriptionGenerator
);
if ($existing) { $skipped++; continue; }
// Member subscription
$gross = bcadd($memberRate, $devFee, 2);
$discount = $discountPct ? bcdiv(bcmul($gross, $discountPct, 4), '100', 2) : '0.00';
$total = bcsub($gross, $discount, 2);
// Member subscription — discount on base only, dev fee added after
$discount = $discountPct ? bcdiv(bcmul($memberRate, $discountPct, 4), '100', 2) : '0.00';
$total = bcadd(bcsub($memberRate, $discount, 2), $devFee, 2);
$db->insert('subscriptions', [
'member_id' => $memberId,
'financial_year' => $financialYear,
......
<?php
declare(strict_types=1);
/**
* Fix 2023/2024 discount calculation: مصاريف تنمية (35 EGP) must NOT be
* included in the discountable base. Discount applies only to base_amount,
* then dev fee is added as a flat non-discountable surcharge.
*
* Correct formula: discount = base_amount × 50%, total = base_amount - discount + dev_fee
*/
return function (\App\Core\Database $db): void {
$rows = $db->select(
"SELECT id, base_amount, development_fee
FROM subscriptions
WHERE financial_year = '2023/2024' AND status IN ('pending', 'overdue')"
);
foreach ($rows as $row) {
$discount = bcdiv($row['base_amount'], '2', 2); // 50% of base only
$newTotal = bcadd(bcsub($row['base_amount'], $discount, 2), $row['development_fee'], 2);
$db->query(
"UPDATE subscriptions SET discount_amount = ?, total_amount = ?, updated_at = NOW() WHERE id = ?",
[$discount, $newTotal, (int) $row['id']]
);
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment