Merge pull request #13968 from eileenmcnaughton/array_format
[civicrm-core.git] / CRM / Member / Form / MembershipRenewal.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class generates form components for Membership Renewal
36 */
37 class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
38
39 /**
40 * Display name of the member.
41 *
42 * @var string
43 */
44 protected $_memberDisplayName = NULL;
45
46 /**
47 * email of the person paying for the membership (used for receipts)
48 */
49 protected $_memberEmail = NULL;
50
51 /**
52 * Contact ID of the member.
53 *
54 *
55 * @var int
56 */
57 public $_contactID = NULL;
58
59 /**
60 * Display name of the person paying for the membership (used for receipts)
61 *
62 * @var string
63 */
64 protected $_contributorDisplayName = NULL;
65
66 /**
67 * email of the person paying for the membership (used for receipts)
68 */
69 protected $_contributorEmail = NULL;
70
71 /**
72 * email of the person paying for the membership (used for receipts)
73 *
74 * @var int
75 */
76 protected $_contributorContactID = NULL;
77
78 /**
79 * ID of the person the receipt is to go to
80 *
81 * @var int
82 */
83 protected $_receiptContactId = NULL;
84
85 /**
86 * context would be set to standalone if the contact is use is being selected from
87 * the form rather than in the URL
88 */
89 public $_context;
90
91 /**
92 * End date of renewed membership.
93 *
94 * @var string
95 */
96 protected $endDate = NULL;
97
98 /**
99 * Has an email been sent.
100 *
101 * @var string
102 */
103 protected $isMailSent = FALSE;
104
105 /**
106 * The name of the renewed membership type.
107 *
108 * @var string
109 */
110 protected $membershipTypeName = '';
111
112 /**
113 * Set entity fields to be assigned to the form.
114 */
115 protected function setEntityFields() {}
116
117 /**
118 * Set the delete message.
119 *
120 * We do this from the constructor in order to do a translation.
121 */
122 public function setDeleteMessage() {}
123
124 /**
125 * Pre-process form.
126 *
127 * @throws \Exception
128 */
129 public function preProcess() {
130
131 // This string makes up part of the class names, differentiating them (not sure why) from the membership fields.
132 $this->assign('formClass', 'membershiprenew');
133 parent::preProcess();
134
135 $this->assign('endDate', CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
136 $this->_id, 'end_date'
137 )
138 ));
139 $this->assign('membershipStatus',
140 CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus',
141 CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
142 $this->_id, 'status_id'
143 ),
144 'name'
145 )
146 );
147
148 if ($this->_mode) {
149 $membershipFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee');
150 if (!$membershipFee) {
151 $statusMsg = ts('Membership Renewal using a credit card requires a Membership fee. Since there is no fee associated with the selected membership type, you can use the normal renewal mode.');
152 CRM_Core_Session::setStatus($statusMsg, '', 'info');
153 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership',
154 "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership"
155 ));
156 }
157 }
158
159 // when custom data is included in this page
160 if (!empty($_POST['hidden_custom'])) {
161 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_memType, 1, 'Membership', $this->_id);
162 CRM_Custom_Form_CustomData::buildQuickForm($this);
163 CRM_Custom_Form_CustomData::setDefaultValues($this);
164 }
165
166 CRM_Utils_System::setTitle(ts('Renew Membership'));
167
168 parent::preProcess();
169 }
170
171 /**
172 * Set default values for the form.
173 * the default values are retrieved from the database
174 *
175 * @return array
176 * Default values.
177 */
178 public function setDefaultValues() {
179
180 $defaults = parent::setDefaultValues();
181
182 // set renewal_date and receive_date to today in correct input format (setDateDefaults uses today if no value passed)
183 $now = date('Y-m-d');
184 $defaults['renewal_date'] = $now;
185 $defaults['receive_date'] = $now . ' ' . date('H:i:s');
186
187 if ($defaults['id']) {
188 $defaults['record_contribution'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment',
189 $defaults['id'],
190 'contribution_id',
191 'membership_id'
192 );
193 }
194
195 $defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'financial_type_id');
196
197 //CRM-13420
198 if (empty($defaults['payment_instrument_id'])) {
199 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
200 }
201
202 $defaults['total_amount'] = CRM_Utils_Money::format(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
203 $this->_memType,
204 'minimum_fee'
205 ), NULL, '%a');
206
207 $defaults['record_contribution'] = 0;
208 $defaults['num_terms'] = 1;
209 $defaults['send_receipt'] = 0;
210
211 //set Soft Credit Type to Gift by default
212 $scTypes = CRM_Core_OptionGroup::values("soft_credit_type");
213 $defaults['soft_credit_type_id'] = CRM_Utils_Array::value(ts('Gift'), array_flip($scTypes));
214
215 $renewalDate = CRM_Utils_Array::value('renewal_date', $defaults);
216 $this->assign('renewalDate', $renewalDate);
217 $this->assign('member_is_test', CRM_Utils_Array::value('member_is_test', $defaults));
218
219 if ($this->_mode) {
220 $defaults = $this->getBillingDefaults($defaults);
221 }
222 return $defaults;
223 }
224
225 /**
226 * Build the form object.
227 */
228 public function buildQuickForm() {
229
230 parent::buildQuickForm();
231
232 $defaults = parent::setDefaultValues();
233 $this->assign('customDataType', 'Membership');
234 $this->assign('customDataSubType', $this->_memType);
235 $this->assign('entityID', $this->_id);
236 $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -');
237
238 $allMembershipInfo = [];
239
240 // CRM-21485
241 if (is_array($defaults['membership_type_id'])) {
242 $defaults['membership_type_id'] = $defaults['membership_type_id'][1];
243 }
244
245 //CRM-16950
246 $taxRate = $this->getTaxRateForFinancialType($this->allMembershipTypeDetails[$defaults['membership_type_id']]['financial_type_id']);
247
248 // auto renew options if enabled for the membership
249 $options = CRM_Core_SelectValues::memberAutoRenew();
250
251 foreach ($this->allMembershipTypeDetails as $key => $values) {
252 if (!empty($values['is_active'])) {
253 if ($this->_mode && empty($values['minimum_fee'])) {
254 continue;
255 }
256 else {
257 $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values);
258 if (empty($selMemTypeOrg[$memberOfContactId])) {
259 $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
260 $memberOfContactId,
261 'display_name',
262 'id'
263 );
264
265 $selOrgMemType[$memberOfContactId][0] = ts('- select -');
266 }
267 if (empty($selOrgMemType[$memberOfContactId][$key])) {
268 $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values);
269 }
270 }
271
272 //CRM-16950
273 $taxAmount = NULL;
274 $totalAmount = CRM_Utils_Array::value('minimum_fee', $values);
275 // @todo - feels a bug - we use taxRate from the form default rather than from the specified type?!?
276 if ($this->getTaxRateForFinancialType($values['financial_type_id'])) {
277 $taxAmount = ($taxRate / 100) * CRM_Utils_Array::value('minimum_fee', $values);
278 $totalAmount = $totalAmount + $taxAmount;
279 }
280
281 // build membership info array, which is used to set the payment information block when
282 // membership type is selected.
283 $allMembershipInfo[$key] = [
284 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values),
285 'total_amount' => CRM_Utils_Money::format($totalAmount, NULL, '%a'),
286 'total_amount_numeric' => $totalAmount,
287 'tax_message' => $taxAmount ? ts("Includes %1 amount of %2", [1 => $this->getSalesTaxTerm(), 2 => CRM_Utils_Money::format($taxAmount)]) : $taxAmount,
288 ];
289
290 if (!empty($values['auto_renew'])) {
291 $allMembershipInfo[$key]['auto_renew'] = $options[$values['auto_renew']];
292 }
293 }
294 }
295
296 $this->assign('allMembershipInfo', json_encode($allMembershipInfo));
297
298 if ($this->_memType) {
299 $this->assign('orgName', $selMemTypeOrg[$this->allMembershipTypeDetails[$this->_memType]['member_of_contact_id']]);
300 $this->assign('memType', $this->allMembershipTypeDetails[$this->_memType]['name']);
301 }
302
303 // force select of organization by default, if only one organization in
304 // the list
305 if (count($selMemTypeOrg) == 2) {
306 unset($selMemTypeOrg[0], $selOrgMemType[0][0]);
307 }
308 //sort membership organization and type, CRM-6099
309 natcasesort($selMemTypeOrg);
310 foreach ($selOrgMemType as $index => $orgMembershipType) {
311 natcasesort($orgMembershipType);
312 $selOrgMemType[$index] = $orgMembershipType;
313 }
314
315 $js = ['onChange' => "setPaymentBlock(); CRM.buildCustomData('Membership', this.value);"];
316 $sel = &$this->addElement('hierselect',
317 'membership_type_id',
318 ts('Renewal Membership Organization and Type'), $js
319 );
320
321 $sel->setOptions([$selMemTypeOrg, $selOrgMemType]);
322 $elements = [];
323 if ($sel) {
324 $elements[] = $sel;
325 }
326
327 $this->applyFilter('__ALL__', 'trim');
328
329 $this->add('datepicker', 'renewal_date', ts('Date Renewal Entered'), [], FALSE, ['time' => FALSE]);
330
331 $this->add('select', 'financial_type_id', ts('Financial Type'),
332 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::financialType()
333 );
334
335 $this->add('number', 'num_terms', ts('Extend Membership by'), ['onchange' => "setPaymentBlock();"], TRUE);
336 $this->addRule('num_terms', ts('Please enter a whole number for how many periods to renew.'), 'integer');
337
338 if (CRM_Core_Permission::access('CiviContribute') && !$this->_mode) {
339 $this->addElement('checkbox', 'record_contribution', ts('Record Renewal Payment?'), NULL, ['onclick' => "checkPayment();"]);
340
341 $this->add('text', 'total_amount', ts('Amount'));
342 $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');
343
344 $this->add('datepicker', 'receive_date', ts('Received'), [], FALSE, ['time' => TRUE]);
345
346 $this->add('select', 'payment_instrument_id', ts('Payment Method'),
347 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::paymentInstrument(),
348 FALSE, ['onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);"]
349 );
350
351 $this->add('text', 'trxn_id', ts('Transaction ID'));
352 $this->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
353 'objectExists', ['CRM_Contribute_DAO_Contribution', $this->_id, 'trxn_id']
354 );
355
356 $this->add('select', 'contribution_status_id', ts('Payment Status'),
357 CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses('membership')
358 );
359
360 $this->add('text', 'check_number', ts('Check Number'),
361 CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
362 );
363 }
364 else {
365 $this->add('text', 'total_amount', ts('Amount'));
366 $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');
367 }
368 $this->addElement('checkbox', 'send_receipt', ts('Send Confirmation and Receipt?'), NULL,
369 ['onclick' => "showHideByValue( 'send_receipt', '', 'notice', 'table-row', 'radio', false ); showHideByValue( 'send_receipt', '', 'fromEmail', 'table-row', 'radio',false);"]
370 );
371
372 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails);
373
374 $this->add('textarea', 'receipt_text_renewal', ts('Renewal Message'));
375
376 // Retrieve the name and email of the contact - this will be the TO for receipt email
377 list($this->_contributorDisplayName,
378 $this->_contributorEmail
379 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
380 $this->assign('email', $this->_contributorEmail);
381 // The member form uses emailExists. Assigning both while we transition / synchronise.
382 $this->assign('emailExists', $this->_contributorEmail);
383
384 $mailingInfo = Civi::settings()->get('mailing_backend');
385 $this->assign('outBound_option', $mailingInfo['outBound_option']);
386
387 if (CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'contribution_recur_id')) {
388 if (CRM_Member_BAO_Membership::isCancelSubscriptionSupported($this->_id)) {
389 $this->assign('cancelAutoRenew',
390 CRM_Utils_System::url('civicrm/contribute/unsubscribe', "reset=1&mid={$this->_id}")
391 );
392 }
393 }
394 $this->addFormRule(['CRM_Member_Form_MembershipRenewal', 'formRule'], $this);
395 $this->addElement('checkbox', 'is_different_contribution_contact', ts('Record Payment from a Different Contact?'));
396 $this->addSelect('soft_credit_type_id', ['entity' => 'contribution_soft']);
397 $this->addEntityRef('soft_credit_contact_id', ts('Payment From'), ['create' => TRUE]);
398 }
399
400 /**
401 * Validation.
402 *
403 * @param array $params
404 * (ref.) an assoc array of name/value pairs.
405 *
406 * @return bool|array
407 * mixed true or array of errors
408 */
409 public static function formRule($params, $files, $self) {
410 $errors = [];
411 if ($params['membership_type_id'][0] == 0) {
412 $errors['membership_type_id'] = ts('Oops. It looks like you are trying to change the membership type while renewing the membership. Please click the "change membership type" link, and select a Membership Organization.');
413 }
414 if ($params['membership_type_id'][1] == 0) {
415 $errors['membership_type_id'] = ts('Oops. It looks like you are trying to change the membership type while renewing the membership. Please click the "change membership type" link and select a Membership Type from the list.');
416 }
417
418 // CRM-20571
419 // Get the Join Date from Membership info as it is not available in the Renewal form
420 $joinDate = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $self->_id, 'join_date');
421
422 // CRM-20571: Check if the renewal date is not before Join Date, if it is then add to 'errors' array
423 // The fields in Renewal form come into this routine in $params array. 'renewal_date' is in the form
424 // We process both the dates before comparison using CRM utils so that they are in same date format
425 if (isset($params['renewal_date'])) {
426 if ($params['renewal_date'] < $joinDate) {
427 $errors['renewal_date'] = ts('Renewal date must be the same or later than Member since (Join Date).');
428 }
429 }
430
431 //total amount condition arise when membership type having no
432 //minimum fee
433 if (isset($params['record_contribution'])) {
434 if (!$params['financial_type_id']) {
435 $errors['financial_type_id'] = ts('Please select a Financial Type.');
436 }
437 if (!$params['total_amount']) {
438 $errors['total_amount'] = ts('Please enter a Contribution Amount.');
439 }
440 if (empty($params['payment_instrument_id'])) {
441 $errors['payment_instrument_id'] = ts('Payment Method is a required field.');
442 }
443 }
444 return empty($errors) ? TRUE : $errors;
445 }
446
447 /**
448 * Process the renewal form.
449 */
450 public function postProcess() {
451 // get the submitted form values.
452 $this->_params = $this->controller->exportValues($this->_name);
453 $this->assignBillingName();
454
455 try {
456 $this->submit();
457 $statusMsg = ts('%1 membership for %2 has been renewed.', [1 => $this->membershipTypeName, 2 => $this->_memberDisplayName]);
458
459 if ($this->endDate) {
460 $statusMsg .= ' ' . ts('The new membership End Date is %1.', [
461 1 => CRM_Utils_Date::customFormat(substr($this->endDate, 0, 8)),
462 ]);
463 }
464
465 if ($this->isMailSent) {
466 $statusMsg .= ' ' . ts('A renewal confirmation and receipt has been sent to %1.', [
467 1 => $this->_contributorEmail,
468 ]);
469 return $statusMsg;
470 }
471 return $statusMsg;
472 }
473 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
474 CRM_Core_Session::singleton()->setStatus($e->getMessage());
475 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership',
476 "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership&mode={$this->_mode}"
477 ));
478 }
479
480 CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success');
481 }
482
483 /**
484 * Process form submission.
485 *
486 * This function is also accessed by a unit test.
487 */
488 protected function submit() {
489 $this->storeContactFields($this->_params);
490 $this->beginPostProcess();
491 $now = CRM_Utils_Date::getToday(NULL, 'YmdHis');
492 $this->assign('receive_date', CRM_Utils_Array::value('receive_date', $this->_params, date('Y-m-d H:i:s')));
493 $this->processBillingAddress();
494 list($userName) = CRM_Contact_BAO_Contact_Location::getEmailDetails(CRM_Core_Session::singleton()->get('userID'));
495 $this->_params['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_params,
496 CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee')
497 );
498 $this->_membershipId = $this->_id;
499 $customFieldsFormatted = CRM_Core_BAO_CustomField::postProcess($this->_params,
500 $this->_id,
501 'Membership'
502 );
503 if (empty($this->_params['financial_type_id'])) {
504 $this->_params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'financial_type_id');
505 }
506 $contributionRecurID = NULL;
507 $this->assign('membershipID', $this->_id);
508 $this->assign('contactID', $this->_contactID);
509 $this->assign('module', 'Membership');
510 $this->assign('receiptType', 'membership renewal');
511 $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency;
512 $this->_params['invoice_id'] = $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
513
514 if (!empty($this->_params['send_receipt'])) {
515 $this->_params['receipt_date'] = $now;
516 $this->assign('receipt_date', CRM_Utils_Date::mysqlToIso($this->_params['receipt_date']));
517 }
518 else {
519 $this->_params['receipt_date'] = NULL;
520 }
521
522 if ($this->_mode) {
523 $this->_params['register_date'] = $now;
524 $this->_params['description'] = ts("Contribution submitted by a staff person using member's credit card for renewal");
525 $this->_params['amount'] = $this->_params['total_amount'];
526 $this->_params['payment_instrument_id'] = $this->_paymentProcessor['payment_instrument_id'];
527
528 // at this point we've created a contact and stored its address etc
529 // all the payment processors expect the name and address to be in the passed params
530 // so we copy stuff over to first_name etc.
531 $paymentParams = $this->_params;
532 if (!empty($this->_params['send_receipt'])) {
533 $paymentParams['email'] = $this->_contributorEmail;
534 }
535 $paymentParams['is_email_receipt'] = !empty($this->_params['send_receipt']);
536
537 $paymentParams['contactID'] = $this->_contributorContactID;
538
539 CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE);
540
541 $payment = $this->_paymentProcessor['object'];
542
543 if (!empty($this->_params['auto_renew'])) {
544 $contributionRecurParams = $this->processRecurringContribution($paymentParams);
545 $contributionRecurID = $contributionRecurParams['contributionRecurID'];
546 $paymentParams = array_merge($paymentParams, $contributionRecurParams);
547 }
548
549 $result = $payment->doPayment($paymentParams);
550 $this->_params = array_merge($this->_params, $result);
551
552 $this->_params['contribution_status_id'] = $result['payment_status_id'];
553 $this->_params['trxn_id'] = $result['trxn_id'];
554 $this->_params['is_test'] = ($this->_mode == 'live') ? 0 : 1;
555 $this->set('params', $this->_params);
556 $this->assign('trxn_id', $result['trxn_id']);
557 }
558
559 $renewalDate = !empty($this->_params['renewal_date']) ? $renewalDate = $this->_params['renewal_date'] : NULL;
560
561 // check for test membership.
562 $isTestMembership = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_membershipId, 'is_test');
563
564 // chk for renewal for multiple terms CRM-8750
565 $numRenewTerms = 1;
566 if (is_numeric(CRM_Utils_Array::value('num_terms', $this->_params))) {
567 $numRenewTerms = $this->_params['num_terms'];
568 }
569
570 //if contribution status is pending then set pay later
571 $this->_params['is_pay_later'] = FALSE;
572 if ($this->_params['contribution_status_id'] == array_search('Pending', CRM_Contribute_PseudoConstant::contributionStatus())) {
573 $this->_params['is_pay_later'] = 1;
574 }
575
576 // These variable sets prior to membership may not be required for this form. They were in
577 // a function this form shared with other forms.
578 $membershipSource = NULL;
579 if (!empty($this->_params['membership_source'])) {
580 $membershipSource = $this->_params['membership_source'];
581 }
582
583 $isPending = ($this->_params['contribution_status_id'] == 2) ? TRUE : FALSE;
584
585 list($membership) = CRM_Member_BAO_Membership::processMembership(
586 $this->_contactID, $this->_params['membership_type_id'][1], $isTestMembership,
587 $renewalDate, NULL, $customFieldsFormatted, $numRenewTerms, $this->_membershipId,
588 $isPending,
589 $contributionRecurID, $membershipSource, $this->_params['is_pay_later'], CRM_Utils_Array::value('campaign_id',
590 $this->_params)
591 );
592
593 $this->endDate = CRM_Utils_Date::processDate($membership->end_date);
594
595 $this->membershipTypeName = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membership->membership_type_id,
596 'name');
597
598 if (!empty($this->_params['record_contribution']) || $this->_mode) {
599 // set the source
600 $this->_params['contribution_source'] = "{$this->membershipTypeName} Membership: Offline membership renewal (by {$userName})";
601
602 //create line items
603 $lineItem = [];
604 $this->_params = $this->setPriceSetParameters($this->_params);
605 CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'],
606 $this->_params, $lineItem[$this->_priceSetId], NULL, $this->_priceSetId
607 );
608 //CRM-11529 for quick config backoffice transactions
609 //when financial_type_id is passed in form, update the
610 //line items with the financial type selected in form
611 if ($submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $this->_params)) {
612 foreach ($lineItem[$this->_priceSetId] as &$li) {
613 $li['financial_type_id'] = $submittedFinancialType;
614 }
615 }
616
617 if (!empty($lineItem)) {
618 $this->_params['lineItems'] = $lineItem;
619 $this->_params['processPriceSet'] = TRUE;
620 }
621
622 //assign contribution contact id to the field expected by recordMembershipContribution
623 if ($this->_contributorContactID != $this->_contactID) {
624 $this->_params['contribution_contact_id'] = $this->_contributorContactID;
625 if (!empty($this->_params['soft_credit_type_id'])) {
626 $this->_params['soft_credit'] = [
627 'soft_credit_type_id' => $this->_params['soft_credit_type_id'],
628 'contact_id' => $this->_contactID,
629 ];
630 }
631 }
632 $this->_params['contact_id'] = $this->_contactID;
633 //recordMembershipContribution receives params as a reference & adds one variable. This is
634 // not a great pattern & ideally it would not receive as a reference. We assign our params as a
635 // temporary variable to avoid e-notice & to make it clear to future refactorer that
636 // this function is NOT reliant on that var being set
637 $temporaryParams = array_merge($this->_params, [
638 'membership_id' => $membership->id,
639 'contribution_recur_id' => $contributionRecurID,
640 ]);
641 //Remove `tax_amount` if it is not calculated.
642 if (CRM_Utils_Array::value('tax_amount', $temporaryParams) === 0) {
643 unset($temporaryParams['tax_amount']);
644 }
645 CRM_Member_BAO_Membership::recordMembershipContribution($temporaryParams);
646 }
647
648 if (!empty($this->_params['send_receipt'])) {
649
650 $receiptFrom = $this->_params['from_email_address'];
651
652 if (!empty($this->_params['payment_instrument_id'])) {
653 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
654 $this->_params['paidBy'] = $paymentInstrument[$this->_params['payment_instrument_id']];
655 }
656 //get the group Tree
657 $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree('Membership', NULL, $this->_id, FALSE, $this->_memType);
658
659 // retrieve custom data
660 $customFields = $customValues = $fo = [];
661 foreach ($this->_groupTree as $groupID => $group) {
662 if ($groupID == 'info') {
663 continue;
664 }
665 foreach ($group['fields'] as $k => $field) {
666 $field['title'] = $field['label'];
667 $customFields["custom_{$k}"] = $field;
668 }
669 }
670 $members = [['member_id', '=', $this->_membershipId, 0, 0]];
671 // check whether its a test drive
672 if ($this->_mode == 'test') {
673 $members[] = ['member_test', '=', 1, 0, 0];
674 }
675 CRM_Core_BAO_UFGroup::getValues($this->_contactID, $customFields, $customValues, FALSE, $members);
676
677 $this->assign_by_ref('formValues', $this->_params);
678 if (!empty($this->_params['contribution_id'])) {
679 $this->assign('contributionID', $this->_params['contribution_id']);
680 }
681
682 $this->assign('membership_name', CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
683 $membership->membership_type_id
684 ));
685 $this->assign('customValues', $customValues);
686 $this->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date));
687 $this->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date));
688 if ($this->_mode) {
689 $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters(
690 $this->_params,
691 $this->_bltID
692 ));
693 $this->assign('contributeMode', 'direct');
694 $this->assign('isAmountzero', 0);
695 $this->assign('is_pay_later', 0);
696 $this->assign('isPrimary', 1);
697 $this->assign('receipt_text_renewal', $this->_params['receipt_text']);
698 if ($this->_mode == 'test') {
699 $this->assign('action', '1024');
700 }
701 }
702
703 list($this->isMailSent) = CRM_Core_BAO_MessageTemplate::sendTemplate(
704 [
705 'groupName' => 'msg_tpl_workflow_membership',
706 'valueName' => 'membership_offline_receipt',
707 'contactId' => $this->_receiptContactId,
708 'from' => $receiptFrom,
709 'toName' => $this->_contributorDisplayName,
710 'toEmail' => $this->_contributorEmail,
711 'isTest' => $this->_mode == 'test',
712 ]
713 );
714 }
715 }
716
717 }