Merge pull request #17298 from demeritcowboy/activity-attachment-delete
[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 'campaign_id' => $this->_params['campaign_id'],
553 'financial_type_id' => $this->_params['financial_type_id'],
554 'is_email_receipt' => !empty($this->_params['send_receipt']),
555 'payment_instrument_id' => $this->_params['payment_instrument_id'],
556 'invoice_id' => $this->_params['invoice_id'],
557 ], $membershipID = $paymentParams['membership_type_id'][1]);
558
559 $contributionRecurID = $contributionRecurParams['contributionRecurID'];
560 $paymentParams = array_merge($paymentParams, $contributionRecurParams);
561 }
562
563 $payment = $this->_paymentProcessor['object'];
564 $result = $payment->doPayment($paymentParams);
565 $this->_params = array_merge($this->_params, $result);
566
567 $this->_params['contribution_status_id'] = $result['payment_status_id'];
568 $this->_params['trxn_id'] = $result['trxn_id'];
569 $this->_params['is_test'] = ($this->_mode === 'live') ? 0 : 1;
570 $this->set('params', $this->_params);
571 $this->assign('trxn_id', $result['trxn_id']);
572 }
573
574 $renewalDate = !empty($this->_params['renewal_date']) ? $renewalDate = $this->_params['renewal_date'] : NULL;
575
576 // check for test membership.
577 $isTestMembership = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_membershipId, 'is_test');
578
579 // chk for renewal for multiple terms CRM-8750
580 $numRenewTerms = 1;
581 if (is_numeric(CRM_Utils_Array::value('num_terms', $this->_params))) {
582 $numRenewTerms = $this->_params['num_terms'];
583 }
584
585 //if contribution status is pending then set pay later
586 $this->_params['is_pay_later'] = FALSE;
587 if ($this->_params['contribution_status_id'] == array_search('Pending', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label'))) {
588 $this->_params['is_pay_later'] = 1;
589 }
590
591 // These variable sets prior to membership may not be required for this form. They were in
592 // a function this form shared with other forms.
593 $membershipSource = NULL;
594 if (!empty($this->_params['membership_source'])) {
595 $membershipSource = $this->_params['membership_source'];
596 }
597
598 $pending = ($this->_params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'));
599 list($membership) = $this->processMembership(
600 $this->_contactID, $this->_params['membership_type_id'][1], $isTestMembership,
601 $renewalDate, NULL, $customFieldsFormatted, $numRenewTerms, $this->_membershipId,
602 $pending,
603 $contributionRecurID, $membershipSource, $this->_params['is_pay_later'], CRM_Utils_Array::value('campaign_id',
604 $this->_params)
605 );
606
607 $this->endDate = CRM_Utils_Date::processDate($membership->end_date);
608
609 $this->membershipTypeName = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membership->membership_type_id,
610 'name');
611
612 if (!empty($this->_params['record_contribution']) || $this->_mode) {
613 // set the source
614 $this->_params['contribution_source'] = "{$this->membershipTypeName} Membership: Offline membership renewal (by {$userName})";
615
616 //create line items
617 $lineItem = [];
618 $this->_params = $this->setPriceSetParameters($this->_params);
619
620 $order = new CRM_Financial_BAO_Order();
621 $order->setPriceSelectionFromUnfilteredInput($this->_params);
622 $order->setPriceSetID(self::getPriceSetID($this->_params));
623 $order->setOverrideTotalAmount($this->_params['total_amount']);
624 $order->setOverrideFinancialTypeID((int) $this->_params['financial_type_id']);
625
626 $this->_params['lineItems'][$this->_priceSetId] = $order->getLineItems();
627 // This is one of those weird & wonderful legacy params we aim to get rid of.
628 $this->_params['processPriceSet'] = TRUE;
629 $this->_params['tax_amount'] = $order->getTotalTaxAmount();
630
631 //assign contribution contact id to the field expected by recordMembershipContribution
632 if ($this->_contributorContactID != $this->_contactID) {
633 $this->_params['contribution_contact_id'] = $this->_contributorContactID;
634 if (!empty($this->_params['soft_credit_type_id'])) {
635 $this->_params['soft_credit'] = [
636 'soft_credit_type_id' => $this->_params['soft_credit_type_id'],
637 'contact_id' => $this->_contactID,
638 ];
639 }
640 }
641 $this->_params['contact_id'] = $this->_contactID;
642 //recordMembershipContribution receives params as a reference & adds one variable. This is
643 // not a great pattern & ideally it would not receive as a reference. We assign our params as a
644 // temporary variable to avoid e-notice & to make it clear to future refactorer that
645 // this function is NOT reliant on that var being set
646 $temporaryParams = array_merge($this->_params, [
647 'membership_id' => $membership->id,
648 'contribution_recur_id' => $contributionRecurID,
649 ]);
650 //Remove `tax_amount` if it is not calculated.
651 // ?? WHY - I haven't been able to figure out...
652 if (CRM_Utils_Array::value('tax_amount', $temporaryParams) === 0.0) {
653 unset($temporaryParams['tax_amount']);
654 }
655 CRM_Member_BAO_Membership::recordMembershipContribution($temporaryParams);
656 }
657
658 if (!empty($this->_params['send_receipt'])) {
659 $this->sendReceipt($membership);
660 }
661 }
662
663 /**
664 * Send a receipt.
665 *
666 * @param array $membership
667 *
668 * @throws \CRM_Core_Exception
669 */
670 protected function sendReceipt($membership) {
671 $receiptFrom = $this->_params['from_email_address'];
672
673 if (!empty($this->_params['payment_instrument_id'])) {
674 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
675 $this->_params['paidBy'] = $paymentInstrument[$this->_params['payment_instrument_id']];
676 }
677 //get the group Tree
678 $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree('Membership', NULL, $this->_id, FALSE, $this->_memType);
679
680 // retrieve custom data
681 $customFields = $customValues = $fo = [];
682 foreach ($this->_groupTree as $groupID => $group) {
683 if ($groupID === 'info') {
684 continue;
685 }
686 foreach ($group['fields'] as $k => $field) {
687 $field['title'] = $field['label'];
688 $customFields["custom_{$k}"] = $field;
689 }
690 }
691 $members = [['member_id', '=', $this->_membershipId, 0, 0]];
692 // check whether its a test drive
693 if ($this->_mode === 'test') {
694 $members[] = ['member_test', '=', 1, 0, 0];
695 }
696 CRM_Core_BAO_UFGroup::getValues($this->_contactID, $customFields, $customValues, FALSE, $members);
697
698 $this->assign_by_ref('formValues', $this->_params);
699 if (!empty($this->_params['contribution_id'])) {
700 $this->assign('contributionID', $this->_params['contribution_id']);
701 }
702
703 $this->assign('membership_name', CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
704 $membership->membership_type_id
705 ));
706 $this->assign('customValues', $customValues);
707 $this->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date));
708 $this->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date));
709 if ($this->_mode) {
710 $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters(
711 $this->_params,
712 $this->_bltID
713 ));
714 $this->assign('contributeMode', 'direct');
715 $this->assign('isAmountzero', 0);
716 $this->assign('is_pay_later', 0);
717 $this->assign('isPrimary', 1);
718 $this->assign('receipt_text_renewal', $this->_params['receipt_text']);
719 if ($this->_mode === 'test') {
720 $this->assign('action', '1024');
721 }
722 }
723
724 list($this->isMailSent) = CRM_Core_BAO_MessageTemplate::sendTemplate(
725 [
726 'groupName' => 'msg_tpl_workflow_membership',
727 'valueName' => 'membership_offline_receipt',
728 'contactId' => $this->_receiptContactId,
729 'from' => $receiptFrom,
730 'toName' => $this->_contributorDisplayName,
731 'toEmail' => $this->_contributorEmail,
732 'isTest' => $this->_mode === 'test',
733 ]
734 );
735 }
736
737 /**
738 * Process membership.
739 *
740 * This is duplicated from the BAO class - on the basis that it's actually easier to divide & conquer when
741 * it comes to clearing up really bad code.
742 *
743 * @param int $contactID
744 * @param int $membershipTypeID
745 * @param bool $is_test
746 * @param string $changeToday
747 * @param int $modifiedID
748 * @param $customFieldsFormatted
749 * @param $numRenewTerms
750 * @param int $membershipID
751 * @param $pending
752 * @param int $contributionRecurID
753 * @param $membershipSource
754 * @param $isPayLater
755 * @param int $campaignId
756 * @param array $formDates
757 * @param null|CRM_Contribute_BAO_Contribution $contribution
758 * @param array $lineItems
759 *
760 * @return array
761 * @throws \CRM_Core_Exception
762 * @throws \CiviCRM_API3_Exception
763 */
764 public function processMembership($contactID, $membershipTypeID, $is_test, $changeToday, $modifiedID, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $contributionRecurID, $membershipSource, $isPayLater, $campaignId, $formDates = [], $contribution = NULL, $lineItems = []) {
765 $renewalMode = $updateStatusId = FALSE;
766 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
767 $format = '%Y%m%d';
768 $statusFormat = '%Y-%m-%d';
769 $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipTypeID);
770 $dates = [];
771 $ids = [];
772
773 // CRM-7297 - allow membership type to be be changed during renewal so long as the parent org of new membershipType
774 // is the same as the parent org of an existing membership of the contact
775 $currentMembership = CRM_Member_BAO_Membership::getContactMembership($contactID, $membershipTypeID,
776 $is_test, $membershipID, TRUE
777 );
778
779 $renewalMode = TRUE;
780
781 // Do NOT do anything.
782 //1. membership with status : PENDING/CANCELLED (CRM-2395)
783 //2. Paylater/IPN renew. CRM-4556.
784 if ($pending || in_array($currentMembership['status_id'], [
785 array_search('Pending', $allStatus),
786 // CRM-15475
787 array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus(NULL, " name = 'Cancelled' ", 'name', FALSE, TRUE)),
788 ])) {
789
790 $memParams = [
791 'id' => $currentMembership['id'],
792 'contribution' => $contribution,
793 'status_id' => $currentMembership['status_id'],
794 'start_date' => $currentMembership['start_date'],
795 'end_date' => $currentMembership['end_date'],
796 'line_item' => $lineItems,
797 'join_date' => $currentMembership['join_date'],
798 'membership_type_id' => $membershipTypeID,
799 'max_related' => !empty($membershipTypeDetails['max_related']) ? $membershipTypeDetails['max_related'] : NULL,
800 'membership_activity_status' => ($pending || $isPayLater) ? 'Scheduled' : 'Completed',
801 ];
802 if ($contributionRecurID) {
803 $memParams['contribution_recur_id'] = $contributionRecurID;
804 }
805 // @todo stop passing $ids - it is empty
806 $membership = CRM_Member_BAO_Membership::create($memParams, $ids);
807 return [$membership, $renewalMode, $dates];
808 }
809
810 // Check and fix the membership if it is STALE
811 CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday);
812
813 // Now Renew the membership
814 if (!$currentMembership['is_current_member']) {
815 // membership is not CURRENT
816
817 // CRM-7297 Membership Upsell - calculate dates based on new membership type
818 $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($currentMembership['id'],
819 $changeToday,
820 $membershipTypeID,
821 $numRenewTerms
822 );
823
824 $currentMembership['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format);
825 foreach (['start_date', 'end_date'] as $dateType) {
826 $currentMembership[$dateType] = $formDates[$dateType] ?? NULL;
827 if (empty($currentMembership[$dateType])) {
828 $currentMembership[$dateType] = $dates[$dateType] ?? NULL;
829 }
830 }
831 $currentMembership['is_test'] = $is_test;
832
833 if (!empty($membershipSource)) {
834 $currentMembership['source'] = $membershipSource;
835 }
836 else {
837 $currentMembership['source'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
838 $currentMembership['id'],
839 'source'
840 );
841 }
842
843 if (!empty($currentMembership['id'])) {
844 $ids['membership'] = $currentMembership['id'];
845 }
846 $memParams = $currentMembership;
847 $memParams['membership_type_id'] = $membershipTypeID;
848
849 //set the log start date.
850 $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format);
851 }
852 else {
853
854 // CURRENT Membership
855 $membership = new CRM_Member_DAO_Membership();
856 $membership->id = $currentMembership['id'];
857 $membership->find(TRUE);
858 // CRM-7297 Membership Upsell - calculate dates based on new membership type
859 $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id,
860 $changeToday,
861 $membershipTypeID,
862 $numRenewTerms
863 );
864
865 // Insert renewed dates for CURRENT membership
866 $memParams = [];
867 $memParams['join_date'] = CRM_Utils_Date::isoToMysql($membership->join_date);
868 $memParams['start_date'] = CRM_Utils_Array::value('start_date', $formDates, CRM_Utils_Date::isoToMysql($membership->start_date));
869 $memParams['end_date'] = $formDates['end_date'] ?? NULL;
870 if (empty($memParams['end_date'])) {
871 $memParams['end_date'] = $dates['end_date'] ?? NULL;
872 }
873 $memParams['membership_type_id'] = $membershipTypeID;
874
875 //set the log start date.
876 $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format);
877
878 //CRM-18067
879 if (!empty($membershipSource)) {
880 $memParams['source'] = $membershipSource;
881 }
882 elseif (empty($membership->source)) {
883 $memParams['source'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',
884 $currentMembership['id'],
885 'source'
886 );
887 }
888
889 if (!empty($currentMembership['id'])) {
890 $ids['membership'] = $currentMembership['id'];
891 }
892 $memParams['membership_activity_status'] = ($pending || $isPayLater) ? 'Scheduled' : 'Completed';
893 }
894 //CRM-4555
895 if ($pending) {
896 $updateStatusId = array_search('Pending', $allStatus);
897 }
898
899 // Putting this in an IF is precautionary as it seems likely that it would be ignored if empty, but
900 // perhaps shouldn't be?
901 if ($contributionRecurID) {
902 $memParams['contribution_recur_id'] = $contributionRecurID;
903 }
904 //CRM-4555
905 //if we decided status here and want to skip status
906 //calculation in create( ); then need to pass 'skipStatusCal'.
907 if ($updateStatusId) {
908 $memParams['status_id'] = $updateStatusId;
909 $memParams['skipStatusCal'] = TRUE;
910 }
911
912 //since we are renewing,
913 //make status override false.
914 $memParams['is_override'] = FALSE;
915
916 //CRM-4027, create log w/ individual contact.
917 if ($modifiedID) {
918 // @todo this param is likely unused now.
919 $memParams['is_for_organization'] = TRUE;
920 }
921 $params['modified_id'] = $modifiedID ?? $contactID;
922
923 //inherit campaign from contrib page.
924 if (isset($campaignId)) {
925 $memParams['campaign_id'] = $campaignId;
926 }
927
928 $memParams['contribution'] = $contribution;
929 $memParams['custom'] = $customFieldsFormatted;
930 // Load all line items & process all in membership. Don't do in contribution.
931 // Relevant tests in api_v3_ContributionPageTest.
932 $memParams['line_item'] = $lineItems;
933 // @todo stop passing $ids (membership and userId may be set by this point)
934 $membership = CRM_Member_BAO_Membership::create($memParams, $ids);
935
936 // not sure why this statement is here, seems quite odd :( - Lobo: 12/26/2010
937 // related to: http://forum.civicrm.org/index.php/topic,11416.msg49072.html#msg49072
938 $membership->find(TRUE);
939
940 return [$membership, $renewalMode, $dates];
941 }
942
943 }