From 836c86c04277639803a5d6b534112d67bebf16b2 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Mon, 8 Jul 2019 13:47:31 -0400 Subject: [PATCH] Fix incorrect calculation of grant budgets over multiple years --- CRM/Grant/BAO/GrantBudget.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/CRM/Grant/BAO/GrantBudget.php b/CRM/Grant/BAO/GrantBudget.php index c041466..e2372bb 100644 --- a/CRM/Grant/BAO/GrantBudget.php +++ b/CRM/Grant/BAO/GrantBudget.php @@ -56,7 +56,7 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget { $grantBudgetTableName = CRM_Grant_DAO_GrantBudget::getTableName(); $sql = "SELECT cgb.id, cft.name, IFNULL(cgb.budget, 0) AS budget, - SUM(IFNULL(trxn." . GRANT_AMOUNT_CHECK_FIELD . ", 0)) as total_amount_granted, + SUM(CASE WHEN cg.id IS NULL THEN 0 ELSE IFNULL(trxn.total_amount, 0) END) as total_amount_granted, cft.id AS financial_type_id FROM civicrm_financial_type cft INNER JOIN " . GRANT_SCHOLARSHIP_CUSTOM_TABLE_NAME . " gs @@ -64,13 +64,14 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget { AND gs." . GRANT_SCHOLARSHIP_CUSTOM_FIELD . " = 1 LEFT JOIN " . $grantBudgetTableName . " cgb ON cgb.financial_type_id = cft.id AND cgb.fiscal_year = %1 - LEFT JOIN civicrm_grant cg - ON cg.financial_type_id = cft.id AND cg.status_id IN (%4) - AND cg." . GRANT_DATE_CHECK_FIELD . " >= %2 - AND cg." . GRANT_DATE_CHECK_FIELD . " < %3 LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_table = 'civicrm_financial_type' AND cft.id = cefa.entity_id AND account_relationship = %5 LEFT JOIN civicrm_financial_trxn trxn ON trxn.from_financial_account_id = cefa.financial_account_id + LEFT JOIN civicrm_entity_financial_trxn ceft on trxn.id = ceft.financial_trxn_id AND ceft.entity_table = 'civicrm_grant' + LEFT JOIN civicrm_grant cg + ON cg.id = ceft.entity_id AND cg.status_id IN (%4) + AND cg." . GRANT_DATE_CHECK_FIELD . " >= %2 + AND cg." . GRANT_DATE_CHECK_FIELD . " < %3 {$where} GROUP BY cft.id ORDER BY cft.name";