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]));
foreach ($params['financial_account'] as $k => $financialAccount) {
if (!$financialAccount) {
break;
}
$financialTypeId = self::getFinancialTypeFromGrantExpenseAccount($financialAccount);
$grantBudget = civicrm_api3('GrantBudget', 'getbudget', [ $grantBudget = civicrm_api3('GrantBudget', 'getbudget', [
'financial_type_id' => $params['financial_type_id'], 'financial_type_id' => $financialTypeId,
'fiscal_year' => $year, 'fiscal_year' => $year,
]); ])['values'][0];
$grantBudget = reset($grantBudget['values']);
$isError = FALSE;
if ($grantBudget) { if ($grantBudget) {
$balanceAmount = CRM_Utils_Rule::cleanMoney($grantBudget['balance_amount']); $balanceAmount = CRM_Utils_Rule::cleanMoney($grantBudget['balance_amount']);
$amountGranted = CRM_Utils_Rule::cleanMoney($params[GRANT_AMOUNT_CHECK_FIELD]); $amountGranted = CRM_Utils_Rule::cleanMoney($params['multifund_amount'][$k]);
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];
}
}
if ($balanceAmount < $amountGranted) { if ($balanceAmount < $amountGranted) {
$isError = TRUE; $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.");
} }
} }
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.");
// }
// }
//}