From 0edd576388dd81d017514314c6c70dcfe421e153 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Tue, 18 Jun 2019 20:06:11 -0400 Subject: [PATCH] rewrite validation to support multifund --- CRM/Grant/BAO/GrantBudget.php | 55 +++++++++++++++++++---------------- annualgrantbudgets.php | 41 ++++++++++---------------- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/CRM/Grant/BAO/GrantBudget.php b/CRM/Grant/BAO/GrantBudget.php index 1c412b5..c041466 100644 --- a/CRM/Grant/BAO/GrantBudget.php +++ b/CRM/Grant/BAO/GrantBudget.php @@ -50,8 +50,8 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget { $where = ''; if (!empty($params['financial_type_id'])) { - $where = 'WHERE cft.id = %5'; - $qParams[5] = [$params['financial_type_id'], 'Integer']; + $where = 'WHERE cft.id = %6'; + $qParams[6] = [$params['financial_type_id'], 'Integer']; } $grantBudgetTableName = CRM_Grant_DAO_GrantBudget::getTableName(); @@ -93,36 +93,41 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget { * Validate grant. * * @param $params array + * @param $grantId Integer * - * @return bool + * @return array An array of errors */ public static function checkBudget($params, $grantId) { $year = date('Y', strtotime($params[GRANT_DATE_CHECK_FIELD])); - $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[GRANT_AMOUNT_CHECK_FIELD]); - if ($grantId) { - $oldGrantValues = civicrm_api3('Grant', 'getsingle', [ - 'return' => [GRANT_AMOUNT_CHECK_FIELD, 'financial_type_id', GRANT_DATE_CHECK_FIELD], - 'id' => $grantId, - ]); - if ($oldGrantValues['financial_type_id'] == $params['financial_type_id'] - && $year == date('Y', strtotime($oldGrantValues[GRANT_DATE_CHECK_FIELD])) - ) { - $amountGranted -= $oldGrantValues[GRANT_AMOUNT_CHECK_FIELD]; + foreach ($params['financial_account'] as $k => $financialAccount) { + if (!$financialAccount) { + break; + } + $financialTypeId = self::getFinancialTypeFromGrantExpenseAccount($financialAccount); + $grantBudget = civicrm_api3('GrantBudget', 'getbudget', [ + 'financial_type_id' => $financialTypeId, + 'fiscal_year' => $year, + ])['values'][0]; + + if ($grantBudget) { + $balanceAmount = CRM_Utils_Rule::cleanMoney($grantBudget['balance_amount']); + $amountGranted = CRM_Utils_Rule::cleanMoney($params['multifund_amount'][$k]); + 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."); } } - if ($balanceAmount < $amountGranted) { - $isError = TRUE; - } } - return [$isError, $grantBudget]; + return $errors; + } + + public static function getFinancialTypeFromGrantExpenseAccount($accountId) { + $financialTypeId = civicrm_api3('EntityFinancialAccount', 'getvalue', [ + 'return' => "entity_id", + 'account_relationship' => "Grant Expense Account is", + 'financial_account_id' => $accountId, + 'entity_table' => "civicrm_financial_type", + ]); + return $financialTypeId; } } diff --git a/annualgrantbudgets.php b/annualgrantbudgets.php index ef62106..0f8c110 100644 --- a/annualgrantbudgets.php +++ b/annualgrantbudgets.php @@ -222,28 +222,19 @@ function annualgrantbudgets_civicrm_apiWrappers(&$wrappers, $apiRequest) { * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_validateForm * */ -// //This whole validation needs rewriting. -//function annualgrantbudgets_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) { -// if ($formName == 'CRM_Grant_Form_Grant' && !($form->_action & CRM_Core_Action::DELETE)) { -// if (empty($fields['financial_type_id']) -// || empty($fields['status_id']) -// || empty($fields[GRANT_DATE_CHECK_FIELD]) -// || empty($fields[GRANT_AMOUNT_CHECK_FIELD]) -// ) { -// return; -// } -// $paidStatusID = CRM_Core_PseudoConstant::getKey( -// 'CRM_Grant_DAO_Grant', -// 'status_id', -// 'Paid' -// ); -// if ($paidStatusID != $fields['status_id']) { -// return; -// } -// -// list($isError, $grantBudget) = CRM_Grant_BAO_GrantBudget::checkBudget($fields, $form->getVar('_id')); -// if ($isError) { -// $errors[GRANT_AMOUNT_CHECK_FIELD] = ts("The annual budget for this grant is {$grantBudget['budget']}. Grants totaling {$grantBudget['total_amount_granted']} have been made. {$grantBudget['balance_amount']} remains."); -// } -// } -//} +function annualgrantbudgets_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) { + if ($formName == 'CRM_Grant_Form_Grant' && !($form->_action & CRM_Core_Action::DELETE)) { + if (empty($fields['financial_type_id']) || empty($fields['status_id']) || empty($fields[GRANT_DATE_CHECK_FIELD]) + ) { + return; + } + $paidStatusID = CRM_Core_PseudoConstant::getKey( + 'CRM_Grant_DAO_Grant', 'status_id', 'Paid' + ); + if ($paidStatusID != $fields['status_id']) { + return; + } + $budgetErrors = CRM_Grant_BAO_GrantBudget::checkBudget($fields, $form->getVar('_id')); + $errors = array_merge($errors, $budgetErrors); + } +}