forked from CiviWare/org.agbu.annualgrantbudgets
added validation
This commit is contained in:
@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user