Commit 3d27cea8 authored by Mayur Jadhav's avatar Mayur Jadhav

Worked on RG-138

parent 545d5844
......@@ -342,4 +342,19 @@ WHERE civicrm_contact.id = $id ";
}
return empty($amountGranted) ? 0 : $amountGranted;
}
/**
* Function to get current grant granted amount
*
* @return int amount granted
* @access public
* @static
*/
static function getCurrentGrantAmount($id = NULL) {
if ($id != NULL) {
$query = "SELECT amount_granted FROM civicrm_grant WHERE id = " . $id;
$amountGranted = CRM_Core_DAO::singleValueQuery($query);
}
return empty($amountGranted) ? 0 : $amountGranted;
}
}
......@@ -98,11 +98,9 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
'grant_program_id' => $_POST['pid'],
'assessment' => 'NOT NULL',
);
$result = CRM_Grant_BAO_GrantProgram::getGrants($params);
if (!empty($result)) {
if ($_POST['algorithm'] == 'Best to Worst, Fully Funded') {
if (trim($_POST['algorithm']) == 'Best To Worst, Fully Funded') {
foreach ($result as $key => $row) {
$order[$key] = $row['assessment'];
}
......@@ -115,18 +113,18 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$contact = array();
$grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds', TRUE);
foreach ($result as $key => $value) {
$value['amount_total'] = str_replace(',', '', $value['amount_total']);
$value['amount_total'] = CRM_Utils_Rule::cleanMoney($value['amount_total']);
$userparams['contact_id'] = $value['contact_id'];
$userparams['grant_program_id'] = $_POST['pid'];
//FIXME pass grant id instead of NULL
$amountGranted = CRM_Grant_BAO_GrantProgram::getUserAllocatedAmount($userparams, NULL);
if ($_POST['algorithm'] == 'Best to Worst, Fully Funded') {
$amountGranted = CRM_Grant_BAO_GrantProgram::getUserAllocatedAmount($userparams, $value['grant_id']);
if ($_POST['algorithm'] == 'Best To Worst, Fully Funded') {
$amountEligible = $grantThresholds['Maximum Grant'] - $amountGranted;
if ($value['amount_total'] > $amountEligible) {
$value['amount_granted'] = $amountEligible;
if ($value['amount_granted'] <= $totalAmount) {
if ($amountEligible <= $totalAmount) {
$grant['granted'][] = $amountEligible;
$totalAmount = $totalAmount - $amountEligible;
$totalAmount = $totalAmount - ($amountEligible - $value['amount_granted']);
$value['amount_granted'] = $amountEligible;
}
else {
$grant['eligible'][] = $value['amount_granted'];
......@@ -136,7 +134,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
else {
if ($value['amount_total'] <= $totalAmount) {
$grant['granted'][] = $value['amount_total'];
$totalAmount = $totalAmount - $value['amount_total'];
$totalAmount = $totalAmount - ($value['amount_total'] - $value['amount_granted']);
$value['amount_granted'] = $value['amount_total'];
}
else {
......@@ -144,7 +142,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
continue;
}
}
$ids['grant'] = $value['grant_id'];
$ids['grant_id'] = $value['grant_id'];
}
else {
$requestedAmount = (($value['assessment']/100) * $value['amount_total'] * ($grantThresholds['Funding factor'] / 100));
......@@ -156,8 +154,8 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
}
else {
if ($amountEligible != 0) {
$totalAmount = $totalAmount - ($amountEligible - $value['amount_granted']);
$value['amount_granted'] = $grant['granted'][] = $amountEligible;
$totalAmount = $totalAmount - $value['amount_granted'];
}
else {
$grant['nonEligible'][] = $requestedAmount;
......@@ -170,16 +168,18 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
continue;
}
else {
$totalAmount = $totalAmount - ($requestedAmount - $value['amount_granted']);
$value['amount_granted'] = $grant['granted'][] = $requestedAmount;
$totalAmount = $totalAmount - $value['amount_granted'];
}
}
$ids['grant'] = $key;
$ids['grant_id'] = $key;
}
$value['allocation'] = TRUE;
$value['grant_program_id'] = $_POST['pid'];
$result = CRM_Grant_BAO_Grant::add(&$value, &$ids);
}
}
$grantProgramParams['remainder_amount'] = $totalAmount;
$grantProgramParams['id'] = $_POST['pid'];
$ids['grant_program'] = $_POST['pid'];
......@@ -202,7 +202,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$page = new CRM_Core_Page();
$grantPrograms = CRM_Grant_BAO_GrantProgram::getGrantPrograms();
$message = "Trial Allocation Completed. $".$grantedAmount.".00 allocated to {$grantedCount} eligible applications. ".$eligibleCount." eligible applications were not allocated $".$eligibleAmount.".00 in funds they would have received were funds available. $".$totalAmount." remains unallocated.";
$page->assign('message', $message);
$page->assign('grant_program_name', $grantPrograms[$_POST['pid']]);
......
......@@ -151,19 +151,25 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
foreach ($this->sorted as $grantId) {
$ids['grant_id'] = $grantId;
$grantParams = array('id' => $grantId);
$grant = CRM_Grant_BAO_Grant::retrieve($grantParams, $defaults=array());
$grant = CRM_Grant_BAO_Grant::retrieve($grantParams, CRM_Core_DAO::$_nullArray);
$values['contact_id'] = $grant->contact_id;
$values['grant_program_id'] = $grant->grant_program_id;
$values['grant_type_id'] = $grant->grant_type_id;
$values['id'] = $grantId;
$values['status_id'] = $grant->status_id;
$values['amount_total'] = $grant->amount_total;
if ( $params['radio_ts'] == 'amount_total') {
unset($params['amount_granted']);
$programParams = array('id' => $grant->grant_program_id);
$grant->status_id = $values['status_id'];
$grantProgram = CRM_Grant_BAO_GrantProgram::retrieve($programParams, $defaults);
self::quickAllocate($grantProgram, $grant);
} else {
$values['contact_id'] = $grant->contact_id;
$values['grant_program_id'] = $grant->grant_program_id;
$values['grant_type_id'] = $grant->grant_type_id;
$values['amount_total'] = $grant->amount_total;
unset($values['amount_granted']);
$values['assessment'] = $grant->assessment;
$values['allocation'] = TRUE;
CRM_Grant_BAO_Grant::add($values, $ids);
}
else {
CRM_Grant_BAO_Grant::add($values, $ids);
if (!empty($params['amount_granted'])) {
CRM_Grant_BAO_GrantProgram::create($grantProgramParams, $ids);
}
}
$updatedGrants++;
}
......@@ -174,47 +180,5 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
CRM_Core_Session::setStatus($status);
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/grant/search', 'force=1&qfKey=' . $qfKey));
}
function quickAllocate($grantProgram, $grant) {
$grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds');
$grantThresholds = array_flip($grantThresholds);
$amountGranted = NULL;
$totalAmount = $grantProgram->remainder_amount;
if (isset($grant->assessment)) {
$userparams['contact_id'] = $grant->contact_id;
$userparams['grant_program_id'] = $grant->grant_program_id;
$userAmountGranted = CRM_Grant_BAO_GrantProgram::getUserAllocatedAmount($userparams, $grant->id);
$amountEligible = $grantThresholds['Maximum Grant'] - $userAmountGranted;
$grant->amount_total = str_replace(',', '', $grant->amount_total);
$requestedAmount = ((($grant->assessment / 100) * $grant->amount_total) * ($grantThresholds['Funding factor'] / 100));
if ($requestedAmount > $amountEligible) {
$requestedAmount = $amountEligible;
}
if ($requestedAmount < $totalAmount) {
$amountGranted = $requestedAmount;
}
}
//Update grant program
$grantProgramParams = array();
$grantProgramParams['remainder_amount'] = $totalAmount - $amountGranted;
$grantProgramParams['id'] = $grantProgram->id;
$ids['grant_program'] = $grantProgram->id;
CRM_Grant_BAO_GrantProgram::create($grantProgramParams, $ids);
if ($amountGranted) {
$grantUpdate = array(
'id' => $grant->id,
'amount_granted' => $amountGranted,
'contact_id' => $grant->contact_id,
'grant_program_id' => $grant->grant_program_id,
'grant_type_id' => $grant->grant_type_id,
'status_id' => $grant->status_id,
);
$grantIds['grant_id'] = $grant->id;
CRM_Grant_BAO_Grant::add($grantUpdate, $grantIds);
}
return true;
}
}
This diff is collapsed.
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