diff --git a/CRM/Grant/BAO/GrantBudget.php b/CRM/Grant/BAO/GrantBudget.php index b100633..13a6151 100644 --- a/CRM/Grant/BAO/GrantBudget.php +++ b/CRM/Grant/BAO/GrantBudget.php @@ -13,7 +13,7 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget { } /** - * Build Fiscal year option list. + * Get grant budget for fiscal year and financial type. * * @param $params array * @@ -78,4 +78,40 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget { return $grantBudget; } + /** + * Validate grant. + * + * @param $params array + * + * @return bool + */ + public static function checkBudget($params, $grantId) { + $year = date('Y', strtotime($params['decision_date'])); + $grantBudget = civicrm_api3('GrantBudget', 'getbudget', [ + 'financial_type_id' => $params['financial_type_id'], + 'fiscal_year' => $year, + ]); + $grantBudget = reset($grantBudget['values']); + $isError = FALSE; + if ($grantBudget) { + $balanceAmount = CRM_Utils_Rule::cleanMoney($grantBudget['balance_amount']); + $amountGranted = CRM_Utils_Rule::cleanMoney($params['amount_granted']); + if ($grantId) { + $oldGrantValues = civicrm_api3('Grant', 'getsingle', [ + 'return' => ["amount_granted", 'financial_type_id', 'decision_date'], + 'id' => $grantId, + ]); + if ($oldGrantValues['financial_type_id'] == $params['financial_type_id'] + && $year == date('Y', strtotime($oldGrantValues['decision_date'])) + ) { + $amountGranted -= $oldGrantValues['amount_granted']; + } + } + if ($balanceAmount < $amountGranted) { + $isError = TRUE; + } + } + return [$isError, $grantBudget]; + } + } diff --git a/annualgrantbudgets.php b/annualgrantbudgets.php index d543b77..6be5457 100644 --- a/annualgrantbudgets.php +++ b/annualgrantbudgets.php @@ -237,18 +237,9 @@ function annualgrantbudgets_civicrm_validateForm($formName, &$fields, &$files, & if ($paidStatusID != $fields['status_id']) { return; } - $year = date('Y', strtotime($fields['decision_date'])); - $grantBudget = civicrm_api3('GrantBudget', 'getbudget', [ - 'financial_type_id' => $fields['financial_type_id'], - 'fiscal_year' => $year, - ]); - $grantBudget = reset($grantBudget['values']); - if ($grantBudget && !empty($grantBudget['id'])) { - $balanceAmount = CRM_Utils_Rule::cleanMoney($grantBudget['balance_amount']); - $amountGranted = CRM_Utils_Rule::cleanMoney($fields['amount_granted']); - if ($balanceAmount < $amountGranted) { - $errors['amount_granted'] = ts("The annual budget for this grant is {$grantBudget['budget']}. Grants totaling {$grantBudget['total_amount_granted']} have been made. {$grantBudget['balance_amount']} remains."); - } + list($isError, $grantBudget) = CRM_Grant_BAO_GrantBudget::checkBudget($fields, $form->getVar('_id')); + if ($isError) { + $errors['amount_granted'] = ts("The annual budget for this grant is {$grantBudget['budget']}. Grants totaling {$grantBudget['total_amount_granted']} have been made. {$grantBudget['balance_amount']} remains."); } } } diff --git a/api/v3/GrantBudget.php b/api/v3/GrantBudget.php index d65cea6..e745658 100644 --- a/api/v3/GrantBudget.php +++ b/api/v3/GrantBudget.php @@ -69,8 +69,5 @@ function _civicrm_api3_grant_budget_getbudget_spec(&$spec) { 'api.required' => 1, 'title' => ts('Fiscal Year'), 'type' => CRM_Utils_Type::T_INT, - 'pseudoconstant' => [ - 'callback' => 'CRM_Grant_BAO_GrantBudget::getFiscalyear', - ], ]; }