diff --git a/CRM/Grant/BAO/GrantBudget.php b/CRM/Grant/BAO/GrantBudget.php index e2372bb..d4ba0a5 100644 --- a/CRM/Grant/BAO/GrantBudget.php +++ b/CRM/Grant/BAO/GrantBudget.php @@ -113,6 +113,20 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget { if ($grantBudget) { $balanceAmount = CRM_Utils_Rule::cleanMoney($grantBudget['balance_amount']); $amountGranted = CRM_Utils_Rule::cleanMoney($params['multifund_amount'][$k]); + // If there's a $grantId, we're updating an existing grant. Make sure we don't + // double-count this money on validation. + if ($grantId) { + $oldGrantLineItem = civicrm_api3('EntityFinancialTrxn', 'get', [ + 'sequential' => 1, + 'return' => ["financial_trxn_id.total_amount", "financial_trxn_id.trxn_date"], + 'entity_table' => "civicrm_grant", + 'entity_id' => $grantId, + 'financial_trxn_id.from_financial_account_id' => $financialAccount, + ])['values'][0]; + if ($oldGrantLineItem && $year == date('Y', strtotime($oldGrantLineItem['financial_trxn_id.trxn_date']))) { + $amountGranted -= $oldGrantLineItem['financial_trxn_id.total_amount']; + } + } if ($balanceAmount < $amountGranted) { $errors["multifund_amount[$k]"] = ts("The annual budget for this grant is {$grantBudget['budget']}. Grants totaling {$grantBudget['total_amount_granted']} have been made. {$grantBudget['balance_amount']} remains."); }