Commit 35569c1b authored by Monish Deb's avatar Monish Deb

Fix grant allocation code

parent c913cf9f
...@@ -411,11 +411,11 @@ class CRM_Grant_BAO_GrantPayment extends CRM_Grant_DAO_GrantPayment { ...@@ -411,11 +411,11 @@ class CRM_Grant_BAO_GrantPayment extends CRM_Grant_DAO_GrantPayment {
$summary['total_grants']['total_average'] = $dao->average_amount ? CRM_Utils_Money::format($dao->average_amount) : CRM_Utils_Money::format(0); $summary['total_grants']['total_average'] = $dao->average_amount ? CRM_Utils_Money::format($dao->average_amount) : CRM_Utils_Money::format(0);
continue; continue;
} }
if (!empty($dao->status_id)) { if (!empty($dao->status_id) && !empty($stats[$dao->status_id])) {
$programs[$dao->label][$stats[$dao->status_id]['weight']] = array( $programs[$dao->label][$stats[$dao->status_id]['weight']] = array(
'label' => $stats[$dao->status_id]['label'], 'label' => CRM_Utils_Array::value('label', CRM_Utils_Array::value($dao->status_id, $stats)),
'total' => $dao->status_total, 'total' => $dao->status_total,
'value' => $stats[$dao->status_id]['value'], 'value' => CRM_Utils_Array::value('value', CRM_Utils_Array::value($dao->status_id, $stats)),
'amount_requested' => $dao->amount_requested ? CRM_Utils_Money::format($dao->amount_requested) : CRM_Utils_Money::format(0), 'amount_requested' => $dao->amount_requested ? CRM_Utils_Money::format($dao->amount_requested) : CRM_Utils_Money::format(0),
'amount_granted' => $dao->amount_granted ? CRM_Utils_Money::format($dao->amount_granted) : CRM_Utils_Money::format(0), 'amount_granted' => $dao->amount_granted ? CRM_Utils_Money::format($dao->amount_granted) : CRM_Utils_Money::format(0),
'total_paid' => $dao->total_paid ? CRM_Utils_Money::format($dao->total_paid) : CRM_Utils_Money::format(0), 'total_paid' => $dao->total_paid ? CRM_Utils_Money::format($dao->total_paid) : CRM_Utils_Money::format(0),
......
...@@ -87,23 +87,25 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form { ...@@ -87,23 +87,25 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
public function allocate() { public function allocate() {
$grantStatus = CRM_Core_OptionGroup::values('grant_status', TRUE); $grantStatus = CRM_Core_OptionGroup::values('grant_status', TRUE);
$algoName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $_POST['algorithm'], 'grouping', 'name'); $algoName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $_POST['algorithm'], 'grouping', 'name');
$statuses = [$grantStatus['Eligible']];
if ($algoName == 'immediate') { if ($algoName == 'immediate') {
$statuses = $grantStatus['Eligible'].', '.$grantStatus['Awaiting Information'].', '.$grantStatus['Submitted']; $statuses = array_merge($statuses, [
$grantStatus['Awaiting Information'],
$grantStatus['Submitted']
]);
} }
else {
$statuses = $grantStatus['Eligible']; $result = civicrm_api3('Grant', 'get', [
} 'status_id' => [
$params = array( 'IN' => $statuses,
'status_id' => $statuses, 'grant_program_id' => $_POST['pid'],
'grant_program_id' => $_POST['pid'], 'assessment' => ['IS NOT NULL' => 1],
'assessment' => 'NOT NULL', ]
); ])['values'];
$result = CRM_Grant_BAO_GrantProgram::getGrants($params);
if (!empty($result)) { if (!empty($result)) {
if (trim($_POST['algorithm']) == 'Best To Worst, Fully Funded') { if (trim($_POST['algorithm']) == 'Best To Worst, Fully Funded') {
foreach ($result as $key => $row) { $order = CRM_Utils_Array::collect('assessment', $result);
$order[$key] = $row['assessment'];
}
$sort_order = SORT_DESC; $sort_order = SORT_DESC;
array_multisort($order, $sort_order, $result); array_multisort($order, $sort_order, $result);
} }
...@@ -220,7 +222,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form { ...@@ -220,7 +222,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$page->assign('message', $message); $page->assign('message', $message);
$page->assign('grant_program_name', $grantPrograms[$_POST['pid']]); $page->assign('grant_program_name', $grantPrograms[$_POST['pid']]);
CRM_Core_Session::setStatus($message, '', 'no-popup'); CRM_Core_Session::setStatus($message, '', 'success');
$params['is_auto_email'] = 1; $params['is_auto_email'] = 1;
CRM_Grant_BAO_GrantProgram::sendMail($_SESSION['CiviCRM']['userID'], $params, 'allocation'); CRM_Grant_BAO_GrantProgram::sendMail($_SESSION['CiviCRM']['userID'], $params, 'allocation');
} }
...@@ -267,17 +269,23 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form { ...@@ -267,17 +269,23 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$algoId = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_GrantProgram', $_POST['pid'], 'allocation_algorithm'); $algoId = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_GrantProgram', $_POST['pid'], 'allocation_algorithm');
$algoName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $algoId, 'grouping'); $algoName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $algoId, 'grouping');
$grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds', TRUE); $grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds', TRUE);
if ($algoName == "immediate") { $statuses = [$grantStatus['Eligible']];
$statuses = $grantStatus['Eligible'].', '.$grantStatus['Awaiting Information'].', '.$grantStatus['Submitted']; if ($algoName == 'immediate') {
} $statuses = array_merge($statuses, [
else { $grantStatus['Awaiting Information'],
$statuses = $grantStatus['Eligible']; $grantStatus['Submitted']
]);
} }
$params = array( $params = array(
'status_id' => $statuses, 'status_id' => $statuses,
'grant_program_id' => $_POST['pid'], 'grant_program_id' => $_POST['pid'],
); );
$result = CRM_Grant_BAO_GrantProgram::getGrants($params); $result = civicrm_api3('Grant', 'get', [
'status_id' => [
'IN' => $statuses,
'grant_program_id' => $_POST['pid'],
]
])['values'];
if (!empty($result)) { if (!empty($result)) {
foreach ($result as $key => $row) { foreach ($result as $key => $row) {
$userparams['contact_id'] = $row['contact_id']; $userparams['contact_id'] = $row['contact_id'];
...@@ -289,6 +297,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form { ...@@ -289,6 +297,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$requestedAmount = $row['amount_total']; $requestedAmount = $row['amount_total'];
} }
$amountEligible = $grantThresholds['Maximum Grant'] - $amountGranted; $amountEligible = $grantThresholds['Maximum Grant'] - $amountGranted;
CRM_Core_Smarty::singleton()->assign('previousGrant', civicrm_api3('Grant', 'getsingle', ['id' => $key]));
if ($requestedAmount > $amountEligible) { if ($requestedAmount > $amountEligible) {
if ($amountEligible == 0) { if ($amountEligible == 0) {
$ids['grant'] = $key; $ids['grant'] = $key;
...@@ -305,7 +314,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form { ...@@ -305,7 +314,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$result = CRM_Grant_BAO_Grant::add($row, $ids); $result = CRM_Grant_BAO_Grant::add($row, $ids);
} }
} }
CRM_Core_Session::setStatus(ts('Approved allocations finalized successfully.'), '', 'no-popup'); CRM_Core_Session::setStatus(ts('Approved allocations finalized successfully.'), '', 'success');
} }
} }
...@@ -339,7 +348,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form { ...@@ -339,7 +348,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$grantProgramParams['id'] = $_POST['pid']; $grantProgramParams['id'] = $_POST['pid'];
$ids['grant_program'] = $_POST['pid']; $ids['grant_program'] = $_POST['pid'];
CRM_Grant_BAO_GrantProgram::create($grantProgramParams, $ids); CRM_Grant_BAO_GrantProgram::create($grantProgramParams, $ids);
CRM_Core_Session::setStatus(ts('Marked remaining unapproved Grants as Ineligible successfully.'), '', 'no-popup'); CRM_Core_Session::setStatus(ts('Marked remaining unapproved Grants as Ineligible successfully.'), '', 'success');
} }
} }
} }
...@@ -659,7 +659,7 @@ function grantprograms_civicrm_validateForm($formName, &$fields, &$files, &$form ...@@ -659,7 +659,7 @@ function grantprograms_civicrm_validateForm($formName, &$fields, &$files, &$form
} }
if (!empty($fields['status_id'])) { if (!empty($fields['status_id'])) {
$grantStatuses = CRM_Core_OptionGroup::values('grant_status'); $grantStatuses = CRM_Core_OptionGroup::values('grant_status');
if (in_array($grantStatuses[$fields['status_id']], ['Paid', 'Withdrawn', 'Approved for Payment']) && empty($fields['financial_type_id'])) { if (in_array($grantStatuses[$fields['status_id']], ['Paid', 'Withdrawn', 'Approved for Payment', 'Eligible']) && empty($fields['financial_type_id'])) {
$errors['financial_type_id'] = ts('Financial Type is a required field'); $errors['financial_type_id'] = ts('Financial Type is a required field');
} }
if ($grantStatuses[$fields['status_id']] == 'Paid') { if ($grantStatuses[$fields['status_id']] == 'Paid') {
......
...@@ -94,7 +94,7 @@ ...@@ -94,7 +94,7 @@
$('.crm-grant-form-block-amount_granted').remove(); $('.crm-grant-form-block-amount_granted').remove();
{/literal}{/if}{literal} {/literal}{/if}{literal}
var statusChange = ($.inArray($("#status_id option:selected").text(), ['Paid', 'Approved for Payment', 'Withdrawn']) > -1); var statusChange = ($.inArray($("#status_id option:selected").text(), ['Paid', 'Approved for Payment', 'Withdrawn', 'Eligible']) > -1);
$('.grant_rejected_reason_id').toggle(($("#status_id option:selected").text() == 'Ineligible')); $('.grant_rejected_reason_id').toggle(($("#status_id option:selected").text() == 'Ineligible'));
$('.grant_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information')); $('.grant_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information'));
$('.crm-grant-form-block-financial_type').toggle(statusChange); $('.crm-grant-form-block-financial_type').toggle(statusChange);
...@@ -105,7 +105,7 @@ ...@@ -105,7 +105,7 @@
$('#payment-details').toggle(($("#status_id option:selected").text() === 'Paid')); $('#payment-details').toggle(($("#status_id option:selected").text() === 'Paid'));
$('#status_id').on('change', function() { $('#status_id').on('change', function() {
var statusChange = ($.inArray($("#status_id option:selected").text(), ['Paid', 'Approved for Payment', 'Withdrawn']) > -1); var statusChange = ($.inArray($("#status_id option:selected").text(), ['Paid', 'Approved for Payment', 'Withdrawn', 'Eligible']) > -1);
$('.grant_rejected_reason_id').toggle(($("#status_id option:selected").text() == 'Ineligible')); $('.grant_rejected_reason_id').toggle(($("#status_id option:selected").text() == 'Ineligible'));
$('.grant_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information')); $('.grant_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information'));
$('.crm-grant-form-block-financial_type').toggle(statusChange); $('.crm-grant-form-block-financial_type').toggle(statusChange);
......
...@@ -88,26 +88,46 @@ ...@@ -88,26 +88,46 @@
</div> </div>
</div> </div>
</div> </div>
<div id="actionDialog" class="crm-container" style="display:none;"></div>
{literal} {literal}
<script type="text/javascript"> <script type="text/javascript">
cj('.allocation').click(function(){ cj('.allocation').click(function(event){
var r = confirm("Do you want to do a trial allocation?"); actionTask('allocation');
if (r == true) return false;
{ });
var data = 'pid={/literal}{$id}{literal}&amount={/literal}{$total_amount}{literal}&remainder_amount={/literal}{$remainder_amount}{literal}&algorithm={/literal}{$grantProgramAlgorithm}{literal}';
var dataURL = {/literal}"{crmURL p='civicrm/grant_program/allocate'}"{literal}; function actionTask(task) {
cj.ajax({ if (task == 'allocation') {
url: dataURL, var msg = {/literal}'{ts}Do you want to do a trial allocation?{/ts}'{literal};
data: data, var data = 'pid={/literal}{$id}{literal}&amount={/literal}{$total_amount}{literal}&remainder_amount={/literal}{$remainder_amount}{literal}&algorithm={/literal}{$grantProgramAlgorithm}{literal}';
type: 'POST', var dataURL = {/literal}"{crmURL p='civicrm/grant_program/allocate'}"{literal};
success: function(output) { }
setTimeout("location.reload(true);",1500); CRM.$('#actionDialog').dialog({
} title: {/literal}'{ts}Grant Allocation{/ts}'{literal},
}); modal: true,
} open:function() {
}); CRM.$(this).show().html(msg);
},
buttons: {
{/literal}"{ts escape='js'}No{/ts}"{literal}: function() {
CRM.$(this).dialog("close");
},
{/literal}"{ts escape='js'}Yes{/ts}"{literal}: function() {
CRM.$(this).dialog("close");
cj.ajax({
url: dataURL,
data: data,
type: 'POST',
success: function(output) {
setTimeout("location.reload(true);",1500);
}
});
return;
}
}
});
}
cj('.finalize').click(function(){ cj('.finalize').click(function(){
var confirmed = 0; var confirmed = 0;
......
...@@ -79,6 +79,7 @@ ...@@ -79,6 +79,7 @@
</div> </div>
{/if} {/if}
{/if} {/if}
<div id="actionDialog" class="crm-container" style="display:none;"></div>
{if $action neq 4}{literal} {if $action neq 4}{literal}
<script type="text/javascript"> <script type="text/javascript">
...@@ -87,26 +88,34 @@ cj(document).ready(function(){ ...@@ -87,26 +88,34 @@ cj(document).ready(function(){
cj('ul.panel').css('width','250px'); cj('ul.panel').css('width','250px');
cj('#allocation').click(function(event){ cj('#allocation').click(function(event){
var r=confirm("Do you want to do a trial allocation?"); actionTask('allocation');
if (r==true) return false;
{
event.preventDefault();
var data = 'pid={/literal}{$id}{literal}&amount={/literal}{$total_amount}{literal}&remainder_amount={/literal}{$remainder_amount}{literal}&algorithm={/literal}{$grantProgramAlgorithm}{literal}';
var dataURL = {/literal}"{crmURL p='civicrm/grant_program/allocate'}"{literal};
cj.ajax({
url: dataURL,
data: data,
type: 'POST',
success: function(output) {
setTimeout("location.reload(true);",1500);
}
});
}
else {
return false;
}
}); });
function actionTask(type) {
if (task == 'allocation') {
CRM.$('#actionDialog').dialog({
title: {/literal}'{ts}Grant Allocation{/ts}'{literal},
modal: true,
open:function() {
CRM.$('#actionDialog').show().html({/literal}"{ts escape='js'}Do you want to do a trial allocation?{/ts}"{literal});
},
buttons: {
{/literal}"{ts escape='js'}No{/ts}"{literal}: function() {
CRM.$(this).dialog("close");
},
{/literal}"{ts escape='js'}Yes{/ts}"{literal}: function() {
CRM.$(this).dialog("close");
var data = 'pid={/literal}{$id}{literal}&amount={/literal}{$total_amount}{literal}&remainder_amount={/literal}{$remainder_amount}{literal}&algorithm={/literal}{$grantProgramAlgorithm}{literal}';
var dataURL = {/literal}"{crmURL p='civicrm/grant_program/allocate'}"{literal};
window.location.href = dataURL;
return;
}
}
});
}
}
cj('#finalize').click(function(event){ cj('#finalize').click(function(event){
var confirmed = 0; var confirmed = 0;
var totalAmounts = 0; var totalAmounts = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment