3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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
)));
116 public function setBatchID($id) {
117 $this->_batchId
= $id;
121 * Build the form object.
123 public function buildQuickForm() {
124 if (!$this->_profileId
) {
125 CRM_Core_Error
::fatal(ts('Profile for bulk data entry is missing.'));
128 $this->addElement('hidden', 'batch_id', $this->_batchId
);
130 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
131 // get the profile information
132 if ($this->_batchInfo
['type_id'] == $batchTypes['Contribution']) {
133 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Contributions'));
134 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution');
136 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Membership']) {
137 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Memberships'));
139 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
140 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Pledge Payments'));
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');
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 $preserveDefaultsArray = array(
196 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
197 $contactReturnProperties = array();
199 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
200 $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array(
202 'placeholder' => ts('- select -'),
205 // special field specific to membership batch udpate
206 if ($this->_batchInfo
['type_id'] == 2) {
208 1 => ts('Add Membership'),
209 2 => ts('Renew Membership'),
211 $this->add('select', "member_option[$rowNumber]", '', $options);
213 if ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
214 $options = array('' => '-select-');
215 $optionTypes = array(
216 '1' => ts('Adjust Pledge Payment Schedule?'),
217 '2' => ts('Adjust Total Pledge Amount?'),
219 $this->add('select', "option_type[$rowNumber]", NULL, $optionTypes);
220 if (!empty($this->_batchId
) && !empty($this->_batchInfo
['data']) && !empty($rowNumber)) {
221 $dataValues = json_decode($this->_batchInfo
['data'], TRUE);
222 if (!empty($dataValues['values']['primary_contact_id'][$rowNumber])) {
223 $pledgeIDs = CRM_Pledge_BAO_Pledge
::getContactPledges($dataValues['values']['primary_contact_id'][$rowNumber]);
224 foreach ($pledgeIDs as $pledgeID) {
225 $pledgePayment = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeID);
226 $options +
= array($pledgeID => CRM_Utils_Date
::customFormat($pledgePayment['schedule_date'], '%m/%d/%Y') . ', ' . $pledgePayment['amount'] . ' ' . $pledgePayment['currency']);
231 $this->add('select', "open_pledges[$rowNumber]", '', $options);
234 foreach ($this->_fields
as $name => $field) {
235 if (in_array($field['field_type'], $contactTypes)) {
236 $fld = explode('-', $field['name']);
237 $contactReturnProperties[$field['name']] = $fld[0];
239 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, NULL, FALSE, FALSE, $rowNumber);
241 if (in_array($field['name'], $preserveDefaultsArray)) {
242 $this->_preserveDefault
= FALSE;
247 // CRM-19477: Display Error for Batch Sizes Exceeding php.ini max_input_vars
248 // Notes: $this->_elementIndex gives an approximate count of the variables being sent
249 // An offset value is set to deal with additional vars that are likely passed.
250 // There may be a more accurate way to do this...
251 $offset = 50; // set an offset to account for other vars we are not counting
252 if ((count($this->_elementIndex
) +
$offset) > ini_get("max_input_vars")) {
253 CRM_Core_Error
::fatal(ts('Batch size is too large. Increase value of php.ini setting "max_input_vars" (current val = ' . ini_get("max_input_vars") . ')'));
256 $this->assign('fields', $this->_fields
);
257 CRM_Core_Resources
::singleton()
260 'return' => implode(',', $contactReturnProperties),
261 'fieldmap' => array_flip($contactReturnProperties),
265 // don't set the status message when form is submitted.
266 $buttonName = $this->controller
->getButtonName('submit');
268 if ($suppressFields && $buttonName != '_qf_Entry_next') {
269 CRM_Core_Session
::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple records."), ts('Some Fields Excluded'), 'info');
276 * @param array $params
277 * Posted values of the form.
278 * @param array $files
279 * List of errors to be posted back to the form.
280 * @param \CRM_Batch_Form_Entry $self
284 * list of errors to be posted back to the form
286 public static function formRule($params, $files, $self) {
288 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
290 'total_amount' => ts('Amount'),
291 'financial_type' => ts('Financial Type'),
292 'payment_instrument' => ts('Payment Method'),
295 //CRM-16480 if contact is selected, validate financial type and amount field.
296 foreach ($params['field'] as $key => $value) {
297 if (isset($value['trxn_id'])) {
298 if (0 < CRM_Core_DAO
::singleValueQuery('SELECT id FROM civicrm_contribution WHERE trxn_id = %1', array(1 => array($value['trxn_id'], 'String')))) {
299 $errors["field[$key][trxn_id]"] = ts('Transaction ID must be unique within the database');
302 foreach ($fields as $field => $label) {
303 if (!empty($params['primary_contact_id'][$key]) && empty($value[$field])) {
304 $errors["field[$key][$field]"] = ts('%1 is a required field.', array(1 => $label));
309 if (!empty($params['_qf_Entry_upload_force'])) {
310 if (!empty($errors)) {
317 foreach ($params['field'] as $key => $value) {
318 $batchTotal +
= $value['total_amount'];
320 //validate for soft credit fields
321 if (!empty($params['soft_credit_contact_id'][$key]) && empty($params['soft_credit_amount'][$key])) {
322 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');
324 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'])) {
325 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount should not be greater than the total amount');
328 //membership type is required for membership batch entry
329 if ($self->_batchInfo
['type_id'] == $batchTypes['Membership']) {
330 if (empty($value['membership_type'][1])) {
331 $errors["field[$key][membership_type]"] = ts('Membership type is a required field.');
335 if ($self->_batchInfo
['type_id'] == $batchTypes['Pledge Payment']) {
336 foreach (array_unique($params["open_pledges"]) as $value) {
337 if (!empty($value)) {
338 $duplicateRows = array_keys($params["open_pledges"], $value);
340 if (!empty($duplicateRows) && count($duplicateRows) > 1) {
341 foreach ($duplicateRows as $key) {
342 $errors["open_pledges[$key]"] = ts('You can not record two payments for the same pledge in a single batch.');
347 if ((string) $batchTotal != $self->_batchInfo
['total']) {
348 $self->assign('batchAmountMismatch', TRUE);
349 $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.');
352 if (!empty($errors)) {
356 $self->assign('batchAmountMismatch', FALSE);
361 * Override default cancel action.
363 public function cancelAction() {
364 // redirect to batch listing
365 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
366 CRM_Utils_System
::civiExit();
370 * Set default values for the form.
372 public function setDefaultValues() {
373 if (empty($this->_fields
)) {
377 // for add mode set smart defaults
378 if ($this->_action
& CRM_Core_Action
::ADD
) {
379 $currentDate = date('Y-m-d H-i-s');
381 $completeStatus = CRM_Contribute_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
382 $specialFields = array(
383 'join_date' => date('Y-m-d'),
384 'receive_date' => $currentDate,
385 'contribution_status_id' => $completeStatus,
388 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
389 foreach ($specialFields as $key => $value) {
390 $defaults['field'][$rowNumber][$key] = $value;
395 // get the cached info from data column of civicrm_batch
396 $data = CRM_Core_DAO
::getFieldValue('CRM_Batch_BAO_Batch', $this->_batchId
, 'data');
397 $defaults = json_decode($data, TRUE);
398 $defaults = $defaults['values'];
405 * Process the form after the input has been submitted and validated.
407 public function postProcess() {
408 $params = $this->controller
->exportValues($this->_name
);
409 $params['actualBatchTotal'] = 0;
411 // get the profile information
412 $batchTypes = CRM_Core_PseudoConstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
413 if (in_array($this->_batchInfo
['type_id'], array($batchTypes['Pledge Payment'], $batchTypes['Contribution']))) {
414 $this->processContribution($params);
416 elseif ($this->_batchInfo
['type_id'] == $batchTypes['Membership']) {
417 $this->processMembership($params);
420 // update batch to close status
421 $paramValues = array(
422 'id' => $this->_batchId
,
424 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Closed'),
425 'total' => $params['actualBatchTotal'],
428 CRM_Batch_BAO_Batch
::create($paramValues);
430 // set success status
431 CRM_Core_Session
::setStatus("", ts("Batch Processed."), "success");
433 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
437 * Process contribution records.
439 * @param array $params
440 * Associated array of submitted values.
444 private function processContribution(&$params) {
446 // get the price set associated with offline contribution record.
447 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
448 $this->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
449 $priceFieldID = CRM_Price_BAO_PriceSet
::getOnlyPriceFieldID($this->_priceSet
);
450 $priceFieldValueID = CRM_Price_BAO_PriceSet
::getOnlyPriceFieldValueID($this->_priceSet
);
452 if (isset($params['field'])) {
453 foreach ($params['field'] as $key => $value) {
454 // if contact is not selected we should skip the row
455 if (empty($params['primary_contact_id'][$key])) {
459 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_id']);
461 // update contact information
462 $this->updateContactInfo($value);
464 //build soft credit params
465 if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) {
466 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
467 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
469 //CRM-15350: if soft-credit-type profile field is disabled or removed then
470 //we choose configured SCT default value
471 if (!empty($params['soft_credit_type'][$key])) {
472 $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
475 $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type");
480 if (!empty($params['pcp_made_through_id'][$key])) {
481 $value['pcp']['pcp_made_through_id'] = $params['pcp_made_through_id'][$key];
482 $value['pcp']['pcp_display_in_roll'] = !empty($params['pcp_display_in_roll'][$key]);
483 if (!empty($params['pcp_roll_nickname'][$key])) {
484 $value['pcp']['pcp_roll_nickname'] = $params['pcp_roll_nickname'][$key];
486 if (!empty($params['pcp_personal_note'][$key])) {
487 $value['pcp']['pcp_personal_note'] = $params['pcp_personal_note'][$key];
491 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
496 if (!empty($value['send_receipt'])) {
497 $value['receipt_date'] = date('Y-m-d His');
499 // these translations & date handling are required because we are calling BAO directly rather than the api
500 $fieldTranslations = array(
501 'financial_type' => 'financial_type_id',
502 'payment_instrument' => 'payment_instrument_id',
503 'contribution_source' => 'source',
504 'contribution_note' => 'note',
507 foreach ($fieldTranslations as $formField => $baoField) {
508 if (isset($value[$formField])) {
509 $value[$baoField] = $value[$formField];
511 unset($value[$formField]);
514 $params['actualBatchTotal'] +
= $value['total_amount'];
515 $value['batch_id'] = $this->_batchId
;
516 $value['skipRecentView'] = TRUE;
518 // build line item params
519 $this->_priceSet
['fields'][$priceFieldID]['options'][$priceFieldValueID]['amount'] = $value['total_amount'];
520 $value['price_' . $priceFieldID] = 1;
523 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'], $value, $lineItem[$priceSetId]);
525 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
526 // function to get correct amount level consistently. Remove setting of the amount level in
527 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
528 // to cover all variants.
529 unset($value['amount_level']);
531 //CRM-11529 for back office transactions
532 //when financial_type_id is passed in form, update the
533 //line items with the financial type selected in form
534 // @todo - create a price set or price field per financial type & simply choose the appropriate
535 // price field rather than working around the fact that each price_field is supposed to have a financial
536 // type & we are allowing that to be overridden.
537 if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
538 foreach ($lineItem[$priceSetId] as &$values) {
539 $values['financial_type_id'] = $value['financial_type_id'];
542 $value['line_item'] = $lineItem;
543 //finally call contribution create for all the magic
544 $contribution = CRM_Contribute_BAO_Contribution
::create($value);
545 $batchTypes = CRM_Core_Pseudoconstant
::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
546 if (!empty($this->_batchInfo
['type_id']) && ($this->_batchInfo
['type_id'] == $batchTypes['Pledge Payment'])) {
547 $adjustTotalAmount = FALSE;
548 if (isset($params['option_type'][$key])) {
549 if ($params['option_type'][$key] == 2) {
550 $adjustTotalAmount = TRUE;
553 $pledgeId = $params['open_pledges'][$key];
554 if (is_numeric($pledgeId)) {
555 $result = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($pledgeId);
556 $pledgePaymentId = 0;
557 foreach ($result as $key => $values) {
558 if ($values['status'] != 'Completed') {
559 $pledgePaymentId = $values['id'];
563 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentId, 'contribution_id', $contribution->id
);
564 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($pledgeId,
565 array($pledgePaymentId),
566 $contribution->contribution_status_id
,
568 $contribution->total_amount
,
575 if (!empty($value['product_name'])) {
576 if ($value['product_name'][0] > 0) {
577 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
579 $value['hidden_Premium'] = 1;
580 $value['product_option'] = CRM_Utils_Array
::value(
581 $value['product_name'][1],
582 $options[$value['product_name'][0]]
585 $premiumParams = array(
586 'product_id' => $value['product_name'][0],
587 'contribution_id' => $contribution->id
,
588 'product_option' => $value['product_option'],
591 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
597 if ($contribution->id
&& !empty($value['send_receipt'])) {
598 // add the domain email id
599 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
600 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
601 $value['from_email_address'] = $domainEmail;
602 $value['contribution_id'] = $contribution->id
;
603 if (!empty($value['soft_credit'])) {
604 $value = array_merge($value, CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($contribution->id
));
606 CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $value);
614 * Process membership records.
616 * @param array $params
617 * Associated array of submitted values.
622 private function processMembership(&$params) {
624 // get the price set associated with offline membership
625 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name');
626 $this->_priceSet
= $priceSets = current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
628 if (isset($params['field'])) {
629 // @todo - most of the wrangling in this function is because the api is not being used, especially date stuff.
630 $customFields = array();
631 foreach ($params['field'] as $key => $value) {
632 foreach ($value as $fieldKey => $fieldValue) {
633 if (isset($this->_fields
[$fieldKey]) && $this->_fields
[$fieldKey]['data_type'] === 'Money') {
634 $value[$fieldKey] = CRM_Utils_Rule
::cleanMoney($fieldValue);
637 // if contact is not selected we should skip the row
638 if (empty($params['primary_contact_id'][$key])) {
642 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_id']);
644 // update contact information
645 $this->updateContactInfo($value);
647 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
649 if (!empty($value['send_receipt'])) {
650 $value['receipt_date'] = date('Y-m-d His');
653 if (!empty($value['membership_source'])) {
654 $value['source'] = $value['membership_source'];
657 unset($value['membership_source']);
659 //Get the membership status
660 if (!empty($value['membership_status'])) {
661 $value['status_id'] = $value['membership_status'];
662 unset($value['membership_status']);
665 if (empty($customFields)) {
666 // membership type custom data
667 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership', FALSE, FALSE, $membershipTypeId);
669 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
670 CRM_Core_BAO_CustomField
::getFields('Membership',
671 FALSE, FALSE, NULL, NULL, TRUE
676 //check for custom data
677 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($params['field'][$key],
683 if (!empty($value['financial_type'])) {
684 $value['financial_type_id'] = $value['financial_type'];
687 if (!empty($value['payment_instrument'])) {
688 $value['payment_instrument_id'] = $value['payment_instrument'];
691 // handle soft credit
692 if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) {
693 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key];
694 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
696 //CRM-15350: if soft-credit-type profile field is disabled or removed then
697 //we choose Gift as default value as per Gift Membership rule
698 if (!empty($params['soft_credit_type'][$key])) {
699 $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
702 $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'Gift');
706 $params['actualBatchTotal'] +
= $value['total_amount'];
708 unset($value['financial_type']);
709 unset($value['payment_instrument']);
711 $value['batch_id'] = $this->_batchId
;
712 $value['skipRecentView'] = TRUE;
714 // make entry in line item for contribution
716 $editedFieldParams = array(
717 'price_set_id' => $priceSetId,
718 'name' => $value['membership_type'][0],
721 $editedResults = array();
722 CRM_Price_BAO_PriceField
::retrieve($editedFieldParams, $editedResults);
724 if (!empty($editedResults)) {
725 unset($this->_priceSet
['fields']);
726 $this->_priceSet
['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']];
727 unset($this->_priceSet
['fields'][$editedResults['id']]['options']);
728 $fid = $editedResults['id'];
729 $editedFieldParams = array(
730 'price_field_id' => $editedResults['id'],
731 'membership_type_id' => $value['membership_type_id'],
734 $editedResults = array();
735 CRM_Price_BAO_PriceFieldValue
::retrieve($editedFieldParams, $editedResults);
736 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']];
737 if (!empty($value['total_amount'])) {
738 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']]['amount'] = $value['total_amount'];
741 $fieldID = key($this->_priceSet
['fields']);
742 $value['price_' . $fieldID] = $editedResults['id'];
745 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
746 $value, $lineItem[$priceSetId]
749 //CRM-11529 for backoffice transactions
750 //when financial_type_id is passed in form, update the
751 //lineitems with the financial type selected in form
752 if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) {
753 foreach ($lineItem[$priceSetId] as &$values) {
754 $values['financial_type_id'] = $value['financial_type_id'];
758 $value['lineItems'] = $lineItem;
759 $value['processPriceSet'] = TRUE;
761 // end of contribution related section
763 unset($value['membership_type']);
765 $value['is_renew'] = FALSE;
766 if (!empty($params['member_option']) && CRM_Utils_Array
::value($key, $params['member_option']) == 2) {
768 // The following parameter setting may be obsolete.
769 $this->_params
= $params;
770 $value['is_renew'] = TRUE;
771 $isPayLater = CRM_Utils_Array
::value('is_pay_later', $params);
773 if (isset($this->_values
) && is_array($this->_values
) && !empty($this->_values
)) {
774 $campaignId = CRM_Utils_Array
::value('campaign_id', $this->_params
);
775 if (!array_key_exists('campaign_id', $this->_params
)) {
776 $campaignId = CRM_Utils_Array
::value('campaign_id', $this->_values
);
781 'end_date' => CRM_Utils_Array
::value('membership_end_date', $value),
782 'start_date' => CRM_Utils_Array
::value('membership_start_date', $value),
784 $membershipSource = CRM_Utils_Array
::value('source', $value);
785 list($membership) = CRM_Member_BAO_Membership
::processMembership(
786 $value['contact_id'], $value['membership_type_id'], FALSE,
787 //$numTerms should be default to 1.
788 NULL, NULL, $value['custom'], 1, NULL, FALSE,
789 NULL, $membershipSource, $isPayLater, $campaignId, $formDates
792 // make contribution entry
793 $contrbutionParams = array_merge($value, array('membership_id' => $membership->id
));
794 $contrbutionParams['skipCleanMoney'] = TRUE;
795 // @todo - calling this from here is pretty hacky since it is called from membership.create anyway
796 // This form should set the correct params & not call this fn directly.
797 CRM_Member_BAO_Membership
::recordMembershipContribution($contrbutionParams);
801 'join_date' => 'joinDate',
802 'membership_start_date' => 'startDate',
803 'membership_end_date' => 'endDate',
812 foreach ($dateTypes as $dateField => $dateVariable) {
813 $
$dateVariable = CRM_Utils_Date
::processDate($value[$dateField]);
814 $fDate[$dateField] = CRM_Utils_Array
::value($dateField, $value);
817 $calcDates = array();
818 $calcDates[$membershipTypeId] = CRM_Member_BAO_MembershipType
::getDatesForMembershipType($membershipTypeId,
819 $joinDate, $startDate, $endDate
822 foreach ($calcDates as $memType => $calcDate) {
823 foreach ($dates as $d) {
824 //first give priority to form values then calDates.
825 $date = CRM_Utils_Array
::value($d, $value);
827 $date = CRM_Utils_Array
::value($d, $calcDate);
830 $value[$d] = CRM_Utils_Date
::processDate($date);
834 unset($value['membership_start_date']);
835 unset($value['membership_end_date']);
837 $membership = CRM_Member_BAO_Membership
::create($value, $ids);
841 if (!empty($value['product_name'])) {
842 if ($value['product_name'][0] > 0) {
843 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
845 $value['hidden_Premium'] = 1;
846 $value['product_option'] = CRM_Utils_Array
::value(
847 $value['product_name'][1],
848 $options[$value['product_name'][0]]
851 $premiumParams = array(
852 'product_id' => $value['product_name'][0],
853 'contribution_id' => CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id
, 'contribution_id', 'membership_id'),
854 'product_option' => $value['product_option'],
857 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
863 if ($membership->id
&& !empty($value['send_receipt'])) {
865 // add the domain email id
866 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
867 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
869 $value['from_email_address'] = $domainEmail;
870 $value['membership_id'] = $membership->id
;
871 $value['contribution_id'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id
, 'contribution_id', 'membership_id');
872 CRM_Member_Form_Membership
::emailReceipt($this, $value, $membership);
880 * Update contact information.
882 * @param array $value
883 * Associated array of submitted values.
885 private function updateContactInfo(&$value) {
886 $value['preserveDBName'] = $this->_preserveDefault
;
888 //parse street address, CRM-7768
889 CRM_Contact_Form_Task_Batch
::parseStreetAddress($value, $this);
891 CRM_Contact_BAO_Contact
::createProfileContact($value, $this->_fields
,
897 * Function exists purely for unit testing purposes.
899 * If you feel tempted to use this in live code then it probably means there is some functionality
900 * that needs to be moved out of the form layer
902 * @param array $params
906 public function testProcessMembership($params) {
907 return $this->processMembership($params);
911 * Function exists purely for unit testing purposes.
913 * If you feel tempted to use this in live code then it probably means there is some functionality
914 * that needs to be moved out of the form layer.
916 * @param array $params
920 public function testProcessContribution($params) {
921 return $this->processContribution($params);