org.agbu.annualgrantbudgets/CRM/AnnualGrantBudgets/BAO/GrantBudget.php

166 lines
6.4 KiB
PHP
Raw Permalink Normal View History

<?php
class CRM_AnnualGrantBudgets_BAO_GrantBudget extends CRM_AnnualGrantBudgets_DAO_GrantBudget {
2018-08-11 23:43:25 +00:00
/**
* Build Fiscal year option list.
*
*/
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) {
2019-08-19 21:06:17 +00:00
$returnTotals = $params['return_totals'];
$totals = [];
2018-08-11 16:46:21 +00:00
$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'
2018-08-11 18:29:59 +00:00
);
$accountRelationshipId = civicrm_api3('OptionValue', 'getvalue', [
'return' => "value",
'option_group_id' => "account_relationship",
'name' => "Grant Expense Account is",
]);
2018-08-11 18:29:59 +00:00
$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'],
5 => [$accountRelationshipId, 'Integer'],
2018-08-11 16:46:21 +00:00
];
$where = '';
if (!empty($params['financial_type_id'])) {
$where = 'WHERE cft.id = %6';
$qParams[6] = [$params['financial_type_id'], 'Integer'];
2018-08-11 16:46:21 +00:00
}
$grantBudgetTableName = CRM_AnnualGrantBudgets_DAO_GrantBudget::getTableName();
2018-08-11 19:37:33 +00:00
$sql = "SELECT cgb.id, cft.name, IFNULL(cgb.budget, 0) AS budget,
SUM(CASE WHEN cg.id IS NULL THEN 0 ELSE IFNULL(trxn.total_amount, 0) END) as total_amount_granted,
2019-08-19 22:59:03 +00:00
cft.id AS financial_type_id, cgb.note, cgb.is_reserved
2018-08-11 16:46:21 +00:00
FROM civicrm_financial_type cft
INNER JOIN " . GRANT_SCHOLARSHIP_CUSTOM_TABLE_NAME . " gs
2018-08-12 00:17:25 +00:00
ON gs.entity_id = cft.id
AND gs." . GRANT_SCHOLARSHIP_CUSTOM_FIELD . " = 1
2018-08-11 16:46:21 +00:00
LEFT JOIN " . $grantBudgetTableName . " cgb
ON cgb.financial_type_id = cft.id AND cgb.fiscal_year = %1
LEFT JOIN civicrm_entity_financial_account cefa
ON cefa.entity_table = 'civicrm_financial_type' AND cft.id = cefa.entity_id AND account_relationship = %5
LEFT JOIN civicrm_financial_trxn trxn ON trxn.from_financial_account_id = cefa.financial_account_id
LEFT JOIN civicrm_entity_financial_trxn ceft on trxn.id = ceft.financial_trxn_id AND ceft.entity_table = 'civicrm_grant'
LEFT JOIN civicrm_grant cg
ON cg.id = ceft.entity_id AND cg.status_id IN (%4)
AND cg." . GRANT_DATE_CHECK_FIELD . " >= %2
AND cg." . GRANT_DATE_CHECK_FIELD . " < %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,
'budget' => $result->budget,
'total_amount_granted' => $result->total_amount_granted,
'balance_amount' => $result->budget - $result->total_amount_granted,
2018-08-11 19:37:33 +00:00
'financial_type_id' => $result->financial_type_id,
2019-08-19 22:59:03 +00:00
'note' => $result->note,
'is_reserved' => $result->is_reserved,
2018-08-11 16:46:21 +00:00
];
2019-08-19 21:06:17 +00:00
if ($returnTotals) {
$totals['budget'] += $result->budget;
$totals['total_amount_granted'] += $result->total_amount_granted;
$totals['balance_amount'] += $result->budget - $result->total_amount_granted;
}
}
if ($returnTotals) {
// Format as money.
foreach ($totals as $k => $v) {
$totals[$k] = CRM_Utils_Money::format($v);
}
return $totals;
2018-08-11 16:46:21 +00:00
}
return $grantBudget;
}
2018-08-12 00:10:36 +00:00
/**
* Validate grant.
*
* @param $params array
* @param $grantId Integer
2018-08-12 00:10:36 +00:00
*
* @return array An array of errors
2018-08-12 00:10:36 +00:00
*/
public static function checkBudget($params, $grantId) {
2018-08-12 00:17:25 +00:00
$year = date('Y', strtotime($params[GRANT_DATE_CHECK_FIELD]));
foreach ($params['financial_account'] as $k => $financialAccount) {
if (!$financialAccount) {
break;
2018-08-12 00:10:36 +00:00
}
$financialTypeId = self::getFinancialTypeFromGrantExpenseAccount($financialAccount);
$grantBudget = civicrm_api3('GrantBudget', 'getbudget', [
'financial_type_id' => $financialTypeId,
'fiscal_year' => $year,
])['values'][0];
if ($grantBudget) {
$balanceAmount = $grantBudget['balance_amount'];
$amountGranted = $params['multifund_amount'][$k];
// If there's a $grantId, we're updating an existing grant. Make sure we don't
// double-count this money on validation.
if ($grantId) {
$oldGrantLineItem = civicrm_api3('EntityFinancialTrxn', 'get', [
'sequential' => 1,
'return' => ["financial_trxn_id.total_amount", "financial_trxn_id.trxn_date"],
'entity_table' => "civicrm_grant",
'entity_id' => $grantId,
'financial_trxn_id.from_financial_account_id' => $financialAccount,
])['values'][0];
if ($oldGrantLineItem && $year == date('Y', strtotime($oldGrantLineItem['financial_trxn_id.trxn_date']))) {
$amountGranted -= $oldGrantLineItem['financial_trxn_id.total_amount'];
}
}
if ($balanceAmount < $amountGranted) {
$budgetDisplay = CRM_Utils_Money::format($grantBudget['budget']);
$totalGrantedDisplay = CRM_Utils_Money::format($grantBudget['total_amount_granted']);
$balanceDisplay = CRM_Utils_Money::format($grantBudget['balance_amount']);
$errors["multifund_amount[$k]"] = ts("The annual budget for this grant is $budgetDisplay. Grants totaling $totalGrantedDisplay have been made. $balanceDisplay remains.");
}
2018-08-12 00:10:36 +00:00
}
}
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;
2018-08-12 00:10:36 +00:00
}
}