Merge pull request #17571 from jitendrapurohit/core-1809
[civicrm-core.git] / CRM / Member / Form / MembershipRenewal.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for Membership Renewal
20 */
21 class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
22
23 /**
24 * Display name of the member.
25 *
26 * @var string
27 */
28 protected $_memberDisplayName = NULL;
29
30 /**
31 * email of the person paying for the membership (used for receipts)
32 *
33 * @var string
34 */
35 protected $_memberEmail = NULL;
36
37 /**
38 * Contact ID of the member.
39 *
40 *
41 * @var int
42 */
43 public $_contactID = NULL;
44
45 /**
46 * Display name of the person paying for the membership (used for receipts)
47 *
48 * @var string
49 */
50 protected $_contributorDisplayName = NULL;
51
52 /**
53 * email of the person paying for the membership (used for receipts)
54 *
55 * @var string
56 */
57 protected $_contributorEmail = NULL;
58
59 /**
60 * email of the person paying for the membership (used for receipts)
61 *
62 * @var int
63 */
64 protected $_contributorContactID = NULL;
65
66 /**
67 * ID of the person the receipt is to go to
68 *
69 * @var int
70 */
71 protected $_receiptContactId = NULL;
72
73 /**
74 * context would be set to standalone if the contact is use is being selected from
75 * the form rather than in the URL
76 *
77 * @var string
78 */
79 public $_context;
80
81 /**
82 * End date of renewed membership.
83 *
84 * @var string
85 */
86 protected $endDate = NULL;
87
88 /**
89 * Has an email been sent.
90 *
91 * @var string
92 */
93 protected $isMailSent = FALSE;
94
95 /**
96 * The name of the renewed membership type.
97 *
98 * @var string
99 */
100 protected $membershipTypeName = '';
101
102 /**
103 * Set entity fields to be assigned to the form.
104 */
105 protected function setEntityFields() {
106 }
107
108 /**
109 * Set the delete message.
110 *
111 * We do this from the constructor in order to do a translation.
112 */
113 public function setDeleteMessage() {
114 }
115
116 /**
117 * Set the renewal notification status message.
118 */
119 public function setRenewalMessage() {
120 $statusMsg = ts('%1 membership for %2 has been renewed.', [1 => $this->membershipTypeName, 2 => $this->_memberDisplayName]);
121
122 if ($this->isMailSent) {
123 $statusMsg .= ' ' . ts('A renewal confirmation and receipt has been sent to %1.', [1 => $this->_contributorEmail]);
124 }
125 CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success');
126 }
127
128 /**
129 * Preprocess form.
130 *
131 * @throws \CRM_Core_Exception
132 * @throws \CiviCRM_API3_Exception
133 */
134 public function preProcess() {
135
136 // This string makes up part of the class names, differentiating them (not sure why) from the membership fields.
137 $this->assign('formClass', 'membershiprenew');
138 parent::preProcess();
139
140 $this->assign('endDate', CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
141 $this->_id, 'end_date'
142 )
143 ));
144 $this->assign('membershipStatus',
145 CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus',
146 CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
147 $this->_id, 'status_id'
148 ),
149 'name'
150 )
151 );
152
153 if ($this->_mode) {
154 $membershipFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee');
155 if (!$membershipFee) {
156 $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.');
157 CRM_Core_Session::setStatus($statusMsg, '', 'info');
158 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership',
159 "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership"
160 ));
161 }
162 }
163
164 // when custom data is included in this page
165 if (!empty($_POST['hidden_custom'])) {
166 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_memType, 1, 'Membership', $this->_id);
167 CRM_Custom_Form_CustomData::buildQuickForm($this);
168 CRM_Custom_Form_CustomData::setDefaultValues($this);
169 }
170
171 CRM_Utils_System::setTitle(ts('Renew Membership'));
172
173 parent::preProcess();
174 }
175
176 /**
177 * Set default values for the form.
178 * the default values are retrieved from the database
179 *
180 * @return array
181 * Default values.
182 * @throws \CRM_Core_Exception
183 */
184 public function setDefaultValues() {
185
186 $defaults = parent::setDefaultValues();
187
188 // set renewal_date and receive_date to today in correct input format (setDateDefaults uses today if no value passed)
189 $now = date('Y-m-d');
190 $defaults['renewal_date'] = $now;
191 $defaults['receive_date'] = $now . ' ' . date('H:i:s');
192
193 if ($defaults['id']) {
194 $defaults['record_contribution'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment',
195 $defaults['id'],
196 'contribution_id',
197 'membership_id'
198 );
199 }
200
201 $defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'financial_type_id');
202
203 //CRM-13420
204 if (empty($defaults['payment_instrument_id'])) {
205 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
206 }
207
208 $defaults['total_amount'] = CRM_Utils_Money::format(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
209 $this->_memType,
210 'minimum_fee'
211 ), NULL, '%a');
212
213 $defaults['record_contribution'] = 0;
214 $defaults['num_terms'] = 1;
215 $defaults['send_receipt'] = 0;
216
217 //set Soft Credit Type to Gift by default
218 $scTypes = CRM_Core_OptionGroup::values('soft_credit_type');
219 $defaults['soft_credit_type_id'] = CRM_Utils_Array::value(ts('Gift'), array_flip($scTypes));
220
221 $renewalDate = $defaults['renewal_date'] ?? NULL;
222 $this->assign('renewalDate', $renewalDate);
223 $this->assign('member_is_test', CRM_Utils_Array::value('member_is_test', $defaults));
224
225 if ($this->_mode) {
226 $defaults = $this->getBillingDefaults($defaults);
227 }
228 return $defaults;
229 }
230
231 /**
232 * Build the form object.
233 *
234 * @throws \CRM_Core_Exception
235 */
236 public function buildQuickForm() {
237
238 parent::buildQuickForm();
239
240 $defaults = parent::setDefaultValues();
241 $this->assign('customDataType', 'Membership');
242 $this->assign('customDataSubType', $this->_memType);
243 $this->assign('entityID', $this->_id);
244 $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -');
245
246 $allMembershipInfo = [];
247
248 // CRM-21485
249 if (is_array($defaults['membership_type_id'])) {
250 $defaults['membership_type_id'] = $defaults['membership_type_id'][1];
251 }
252
253 //CRM-16950
254 $taxRate = $this->getTaxRateForFinancialType($this->allMembershipTypeDetails[$defaults['membership_type_id']]['financial_type_id']);
255
256 $contactField = $this->addEntityRef('contact_id', ts('Member'), ['create' => TRUE, 'api' => ['extra' => ['email']]], TRUE);
257 $contactField->freeze();
258
259 // auto renew options if enabled for the membership
260 $options = CRM_Core_SelectValues::memberAutoRenew();
261
262 foreach ($this->allMembershipTypeDetails as $key => $values) {
263 if (!empty($values['is_active'])) {
264 if ($this->_mode && empty($values['minimum_fee'])) {
265 continue;
266 }
267 else {
268 $memberOfContactId = $values['member_of_contact_id'] ?? NULL;
269 if (empty($selMemTypeOrg[$memberOfContactId])) {
270 $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
271 $memberOfContactId,
272 'display_name',
273 'id'
274 );
275
276 $selOrgMemType[$memberOfContactId][0] = ts('- select -');
277 }
278 if (empty($selOrgMemType[$memberOfContactId][$key])) {
279 $selOrgMemType[$memberOfContactId][$key] = $values['name'] ?? NULL;
280 }
281 }
282
283 //CRM-16950
284 $taxAmount = NULL;
285 $totalAmount = $values['minimum_fee'] ?? NULL;
286 // @todo - feels a bug - we use taxRate from the form default rather than from the specified type?!?
287 if ($this->getTaxRateForFinancialType($values['financial_type_id'])) {
288 $taxAmount = ($taxRate / 100) * CRM_Utils_Array::value('minimum_fee', $values);
289 $totalAmount = $totalAmount + $taxAmount;
290 }
291
292 // build membership info array, which is used to set the payment information block when
293 // membership type is selected.
294 $allMembershipInfo[$key] = [
295 'financial_type_id' => $values['financial_type_id'] ?? NULL,
296 'total_amount' => CRM_Utils_Money::format($totalAmount, NULL, '%a'),
297 'total_amount_numeric' => $totalAmount,
298 'tax_message' => $taxAmount ? ts("Includes %1 amount of %2", [1 => $this->getSalesTaxTerm(), 2 => CRM_Utils_Money::format($taxAmount)]) : $taxAmount,
299 ];
300
301 if (!empty($values['auto_renew'])) {
302 $allMembershipInfo[$key]['auto_renew'] = $options[$values['auto_renew']];
303 }
304 }
305 }
306
307 $this->assign('allMembershipInfo', json_encode($allMembershipInfo));
308
309 if ($this->_memType) {
310 $this->assign('orgName', $selMemTypeOrg[$this->allMembershipTypeDetails[$this->_memType]['member_of_contact_id']]);
311 $this->assign('memType', $this->allMembershipTypeDetails[$this->_memType]['name']);
312 }
313
314 // force select of organization by default, if only one organization in
315 // the list
316 if (count($selMemTypeOrg) == 2) {
317 unset($selMemTypeOrg[0], $selOrgMemType[0][0]);
318 }
319 //sort membership organization and type, CRM-6099
320 natcasesort($selMemTypeOrg);
321 foreach ($selOrgMemType as $index => $orgMembershipType) {
322 natcasesort($orgMembershipType);
323 $selOrgMemType[$index] = $orgMembershipType;
324 }
325
326 $js = ['onChange' => "setPaymentBlock(); CRM.buildCustomData('Membership', this.value);"];
327 $sel = &$this->addElement('hierselect',
328 'membership_type_id',
329 ts('Renewal Membership Organization and Type'), $js
330 );
331
332 $sel->setOptions([$selMemTypeOrg, $selOrgMemType]);
333 $elements = [];
334 if ($sel) {
335 $elements[] = $sel;
336 }
337
338 $this->applyFilter('__ALL__', 'trim');
339
340 $this->add('datepicker', 'renewal_date', ts('Date Renewal Entered'), [], FALSE, ['time' => FALSE]);
341
342 $this->add('select', 'financial_type_id', ts('Financial Type'),
343 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::financialType()
344 );
345
346 $this->add('number', 'num_terms', ts('Extend Membership by'), ['onchange' => "setPaymentBlock();"], TRUE);
347 $this->addRule('num_terms', ts('Please enter a whole number for how many periods to renew.'), 'integer');
348
349 if (CRM_Core_Permission::access('CiviContribute') && !$this->_mode) {
350 $this->addElement('checkbox', 'record_contribution', ts('Record Renewal Payment?'), NULL, ['onclick' => "checkPayment();"]);
351
352 $this->add('text', 'total_amount', ts('Amount'));
353 $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');
354
355 $this->add('datepicker', 'receive_date', ts('Received'), [], FALSE, ['time' => TRUE]);
356
357 $this->add('select', 'payment_instrument_id', ts('Payment Method'),
358 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::paymentInstrument(),
359 FALSE, ['onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);"]
360 );
361
362 $this->add('text', 'trxn_id', ts('Transaction ID'));
363 $this->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
364 'objectExists', ['CRM_Contribute_DAO_Contribution', $this->_id, 'trxn_id']
365 );
366
367 $this->add('select', 'contribution_status_id', ts('Payment Status'),
368 CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses('membership')
369 );
370
371 $this->add('text', 'check_number', ts('Check Number'),
372 CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
373 );
374 }
375 else {
376 $this->add('text', 'total_amount', ts('Amount'));
377 $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');
378 }
379 $this->addElement('checkbox', 'send_receipt', ts('Send Confirmation and Receipt?'), NULL,
380 ['onclick' => "showHideByValue( 'send_receipt', '', 'notice', 'table-row', 'radio', false ); showHideByValue( 'send_receipt', '', 'fromEmail', 'table-row', 'radio',false);"]
381 );
382
383 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails);
384
385 $this->add('textarea', 'receipt_text_renewal', ts('Renewal Message'));
386
387 // Retrieve the name and email of the contact - this will be the TO for receipt email
388 list($this->_contributorDisplayName,
389 $this->_contributorEmail
390 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
391 $this->assign('email', $this->_contributorEmail);
392 // The member form uses emailExists. Assigning both while we transition / synchronise.
393 $this->assign('emailExists', $this->_contributorEmail);
394
395 $mailingInfo = Civi::settings()->get('mailing_backend');
396 $this->assign('outBound_option', $mailingInfo['outBound_option']);
397
398 if (CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'contribution_recur_id')) {
399 if (CRM_Member_BAO_Membership::isCancelSubscriptionSupported($this->_id)) {
400 $this->assign('cancelAutoRenew',
401 CRM_Utils_System::url('civicrm/contribute/unsubscribe', "reset=1&mid={$this->_id}")
402 );
403 }
404 }
405 $this->addFormRule(['CRM_Member_Form_MembershipRenewal', 'formRule'], $this);
406 $this->addElement('checkbox', 'is_different_contribution_contact', ts('Record Payment from a Different Contact?'));
407 $this->addSelect('soft_credit_type_id', ['entity' => 'contribution_soft']);
408 $this->addEntityRef('soft_credit_contact_id', ts('Payment From'), ['create' => TRUE]);
409 }
410
411 /**
412 * Validation.
413 *
414 * @param array $params
415 * (ref.) an assoc array of name/value pairs.
416 * @param $files
417 * @param $self
418 *
419 * @return bool|array
420 * mixed true or array of errors
421 * @throws \CRM_Core_Exception
422 */
423 public static function formRule($params, $files, $self) {
424 $errors = [];
425 if ($params['membership_type_id'][0] == 0) {
426 $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.');
427 }
428 if ($params['membership_type_id'][1] == 0) {
429 $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.');
430 }
431
432 // CRM-20571
433 // Get the Join Date from Membership info as it is not available in the Renewal form
434 $joinDate = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $self->_id, 'join_date');
435
436 // CRM-20571: Check if the renewal date is not before Join Date, if it is then add to 'errors' array
437 // The fields in Renewal form come into this routine in $params array. 'renewal_date' is in the form
438 // We process both the dates before comparison using CRM utils so that they are in same date format
439 if (isset($params['renewal_date'])) {
440 if ($params['renewal_date'] < $joinDate) {
441 $errors['renewal_date'] = ts('Renewal date must be the same or later than Member since (Join Date).');
442 }
443 }
444
445 //total amount condition arise when membership type having no
446 //minimum fee
447 if (isset($params['record_contribution'])) {
448 if (!$params['financial_type_id']) {
449 $errors['financial_type_id'] = ts('Please select a Financial Type.');
450 }
451 if (!$params['total_amount']) {
452 $errors['total_amount'] = ts('Please enter a Contribution Amount.');
453 }
454 if (empty($params['payment_instrument_id'])) {
455 $errors['payment_instrument_id'] = ts('Payment Method is a required field.');
456 }
457 }
458 return empty($errors) ? TRUE : $errors;
459 }
460
461 /**
462 * Process the renewal form.
463 *
464 * @throws \CRM_Core_Exception
465 * @throws \CiviCRM_API3_Exception
466 */
467 public function postProcess() {
468 // get the submitted form values.
469 $this->_params = $this->controller->exportValues($this->_name);
470 $this->assignBillingName();
471
472 try {
473 $this->submit();
474 $this->setRenewalMessage();
475 }
476 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
477 CRM_Core_Session::singleton()->setStatus($e->getMessage());
478 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership',
479 "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership&mode={$this->_mode}"
480 ));
481 }
482 }
483
484 /**
485 * Process form submission.
486 *
487 * This function is also accessed by a unit test.
488 *
489 * @throws \CRM_Core_Exception
490 * @throws \CiviCRM_API3_Exception
491 */
492 protected function submit() {
493 $this->storeContactFields($this->_params);
494 $this->beginPostProcess();
495 $now = CRM_Utils_Date::getToday(NULL, 'YmdHis');
496 $this->assign('receive_date', CRM_Utils_Array::value('receive_date', $this->_params, date('Y-m-d H:i:s')));
497 $this->processBillingAddress();
498 list($userName) = CRM_Contact_BAO_Contact_Location::getEmailDetails(CRM_Core_Session::singleton()->get('userID'));
499 $this->_params['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_params,
500 CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee')
501 );
502 $this->_membershipId = $this->_id;
503 $customFieldsFormatted = CRM_Core_BAO_CustomField::postProcess($this->_params,
504 $this->_id,
505 'Membership'
506 );
507 if (empty($this->_params['financial_type_id'])) {
508 $this->_params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'financial_type_id');
509 }
510 $contributionRecurID = NULL;
511 $this->assign('membershipID', $this->_id);
512 $this->assign('contactID', $this->_contactID);
513 $this->assign('module', 'Membership');
514 $this->assign('receiptType', 'membership renewal');
515 $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency;
516 $this->_params['invoice_id'] = $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
517
518 if (!empty($this->_params['send_receipt'])) {
519 $this->_params['receipt_date'] = $now;
520 $this->assign('receipt_date', CRM_Utils_Date::mysqlToIso($this->_params['receipt_date']));
521 }
522 else {
523 $this->_params['receipt_date'] = NULL;
524 }
525
526 if ($this->_mode) {
527 $this->_params['register_date'] = $now;
528 $this->_params['description'] = ts("Contribution submitted by a staff person using member's credit card for renewal");
529 $this->_params['amount'] = $this->_params['total_amount'];
530 $this->_params['payment_instrument_id'] = $this->_paymentProcessor['payment_instrument_id'];
531 $this->_params['receive_date'] = $now;
532
533 // at this point we've created a contact and stored its address etc
534 // all the payment processors expect the name and address to be in the passed params
535 // so we copy stuff over to first_name etc.
536 $paymentParams = $this->_params;
537 if (!empty($this->_params['send_receipt'])) {
538 $paymentParams['email'] = $this->_contributorEmail;
539 }
540
541 $paymentParams['contactID'] = $this->_contributorContactID;
542
543 CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE);
544
545 if (!empty($this->_params['auto_renew'])) {
546
547 $contributionRecurParams = $this->processRecurringContribution([
548 'contact_id' => $this->_contributorContactID,
549 'amount' => $this->_params['total_amount'],
550 'contribution_status_id' => 'Pending',
551 'payment_processor_id' => $this->_params['payment_processor_id'],
552 'financial_type_id' => $this->_params['financial_type_id'],
553 'is_email_receipt' => !empty($this->_params['send_receipt']),
554 'payment_instrument_id' => $this->_params['payment_instrument_id'],
555 'invoice_id' => $this->_params['invoice_id'],
556 ], $membershipID = $paymentParams['membership_type_id'][1]);
557
558 $contributionRecurID = $contributionRecurParams['contributionRecurID'];
559 $paymentParams = array_merge($paymentParams, $contributionRecurParams);
560 }
561
562 $payment = $this->_paymentProcessor['object'];
563 $result = $payment->doPayment($paymentParams);
564 $this->_params = array_merge($this->_params, $result);
565
566 $this->_params['contribution_status_id'] = $result['payment_status_id'];
567 $this->_params['trxn_id'] = $result['trxn_id'];
568 $this->_params['is_test'] = ($this->_mode === 'live') ? 0 : 1;
569 $this->set('params', $this->_params);
570 $this->assign('trxn_id', $result['trxn_id']);
571 }
572
573 $renewalDate = !empty($this->_params['renewal_date']) ? $renewalDate = $this->_params['renewal_date'] : NULL;
574
575 // check for test membership.
576 $isTestMembership = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_membershipId, 'is_test');
577
578 // chk for renewal for multiple terms CRM-8750
579 $numRenewTerms = 1;
580 if (is_numeric(CRM_Utils_Array::value('num_terms', $this->_params))) {
581 $numRenewTerms = $this->_params['num_terms'];
582 }
583
584 //if contribution status is pending then set pay later
585 $this->_params['is_pay_later'] = FALSE;
586 if ($this->_params['contribution_status_id'] == array_search('Pending', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label'))) {
587 $this->_params['is_pay_later'] = 1;
588 }
589
590 $pending = ($this->_params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'));
591
592 $membership = $this->processMembership(
593 $this->_contactID, $this->_params['membership_type_id'][1], $isTestMembership,
594 $renewalDate, $customFieldsFormatted, $numRenewTerms, $this->_membershipId,
595 $pending,
596 $contributionRecurID, $this->_params['is_pay_later']);
597
598 $this->endDate = CRM_Utils_Date::processDate($membership->end_date);
599
600 $this->membershipTypeName = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membership->membership_type_id,
601 'name');
602
603 if (!empty($this->_params['record_contribution']) || $this->_mode) {
604 // set the source
605 $this->_params['contribution_source'] = "{$this->membershipTypeName} Membership: Offline membership renewal (by {$userName})";
606
607 //create line items
608 $lineItem = [];
609 $this->_params = $this->setPriceSetParameters($this->_params);
610
611 $order = new CRM_Financial_BAO_Order();
612 $order->setPriceSelectionFromUnfilteredInput($this->_params);
613 $order->setPriceSetID(self::getPriceSetID($this->_params));
614 $order->setOverrideTotalAmount($this->_params['total_amount']);
615 $order->setOverrideFinancialTypeID((int) $this->_params['financial_type_id']);
616
617 $this->_params['lineItems'][$this->_priceSetId] = $order->getLineItems();
618 // This is one of those weird & wonderful legacy params we aim to get rid of.
619 $this->_params['processPriceSet'] = TRUE;
620 $this->_params['tax_amount'] = $order->getTotalTaxAmount();
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 // ?? WHY - I haven't been able to figure out...
643 if (CRM_Utils_Array::value('tax_amount', $temporaryParams) === 0.0) {
644 unset($temporaryParams['tax_amount']);
645 }
646 CRM_Member_BAO_Membership::recordMembershipContribution($temporaryParams);
647 }
648
649 if (!empty($this->_params['send_receipt'])) {
650 $this->sendReceipt($membership);
651 }
652 }
653
654 /**
655 * Send a receipt.
656 *
657 * @param array $membership
658 *
659 * @throws \CRM_Core_Exception
660 */
661 protected function sendReceipt($membership) {
662 $receiptFrom = $this->_params['from_email_address'];
663
664 if (!empty($this->_params['payment_instrument_id'])) {
665 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
666 $this->_params['paidBy'] = $paymentInstrument[$this->_params['payment_instrument_id']];
667 }
668 //get the group Tree
669 $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree('Membership', NULL, $this->_id, FALSE, $this->_memType);
670
671 // retrieve custom data
672 $customFields = $customValues = $fo = [];
673 foreach ($this->_groupTree as $groupID => $group) {
674 if ($groupID === 'info') {
675 continue;
676 }
677 foreach ($group['fields'] as $k => $field) {
678 $field['title'] = $field['label'];
679 $customFields["custom_{$k}"] = $field;
680 }
681 }
682 $members = [['member_id', '=', $this->_membershipId, 0, 0]];
683 // check whether its a test drive
684 if ($this->_mode === 'test') {
685 $members[] = ['member_test', '=', 1, 0, 0];
686 }
687 CRM_Core_BAO_UFGroup::getValues($this->_contactID, $customFields, $customValues, FALSE, $members);
688
689 $this->assign_by_ref('formValues', $this->_params);
690 if (!empty($this->_params['contribution_id'])) {
691 $this->assign('contributionID', $this->_params['contribution_id']);
692 }
693
694 $this->assign('membership_name', CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
695 $membership->membership_type_id
696 ));
697 $this->assign('customValues', $customValues);
698 $this->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date));
699 $this->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date));
700 if ($this->_mode) {
701 $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters(
702 $this->_params,
703 $this->_bltID
704 ));
705 $this->assign('contributeMode', 'direct');
706 $this->assign('isAmountzero', 0);
707 $this->assign('is_pay_later', 0);
708 $this->assign('isPrimary', 1);
709 $this->assign('receipt_text_renewal', $this->_params['receipt_text']);
710 if ($this->_mode === 'test') {
711 $this->assign('action', '1024');
712 }
713 }
714
715 list($this->isMailSent) = CRM_Core_BAO_MessageTemplate::sendTemplate(
716 [
717 'groupName' => 'msg_tpl_workflow_membership',
718 'valueName' => 'membership_offline_receipt',
719 'contactId' => $this->_receiptContactId,
720 'from' => $receiptFrom,
721 'toName' => $this->_contributorDisplayName,
722 'toEmail' => $this->_contributorEmail,
723 'isTest' => $this->_mode === 'test',
724 ]
725 );
726 }
727
728 /**
729 * Process membership.
730 *
731 * This is duplicated from the BAO class - on the basis that it's actually easier to divide & conquer when
732 * it comes to clearing up really bad code.
733 *
734 * @param int $contactID
735 * @param int $membershipTypeID
736 * @param bool $is_test
737 * @param string $changeToday
738 * @param $customFieldsFormatted
739 * @param $numRenewTerms
740 * @param int $membershipID
741 * @param $pending
742 * @param int $contributionRecurID
743 * @param $membershipSource
744 * @param $isPayLater
745 *
746 * @return CRM_Member_BAO_Membership
747 * @throws \CRM_Core_Exception
748 * @throws \CiviCRM_API3_Exception
749 */
750 public function processMembership($contactID, $membershipTypeID, $is_test, $changeToday, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $contributionRecurID, $isPayLater) {
751 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
752 $format = '%Y%m%d';
753 $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipTypeID);
754 $ids = [];
755
756 // CRM-7297 - allow membership type to be be changed during renewal so long as the parent org of new membershipType
757 // is the same as the parent org of an existing membership of the contact
758 $currentMembership = CRM_Member_BAO_Membership::getContactMembership($contactID, $membershipTypeID,
759 $is_test, $membershipID, TRUE
760 );
761
762 // Do NOT do anything.
763 //1. membership with status : PENDING/CANCELLED (CRM-2395)
764 //2. Paylater/IPN renew. CRM-4556.
765 if ($pending || in_array($currentMembership['status_id'], [
766 array_search('Pending', $allStatus),
767 // CRM-15475
768 array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus(NULL, " name = 'Cancelled' ", 'name', FALSE, TRUE)),
769 ])) {
770
771 $memParams = [
772 'id' => $currentMembership['id'],
773 'status_id' => $currentMembership['status_id'],
774 'start_date' => $currentMembership['start_date'],
775 'end_date' => $currentMembership['end_date'],
776 'join_date' => $currentMembership['join_date'],
777 'membership_type_id' => $membershipTypeID,
778 'max_related' => !empty($membershipTypeDetails['max_related']) ? $membershipTypeDetails['max_related'] : NULL,
779 'membership_activity_status' => ($pending || $isPayLater) ? 'Scheduled' : 'Completed',
780 ];
781 if ($contributionRecurID) {
782 $memParams['contribution_recur_id'] = $contributionRecurID;
783 }
784 // @todo stop passing $ids - it is empty
785 return CRM_Member_BAO_Membership::create($memParams, $ids);
786 }
787
788 // Check and fix the membership if it is STALE
789 CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday);
790
791 // Now Renew the membership
792 if (!$currentMembership['is_current_member']) {
793 // membership is not CURRENT
794
795 // CRM-7297 Membership Upsell - calculate dates based on new membership type
796 $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($currentMembership['id'],
797 $changeToday,
798 $membershipTypeID,
799 $numRenewTerms
800 );
801
802 $currentMembership['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format);
803 foreach (['start_date', 'end_date'] as $dateType) {
804 $currentMembership[$dateType] = $dates[$dateType] ?? NULL;
805 }
806 $currentMembership['is_test'] = $is_test;
807
808 $currentMembership['source'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
809 $currentMembership['id'],
810 'source'
811 );
812
813 if (!empty($currentMembership['id'])) {
814 $ids['membership'] = $currentMembership['id'];
815 }
816 $memParams = $currentMembership;
817 $memParams['membership_type_id'] = $membershipTypeID;
818
819 //set the log start date.
820 $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format);
821 }
822 else {
823
824 // CURRENT Membership
825 $membership = new CRM_Member_DAO_Membership();
826 $membership->id = $currentMembership['id'];
827 $membership->find(TRUE);
828 // CRM-7297 Membership Upsell - calculate dates based on new membership type
829 $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id,
830 $changeToday,
831 $membershipTypeID,
832 $numRenewTerms
833 );
834
835 // Insert renewed dates for CURRENT membership
836 $memParams = [];
837 $memParams['join_date'] = $membership->join_date;
838 $memParams['start_date'] = $membership->start_date;
839 $memParams['end_date'] = $dates['end_date'] ?? NULL;
840 $memParams['membership_type_id'] = $membershipTypeID;
841
842 //set the log start date.
843 $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format);
844
845 if (empty($membership->source)) {
846 $memParams['source'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
847 $currentMembership['id'],
848 'source'
849 );
850 }
851
852 if (!empty($currentMembership['id'])) {
853 $ids['membership'] = $currentMembership['id'];
854 }
855 $memParams['membership_activity_status'] = ($pending || $isPayLater) ? 'Scheduled' : 'Completed';
856 }
857
858 // Putting this in an IF is precautionary as it seems likely that it would be ignored if empty, but
859 // perhaps shouldn't be?
860 if ($contributionRecurID) {
861 $memParams['contribution_recur_id'] = $contributionRecurID;
862 }
863
864 //since we are renewing,
865 //make status override false.
866 $memParams['is_override'] = FALSE;
867
868 $params['modified_id'] = $contactID;
869
870 $memParams['custom'] = $customFieldsFormatted;
871 // @todo stop passing $ids (membership and userId may be set by this point)
872 $membership = CRM_Member_BAO_Membership::create($memParams, $ids);
873
874 // not sure why this statement is here, seems quite odd :( - Lobo: 12/26/2010
875 // related to: http://forum.civicrm.org/index.php/topic,11416.msg49072.html#msg49072
876 $membership->find(TRUE);
877
878 return $membership;
879 }
880
881 }