Skip to content

Commit

Permalink
Merge pull request civicrm#1 from manishmore/master
Browse files Browse the repository at this point in the history
batch entry for pledges
  • Loading branch information
kurund committed Dec 9, 2014
2 parents 3db4e3c + d78a705 commit 081efd2
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 80 deletions.
4 changes: 3 additions & 1 deletion CRM/Batch/BAO/Batch.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ static function getProfileId($batchTypeId) {
//retrieve the profile specific to batch type
switch ($batchTypeId) {
case 1:
//batch profile used for contribution
case 3:
//batch profile used for pledges
$profileName = "contribution_batch_entry";
break;

case 2:
//batch profile used for memberships
$profileName = "membership_batch_entry";
break;
}

// get and return the profile id
Expand Down
3 changes: 0 additions & 3 deletions CRM/Batch/Form/Batch.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ public function buildQuickForm() {

$batchTypes = CRM_Batch_BAO_Batch::buildOptions('type_id');

// unset non-related types
unset($batchTypes[3]);
unset($batchTypes[4]);
$type = $this->add('select', 'type_id', ts('Type'), $batchTypes);

if ($this->_action & CRM_Core_Action::UPDATE) {
Expand Down
59 changes: 52 additions & 7 deletions CRM/Batch/Form/Entry.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,20 @@ function buildQuickForm() {

$this->addElement('hidden', 'batch_id', $this->_batchId);

$batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id',array('flip' => 1), 'validate');
// get the profile information
if ($this->_batchInfo['type_id'] == 1) {
if ($this->_batchInfo['type_id'] == $batchTypes['Contribution']) {
CRM_Utils_System::setTitle(ts('Batch Data Entry for Contributions'));
$customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
}
else {
elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) {
CRM_Utils_System::setTitle(ts('Batch Data Entry for Memberships'));
$customFields = CRM_Core_BAO_CustomField::getFields('Membership');
}

elseif ($this->_batchInfo['type_id'] == $batchTypes['Pledge']) {
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);

Expand Down Expand Up @@ -204,6 +208,23 @@ function buildQuickForm() {
);
$this->add('select', "member_option[$rowNumber]", '', $options);
}
if ($this->_batchInfo['type_id'] == $batchTypes['Pledge']) {
$options = array('' => '-select-');
$optionTypes = array(
'1' => ts('Adjust Pledge Payment Schedule?'),
'2' => ts('Adjust Total Pledge Amount?'),
);
$this->add('select', "option_type[$rowNumber]", NULL, $optionTypes);
if (!empty($this->_batchId) && !empty($this->_batchInfo['data']) && !empty($rowNumber)) {
$dataValues = json_decode($this->_batchInfo['data'], TRUE);
$PledgeIDs = CRM_Pledge_BAO_Pledge::getContactPledges($dataValues['values']['primary_contact_id'][$rowNumber]);
foreach ($PledgeIDs as $pledgeID) {
$pledgePayment = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeID);
$options += array($pledgeID => CRM_Utils_Date::customFormat($pledgePayment['schedule_date'], '%d/%m/%Y') . ', ' . $pledgePayment['amount'] . ' ' . $pledgePayment['currency']);
}
}
$this->add('select', "open_pledges[$rowNumber]", ts('Open Pledges'), $options);
}

foreach ($this->_fields as $name => $field) {
if (in_array($field['field_type'], $contactTypes)) {
Expand Down Expand Up @@ -344,14 +365,14 @@ function setDefaultValues() {
*/
public function postProcess() {
$params = $this->controller->exportValues($this->_name);

$params['actualBatchTotal'] = 0;

// get the profile information
if ($this->_batchInfo['type_id'] == 1) {
$batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
if (in_array($this->_batchInfo['type_id'], array($batchTypes['Pledge'], $batchTypes['Contribution']))) {
$this->processContribution($params);
}
else {
elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) {
$this->processMembership($params);
}

Expand Down Expand Up @@ -477,7 +498,31 @@ private function processContribution(&$params) {

//finally call contribution create for all the magic
$contribution = CRM_Contribute_BAO_Contribution::create($value, CRM_Core_DAO::$_nullArray);

$pledgeId = $params['open_pledges'][$key];
$batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
if ($this->_batchInfo['type_id'] == $batchTypes['Pledge'] && is_numeric($pledgeId)) {
$adjustTotalAmount = FALSE;
if ($params['option_type'][$key] == 2) {
$adjustTotalAmount=TRUE;
}
$pledgeId = $params['open_pledges'][$key];
$result = CRM_Pledge_BAO_PledgePayment::getPledgePayments($pledgeId);
$pledgePaymentId = 0;
foreach ($result as $key => $value ) {
if ($value['status'] != 'Completed') {
$pledgePaymentId = $value['id'];
break;
}
}
CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentId, 'contribution_id', $contribution->id);
CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId,
array($pledgePaymentId),
$contribution->contribution_status_id,
NULL,
$contribution->total_amount,
$adjustTotalAmount
);
}
//process premiums
if (!empty($value['product_name'])) {
if ($value['product_name'][0] > 0) {
Expand Down
3 changes: 2 additions & 1 deletion CRM/Pledge/BAO/PledgePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ static function getOldestPledgePayment($pledgeID, $limit = 1) {
$statusClause = " IN (" . implode(',', $status) . ")";

$query = "
SELECT civicrm_pledge_payment.id id, civicrm_pledge_payment.scheduled_amount amount, civicrm_pledge_payment.currency
SELECT civicrm_pledge_payment.id id, civicrm_pledge_payment.scheduled_amount amount, civicrm_pledge_payment.currency, civicrm_pledge_payment.scheduled_date
FROM civicrm_pledge, civicrm_pledge_payment
WHERE civicrm_pledge.id = civicrm_pledge_payment.pledge_id
AND civicrm_pledge_payment.status_id {$statusClause}
Expand All @@ -713,6 +713,7 @@ static function getOldestPledgePayment($pledgeID, $limit = 1) {
'id' => $payment->id,
'amount' => $payment->amount,
'currency' => $payment->currency,
'schedule_date' => $payment->scheduled_date,
'count' => $count,
);
$count++;
Expand Down
13 changes: 13 additions & 0 deletions CRM/Pledge/Page/AJAX.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,18 @@ static function pledgeName() {
echo CRM_Utils_JSON::encode($elements, 'value');
CRM_Utils_System::civiExit();
}
/**
* Function to setDefaults according to Pledge Id
* for batch entry pledges
*/
function getPledgeDefaults() {
$details = array();
if (!empty($_POST['pid'])) {
$pledgeID = CRM_Utils_Type::escape($_POST['pid'], 'Integer');
$details = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeID);
}
echo json_encode($details);
CRM_Utils_System::civiExit();
}
}

5 changes: 5 additions & 0 deletions CRM/Pledge/xml/Menu/Pledge.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@
<page_callback>CRM_Pledge_Page_AJAX::pledgeName</page_callback>
<access_arguments>access CiviCRM,access CiviPledge</access_arguments>
</item>
<item>
<path>civicrm/ajax/pledgeAmount</path>
<page_callback>CRM_Pledge_Page_AJAX::getPledgeDefaults</page_callback>
<access_arguments>access CiviPledge,administer CiviCRM</access_arguments>
</item>
</menu>
9 changes: 9 additions & 0 deletions CRM/Upgrade/Incremental/sql/4.6.alpha1.mysql.tpl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ 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,weight)
VALUES (@option_group_id, {localize}'{ts escape="sql"}Pledge{/ts}'{/localize}, @max_option_value+1, 'Pledge','3');

72 changes: 12 additions & 60 deletions templates/CRM/Batch/Form/Entry.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -41,18 +46,17 @@ 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 () {
setPaymentBlock($(this), null);
});

$('select[id*="_membership_type_1"]').change(function () {
setPaymentBlock($(this), $(this).val());
});
Expand All @@ -69,58 +73,6 @@ CRM.$(function($) {
});


function updateContactInfo(blockNo, prefix) {
var contactHiddenElement = 'input[name="' + prefix + 'contact_select_id[' + blockNo + ']"]';
var contactId = cj(contactHiddenElement).val();

var profileFields = CRM.contact.fieldmap;

CRM.api('Contact', 'get', {
'sequential': '1',
'contact_id': contactId,
'return': CRM.contact.return },
{ success: function (data) {
cj.each(data.values[0], function (key, value) {
// set the values
var actualFldName = profileFields[key];
if (key == 'country' || key == 'state_province') {
idFldName = key + '_id';
value = data.values[0][idFldName];
}
setFieldValue(actualFldName, value, blockNo)
});

// for membership batch entry based on contact we need to enable / disable
// add membership select
if(CRM.batch.type_id == 2) {
CRM.api('Membership', 'get', {
'sequential': '1',
'contact_id': contactId
},
{ success: function (data) {
if (data.count > 0) {
//get the information on membership type
var membershipTypeId = data.values[0].membership_type_id;
var membershipJoinDate = data.values[0].join_date;
CRM.api('MembershipType', 'get', {
'sequential': '1',
'id': membershipTypeId
},
{ success: function (data) {
var memTypeContactId = data.values[0].member_of_contact_id;
cj('select[id="member_option_' + blockNo + '"]').prop('disabled', false).val(2);
cj('select[id="field_' + blockNo + '_membership_type_0"]').val(memTypeContactId).change();
cj('select[id="field_' + blockNo + '_membership_type_1"]').val(membershipTypeId).change();
setDateFieldValue('join_date', membershipJoinDate, blockNo)
}
});
}
}
});
}
}
});
}

function setPaymentBlock(form, memType) {
var rowID = form.closest('div.crm-grid-row').attr('entity_id');
Expand Down
Loading

0 comments on commit 081efd2

Please sign in to comment.