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 {
$summary['total_grants']['total_average'] = $dao->average_amount ? CRM_Utils_Money::format($dao->average_amount) : CRM_Utils_Money::format(0);
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(
'label' => $stats[$dao->status_id]['label'],
'label' => CRM_Utils_Array::value('label', CRM_Utils_Array::value($dao->status_id, $stats)),
'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_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),
......
......@@ -87,23 +87,25 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
public function allocate() {
$grantStatus = CRM_Core_OptionGroup::values('grant_status', TRUE);
$algoName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $_POST['algorithm'], 'grouping', 'name');
$statuses = [$grantStatus['Eligible']];
if ($algoName == 'immediate') {
$statuses = $grantStatus['Eligible'].', '.$grantStatus['Awaiting Information'].', '.$grantStatus['Submitted'];
$statuses = array_merge($statuses, [
$grantStatus['Awaiting Information'],
$grantStatus['Submitted']
]);
}
else {
$statuses = $grantStatus['Eligible'];
}
$params = array(
'status_id' => $statuses,
'grant_program_id' => $_POST['pid'],
'assessment' => 'NOT NULL',
);
$result = CRM_Grant_BAO_GrantProgram::getGrants($params);
$result = civicrm_api3('Grant', 'get', [
'status_id' => [
'IN' => $statuses,
'grant_program_id' => $_POST['pid'],
'assessment' => ['IS NOT NULL' => 1],
]
])['values'];
if (!empty($result)) {
if (trim($_POST['algorithm']) == 'Best To Worst, Fully Funded') {
foreach ($result as $key => $row) {
$order[$key] = $row['assessment'];
}
$order = CRM_Utils_Array::collect('assessment', $result);
$sort_order = SORT_DESC;
array_multisort($order, $sort_order, $result);
}
......@@ -220,7 +222,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$page->assign('message', $message);
$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;
CRM_Grant_BAO_GrantProgram::sendMail($_SESSION['CiviCRM']['userID'], $params, 'allocation');
}
......@@ -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');
$algoName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $algoId, 'grouping');
$grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds', TRUE);
if ($algoName == "immediate") {
$statuses = $grantStatus['Eligible'].', '.$grantStatus['Awaiting Information'].', '.$grantStatus['Submitted'];
}
else {
$statuses = $grantStatus['Eligible'];
$statuses = [$grantStatus['Eligible']];
if ($algoName == 'immediate') {
$statuses = array_merge($statuses, [
$grantStatus['Awaiting Information'],
$grantStatus['Submitted']
]);
}
$params = array(
'status_id' => $statuses,
'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)) {
foreach ($result as $key => $row) {
$userparams['contact_id'] = $row['contact_id'];
......@@ -289,6 +297,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$requestedAmount = $row['amount_total'];
}
$amountEligible = $grantThresholds['Maximum Grant'] - $amountGranted;
CRM_Core_Smarty::singleton()->assign('previousGrant', civicrm_api3('Grant', 'getsingle', ['id' => $key]));
if ($requestedAmount > $amountEligible) {
if ($amountEligible == 0) {
$ids['grant'] = $key;
......@@ -305,7 +314,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$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 {
$grantProgramParams['id'] = $_POST['pid'];
$ids['grant_program'] = $_POST['pid'];
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
}
if (!empty($fields['status_id'])) {
$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');
}
if ($grantStatuses[$fields['status_id']] == 'Paid') {
......
......@@ -94,7 +94,7 @@
$('.crm-grant-form-block-amount_granted').remove();
{/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_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information'));
$('.crm-grant-form-block-financial_type').toggle(statusChange);
......@@ -105,7 +105,7 @@
$('#payment-details').toggle(($("#status_id option:selected").text() === 'Paid'));
$('#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_incomplete_reason_id').toggle(($("#status_id option:selected").text() == 'Awaiting Information'));
$('.crm-grant-form-block-financial_type').toggle(statusChange);
......
......@@ -88,26 +88,46 @@
</div>
</div>
</div>
<div id="actionDialog" class="crm-container" style="display:none;"></div>
{literal}
<script type="text/javascript">
cj('.allocation').click(function(){
var r = confirm("Do you want to do a trial allocation?");
if (r == true)
{
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);
}
});
}
});
cj('.allocation').click(function(event){
actionTask('allocation');
return false;
});
function actionTask(task) {
if (task == 'allocation') {
var msg = {/literal}'{ts}Do you want to do a trial allocation?{/ts}'{literal};
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};
}
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(){
var confirmed = 0;
......
......@@ -79,6 +79,7 @@
</div>
{/if}
{/if}
<div id="actionDialog" class="crm-container" style="display:none;"></div>
{if $action neq 4}{literal}
<script type="text/javascript">
......@@ -87,26 +88,34 @@ cj(document).ready(function(){
cj('ul.panel').css('width','250px');
cj('#allocation').click(function(event){
var r=confirm("Do you want to do a trial allocation?");
if (r==true)
{
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;
}
actionTask('allocation');
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){
var confirmed = 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