From 04e6444d0685b43d4e7edbb4e54feeba76b12d7f Mon Sep 17 00:00:00 2001 From: moremanish Date: Fri, 24 Oct 2014 15:14:05 +0530 Subject: [PATCH] batch entry for pledges --- CRM/Batch/BAO/Batch.php | 4 + CRM/Batch/Form/Batch.php | 4 +- CRM/Batch/Form/Entry.php | 79 ++++++++++++++++--- CRM/Pledge/Page/AJAX.php | 27 +++++++ CRM/Pledge/xml/Menu/Pledge.xml | 5 ++ .../Incremental/sql/4.6.alpha1.mysql.tpl | 8 ++ templates/CRM/Batch/Form/Entry.js | 43 +++++++--- templates/CRM/Batch/Form/Entry.tpl | 34 +++++++- xml/templates/civicrm_data.tpl | 1 + 9 files changed, 183 insertions(+), 22 deletions(-) diff --git a/CRM/Batch/BAO/Batch.php b/CRM/Batch/BAO/Batch.php index a97216f6f7..a84b889026 100644 --- a/CRM/Batch/BAO/Batch.php +++ b/CRM/Batch/BAO/Batch.php @@ -113,6 +113,10 @@ class CRM_Batch_BAO_Batch extends CRM_Batch_DAO_Batch { case 2: //batch profile used for memberships $profileName = "membership_batch_entry"; + break; + default: + //batch profile used for pledges + $profileName = "contribution_batch_entry"; } // get and return the profile id diff --git a/CRM/Batch/Form/Batch.php b/CRM/Batch/Form/Batch.php index 31dd314599..035c472f66 100644 --- a/CRM/Batch/Form/Batch.php +++ b/CRM/Batch/Form/Batch.php @@ -66,8 +66,8 @@ class CRM_Batch_Form_Batch extends CRM_Admin_Form { $batchTypes = CRM_Batch_BAO_Batch::buildOptions('type_id'); // unset non-related types - unset($batchTypes[3]); - unset($batchTypes[4]); + //unset($batchTypes[3]); + //unset($batchTypes[4]); $type = $this->add('select', 'type_id', ts('Type'), $batchTypes); if ($this->_action & CRM_Core_Action::UPDATE) { diff --git a/CRM/Batch/Form/Entry.php b/CRM/Batch/Form/Entry.php index 40540f4872..d32e901a32 100644 --- a/CRM/Batch/Form/Entry.php +++ b/CRM/Batch/Form/Entry.php @@ -133,11 +133,14 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { CRM_Utils_System::setTitle(ts('Batch Data Entry for Contributions')); $customFields = CRM_Core_BAO_CustomField::getFields('Contribution'); } - else { + elseif ($this->_batchInfo['type_id'] == 2) { CRM_Utils_System::setTitle(ts('Batch Data Entry for Memberships')); $customFields = CRM_Core_BAO_CustomField::getFields('Membership'); } - + else { + CRM_Utils_System::setTitle(ts('Batch Data Entry for Pledges')); + $customFields = CRM_Core_BAO_CustomField::getFields('Contribution'); + } $this->_fields = array(); $this->_fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, CRM_Core_Action::VIEW); @@ -204,7 +207,37 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { ); $this->add('select', "member_option[$rowNumber]", '', $options); } - + $batchValue=CRM_Core_OptionGroup::values('batch_type',TRUE); + if($this->_batchInfo['type_id'] == $batchValue['Pledge']) { + $openPledge = array( + '' => ts('- select -'), + ); + $info=$this->getVar('_batchInfo'); + if(!empty($info['data'])) { + $json_value=json_decode($info['data'],true); + $primContactId = $json_value['values']['primary_contact_select_id'][$rowNumber] ; + if(!empty($primContactId)) { + $paramsForPledge = array( + 'version' => 3, + 'sequential' => 1, + 'contact_id' => $primContactId, + 'sort' => 'pledge_create_date desc', + ); + $openDefaultPledge = civicrm_api('Pledge', 'get', $paramsForPledge); + foreach($openDefaultPledge['values'] as $key => $value) { + $keyforopen=$value['id']; + $openPledge[$keyforopen]=$value['pledge_next_pay_date'].' '.$value['pledge_next_pay_amount']; + } + } + } + $this->add('select', "open_pledges[$rowNumber]", ts('Open Pledges'), $openPledge); + $optionTypes = array( + '1' => ts('Adjust Pledge Payment Schedule?'), + '2' => ts('Adjust Total Pledge Amount?'), + ); + $this->add('select', "option_type[$rowNumber]",NULL, $optionTypes); + $this->assign('batchoptionvalue', $batchValue['Pledge']); + } foreach ($this->_fields as $name => $field) { if (in_array($field['field_type'], $contactTypes)) { $fld = explode('-', $field['name']); @@ -344,17 +377,21 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { */ public function postProcess() { $params = $this->controller->exportValues($this->_name); - $params['actualBatchTotal'] = 0; - - // get the profile information + // get the profile information if ($this->_batchInfo['type_id'] == 1) { $this->processContribution($params); } - else { + elseif ($this->_batchInfo['type_id'] == 2) { $this->processMembership($params); } - + else { + if(!CRM_Utils_Array::value( 'open_pledges', $params )) { + $params['open_pledges']=$this->_submitValues['open_pledges']; + } + $params['type_id']=$this->_batchInfo['type_id']; + $this->processContribution($params); + } // update batch to close status $paramValues = array( 'id' => $this->_batchId, @@ -477,7 +514,31 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { //finally call contribution create for all the magic $contribution = CRM_Contribute_BAO_Contribution::create($value, CRM_Core_DAO::$_nullArray); - + $batchValue = CRM_Core_OptionGroup::values('batch_type',TRUE); + if($params['type_id'] == $batchValue['Pledge']) { + $adjustTotalAmount = FALSE; + if($params['option_type'][$key]==2) { + $adjustTotalAmount=TRUE; + } + $pledgeId=$params['open_pledges'][$key]; + //$contribution->id; + $result=CRM_Pledge_BAO_PledgePayment::getPledgePayments($pledgeId); + $ppid=0; + foreach($result as $key => $value ) { + if($value['status'] != 'Completed') { + $ppid=$value['id']; + break; + } + } + CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $ppid, 'contribution_id', $contribution->id); + CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, + array($ppid), + $contribution->contribution_status_id, + NULL, + $contribution->total_amount, + $adjustTotalAmount + ); + } //process premiums if (!empty($value['product_name'])) { if ($value['product_name'][0] > 0) { diff --git a/CRM/Pledge/Page/AJAX.php b/CRM/Pledge/Page/AJAX.php index f242f79dd3..7f0f5256ff 100644 --- a/CRM/Pledge/Page/AJAX.php +++ b/CRM/Pledge/Page/AJAX.php @@ -86,5 +86,32 @@ WHERE {$whereClause} echo CRM_Utils_JSON::encode($elements, 'value'); CRM_Utils_System::civiExit(); } + /** + * Function to setDefaults according to Pledge Id + * for batch entry pledges + */ + function getPledgeDefaults($config) { + if (!$_POST['mtype']) { + $details['scheduled_amount'] = ''; + echo json_encode($details); + CRM_Utils_System::civiExit(); + } + $memType = CRM_Utils_Type::escape($_POST['mtype'], 'Integer'); + $query = "SELECT min(id),scheduled_amount + FROM civicrm_pledge_payment + WHERE pledge_id = %1 and status_id !=1"; + $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($memType, 'Positive'))); + $properties = array( 'scheduled_amount'); + while ($dao->fetch()) { + foreach ($properties as $property) { + $details[$property] = $dao->$property; + } + } + $details['total_amount_numeric'] = $details['total_amount']; + // fix the display of the monetary value, CRM-4038 + $details['scheduled_amount'] = CRM_Utils_Money::format($details['scheduled_amount'], NULL, '%a'); + echo json_encode($details); + CRM_Utils_System::civiExit(); + } } diff --git a/CRM/Pledge/xml/Menu/Pledge.xml b/CRM/Pledge/xml/Menu/Pledge.xml index d89b410330..25fdae88fa 100644 --- a/CRM/Pledge/xml/Menu/Pledge.xml +++ b/CRM/Pledge/xml/Menu/Pledge.xml @@ -47,4 +47,9 @@ CRM_Pledge_Page_AJAX::pledgeName access CiviCRM,access CiviPledge + + civicrm/ajax/pledgeAmount + CRM_Pledge_Page_AJAX::getPledgeDefaults + access CiviPledge,administer CiviCRM + diff --git a/CRM/Upgrade/Incremental/sql/4.6.alpha1.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.6.alpha1.mysql.tpl index 4fcfbad697..f474c5af2f 100644 --- a/CRM/Upgrade/Incremental/sql/4.6.alpha1.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.6.alpha1.mysql.tpl @@ -68,3 +68,11 @@ CREATE TABLE IF NOT EXISTS `civicrm_recurring_entity` ( `mode` tinyint(4) NOT NULL DEFAULT '1' COMMENT '1-this entity, 2-this and the following entities, 3-all the entities', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=87 ; + +-- add batch type for pledges +SELECT @option_group_id := id FROM civicrm_option_group WHERE name = 'batch_type'; + +SELECT @max_option_value:= max(value) FROM civicrm_option_value WHERE option_group_id = @option_group_id; + +INSERT INTO civicrm_option_value(option_group_id, {localize field='label'}`label`{/localize}, value, name) +VALUES (@option_group_id, {localize}'{ts escape="sql"}Pledge{/ts}'{/localize}, @max_option_value+1, 'Pledge'); \ No newline at end of file diff --git a/templates/CRM/Batch/Form/Entry.js b/templates/CRM/Batch/Form/Entry.js index 541cf01490..7ef64b7e12 100644 --- a/templates/CRM/Batch/Form/Entry.js +++ b/templates/CRM/Batch/Form/Entry.js @@ -11,7 +11,12 @@ CRM.$(function($) { // validate rows checkColumns($(this)); }); - + cj('.pledge-adjust-option').click(function(){ + var blockNo = cj(this).attr('id'); + cj('select[id="option_type_' + blockNo + '"]').show(); + cj('select[id="option_type_' + blockNo + '"]').removeAttr('disabled'); + cj('#field_' + blockNo + '_total_amount').removeAttr('readonly'); + }); $('input[name^="soft_credit_contact_"]').on('change', function(){ var rowNum = $(this).attr('id').replace('soft_credit_contact_id_',''); var totalAmount = $('#field_'+rowNum+'_total_amount').val(); @@ -41,12 +46,12 @@ CRM.$(function($) { }); } - else{ - $('select[id^="member_option_"]').each(function () { - if ($(this).val() == 1) { - $(this).prop('disabled', true); - } - }); + else if (CRM.batch.type_id == 2){ + cj('select[id^="member_option_"]').each(function () { + if (cj(this).val() == 1) { + cj(this).attr('disabled', true); + } + }); // set payment info accord to membership type $('select[id*="_membership_type_0"]').change(function () { @@ -118,9 +123,27 @@ function updateContactInfo(blockNo, prefix) { } }); } - } - }); -} + if(CRM.batch.type_id == 3) { + CRM.api('Pledge', 'get', { + 'q': 'civicrm/ajax/rest', + 'sequential': 1, + 'contact_id': contactId + }, + {success: function(data) { + cj.each(data['values'], function(key, value) { + if(value['pledge_status']!='Completed'){ + cj('#open_pledges_'+ blockNo).append(cj('