From c5c492463edbbd41c8cd9ea35355300f70377f80 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Sat, 20 Jun 2026 17:28:00 -0500 Subject: [PATCH] fix: enforce restricted fund balance check before recording expenses (GAAP) --- .../src/Helper/FundAccountingHelper.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/source/packages/plg_system_mokosuitenpo/src/Helper/FundAccountingHelper.php b/source/packages/plg_system_mokosuitenpo/src/Helper/FundAccountingHelper.php index a74d6a3..6f29510 100644 --- a/source/packages/plg_system_mokosuitenpo/src/Helper/FundAccountingHelper.php +++ b/source/packages/plg_system_mokosuitenpo/src/Helper/FundAccountingHelper.php @@ -50,6 +50,19 @@ class FundAccountingHelper if (!$fundType) throw new \RuntimeException('Fund not found'); + // Enforce balance check on restricted funds (GAAP compliance) + if ($fundType === 'restricted' || $fundType === 'temporarily_restricted') { + $db->setQuery($db->getQuery(true) + ->select('COALESCE((SELECT SUM(d.amount) FROM #__mokosuitenpo_donations d WHERE d.fund_id = ' . (int) $fundId . '), 0)' + . ' - COALESCE((SELECT SUM(e.amount) FROM #__mokosuitenpo_fund_expenses e WHERE e.fund_id = ' . (int) $fundId . '), 0) AS balance') + ->from('DUAL')); + $balance = (float) $db->loadResult(); + + if ($amount > $balance) { + throw new \RuntimeException('Insufficient balance in restricted fund (available: $' . number_format($balance, 2) . ', requested: $' . number_format($amount, 2) . ')'); + } + } + $expense = (object) [ 'fund_id' => $fundId, 'amount' => $amount,