Commit 059c7b8d authored by Mayur Jadhav's avatar Mayur Jadhav

Worked on RG-78

parent fbf01f72
......@@ -315,11 +315,20 @@ WHERE civicrm_contact.id = $id ";
}
}
}
static function getUserAllocatedAmount($userparams) {
/**
* Function to get sum of amount granted for a Contact
*
* @param int $params
*
* @return int sum of amount granted
* @access public
* @static
*/
static function getUserAllocatedAmount($params) {
$where = NULL;
if (!empty($userparams)) {
foreach ($userparams as $key => $value) {
if (!empty($params)) {
foreach ($params as $key => $value) {
$where .= " AND {$key} = {$value}";
}
$query = "SELECT SUM(amount_granted) as amount_granted FROM civicrm_grant WHERE " .ltrim($where, ' AND');
......
......@@ -202,7 +202,11 @@ class CRM_Grant_Form_GrantProgram extends CRM_Core_Form {
$dao->name = $values['label'];
$dao->grant_type_id = $values['grant_type_id'];
$dao->total_amount = $values['total_amount'];
$dao->remainder_amount = $values['remainder_amount'];
if ($this->_action & CRM_Core_Action::ADD) {
$dao->remainder_amount = $values['total_amount'];
} else {
$dao->remainder_amount = $values['total_amount'] - $this->_defaultValues['total_amount'] + $values['remainder_amount'];
}
$dao->financial_type_id = $values['financial_type_id'];
$dao->status_id = CRM_Grant_BAO_GrantProgram::getOptionValueID(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup','grant_program_status','id','name'), $values['status_id']);
$dao->allocation_date = CRM_Utils_Date::processDate($values['allocation_date']);
......
......@@ -104,12 +104,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
array_multisort($order, $sort_order, $result);
}
if ($_POST['remainder_amount'] == '0.00') {
$totalAmount = $_POST['amount'];
}
else {
$totalAmount = $_POST['remainder_amount'];
}
$totalAmount = $_POST['remainder_amount'];
$contact = array();
$grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds', TRUE);
......@@ -145,7 +140,7 @@ class CRM_Grant_Form_GrantProgramView extends CRM_Core_Form {
$ids['grant'] = $value['grant_id'];
}
else {
$requestedAmount = (($value['assessment']/100 )*$value['amount_total']*($grantThresholds['Funding factor']/100));
$requestedAmount = (($value['assessment']/100) * $value['amount_total'] * ($grantThresholds['Funding factor'] / 100));
$amountEligible = $grantThresholds['Maximum Grant'] - $amountGranted;
if ($requestedAmount > $amountEligible) {
if ($amountEligible > $totalAmount) {
......
......@@ -122,17 +122,21 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
}
foreach ($sorted as $grantId) {
$ids['grant_id'] = $grantId;
if ( $params['radio_ts'] == 'amount_total' ) {
$grantParams = array('id'=>$grantId);
$grant = CRM_Grant_BAO_Grant::retrieve($grantParams, $defaults=array());
$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;
if ( $params['radio_ts'] == 'amount_total') {
unset($params['amount_granted']);
$grantParams = array('id'=>$grantId);
$grant = CRM_Grant_BAO_Grant::retrieve($grantParams, $defaults=array());
self::allocation($grant);
self::allocation($grant, $values);
} else {
CRM_Grant_BAO_Grant::add($values, $ids);
}
CRM_Grant_BAO_Grant::add($values, $ids);
$updatedGrants++;
}
}
$status =
ts('Updated Grant(s): %1', array(1 => $updatedGrants)).', '.
ts('Total Selected Grant(s): %1', array(1 => count($this->_grantIds)));
......@@ -140,17 +144,17 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/grant/search', 'force=1&qfKey=' . $qfKey));
}
function allocation($grant) {
function allocation($grant, $values) {
$defaults = $grants = array();
$programId = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $grant->id, 'grant_program_id');
$programParams = array('id' => $programId);
$grantProgram = CRM_Grant_BAO_GrantProgram::retrieve($programParams, $defaults);
$algoType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $grantProgram->allocation_algorithm, 'grouping');
if ($algoType == 'immediate') {
$grantedAmount = self::quickAllocate($grantProgram, $grant);
$grantedAmount = self::quickAllocate($grantProgram, $grant, $values);
}
else if ($algoType == 'batch') {
$grantedAmount = self::batchAllocate($grantProgram, $grant);
//FIXME needs error message
}
if ($grantedAmount) {
......@@ -158,27 +162,33 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
'id' => $grant->id,
'amount_granted' => $grantedAmount,
);
$grantIds['grant'] = $grant->id;
$grantIds['grant_id'] = $grant->id;
CRM_Grant_BAO_Grant::add($grantUpdate, $grantIds);
}
}
function quickAllocate($grantProgram, $grant) {
$grantThresholds = CRM_Core_OptionGroup::values( 'grant_thresholds' );
function quickAllocate($grantProgram, $grant, $values) {
$grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds');
$grantThresholds = array_flip($grantThresholds);
$amountGranted = NULL;
if( $grantProgram->remainder_amount == '0.00' ) {
$totalAmount = $grantProgram->total_amount;
} else {
$totalAmount = $grantProgram->remainder_amount;
}
$amountGranted = NULL;
$totalAmount = $grantProgram->remainder_amount;
if (isset($grant->assessment)) {
$userparams['contact_id'] = $values['contact_id'];
$userparams['grant_program_id'] = $values['grant_program_id'];
$userAmountGranted = CRM_Grant_BAO_GrantProgram::getUserAllocatedAmount($userparams);
$amountEligible = $grantThresholds['Maximum Grant'] - $userAmountGranted;
$grant->amount_total = str_replace(',', '', $grant->amount_total);
if ($grant->assessment > $grantThresholds['Minimum Score For Grant Award']) {
if ((($grantThresholds['Fixed Percentage Of Grant']/100) * $grant->amount_total) < $totalAmount) {
$amountGranted = ($grantThresholds['Fixed Percentage Of Grant']/100) * $grant->amount_total;
}
} else if ((($grant->assessment/100) * $grant->amount_total) < $totalAmount) {
$amountGranted = ($grant->assessment/100) * $grant->amount_total;
$requestedAmount = ((($grantThresholds['Fixed Percentage Of Grant'] / 100) * $grant->amount_total) * ($grantThresholds['Funding factor'] / 100));
}
else {
$requestedAmount = ((($grant->assessment / 100) * $grant->amount_total) * ($grantThresholds['Funding factor'] / 100));
}
if ($requestedAmount > $amountEligible) {
$requestedAmount = $amountEligible;
}
if ($requestedAmount < $totalAmount) {
$amountGranted = $requestedAmount;
}
}
......@@ -190,32 +200,5 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
CRM_Grant_BAO_GrantProgram::create( $grantProgramParams, $ids );
return $amountGranted;
}
function batchAllocate($grantProgram, $grant) {
$grantThresholds = CRM_Core_OptionGroup::values( 'grant_thresholds' );
$grantThresholds = array_flip($grantThresholds);
$amountGranted = NULL;
if( $grantProgram->remainder_amount == '0.00' ) {
$totalAmount = $grantProgram->total_amount;
} else {
$totalAmount = $grantProgram->remainder_amount;
}
if( $grant->amount_total < $totalAmount ) {
if ( $grant->amount_total >= $grantThresholds['Maximum Grant'] ) {
$totalAmount = $totalAmount - $grantThresholds['Maximum Grant'];
$amountGranted = $grantThresholds['Maximum Grant'];
} else {
$totalAmount = $totalAmount - $grant->amount_total;
$amountGranted = $grant->amount_total;
}
//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 );
return $amountGranted;
}
}
}
......@@ -100,14 +100,14 @@ function grantprograms_civicrm_grantAssessment(&$params) {
$grantProgramParams['id'] = $params['grant_program_id'];
$grantProgram = CRM_Grant_BAO_GrantProgram::retrieve($grantProgramParams, CRM_Core_DAO::$_nullArray);
if (!empty($grantProgram->grant_program_id)) {
$previousGrantedAmount = CRM_Core_DAO::singleValueQuery("SELECT SUM(amount_granted) as amount_granted FROM civicrm_grant WHERE status_id = " . CRM_Core_OptionGroup::getValue('grant_status', 'Paid', 'name') . " AND grant_program_id = {$grantProgram->grant_program_id} AND contact_id = {$params['contact_id']}");
$sumAmountGranted = CRM_Core_DAO::singleValueQuery("SELECT SUM(amount_granted) as sum_amount_granted FROM civicrm_grant WHERE status_id = " . CRM_Core_OptionGroup::getValue('grant_status', 'Paid', 'name') . " AND grant_program_id = {$grantProgram->grant_program_id} AND contact_id = {$params['contact_id']}");
$grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds');
$grantThresholds = array_flip($grantThresholds);
if (!empty($previousGrantedAmount)) {
if ($previousGrantedAmount >= $grantThresholds['Maximum Grant']) {
if (!empty($sumAmountGranted)) {
if ($sumAmountGranted >= $grantThresholds['Maximum Grant']) {
$priority = 10;
}
else {
elseif ($sumAmountGranted > 0) {
$priority = 0;
}
}
......@@ -126,7 +126,7 @@ function grantprograms_civicrm_grantAssessment(&$params) {
$grantProgram = CRM_Grant_BAO_GrantProgram::retrieve($programParams, $defaults);
$algoType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $grantProgram->allocation_algorithm, 'grouping');
$grantStatuses = CRM_Core_OptionGroup::values('grant_status', TRUE);
if ($algoType == 'immediate' && !CRM_Utils_Array::value('manualEdit', $params) && ($params['status_id'] == $grantStatuses['Submitted'] || $params['status_id'] == $grantStatuses['Eligible'] || $params['status_id'] == $grantStatuses['Awaiting Information']) && empty($params['amount_granted'])) {
if ($algoType == 'immediate' && !CRM_Utils_Array::value('manualEdit', $params) && ($params['status_id'] == $grantStatuses['Submitted'] || $params['status_id'] == $grantStatuses['Eligible'] || $params['status_id'] == $grantStatuses['Awaiting Information'])) {
$params['amount_granted'] = quickAllocate($grantProgram, $params);
if (empty($params['amount_granted'])) {
unset($params['amount_granted']);
......@@ -140,35 +140,33 @@ function grantprograms_civicrm_grantAssessment(&$params) {
*/
function quickAllocate($grantProgram, $value) {
$grantThresholds = CRM_Core_OptionGroup::values('grant_thresholds', TRUE);
$amountGranted = NULL;
if( $grantProgram->remainder_amount == '0.00' ) {
$totalAmount = $grantProgram->total_amount;
} else {
$totalAmount = $grantProgram->remainder_amount;
}
$amountGranted = NULL;
if (isset($value['assessment'])) {
$userparams['contact_id'] = $value['contact_id'];
$userparams['grant_program_id'] = $grantProgram->id;
$userAmountGranted = CRM_Grant_BAO_GrantProgram::getUserAllocatedAmount($userparams);
$amountEligible = $grantThresholds['Maximum Grant'] - $userAmountGranted;
if ($amountEligible > $grantProgram->remainder_amount) {
$amountEligible = $grantProgram->remainder_amount;
}
$value['amount_total'] = str_replace(',', '', $value['amount_total']);
$requestedAmount = ((($value['assessment'] / 100) * $value['amount_total']) * ($grantThresholds['Funding factor'] / 100));
if ($requestedAmount > $amountEligible) {
$requestedAmount = $amountEligible;
}
if ($requestedAmount < $totalAmount) {
if ($requestedAmount < $grantProgram->remainder_amount) {
$amountGranted = $requestedAmount;
}
}
//Update grant program
$grantProgramParams['remainder_amount'] = $totalAmount - $amountGranted;
$grantProgramParams['id'] = $grantProgram->id;
$ids['grant_program'] = $grantProgram->id;
CRM_Grant_BAO_GrantProgram::create($grantProgramParams, $ids);
if ($amountGranted > 0) {
$grantProgramParams['remainder_amount'] = $totalAmount - $amountGranted;
$grantProgramParams['id'] = $grantProgram->id;
$ids['grant_program'] = $grantProgram->id;
CRM_Grant_BAO_GrantProgram::create($grantProgramParams, $ids);
}
return $amountGranted;
}
......@@ -676,8 +674,8 @@ function grantprograms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
}
}
if ($objectName == 'Grant') {
$config = CRM_Core_Config::singleton();
$config = CRM_Core_Config::singleton();
if ($objectName == 'Grant' && $config->_params) {
$params = $config->_params;
// added by JMA fixme in module
$grantProgram = new CRM_Grant_DAO_GrantProgram();
......
......@@ -88,7 +88,7 @@ var response = cj.ajax({
}).responseText;
});
cj(document).ready( function(){
alert('hi');
// RG-116 hide attachments
{/literal}{if $hideAttachments}{literal}
cj('div.crm-grant-form-block-attachment').hide();
......
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