Skip to content
Snippets Groups Projects
Commit b3b486a0 authored by Mayur Jadhav's avatar Mayur Jadhav
Browse files

Worked on RG-78 to add the form rule for restricing batch allocation and...

Worked on RG-78 to add the form rule for restricing batch allocation and jquery work was break due to hardcoded value
parent 6c25cd18
No related branches found
No related tags found
No related merge requests found
......@@ -66,21 +66,60 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
$grantStatus = CRM_Grant_PseudoConstant::grantStatus();
CRM_Utils_System::setTitle(ts('Update Grants'));
$this->addElement('select', 'status_id', ts('Grant Status'), array('' => '') + $grantStatus);
$this->addElement('radio', 'radio_ts', null, ts(' Do not update'), 'no_update' );
$this->addElement('radio', 'radio_ts', null, ts(' Other Amount'), 'amount_granted' );
$this->setDefaults( array( 'radio_ts'=> 'no_update' ) );
$this->addElement('radio', 'radio_ts', NULL, ts(' Do not update'), 'no_update' );
$this->addElement('radio', 'radio_ts', NULL, ts(' Other Amount'), 'amount_granted');
$this->setDefaults(array('radio_ts'=> 'no_update'));
$this->addElement('text', 'amount_granted', ts('Amount Granted'));
$this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money');
$this->addElement('radio', 'radio_ts', null, ts(' Standard Allocation'), 'amount_total');
$this->addElement('radio', 'radio_ts', NULL, ts(' Standard Allocation'), 'amount_total');
$this->addDate('decision_date', ts('Grant Decision'), FALSE, array('formatType' => 'custom'));
// $this->assign('elements', array('status_id', 'amount_granted', 'decision_date'));
$this->addElement('hidden', 'standard_allocation', 'Text', '');
$this->assign('totalSelectedGrants', count($this->_grantIds));
$this->addDefaultButtons(ts('Update Grants'), 'done');
$this->addFormRule(array('CRM_Grant_Form_Task_Update', 'formRule'), $this);
}
/**
* form validations
*
* @param array $params posted values of the form
* @param array $files list of errors to be posted back to the form
* @param array $self form object
*
* @return array list of errors to be posted back to the form
* @static
* @access public
*/
static function formRule($params, $files, $self) {
$errors = $defaults = array();
$grantIds = implode(', ', $self->_grantIds);
$query = "SELECT id, assessment FROM civicrm_grant WHERE id IN ({$grantIds})";
$dao = CRM_Core_DAO::executeQuery($query);
while ($dao->fetch()) {
$sort[$dao->id] = $dao->assessment;
$programId = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $dao->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 == 'batch' && $params['radio_ts'] == 'amount_total') {
$errors['standard_allocation'] = 'It is not possible to update the allocation amount for less than a full batch. If you would like to (re-)run the batch allocation algorithm, go to Administer > CiviGrants > Grant Programs, click View beside the Grant Program of interest, then click Allocate Approved (Trial) button.';
}
}
if (!empty($errors)) {
return $errors;
}
arsort($sort);
foreach ($sort as $keys => $vals) {
if (array_key_exists($keys, array_flip($self->_grantIds))) {
$self->sorted[] = $keys;
}
}
return TRUE;
}
/**
......@@ -96,12 +135,6 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
// get the submitted form values.
$params = $this->controller->exportValues($this->_name);
$qfKey = $params['qfKey'];
$grantIds = implode(', ', $this->_grantIds);
$query = "SELECT id, assessment FROM civicrm_grant WHERE id IN ({$grantIds})";
$dao = CRM_Core_DAO::executeQuery($query);
while ($dao->fetch()) {
$sort[$dao->id] = $dao->assessment;
}
foreach ($params as $key => $value) {
if ($value == '' || $key == 'qfKey') {
unset($params[$key]);
......@@ -114,24 +147,22 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
$values[$key] = $value;
}
}
arsort($sort);
foreach ($sort as $keys => $vals) {
if (array_key_exists($keys, array_flip($this->_grantIds))) {
$sorted[] = $keys;
}
}
foreach ($sorted as $grantId) {
foreach ($this->sorted as $grantId) {
$ids['grant_id'] = $grantId;
$grantParams = array('id'=>$grantId);
$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']);
self::allocation($grant, $values);
$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;
CRM_Grant_BAO_Grant::add($values, $ids);
}
$updatedGrants++;
......@@ -144,46 +175,18 @@ 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, $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, $values);
}
else if ($algoType == 'batch') {
//FIXME needs error message
}
if ($grantedAmount) {
$grantUpdate = array(
'id' => $grant->id,
'amount_granted' => $grantedAmount,
);
$grantIds['grant_id'] = $grant->id;
CRM_Grant_BAO_Grant::add($grantUpdate, $grantIds);
}
}
function quickAllocate($grantProgram, $grant, $values) {
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'] = $values['contact_id'];
$userparams['grant_program_id'] = $values['grant_program_id'];
$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);
if ($grant->assessment > $grantThresholds['Minimum Score For Grant Award']) {
$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));
}
$requestedAmount = ((($grant->assessment / 100) * $grant->amount_total) * ($grantThresholds['Funding factor'] / 100));
if ($requestedAmount > $amountEligible) {
$requestedAmount = $amountEligible;
}
......@@ -197,8 +200,21 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task {
$grantProgramParams['remainder_amount'] = $totalAmount - $amountGranted;
$grantProgramParams['id'] = $grantProgram->id;
$ids['grant_program'] = $grantProgram->id;
CRM_Grant_BAO_GrantProgram::create( $grantProgramParams, $ids );
return $amountGranted;
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;
}
}
......@@ -48,7 +48,8 @@
</tr>
<tr class="crm-contact-custom-search-form-row-radio_ts">
<td class="label">{$form.radio_ts.amount_total.label}</td>
<td>{$form.radio_ts.amount_total.html}<br />
<td>{$form.radio_ts.amount_total.html}
<td>{$form.standard_allocation.html}</td><br />
<span class="description">{ts}Set to the amount determined by the current allocation algorithm.{/ts}</span></td>
</tr>
......@@ -63,17 +64,19 @@
</div><!-- /.crm-form-block -->
{literal}
<script type="text/javascript">
{literal}
cj("#CIVICRM_QFID_amount_total_4").click(function() {
cj("#amount_total").show();
cj("#amount_granted").hide();
cj("#amount_granted").val(null);
cj(document).ready( function() {
cj("#amount_granted").parent().parent().hide();
cj('input[name="radio_ts"]').click(function(){
if (this.value == 'amount_total' || this.value == 'no_update') {
cj("#amount_granted").parent().parent().hide();
cj("#amount_granted").val(null);
}
else {
cj("#amount_granted").parent().parent().show();
}
});
cj("#CIVICRM_QFID_amount_granted_2").click(function() {
cj("#amount_granted").show();
});
{/literal}
</script>
\ No newline at end of file
</script>
{/literal}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment