3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * This class provides the functionality for batch entry for contributions/memberships
39 class CRM_Batch_Form_Entry
extends CRM_Core_Form
{
42 * Maximum profile fields that will be displayed.
44 protected $_rowCount = 1;
54 protected $_batchInfo = array();
57 * Store the profile id associated with the batch type.
59 protected $_profileId;
68 * When not to reset sort_name.
70 protected $_preserveDefault = TRUE;
75 protected $_contactFields = array();
78 * Fields array of fields in the batch profile.
79 * (based on the uf_field table data)
80 * (this can't be protected as it is passed into the CRM_Contact_Form_Task_Batch::parseStreetAddress function
81 * (although a future refactoring might hopefully change that so it uses the api & the function is not
85 public $_fields = array();
88 * Build all the data structures needed to build the form.
92 public function preProcess() {
93 $this->_batchId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
95 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
97 if (empty($this->_batchInfo
)) {
98 $params = array('id' => $this->_batchId
);
99 CRM_Batch_BAO_Batch
::retrieve($params, $this->_batchInfo
);
101 $this->assign('batchTotal', !empty($this->_batchInfo
['total']) ?
$this->_batchInfo
['total'] : NULL);
102 $this->assign('batchType', $this->_batchInfo
['type_id']);
104 // get the profile id associted with this batch type
105 $this->_profileId
= CRM_Batch_BAO_Batch
::getProfileId($this->_batchInfo
['type_id']);
107 CRM_Core_Resources
::singleton()
108 ->addScriptFile('civicrm', 'templates/CRM/Batch/Form/Entry.js', 1, 'html-header')
109 ->addSetting(array('batch' => array('type_id' => $this->_batchInfo
['type_id'])))
110 ->addSetting(array('setting' => array('monetaryThousandSeparator' => CRM_Core_Config
::singleton()->monetaryThousandSeparator
)))
111 ->addSetting(array('setting' => array('monetaryDecimalPoint' => CRM_Core_Config
::singleton()->monetaryDecimalPoint
)));
116 * Build the form object.
121 public function buildQuickForm() {
122 if (!$this->_profileId
) {
123 CRM_Core_Error
::fatal(ts('Profile for bulk data entry is missing.'));
126 $this->addElement('hidden', 'batch_id', $this->_batchId
);
128 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
129 // get the profile information
130 if ($this->_batchInfo
['type_id'] == $batchTypes['Contribution']) {
131 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Contributions'));
132 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution');
134 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Membership']) {
135 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Memberships'));
136 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership');
138 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
139 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Pledge Payments'));
140 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution');
142 $this->_fields
= array();
143 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($this->_profileId
, FALSE, CRM_Core_Action
::VIEW
);
145 // remove file type field and then limit fields
146 $suppressFields = FALSE;
147 $removehtmlTypes = array('File', 'Autocomplete-Select');
148 foreach ($this->_fields
as $name => $field) {
149 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
150 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
152 $suppressFields = TRUE;
153 unset($this->_fields
[$name]);
156 //fix to reduce size as we are using this field in grid
157 if (is_array($field['attributes']) && $this->_fields
[$name]['attributes']['size'] > 19) {
158 //shrink class to "form-text-medium"
159 $this->_fields
[$name]['attributes']['size'] = 19;
163 $this->addFormRule(array('CRM_Batch_Form_Entry', 'formRule'), $this);
165 // add the force save button
166 $forceSave = $this->getButtonName('upload', 'force');
168 $this->addElement('submit',
170 ts('Ignore Mismatch & Process the Batch?')
173 $this->addButtons(array(
176 'name' => ts('Validate & Process the Batch'),
181 'name' => ts('Save & Continue Later'),
186 $this->assign('rowCount', $this->_batchInfo
['item_count'] +
1);
188 $fileFieldExists = FALSE;
189 $preserveDefaultsArray = array(
197 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
198 $contactReturnProperties = array();
199 $config = CRM_Core_Config
::singleton();
201 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
202 $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array(
204 'placeholder' => ts('- select -'),
207 // special field specific to membership batch udpate
208 if ($this->_batchInfo
['type_id'] == 2) {
210 1 => ts('Add Membership'),
211 2 => ts('Renew Membership'),
213 $this->add('select', "member_option[$rowNumber]", '', $options);
215 if ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
216 $options = array('' => '-select-');
217 $optionTypes = array(
218 '1' => ts('Adjust Pledge Payment Schedule?'),
219 '2' => ts('Adjust Total Pledge Amount?'),
221 $this->add('select', "option_type[$rowNumber]", NULL, $optionTypes);
222 if (!empty($this->_batchId
) && !empty($this->_batchInfo
['data']) && !empty($rowNumber)) {
223 $dataValues = json_decode($this->_batchInfo
['data'], TRUE);
224 if (!empty($dataValues['values']['primary_contact_id'][$rowNumber])) {
225 $pledgeIDs = CRM_Pledge_BAO_Pledge
::getContactPledges($dataValues['values']['primary_contact_id'][$rowNumber]);
226 foreach ($pledgeIDs as $pledgeID) {
227 $pledgePayment = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeID);
228 $options +
= array($pledgeID => CRM_Utils_Date
::customFormat($pledgePayment['schedule_date'], '%m/%d/%Y') . ', ' . $pledgePayment['amount'] . ' ' . $pledgePayment['currency']);
233 $this->add('select', "open_pledges[$rowNumber]", '', $options);
236 foreach ($this->_fields
as $name => $field) {
237 if (in_array($field['field_type'], $contactTypes)) {
238 $fld = explode('-', $field['name']);
239 $contactReturnProperties[$field['name']] = $fld[0];
241 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, NULL, FALSE, FALSE, $rowNumber);
243 if (in_array($field['name'], $preserveDefaultsArray)) {
244 $this->_preserveDefault
= FALSE;
249 $this->assign('fields', $this->_fields
);
250 CRM_Core_Resources
::singleton()
253 'return' => implode(',', $contactReturnProperties),
254 'fieldmap' => array_flip($contactReturnProperties),
258 // don't set the status message when form is submitted.
259 $buttonName = $this->controller
->getButtonName('submit');
261 if ($suppressFields && $buttonName != '_qf_Entry_next') {
262 CRM_Core_Session
::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Batch Update."), ts('Some Fields Excluded'), 'info');
269 * @param array $params
270 * Posted values of the form.
271 * @param array $files
272 * List of errors to be posted back to the form.
277 * list of errors to be posted back to the form
279 public static function formRule($params, $files, $self) {
281 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
283 'total_amount' => 'Amount',
284 'financial_type' => 'Financial Type',
285 'payment_instrument' => 'Paid By',
288 //CRM-16480 if contact is selected, validate financial type and amount field.
289 foreach ($params['field'] as $key => $value) {
290 foreach ($fields as $field => $label) {
291 if (!empty($params['primary_contact_id'][$key]) && empty($value[$field])) {
292 $errors["field[$key][$field]"] = ts('%1 is a required field.', array(1 => $label));
297 if (!empty($params['_qf_Entry_upload_force'])) {
298 if (!empty($errors)) {
305 foreach ($params['field'] as $key => $value) {
306 $batchTotal +
= $value['total_amount'];
308 //validate for soft credit fields
309 if (!empty($params['soft_credit_contact_id'][$key]) && empty($params['soft_credit_amount'][$key])) {
310 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');
312 if (!empty($params['soft_credit_amount']) && !empty($params['soft_credit_amount'][$key]) && CRM_Utils_Rule
::cleanMoney(CRM_Utils_Array
::value($key, $params['soft_credit_amount'])) > CRM_Utils_Rule
::cleanMoney($value['total_amount'])) {
313 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount should not be greater than the total amount');
316 //membership type is required for membership batch entry
317 if ($self->_batchInfo
['type_id'] == $batchTypes['Membership']) {
318 if (empty($value['membership_type'][1])) {
319 $errors["field[$key][membership_type]"] = ts('Membership type is a required field.');
323 if ($self->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
324 foreach (array_unique($params["open_pledges"]) as $value) {
325 if (!empty($value)) {
326 $duplicateRows = array_keys($params["open_pledges"], $value);
328 if (!empty($duplicateRows) && count($duplicateRows) > 1) {
329 foreach ($duplicateRows as $key) {
330 $errors["open_pledges[$key]"] = ts('You can not record two payments for the same pledge in a single batch.');
335 if ((string) $batchTotal != $self->_batchInfo
['total']) {
336 $self->assign('batchAmountMismatch', TRUE);
337 $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.');
340 if (!empty($errors)) {
344 $self->assign('batchAmountMismatch', FALSE);
349 * Override default cancel action.
351 public function cancelAction() {
352 // redirect to batch listing
353 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
354 CRM_Utils_System
::civiExit();
358 * Set default values for the form.
363 public function setDefaultValues() {
364 if (empty($this->_fields
)) {
368 // for add mode set smart defaults
369 if ($this->_action
& CRM_Core_Action
::ADD
) {
370 list($currentDate, $currentTime) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
373 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
374 $completeStatus = array_search('Completed', $allStatus);
375 $specialFields = array(
376 'join_date' => $currentDate,
377 'receive_date' => $currentDate,
378 'receive_date_time' => $currentTime,
379 'contribution_status_id' => $completeStatus,
382 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
383 foreach ($specialFields as $key => $value) {
384 $defaults['field'][$rowNumber][$key] = $value;
389 // get the cached info from data column of civicrm_batch
390 $data = CRM_Core_DAO
::getFieldValue('CRM_Batch_BAO_Batch', $this->_batchId
, 'data');
391 $defaults = json_decode($data, TRUE);
392 $defaults = $defaults['values'];
399 * Process the form after the input has been submitted and validated.
404 public function postProcess() {
405 $params = $this->controller
->exportValues($this->_name
);
406 $params['actualBatchTotal'] = 0;
408 // get the profile information
409 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
410 if (in_array($this->_batchInfo
['type_id'], array($batchTypes['Pledge Payment'], $batchTypes['Contribution']))) {
411 $this->processContribution($params);
413 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Membership']) {
414 $this->processMembership($params);
417 // update batch to close status
418 $paramValues = array(
419 'id' => $this->_batchId
,
421 'status_id' => CRM_Core_OptionGroup
::getValue('batch_status', 'Closed', 'name'),
422 'total' => $params['actualBatchTotal'],
425 CRM_Batch_BAO_Batch
::create($paramValues);
427 // set success status
428 CRM_Core_Session
::setStatus("", ts("Batch Processed."), "success");
430 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
434 * Process contribution records.
436 * @param array $params
437 * Associated array of submitted values.
442 private function processContribution(&$params) {
450 // get the price set associated with offline contribution record.
451 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
452 $this->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
453 $priceFieldID = CRM_Price_BAO_PriceSet
::getOnlyPriceFieldID($this->_priceSet
);
454 $priceFieldValueID = CRM_Price_BAO_PriceSet
::getOnlyPriceFieldValueID($this->_priceSet
);
456 if (isset($params['field'])) {
457 foreach ($params['field'] as $key => $value) {
458 // if contact is not selected we should skip the row
459 if (empty($params['primary_contact_id'][$key])) {
463 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_id']);
465 // update contact information
466 $this->updateContactInfo($value);
468 //build soft credit params
469 if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) {
470 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
471 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
473 //CRM-15350: if soft-credit-type profile field is disabled or removed then
474 //we choose configured SCT default value
475 if (!empty($params['soft_credit_type'][$key])) {
476 $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
479 $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type");
483 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
488 foreach ($dates as $val) {
489 if (!empty($value[$val])) {
490 $value[$val] = CRM_Utils_Date
::processDate($value[$val], $value[$val . '_time'], TRUE);
494 if (!empty($value['send_receipt'])) {
495 $value['receipt_date'] = date('Y-m-d His');
497 // these translations & date handling are required because we are calling BAO directly rather than the api
498 $fieldTranslations = array(
499 'financial_type' => 'financial_type_id',
500 'payment_instrument' => 'payment_instrument_id',
501 'contribution_source' => 'source',
502 'contribution_note' => 'note',
505 foreach ($fieldTranslations as $formField => $baoField) {
506 if (isset($value[$formField])) {
507 $value[$baoField] = $value[$formField];
509 unset($value[$formField]);
512 $params['actualBatchTotal'] +
= $value['total_amount'];
513 $value['batch_id'] = $this->_batchId
;
514 $value['skipRecentView'] = TRUE;
516 // build line item params
517 $this->_priceSet
['fields'][$priceFieldID]['options'][$priceFieldValueID]['amount'] = $value['total_amount'];
518 $value['price_' . $priceFieldID] = 1;
521 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'], $value, $lineItem[$priceSetId]);
523 //unset amount level since we always use quick config price set
524 unset($value['amount_level']);
526 //CRM-11529 for back office transactions
527 //when financial_type_id is passed in form, update the
528 //line items with the financial type selected in form
529 if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
530 foreach ($lineItem[$priceSetId] as &$values) {
531 $values['financial_type_id'] = $value['financial_type_id'];
534 $value['line_item'] = $lineItem;
535 //finally call contribution create for all the magic
536 $contribution = CRM_Contribute_BAO_Contribution
::create($value, CRM_Core_DAO
::$_nullArray);
537 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
538 if (!empty($this->_batchInfo
['type_id']) && ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment'])) {
539 $adjustTotalAmount = FALSE;
540 if (isset($params['option_type'][$key])) {
541 if ($params['option_type'][$key] == 2) {
542 $adjustTotalAmount = TRUE;
545 $pledgeId = $params['open_pledges'][$key];
546 if (is_numeric($pledgeId)) {
547 $result = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($pledgeId);
548 $pledgePaymentId = 0;
549 foreach ($result as $key => $values) {
550 if ($values['status'] != 'Completed') {
551 $pledgePaymentId = $values['id'];
555 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentId, 'contribution_id', $contribution->id
);
556 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($pledgeId,
557 array($pledgePaymentId),
558 $contribution->contribution_status_id
,
560 $contribution->total_amount
,
567 if (!empty($value['product_name'])) {
568 if ($value['product_name'][0] > 0) {
569 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
571 $value['hidden_Premium'] = 1;
572 $value['product_option'] = CRM_Utils_Array
::value(
573 $value['product_name'][1],
574 $options[$value['product_name'][0]]
577 $premiumParams = array(
578 'product_id' => $value['product_name'][0],
579 'contribution_id' => $contribution->id
,
580 'product_option' => $value['product_option'],
583 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
589 if ($contribution->id
&& !empty($value['send_receipt'])) {
590 // add the domain email id
591 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
592 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
593 $value['from_email_address'] = $domainEmail;
594 $value['contribution_id'] = $contribution->id
;
595 CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $value);
603 * Process membership records.
605 * @param array $params
606 * Associated array of submitted values.
611 private function processMembership(&$params) {
613 'join_date' => 'joinDate',
614 'membership_start_date' => 'startDate',
615 'membership_end_date' => 'endDate',
625 // get the price set associated with offline membership
626 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name');
627 $this->_priceSet
= $priceSets = current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
629 if (isset($params['field'])) {
630 $customFields = array();
631 foreach ($params['field'] as $key => $value) {
632 // if contact is not selected we should skip the row
633 if (empty($params['primary_contact_id'][$key])) {
637 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_id']);
639 // update contact information
640 $this->updateContactInfo($value);
642 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
644 foreach ($dateTypes as $dateField => $dateVariable) {
645 $
$dateVariable = CRM_Utils_Date
::processDate($value[$dateField]);
648 $calcDates = array();
649 $calcDates[$membershipTypeId] = CRM_Member_BAO_MembershipType
::getDatesForMembershipType($membershipTypeId,
650 $joinDate, $startDate, $endDate
653 foreach ($calcDates as $memType => $calcDate) {
654 foreach ($dates as $d) {
655 //first give priority to form values then calDates.
656 $date = CRM_Utils_Array
::value($d, $value);
658 $date = CRM_Utils_Array
::value($d, $calcDate);
661 $value[$d] = CRM_Utils_Date
::processDate($date);
665 if (!empty($value['send_receipt'])) {
666 $value['receipt_date'] = date('Y-m-d His');
669 if (!empty($value['membership_source'])) {
670 $value['source'] = $value['membership_source'];
673 unset($value['membership_source']);
675 //Get the membership status
676 if (!empty($value['membership_status'])) {
677 $value['status_id'] = $value['membership_status'];
678 unset($value['membership_status']);
681 if (empty($customFields)) {
682 // membership type custom data
683 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership', FALSE, FALSE, $membershipTypeId);
685 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
686 CRM_Core_BAO_CustomField
::getFields('Membership',
687 FALSE, FALSE, NULL, NULL, TRUE
692 //check for custom data
693 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($params['field'][$key],
699 if (!empty($value['financial_type'])) {
700 $value['financial_type_id'] = $value['financial_type'];
703 if (!empty($value['payment_instrument'])) {
704 $value['payment_instrument_id'] = $value['payment_instrument'];
707 // handle soft credit
708 if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) {
709 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
710 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
712 //CRM-15350: if soft-credit-type profile field is disabled or removed then
713 //we choose Gift as default value as per Gift Membership rule
714 if (!empty($params['soft_credit_type'][$key])) {
715 $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
718 $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup
::getValue('soft_credit_type', 'Gift', 'name');
722 if (!empty($value['receive_date'])) {
723 $value['receive_date'] = CRM_Utils_Date
::processDate($value['receive_date'], $value['receive_date_time'], TRUE);
726 $params['actualBatchTotal'] +
= $value['total_amount'];
728 unset($value['financial_type']);
729 unset($value['payment_instrument']);
731 $value['batch_id'] = $this->_batchId
;
732 $value['skipRecentView'] = TRUE;
734 // make entry in line item for contribution
736 $editedFieldParams = array(
737 'price_set_id' => $priceSetId,
738 'name' => $value['membership_type'][0],
741 $editedResults = array();
742 CRM_Price_BAO_PriceField
::retrieve($editedFieldParams, $editedResults);
744 if (!empty($editedResults)) {
745 unset($this->_priceSet
['fields']);
746 $this->_priceSet
['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']];
747 unset($this->_priceSet
['fields'][$editedResults['id']]['options']);
748 $fid = $editedResults['id'];
749 $editedFieldParams = array(
750 'price_field_id' => $editedResults['id'],
751 'membership_type_id' => $value['membership_type_id'],
754 $editedResults = array();
755 CRM_Price_BAO_PriceFieldValue
::retrieve($editedFieldParams, $editedResults);
756 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']];
757 if (!empty($value['total_amount'])) {
758 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']]['amount'] = $value['total_amount'];
761 $fieldID = key($this->_priceSet
['fields']);
762 $value['price_' . $fieldID] = $editedResults['id'];
765 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
766 $value, $lineItem[$priceSetId]
769 //CRM-11529 for backoffice transactions
770 //when financial_type_id is passed in form, update the
771 //lineitems with the financial type selected in form
772 if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
773 foreach ($lineItem[$priceSetId] as &$values) {
774 $values['financial_type_id'] = $value['financial_type_id'];
778 $value['lineItems'] = $lineItem;
779 $value['processPriceSet'] = TRUE;
781 // end of contribution related section
783 unset($value['membership_type']);
784 unset($value['membership_start_date']);
785 unset($value['membership_end_date']);
787 $value['is_renew'] = FALSE;
788 if (!empty($params['member_option']) && CRM_Utils_Array
::value($key, $params['member_option']) == 2) {
790 // The following parameter setting may be obsolete.
791 $this->_params
= $params;
792 $value['is_renew'] = TRUE;
793 $isPayLater = CRM_Utils_Array
::value('is_pay_later', $params);
795 if (isset($this->_values
) && is_array($this->_values
) && !empty($this->_values
)) {
796 $campaignId = CRM_Utils_Array
::value('campaign_id', $this->_params
);
797 if (!array_key_exists('campaign_id', $this->_params
)) {
798 $campaignId = CRM_Utils_Array
::value('campaign_id', $this->_values
);
802 list($membership) = CRM_Member_BAO_Membership
::renewMembership(
803 $value['contact_id'], $value['membership_type_id'], FALSE,
804 NULL, NULL, $value['custom'], NULL, NULL, FALSE,
805 NULL, NULL, $isPayLater, $campaignId
808 // make contribution entry
809 $contrbutionParams = array_merge($value, array('membership_id' => $membership->id
));
810 CRM_Member_BAO_Membership
::recordMembershipContribution($contrbutionParams);
813 $membership = CRM_Member_BAO_Membership
::create($value, CRM_Core_DAO
::$_nullArray);
817 if (!empty($value['product_name'])) {
818 if ($value['product_name'][0] > 0) {
819 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
821 $value['hidden_Premium'] = 1;
822 $value['product_option'] = CRM_Utils_Array
::value(
823 $value['product_name'][1],
824 $options[$value['product_name'][0]]
827 $premiumParams = array(
828 'product_id' => $value['product_name'][0],
829 'contribution_id' => CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id
, 'contribution_id', 'membership_id'),
830 'product_option' => $value['product_option'],
833 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
839 if ($membership->id
&& !empty($value['send_receipt'])) {
841 // add the domain email id
842 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
843 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
845 $value['from_email_address'] = $domainEmail;
846 $value['membership_id'] = $membership->id
;
847 $value['contribution_id'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id
, 'contribution_id', 'membership_id');
848 CRM_Member_Form_Membership
::emailReceipt($this, $value, $membership);
856 * Update contact information.
858 * @param array $value
859 * Associated array of submitted values.
864 private function updateContactInfo(&$value) {
865 $value['preserveDBName'] = $this->_preserveDefault
;
867 //parse street address, CRM-7768
868 CRM_Contact_Form_Task_Batch
::parseStreetAddress($value, $this);
870 CRM_Contact_BAO_Contact
::createProfileContact($value, $this->_fields
,
876 * Function exists purely for unit testing purposes. If you feel tempted to use this in live code
877 * then it probably means there is some functionality that needs to be moved
878 * out of the form layer
880 * @param array $params
884 public function testProcessMembership($params) {
885 return $this->processMembership($params);
889 * Function exists purely for unit testing purposes. If you feel tempted to use this in live code
890 * then it probably means there is some functionality that needs to be moved
891 * out of the form layer
893 * @param array $params
897 public function testProcessContribution($params) {
898 return $this->processContribution($params);