rewrite validation to support multifund

This commit is contained in:
Jon Goldberg 2019-06-18 20:06:11 -04:00
parent f05a93ddc5
commit 0edd576388
No known key found for this signature in database
GPG Key ID: C2D2247364F9DB13
2 changed files with 46 additions and 50 deletions

View File

@ -50,8 +50,8 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget {
$where = ''; $where = '';
if (!empty($params['financial_type_id'])) { if (!empty($params['financial_type_id'])) {
$where = 'WHERE cft.id = %5'; $where = 'WHERE cft.id = %6';
$qParams[5] = [$params['financial_type_id'], 'Integer']; $qParams[6] = [$params['financial_type_id'], 'Integer'];
} }
$grantBudgetTableName = CRM_Grant_DAO_GrantBudget::getTableName(); $grantBudgetTableName = CRM_Grant_DAO_GrantBudget::getTableName();
@ -93,36 +93,41 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget {
* Validate grant. * Validate grant.
* *
* @param $params array * @param $params array
* @param $grantId Integer
* *
* @return bool * @return array An array of errors
*/ */
public static function checkBudget($params, $grantId) { public static function checkBudget($params, $grantId) {
$year = date('Y', strtotime($params[GRANT_DATE_CHECK_FIELD])); $year = date('Y', strtotime($params[GRANT_DATE_CHECK_FIELD]));
$grantBudget = civicrm_api3('GrantBudget', 'getbudget', [ foreach ($params['financial_account'] as $k => $financialAccount) {
'financial_type_id' => $params['financial_type_id'], if (!$financialAccount) {
'fiscal_year' => $year, break;
]); }
$grantBudget = reset($grantBudget['values']); $financialTypeId = self::getFinancialTypeFromGrantExpenseAccount($financialAccount);
$isError = FALSE; $grantBudget = civicrm_api3('GrantBudget', 'getbudget', [
if ($grantBudget) { 'financial_type_id' => $financialTypeId,
$balanceAmount = CRM_Utils_Rule::cleanMoney($grantBudget['balance_amount']); 'fiscal_year' => $year,
$amountGranted = CRM_Utils_Rule::cleanMoney($params[GRANT_AMOUNT_CHECK_FIELD]); ])['values'][0];
if ($grantId) {
$oldGrantValues = civicrm_api3('Grant', 'getsingle', [ if ($grantBudget) {
'return' => [GRANT_AMOUNT_CHECK_FIELD, 'financial_type_id', GRANT_DATE_CHECK_FIELD], $balanceAmount = CRM_Utils_Rule::cleanMoney($grantBudget['balance_amount']);
'id' => $grantId, $amountGranted = CRM_Utils_Rule::cleanMoney($params['multifund_amount'][$k]);
]); if ($balanceAmount < $amountGranted) {
if ($oldGrantValues['financial_type_id'] == $params['financial_type_id'] $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.");
&& $year == date('Y', strtotime($oldGrantValues[GRANT_DATE_CHECK_FIELD]))
) {
$amountGranted -= $oldGrantValues[GRANT_AMOUNT_CHECK_FIELD];
} }
} }
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;
} }
} }

View File

@ -222,28 +222,19 @@ function annualgrantbudgets_civicrm_apiWrappers(&$wrappers, $apiRequest) {
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_validateForm * @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) {
//function annualgrantbudgets_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) { if ($formName == 'CRM_Grant_Form_Grant' && !($form->_action & CRM_Core_Action::DELETE)) {
// 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])
// if (empty($fields['financial_type_id']) ) {
// || empty($fields['status_id']) return;
// || empty($fields[GRANT_DATE_CHECK_FIELD]) }
// || empty($fields[GRANT_AMOUNT_CHECK_FIELD]) $paidStatusID = CRM_Core_PseudoConstant::getKey(
// ) { 'CRM_Grant_DAO_Grant', 'status_id', 'Paid'
// return; );
// } if ($paidStatusID != $fields['status_id']) {
// $paidStatusID = CRM_Core_PseudoConstant::getKey( return;
// 'CRM_Grant_DAO_Grant', }
// 'status_id', $budgetErrors = CRM_Grant_BAO_GrantBudget::checkBudget($fields, $form->getVar('_id'));
// 'Paid' $errors = array_merge($errors, $budgetErrors);
// ); }
// 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.");
// }
// }
//}