2018-08-11 11:33:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget {
|
|
|
|
|
2018-08-11 23:43:25 +00:00
|
|
|
/**
|
|
|
|
* Build Fiscal year option list.
|
|
|
|
*
|
|
|
|
*/
|
2018-08-11 11:33:32 +00:00
|
|
|
public static function getFiscalyear() {
|
2018-08-11 18:29:59 +00:00
|
|
|
$from = date("Y", strtotime(date('Y') . ' - ' . GRANT_SCHOLARSHIP_YEAR_DIFF . ' years'));
|
|
|
|
$to = date("Y", strtotime(date('Y') . ' + ' . GRANT_SCHOLARSHIP_YEAR_DIFF . ' years'));
|
|
|
|
return array_combine(range($to, $from), range($to, $from));
|
2018-08-11 16:46:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 23:43:25 +00:00
|
|
|
/**
|
2018-08-12 00:10:36 +00:00
|
|
|
* Get grant budget for fiscal year and financial type.
|
2018-08-11 23:43:25 +00:00
|
|
|
*
|
|
|
|
* @param $params array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-08-11 16:46:21 +00:00
|
|
|
public static function getGrantBudget($params) {
|
|
|
|
$fiscalYear = $params['fiscal_year'];
|
|
|
|
if (empty($fiscalYear)) {
|
|
|
|
$fiscalYear = date('Y');
|
|
|
|
}
|
|
|
|
$grantBudget = [];
|
2018-08-11 18:29:59 +00:00
|
|
|
$paidStatusID = CRM_Core_PseudoConstant::getKey(
|
|
|
|
'CRM_Grant_DAO_Grant',
|
|
|
|
'status_id',
|
|
|
|
'Paid'
|
|
|
|
);
|
|
|
|
|
|
|
|
$config = CRM_Core_Config::singleton();
|
2018-08-11 23:43:25 +00:00
|
|
|
$mkTime = mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], $fiscalYear);
|
2018-08-11 18:29:59 +00:00
|
|
|
$fiscalStartDate = date('Y-m-d', $mkTime);
|
2018-08-11 19:37:33 +00:00
|
|
|
$fiscalEndDate = date('Y-m-d', strtotime($fiscalStartDate . '+ 1 year'));
|
2018-08-11 18:29:59 +00:00
|
|
|
|
2018-08-11 16:46:21 +00:00
|
|
|
$qParams = [
|
|
|
|
1 => [$fiscalYear, 'String'],
|
2018-08-11 18:29:59 +00:00
|
|
|
2 => [$fiscalStartDate, 'String'],
|
|
|
|
3 => [$fiscalEndDate, 'String'],
|
2018-08-11 16:46:21 +00:00
|
|
|
4 => [$paidStatusID, 'Integer'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$where = '';
|
|
|
|
if (!empty($params['financial_type_id'])) {
|
|
|
|
$where = 'WHERE cft.id = %5';
|
|
|
|
$qParams[5] = [$params['financial_type_id'], 'Integer'];
|
|
|
|
}
|
|
|
|
$grantBudgetTableName = CRM_Grant_DAO_GrantBudget::getTableName();
|
2018-08-11 19:37:33 +00:00
|
|
|
$sql = "SELECT cgb.id, cft.name, IFNULL(cgb.budget, 0) AS budget,
|
|
|
|
SUM(IFNULL(cg.amount_granted, 0)) as total_amount_granted,
|
|
|
|
cft.id AS financial_type_id
|
2018-08-11 16:46:21 +00:00
|
|
|
FROM civicrm_financial_type cft
|
|
|
|
INNER JOIN " . GRANT_SCHOLARSHIP_CUSTOM_TABLE_NAME . " gs
|
|
|
|
ON gs.entity_id = cft.id AND gs." . GRANT_SCHOLARSHIP_CUSTOM_FIELD . " = 1
|
|
|
|
LEFT JOIN " . $grantBudgetTableName . " cgb
|
|
|
|
ON cgb.financial_type_id = cft.id AND cgb.fiscal_year = %1
|
|
|
|
LEFT JOIN civicrm_grant cg
|
|
|
|
ON cg.financial_type_id = cft.id AND cg.status_id IN (%4)
|
2018-08-11 18:29:59 +00:00
|
|
|
AND cg.decision_date >= %2 AND cg.decision_date < %3
|
2018-08-11 17:51:25 +00:00
|
|
|
{$where}
|
2018-08-11 16:46:21 +00:00
|
|
|
GROUP BY cft.id
|
|
|
|
ORDER BY cft.name";
|
|
|
|
$result = CRM_Core_DAO::executeQuery($sql, $qParams);
|
|
|
|
|
|
|
|
while ($result->fetch()) {
|
2018-08-11 19:37:33 +00:00
|
|
|
$grantBudget[] = [
|
2018-08-11 17:51:25 +00:00
|
|
|
'id' => $result->id,
|
2018-08-11 16:46:21 +00:00
|
|
|
'name' => $result->name,
|
2018-08-11 17:51:25 +00:00
|
|
|
'budget' => CRM_Utils_Money::format($result->budget),
|
|
|
|
'total_amount_granted' => CRM_Utils_Money::format($result->total_amount_granted),
|
|
|
|
'balance_amount' => CRM_Utils_Money::format(($result->budget - $result->total_amount_granted)),
|
2018-08-11 19:37:33 +00:00
|
|
|
'financial_type_id' => $result->financial_type_id,
|
2018-08-11 16:46:21 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return $grantBudget;
|
2018-08-11 11:33:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 00:10:36 +00:00
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
}
|
|
|
|
|
2018-08-11 11:33:32 +00:00
|
|
|
}
|