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