rewrite validation to support multifund

This commit is contained in:
2019-06-18 20:06:11 -04:00
parent f05a93ddc5
commit 0edd576388
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 = '';
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;
}
}