Use constant

This commit is contained in:
CiviWare Solutions 2018-08-12 05:47:25 +05:30
parent dc36f24333
commit 6559524f46
2 changed files with 15 additions and 11 deletions

View File

@ -50,16 +50,18 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget {
} }
$grantBudgetTableName = CRM_Grant_DAO_GrantBudget::getTableName(); $grantBudgetTableName = CRM_Grant_DAO_GrantBudget::getTableName();
$sql = "SELECT cgb.id, cft.name, IFNULL(cgb.budget, 0) AS budget, $sql = "SELECT cgb.id, cft.name, IFNULL(cgb.budget, 0) AS budget,
SUM(IFNULL(cg.amount_granted, 0)) as total_amount_granted, SUM(IFNULL(cg." . GRANT_AMOUNT_CHECK_FIELD . ", 0)) as total_amount_granted,
cft.id AS financial_type_id cft.id AS financial_type_id
FROM civicrm_financial_type cft FROM civicrm_financial_type cft
INNER JOIN " . GRANT_SCHOLARSHIP_CUSTOM_TABLE_NAME . " gs INNER JOIN " . GRANT_SCHOLARSHIP_CUSTOM_TABLE_NAME . " gs
ON gs.entity_id = cft.id AND gs." . GRANT_SCHOLARSHIP_CUSTOM_FIELD . " = 1 ON gs.entity_id = cft.id
AND gs." . GRANT_SCHOLARSHIP_CUSTOM_FIELD . " = 1
LEFT JOIN " . $grantBudgetTableName . " cgb LEFT JOIN " . $grantBudgetTableName . " cgb
ON cgb.financial_type_id = cft.id AND cgb.fiscal_year = %1 ON cgb.financial_type_id = cft.id AND cgb.fiscal_year = %1
LEFT JOIN civicrm_grant cg LEFT JOIN civicrm_grant cg
ON cg.financial_type_id = cft.id AND cg.status_id IN (%4) ON cg.financial_type_id = cft.id AND cg.status_id IN (%4)
AND cg.decision_date >= %2 AND cg.decision_date < %3 AND cg." . GRANT_DATE_CHECK_FIELD . " >= %2
AND cg." . GRANT_DATE_CHECK_FIELD . " < %3
{$where} {$where}
GROUP BY cft.id GROUP BY cft.id
ORDER BY cft.name"; ORDER BY cft.name";
@ -86,7 +88,7 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget {
* @return bool * @return bool
*/ */
public static function checkBudget($params, $grantId) { public static function checkBudget($params, $grantId) {
$year = date('Y', strtotime($params['decision_date'])); $year = date('Y', strtotime($params[GRANT_DATE_CHECK_FIELD]));
$grantBudget = civicrm_api3('GrantBudget', 'getbudget', [ $grantBudget = civicrm_api3('GrantBudget', 'getbudget', [
'financial_type_id' => $params['financial_type_id'], 'financial_type_id' => $params['financial_type_id'],
'fiscal_year' => $year, 'fiscal_year' => $year,
@ -95,16 +97,16 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget {
$isError = FALSE; $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['amount_granted']); $amountGranted = CRM_Utils_Rule::cleanMoney($params[GRANT_AMOUNT_CHECK_FIELD]);
if ($grantId) { if ($grantId) {
$oldGrantValues = civicrm_api3('Grant', 'getsingle', [ $oldGrantValues = civicrm_api3('Grant', 'getsingle', [
'return' => ["amount_granted", 'financial_type_id', 'decision_date'], 'return' => [GRANT_AMOUNT_CHECK_FIELD, 'financial_type_id', GRANT_DATE_CHECK_FIELD],
'id' => $grantId, 'id' => $grantId,
]); ]);
if ($oldGrantValues['financial_type_id'] == $params['financial_type_id'] if ($oldGrantValues['financial_type_id'] == $params['financial_type_id']
&& $year == date('Y', strtotime($oldGrantValues['decision_date'])) && $year == date('Y', strtotime($oldGrantValues[GRANT_DATE_CHECK_FIELD]))
) { ) {
$amountGranted -= $oldGrantValues['amount_granted']; $amountGranted -= $oldGrantValues[GRANT_AMOUNT_CHECK_FIELD];
} }
} }
if ($balanceAmount < $amountGranted) { if ($balanceAmount < $amountGranted) {

View File

@ -7,6 +7,8 @@ define('GRANT_SCHOLARSHIP_CUSTOM_GROUP', 'grant_scholarship');
define('GRANT_SCHOLARSHIP_CUSTOM_FIELD', 'is_grant_scholarship'); define('GRANT_SCHOLARSHIP_CUSTOM_FIELD', 'is_grant_scholarship');
define('GRANT_SCHOLARSHIP_CUSTOM_TABLE_NAME', 'civicrm_value_grant_scholarship'); define('GRANT_SCHOLARSHIP_CUSTOM_TABLE_NAME', 'civicrm_value_grant_scholarship');
define('GRANT_SCHOLARSHIP_YEAR_DIFF', 10); define('GRANT_SCHOLARSHIP_YEAR_DIFF', 10);
define('GRANT_DATE_CHECK_FIELD', 'decision_date');
define('GRANT_AMOUNT_CHECK_FIELD', 'amount_granted');
/** /**
* Implements hook_civicrm_config(). * Implements hook_civicrm_config().
* *
@ -224,8 +226,8 @@ function annualgrantbudgets_civicrm_validateForm($formName, &$fields, &$files, &
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']) if (empty($fields['financial_type_id'])
|| empty($fields['status_id']) || empty($fields['status_id'])
|| empty($fields['decision_date']) || empty($fields[GRANT_DATE_CHECK_FIELD])
|| empty($fields['amount_granted']) || empty($fields[GRANT_AMOUNT_CHECK_FIELD])
) { ) {
return; return;
} }
@ -239,7 +241,7 @@ function annualgrantbudgets_civicrm_validateForm($formName, &$fields, &$files, &
} }
list($isError, $grantBudget) = CRM_Grant_BAO_GrantBudget::checkBudget($fields, $form->getVar('_id')); list($isError, $grantBudget) = CRM_Grant_BAO_GrantBudget::checkBudget($fields, $form->getVar('_id'));
if ($isError) { if ($isError) {
$errors['amount_granted'] = ts("The annual budget for this grant is {$grantBudget['budget']}. Grants totaling {$grantBudget['total_amount_granted']} have been made. {$grantBudget['balance_amount']} remains."); $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.");
} }
} }
} }