3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 * This class provides the functionality for batch entry for contributions/memberships.
37 class CRM_Batch_Form_Entry
extends CRM_Core_Form
{
40 * Maximum profile fields that will be displayed.
42 protected $_rowCount = 1;
52 protected $_batchInfo = array();
55 * Store the profile id associated with the batch type.
57 protected $_profileId;
66 * When not to reset sort_name.
68 protected $_preserveDefault = TRUE;
73 protected $_contactFields = array();
76 * Fields array of fields in the batch profile.
77 * (based on the uf_field table data)
78 * (this can't be protected as it is passed into the CRM_Contact_Form_Task_Batch::parseStreetAddress function
79 * (although a future refactoring might hopefully change that so it uses the api & the function is not
83 public $_fields = array();
86 * Build all the data structures needed to build the form.
88 public function preProcess() {
89 $this->_batchId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
91 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
93 if (empty($this->_batchInfo
)) {
94 $params = array('id' => $this->_batchId
);
95 CRM_Batch_BAO_Batch
::retrieve($params, $this->_batchInfo
);
97 $this->assign('batchTotal', !empty($this->_batchInfo
['total']) ?
$this->_batchInfo
['total'] : NULL);
98 $this->assign('batchType', $this->_batchInfo
['type_id']);
100 // get the profile id associted with this batch type
101 $this->_profileId
= CRM_Batch_BAO_Batch
::getProfileId($this->_batchInfo
['type_id']);
103 CRM_Core_Resources
::singleton()
104 ->addScriptFile('civicrm', 'templates/CRM/Batch/Form/Entry.js', 1, 'html-header')
105 ->addSetting(array('batch' => array('type_id' => $this->_batchInfo
['type_id'])))
106 ->addSetting(array('setting' => array('monetaryThousandSeparator' => CRM_Core_Config
::singleton()->monetaryThousandSeparator
)))
107 ->addSetting(array('setting' => array('monetaryDecimalPoint' => CRM_Core_Config
::singleton()->monetaryDecimalPoint
)));
112 * Build the form object.
114 public function buildQuickForm() {
115 if (!$this->_profileId
) {
116 CRM_Core_Error
::fatal(ts('Profile for bulk data entry is missing.'));
119 $this->addElement('hidden', 'batch_id', $this->_batchId
);
121 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
122 // get the profile information
123 if ($this->_batchInfo
['type_id'] == $batchTypes['Contribution']) {
124 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Contributions'));
125 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution');
127 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Membership']) {
128 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Memberships'));
129 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership');
131 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
132 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Pledge Payments'));
133 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution');
135 $this->_fields
= array();
136 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($this->_profileId
, FALSE, CRM_Core_Action
::VIEW
);
138 // remove file type field and then limit fields
139 $suppressFields = FALSE;
140 $removehtmlTypes = array('File', 'Autocomplete-Select');
141 foreach ($this->_fields
as $name => $field) {
142 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
143 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
145 $suppressFields = TRUE;
146 unset($this->_fields
[$name]);
149 //fix to reduce size as we are using this field in grid
150 if (is_array($field['attributes']) && $this->_fields
[$name]['attributes']['size'] > 19) {
151 //shrink class to "form-text-medium"
152 $this->_fields
[$name]['attributes']['size'] = 19;
156 $this->addFormRule(array('CRM_Batch_Form_Entry', 'formRule'), $this);
158 // add the force save button
159 $forceSave = $this->getButtonName('upload', 'force');
161 $this->addElement('submit',
163 ts('Ignore Mismatch & Process the Batch?')
166 $this->addButtons(array(
169 'name' => ts('Validate & Process the Batch'),
174 'name' => ts('Save & Continue Later'),
179 $this->assign('rowCount', $this->_batchInfo
['item_count'] +
1);
181 $fileFieldExists = FALSE;
182 $preserveDefaultsArray = array(
190 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
191 $contactReturnProperties = array();
192 $config = CRM_Core_Config
::singleton();
194 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
195 $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array(
197 'placeholder' => ts('- select -'),
200 // special field specific to membership batch udpate
201 if ($this->_batchInfo
['type_id'] == 2) {
203 1 => ts('Add Membership'),
204 2 => ts('Renew Membership'),
206 $this->add('select', "member_option[$rowNumber]", '', $options);
208 if ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
209 $options = array('' => '-select-');
210 $optionTypes = array(
211 '1' => ts('Adjust Pledge Payment Schedule?'),
212 '2' => ts('Adjust Total Pledge Amount?'),
214 $this->add('select', "option_type[$rowNumber]", NULL, $optionTypes);
215 if (!empty($this->_batchId
) && !empty($this->_batchInfo
['data']) && !empty($rowNumber)) {
216 $dataValues = json_decode($this->_batchInfo
['data'], TRUE);
217 if (!empty($dataValues['values']['primary_contact_id'][$rowNumber])) {
218 $pledgeIDs = CRM_Pledge_BAO_Pledge
::getContactPledges($dataValues['values']['primary_contact_id'][$rowNumber]);
219 foreach ($pledgeIDs as $pledgeID) {
220 $pledgePayment = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeID);
221 $options +
= array($pledgeID => CRM_Utils_Date
::customFormat($pledgePayment['schedule_date'], '%m/%d/%Y') . ', ' . $pledgePayment['amount'] . ' ' . $pledgePayment['currency']);
226 $this->add('select', "open_pledges[$rowNumber]", '', $options);
229 foreach ($this->_fields
as $name => $field) {
230 if (in_array($field['field_type'], $contactTypes)) {
231 $fld = explode('-', $field['name']);
232 $contactReturnProperties[$field['name']] = $fld[0];
234 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, NULL, FALSE, FALSE, $rowNumber);
236 if (in_array($field['name'], $preserveDefaultsArray)) {
237 $this->_preserveDefault
= FALSE;
242 $this->assign('fields', $this->_fields
);
243 CRM_Core_Resources
::singleton()
246 'return' => implode(',', $contactReturnProperties),
247 'fieldmap' => array_flip($contactReturnProperties),
251 // don't set the status message when form is submitted.
252 $buttonName = $this->controller
->getButtonName('submit');
254 if ($suppressFields && $buttonName != '_qf_Entry_next') {
255 CRM_Core_Session
::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Update multiple records."), ts('Some Fields Excluded'), 'info');
262 * @param array $params
263 * Posted values of the form.
264 * @param array $files
265 * List of errors to be posted back to the form.
270 * list of errors to be posted back to the form
272 public static function formRule($params, $files, $self) {
274 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
276 'total_amount' => ts('Amount'),
277 'financial_type' => ts('Financial Type'),
278 'payment_instrument' => ts('Payment Method'),
281 //CRM-16480 if contact is selected, validate financial type and amount field.
282 foreach ($params['field'] as $key => $value) {
283 foreach ($fields as $field => $label) {
284 if (!empty($params['primary_contact_id'][$key]) && empty($value[$field])) {
285 $errors["field[$key][$field]"] = ts('%1 is a required field.', array(1 => $label));
290 if (!empty($params['_qf_Entry_upload_force'])) {
291 if (!empty($errors)) {
298 foreach ($params['field'] as $key => $value) {
299 $batchTotal +
= $value['total_amount'];
301 //validate for soft credit fields
302 if (!empty($params['soft_credit_contact_id'][$key]) && empty($params['soft_credit_amount'][$key])) {
303 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');
305 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'])) {
306 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount should not be greater than the total amount');
309 //membership type is required for membership batch entry
310 if ($self->_batchInfo
['type_id'] == $batchTypes['Membership']) {
311 if (empty($value['membership_type'][1])) {
312 $errors["field[$key][membership_type]"] = ts('Membership type is a required field.');
316 if ($self->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
317 foreach (array_unique($params["open_pledges"]) as $value) {
318 if (!empty($value)) {
319 $duplicateRows = array_keys($params["open_pledges"], $value);
321 if (!empty($duplicateRows) && count($duplicateRows) > 1) {
322 foreach ($duplicateRows as $key) {
323 $errors["open_pledges[$key]"] = ts('You can not record two payments for the same pledge in a single batch.');
328 if ((string) $batchTotal != $self->_batchInfo
['total']) {
329 $self->assign('batchAmountMismatch', TRUE);
330 $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.');
333 if (!empty($errors)) {
337 $self->assign('batchAmountMismatch', FALSE);
342 * Override default cancel action.
344 public function cancelAction() {
345 // redirect to batch listing
346 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
347 CRM_Utils_System
::civiExit();
351 * Set default values for the form.
353 public function setDefaultValues() {
354 if (empty($this->_fields
)) {
358 // for add mode set smart defaults
359 if ($this->_action
& CRM_Core_Action
::ADD
) {
360 list($currentDate, $currentTime) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
363 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
364 $completeStatus = array_search('Completed', $allStatus);
365 $specialFields = array(
366 'join_date' => $currentDate,
367 'receive_date' => $currentDate,
368 'receive_date_time' => $currentTime,
369 'contribution_status_id' => $completeStatus,
372 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
373 foreach ($specialFields as $key => $value) {
374 $defaults['field'][$rowNumber][$key] = $value;
379 // get the cached info from data column of civicrm_batch
380 $data = CRM_Core_DAO
::getFieldValue('CRM_Batch_BAO_Batch', $this->_batchId
, 'data');
381 $defaults = json_decode($data, TRUE);
382 $defaults = $defaults['values'];
389 * Process the form after the input has been submitted and validated.
391 public function postProcess() {
392 $params = $this->controller
->exportValues($this->_name
);
393 $params['actualBatchTotal'] = 0;
395 // get the profile information
396 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
397 if (in_array($this->_batchInfo
['type_id'], array($batchTypes['Pledge Payment'], $batchTypes['Contribution']))) {
398 $this->processContribution($params);
400 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Membership']) {
401 $this->processMembership($params);
404 // update batch to close status
405 $paramValues = array(
406 'id' => $this->_batchId
,
408 'status_id' => CRM_Core_OptionGroup
::getValue('batch_status', 'Closed', 'name'),
409 'total' => $params['actualBatchTotal'],
412 CRM_Batch_BAO_Batch
::create($paramValues);
414 // set success status
415 CRM_Core_Session
::setStatus("", ts("Batch Processed."), "success");
417 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
421 * Process contribution records.
423 * @param array $params
424 * Associated array of submitted values.
428 private function processContribution(&$params) {
436 // get the price set associated with offline contribution record.
437 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
438 $this->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
439 $priceFieldID = CRM_Price_BAO_PriceSet
::getOnlyPriceFieldID($this->_priceSet
);
440 $priceFieldValueID = CRM_Price_BAO_PriceSet
::getOnlyPriceFieldValueID($this->_priceSet
);
442 if (isset($params['field'])) {
443 foreach ($params['field'] as $key => $value) {
444 // if contact is not selected we should skip the row
445 if (empty($params['primary_contact_id'][$key])) {
449 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_id']);
451 // update contact information
452 $this->updateContactInfo($value);
454 //build soft credit params
455 if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) {
456 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
457 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
459 //CRM-15350: if soft-credit-type profile field is disabled or removed then
460 //we choose configured SCT default value
461 if (!empty($params['soft_credit_type'][$key])) {
462 $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
465 $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type");
470 if (!empty($params['pcp_made_through_id'][$key])) {
471 $value['pcp']['pcp_made_through_id'] = $params['pcp_made_through_id'][$key];
472 $value['pcp']['pcp_display_in_roll'] = !empty($params['pcp_display_in_roll'][$key]);
473 if (!empty($params['pcp_roll_nickname'][$key])) {
474 $value['pcp']['pcp_roll_nickname'] = $params['pcp_roll_nickname'][$key];
476 if (!empty($params['pcp_personal_note'][$key])) {
477 $value['pcp']['pcp_personal_note'] = $params['pcp_personal_note'][$key];
481 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
486 foreach ($dates as $val) {
487 if (!empty($value[$val])) {
488 $value[$val] = CRM_Utils_Date
::processDate($value[$val], $value[$val . '_time'], TRUE);
492 if (!empty($value['send_receipt'])) {
493 $value['receipt_date'] = date('Y-m-d His');
495 // these translations & date handling are required because we are calling BAO directly rather than the api
496 $fieldTranslations = array(
497 'financial_type' => 'financial_type_id',
498 'payment_instrument' => 'payment_instrument_id',
499 'contribution_source' => 'source',
500 'contribution_note' => 'note',
503 foreach ($fieldTranslations as $formField => $baoField) {
504 if (isset($value[$formField])) {
505 $value[$baoField] = $value[$formField];
507 unset($value[$formField]);
510 $params['actualBatchTotal'] +
= $value['total_amount'];
511 $value['batch_id'] = $this->_batchId
;
512 $value['skipRecentView'] = TRUE;
514 // build line item params
515 $this->_priceSet
['fields'][$priceFieldID]['options'][$priceFieldValueID]['amount'] = $value['total_amount'];
516 $value['price_' . $priceFieldID] = 1;
519 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'], $value, $lineItem[$priceSetId]);
521 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
522 // function to get correct amount level consistently. Remove setting of the amount level in
523 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
524 // to cover all variants.
525 unset($value['amount_level']);
527 //CRM-11529 for back office transactions
528 //when financial_type_id is passed in form, update the
529 //line items with the financial type selected in form
530 // @todo - create a price set or price field per financial type & simply choose the appropriate
531 // price field rather than working around the fact that each price_field is supposed to have a financial
532 // type & we are allowing that to be overridden.
533 if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
534 foreach ($lineItem[$priceSetId] as &$values) {
535 $values['financial_type_id'] = $value['financial_type_id'];
538 $value['line_item'] = $lineItem;
539 //finally call contribution create for all the magic
540 $contribution = CRM_Contribute_BAO_Contribution
::create($value, CRM_Core_DAO
::$_nullArray);
541 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
542 if (!empty($this->_batchInfo
['type_id']) && ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment'])) {
543 $adjustTotalAmount = FALSE;
544 if (isset($params['option_type'][$key])) {
545 if ($params['option_type'][$key] == 2) {
546 $adjustTotalAmount = TRUE;
549 $pledgeId = $params['open_pledges'][$key];
550 if (is_numeric($pledgeId)) {
551 $result = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($pledgeId);
552 $pledgePaymentId = 0;
553 foreach ($result as $key => $values) {
554 if ($values['status'] != 'Completed') {
555 $pledgePaymentId = $values['id'];
559 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentId, 'contribution_id', $contribution->id
);
560 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($pledgeId,
561 array($pledgePaymentId),
562 $contribution->contribution_status_id
,
564 $contribution->total_amount
,
571 if (!empty($value['product_name'])) {
572 if ($value['product_name'][0] > 0) {
573 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
575 $value['hidden_Premium'] = 1;
576 $value['product_option'] = CRM_Utils_Array
::value(
577 $value['product_name'][1],
578 $options[$value['product_name'][0]]
581 $premiumParams = array(
582 'product_id' => $value['product_name'][0],
583 'contribution_id' => $contribution->id
,
584 'product_option' => $value['product_option'],
587 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
593 if ($contribution->id
&& !empty($value['send_receipt'])) {
594 // add the domain email id
595 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
596 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
597 $value['from_email_address'] = $domainEmail;
598 $value['contribution_id'] = $contribution->id
;
599 if (!empty($value['soft_credit'])) {
600 $value = array_merge($value, CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($contribution->id
));
602 CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $value);
610 * Process membership records.
612 * @param array $params
613 * Associated array of submitted values.
618 private function processMembership(&$params) {
620 'join_date' => 'joinDate',
621 'membership_start_date' => 'startDate',
622 'membership_end_date' => 'endDate',
632 // get the price set associated with offline membership
633 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name');
634 $this->_priceSet
= $priceSets = current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
636 if (isset($params['field'])) {
637 $customFields = array();
638 foreach ($params['field'] as $key => $value) {
639 // if contact is not selected we should skip the row
640 if (empty($params['primary_contact_id'][$key])) {
644 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_id']);
646 // update contact information
647 $this->updateContactInfo($value);
649 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
651 foreach ($dateTypes as $dateField => $dateVariable) {
652 $
$dateVariable = CRM_Utils_Date
::processDate($value[$dateField]);
653 $fDate[$dateField] = CRM_Utils_Array
::value($dateField, $value);
656 $calcDates = array();
657 $calcDates[$membershipTypeId] = CRM_Member_BAO_MembershipType
::getDatesForMembershipType($membershipTypeId,
658 $joinDate, $startDate, $endDate
661 foreach ($calcDates as $memType => $calcDate) {
662 foreach ($dates as $d) {
663 //first give priority to form values then calDates.
664 $date = CRM_Utils_Array
::value($d, $value);
666 $date = CRM_Utils_Array
::value($d, $calcDate);
669 $value[$d] = CRM_Utils_Date
::processDate($date);
673 if (!empty($value['send_receipt'])) {
674 $value['receipt_date'] = date('Y-m-d His');
677 if (!empty($value['membership_source'])) {
678 $value['source'] = $value['membership_source'];
681 unset($value['membership_source']);
683 //Get the membership status
684 if (!empty($value['membership_status'])) {
685 $value['status_id'] = $value['membership_status'];
686 unset($value['membership_status']);
689 if (empty($customFields)) {
690 // membership type custom data
691 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership', FALSE, FALSE, $membershipTypeId);
693 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
694 CRM_Core_BAO_CustomField
::getFields('Membership',
695 FALSE, FALSE, NULL, NULL, TRUE
700 //check for custom data
701 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($params['field'][$key],
707 if (!empty($value['financial_type'])) {
708 $value['financial_type_id'] = $value['financial_type'];
711 if (!empty($value['payment_instrument'])) {
712 $value['payment_instrument_id'] = $value['payment_instrument'];
715 // handle soft credit
716 if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) {
717 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
718 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
720 //CRM-15350: if soft-credit-type profile field is disabled or removed then
721 //we choose Gift as default value as per Gift Membership rule
722 if (!empty($params['soft_credit_type'][$key])) {
723 $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
726 $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup
::getValue('soft_credit_type', 'Gift', 'name');
730 if (!empty($value['receive_date'])) {
731 $value['receive_date'] = CRM_Utils_Date
::processDate($value['receive_date'], $value['receive_date_time'], TRUE);
734 $params['actualBatchTotal'] +
= $value['total_amount'];
736 unset($value['financial_type']);
737 unset($value['payment_instrument']);
739 $value['batch_id'] = $this->_batchId
;
740 $value['skipRecentView'] = TRUE;
742 // make entry in line item for contribution
744 $editedFieldParams = array(
745 'price_set_id' => $priceSetId,
746 'name' => $value['membership_type'][0],
749 $editedResults = array();
750 CRM_Price_BAO_PriceField
::retrieve($editedFieldParams, $editedResults);
752 if (!empty($editedResults)) {
753 unset($this->_priceSet
['fields']);
754 $this->_priceSet
['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']];
755 unset($this->_priceSet
['fields'][$editedResults['id']]['options']);
756 $fid = $editedResults['id'];
757 $editedFieldParams = array(
758 'price_field_id' => $editedResults['id'],
759 'membership_type_id' => $value['membership_type_id'],
762 $editedResults = array();
763 CRM_Price_BAO_PriceFieldValue
::retrieve($editedFieldParams, $editedResults);
764 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']];
765 if (!empty($value['total_amount'])) {
766 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']]['amount'] = $value['total_amount'];
769 $fieldID = key($this->_priceSet
['fields']);
770 $value['price_' . $fieldID] = $editedResults['id'];
773 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
774 $value, $lineItem[$priceSetId]
777 //CRM-11529 for backoffice transactions
778 //when financial_type_id is passed in form, update the
779 //lineitems with the financial type selected in form
780 if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
781 foreach ($lineItem[$priceSetId] as &$values) {
782 $values['financial_type_id'] = $value['financial_type_id'];
786 $value['lineItems'] = $lineItem;
787 $value['processPriceSet'] = TRUE;
789 // end of contribution related section
791 unset($value['membership_type']);
792 unset($value['membership_start_date']);
793 unset($value['membership_end_date']);
795 $value['is_renew'] = FALSE;
796 if (!empty($params['member_option']) && CRM_Utils_Array
::value($key, $params['member_option']) == 2) {
798 // The following parameter setting may be obsolete.
799 $this->_params
= $params;
800 $value['is_renew'] = TRUE;
801 $isPayLater = CRM_Utils_Array
::value('is_pay_later', $params);
803 if (isset($this->_values
) && is_array($this->_values
) && !empty($this->_values
)) {
804 $campaignId = CRM_Utils_Array
::value('campaign_id', $this->_params
);
805 if (!array_key_exists('campaign_id', $this->_params
)) {
806 $campaignId = CRM_Utils_Array
::value('campaign_id', $this->_values
);
809 foreach (array('join_date', 'start_date', 'end_date') as $dateType) {
810 //CRM-18000 - ignore $dateType if its not explicitly passed
811 if (!empty($fDate[$dateType]) ||
!empty($fDate['membership_' . $dateType])) {
812 $formDates[$dateType] = CRM_Utils_Array
::value($dateType, $value);
815 $membershipSource = CRM_Utils_Array
::value('source', $value);
816 list($membership) = CRM_Member_BAO_Membership
::renewMembership(
817 $value['contact_id'], $value['membership_type_id'], FALSE,
818 //$numTerms should be default to 1.
819 NULL, NULL, $value['custom'], 1, NULL, FALSE,
820 NULL, $membershipSource, $isPayLater, $campaignId, $formDates
823 // make contribution entry
824 $contrbutionParams = array_merge($value, array('membership_id' => $membership->id
));
825 // @todo - calling this from here is pretty hacky since it is called from membership.create anyway
826 // This form should set the correct params & not call this fn directly.
827 CRM_Member_BAO_Membership
::recordMembershipContribution($contrbutionParams);
830 $membership = CRM_Member_BAO_Membership
::create($value, CRM_Core_DAO
::$_nullArray);
834 if (!empty($value['product_name'])) {
835 if ($value['product_name'][0] > 0) {
836 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
838 $value['hidden_Premium'] = 1;
839 $value['product_option'] = CRM_Utils_Array
::value(
840 $value['product_name'][1],
841 $options[$value['product_name'][0]]
844 $premiumParams = array(
845 'product_id' => $value['product_name'][0],
846 'contribution_id' => CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id
, 'contribution_id', 'membership_id'),
847 'product_option' => $value['product_option'],
850 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
856 if ($membership->id
&& !empty($value['send_receipt'])) {
858 // add the domain email id
859 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
860 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
862 $value['from_email_address'] = $domainEmail;
863 $value['membership_id'] = $membership->id
;
864 $value['contribution_id'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id
, 'contribution_id', 'membership_id');
865 CRM_Member_Form_Membership
::emailReceipt($this, $value, $membership);
873 * Update contact information.
875 * @param array $value
876 * Associated array of submitted values.
878 private function updateContactInfo(&$value) {
879 $value['preserveDBName'] = $this->_preserveDefault
;
881 //parse street address, CRM-7768
882 CRM_Contact_Form_Task_Batch
::parseStreetAddress($value, $this);
884 CRM_Contact_BAO_Contact
::createProfileContact($value, $this->_fields
,
890 * Function exists purely for unit testing purposes.
892 * If you feel tempted to use this in live code then it probably means there is some functionality
893 * that needs to be moved out of the form layer
895 * @param array $params
899 public function testProcessMembership($params) {
900 return $this->processMembership($params);
904 * Function exists purely for unit testing purposes.
906 * If you feel tempted to use this in live code then it probably means there is some functionality
907 * that needs to be moved out of the form layer.
909 * @param array $params
913 public function testProcessContribution($params) {
914 return $this->processContribution($params);