Merge pull request #15535 from eileenmcnaughton/payment_simp
[civicrm-core.git] / CRM / Member / Form / Membership.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/**
5e56c7a5 35 * This class generates form components for offline membership form.
6a488035
TO
36 */
37class CRM_Member_Form_Membership extends CRM_Member_Form {
38
39 protected $_memType = NULL;
40
41 protected $_onlinePendingContributionId;
42
43 public $_mode;
44
45 public $_contributeMode = 'direct';
46
47 protected $_recurMembershipTypes;
48
49 protected $_memTypeSelected;
50
d424ffde 51 /**
fe482240 52 * Display name of the member.
d424ffde
CW
53 *
54 * @var string
6a488035 55 */
b11c92be 56 protected $_memberDisplayName = NULL;
6a488035 57
d424ffde
CW
58 /**
59 * email of the person paying for the membership (used for receipts)
971e129b 60 * @var string
d424ffde 61 */
b11c92be 62 protected $_memberEmail = NULL;
6a488035 63
d424ffde 64 /**
fe482240 65 * Contact ID of the member.
d424ffde
CW
66 *
67 * @var int
68 */
cc984198 69 public $_contactID = NULL;
6a488035 70
d424ffde
CW
71 /**
72 * Display name of the person paying for the membership (used for receipts)
73 *
74 * @var string
75 */
b11c92be 76 protected $_contributorDisplayName = NULL;
6a488035 77
d424ffde 78 /**
b11c92be 79 * email of the person paying for the membership (used for receipts)
971e129b 80 * @var string
b11c92be 81 */
82 protected $_contributorEmail = NULL;
6a488035 83
d424ffde
CW
84 /**
85 * email of the person paying for the membership (used for receipts)
86 *
87 * @var int
88 */
b11c92be 89 protected $_contributorContactID = NULL;
6a488035 90
d424ffde 91 /**
fe482240 92 * ID of the person the receipt is to go to.
d424ffde
CW
93 *
94 * @var int
b11c92be 95 */
96 protected $_receiptContactId = NULL;
6a488035 97
d424ffde 98 /**
4aa7d844 99 * Keep a class variable for ALL membership IDs so
6a488035 100 * postProcess hook function can do something with it
d424ffde
CW
101 *
102 * @var array
6a488035 103 */
be2fb01f 104 protected $_membershipIDs = [];
6a488035 105
e4f5c851 106 /**
59c798c9 107 * Set entity fields to be assigned to the form.
108 */
109 protected function setEntityFields() {
110 $this->entityFields = [
111 'join_date' => [
112 'name' => 'join_date',
113 'description' => ts('Member Since'),
114 ],
115 'start_date' => [
116 'name' => 'start_date',
117 'description' => ts('Start Date'),
118 ],
119 'end_date' => [
120 'name' => 'end_date',
121 'description' => ts('End Date'),
122 ],
123 ];
124 }
125
126 /**
127 * Set the delete message.
e4f5c851 128 *
59c798c9 129 * We do this from the constructor in order to do a translation.
e4f5c851 130 */
59c798c9 131 public function setDeleteMessage() {
132 $this->deleteMessage = '<span class="font-red bold">'
971e129b 133 . ts("WARNING: Deleting this membership will also delete any related payment (contribution) records." . ts("This action cannot be undone.")
59c798c9 134 . '</span><p>'
135 . ts("Consider modifying the membership status instead if you want to maintain an audit trail and avoid losing payment data. You can set the status to Cancelled by editing the membership and clicking the Status Override checkbox.")
136 . '</p><p>'
137 . ts("Click 'Delete' if you want to continue.") . '</p>');
e4f5c851 138 }
139
59c798c9 140 /**
141 * Overriding this entity trait function as not yet tested.
142 *
143 * We continue to rely on legacy handling.
144 */
145 public function addCustomDataToForm() {}
146
59c798c9 147 /**
148 * Overriding this entity trait function as not yet tested.
149 *
150 * We continue to rely on legacy handling.
151 */
152 public function addFormButtons() {}
153
18aa3e1e 154 /**
4efc56ef
EM
155 * Get selected membership type from the form values.
156 *
e4a6290d 157 * @param array $priceSet
4efc56ef
EM
158 * @param array $params
159 *
160 * @return array
18aa3e1e 161 */
e4a6290d 162 public static function getSelectedMemberships($priceSet, $params) {
be2fb01f 163 $memTypeSelected = [];
e4a6290d 164 $priceFieldIDS = self::getPriceFieldIDs($params, $priceSet);
ccb02c2d 165 if (isset($params['membership_type_id']) && !empty($params['membership_type_id'][1])) {
be2fb01f 166 $memTypeSelected = [$params['membership_type_id'][1] => $params['membership_type_id'][1]];
ccb02c2d 167 }
168 else {
18aa3e1e
EM
169 foreach ($priceFieldIDS as $priceFieldId) {
170 if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) {
171 $memTypeSelected[$id] = $id;
172 }
173 }
174 }
18aa3e1e
EM
175 return $memTypeSelected;
176 }
177
178 /**
179 * Extract price set fields and values from $params.
180 *
5e56c7a5 181 * @param array $params
e4a6290d 182 * @param array $priceSet
5e56c7a5 183 *
18aa3e1e
EM
184 * @return array
185 */
e4a6290d 186 public static function getPriceFieldIDs($params, $priceSet) {
be2fb01f 187 $priceFieldIDS = [];
e4a6290d 188 if (isset($priceSet['fields']) && is_array($priceSet['fields'])) {
15724d0a 189 foreach ($priceSet['fields'] as $fieldId => $field) {
e4a6290d 190 if (!empty($params['price_' . $fieldId])) {
191 if (is_array($params['price_' . $fieldId])) {
192 foreach ($params['price_' . $fieldId] as $priceFldVal => $isSet) {
193 if ($isSet) {
194 $priceFieldIDS[] = $priceFldVal;
195 }
18aa3e1e
EM
196 }
197 }
15724d0a 198 elseif (!$field['is_enter_qty']) {
e4a6290d 199 $priceFieldIDS[] = $params['price_' . $fieldId];
200 }
18aa3e1e
EM
201 }
202 }
203 }
204 return $priceFieldIDS;
205 }
206
5e56c7a5 207 /**
208 * Form preProcess function.
209 *
210 * @throws \Exception
211 */
6a488035 212 public function preProcess() {
186a737c
EM
213 // This string makes up part of the class names, differentiating them (not sure why) from the membership fields.
214 $this->assign('formClass', 'membership');
a6513ad5 215 parent::preProcess();
59c798c9 216 if ($this->isUpdateToExistingRecurringMembership()) {
217 $this->entityFields['end_date']['is_freeze'] = TRUE;
218 }
6a488035
TO
219 // get price set id.
220 $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET);
221 $this->set('priceSetId', $this->_priceSetId);
222 $this->assign('priceSetId', $this->_priceSetId);
223
6a488035
TO
224 if ($this->_action & CRM_Core_Action::DELETE) {
225 $contributionID = CRM_Member_BAO_Membership::getMembershipContributionId($this->_id);
226 // check delete permission for contribution
227 if ($this->_id && $contributionID && !CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) {
96ca8376 228 CRM_Core_Error::fatal(ts("This Membership is linked to a contribution. You must have 'delete in CiviContribute' permission in order to delete this record."));
6a488035
TO
229 }
230 }
231
6a488035 232 if ($this->_action & CRM_Core_Action::ADD) {
6a488035
TO
233 if ($this->_contactID) {
234 //check whether contact has a current membership so we can alert user that they may want to do a renewal instead
be2fb01f
CW
235 $contactMemberships = [];
236 $memParams = ['contact_id' => $this->_contactID];
4256ea25 237 CRM_Member_BAO_Membership::getValues($memParams, $contactMemberships, TRUE);
be2fb01f 238 $cMemTypes = [];
4256ea25
AH
239 foreach ($contactMemberships as $mem) {
240 $cMemTypes[] = $mem['membership_type_id'];
241 }
242 if (count($cMemTypes) > 0) {
243 $memberorgs = CRM_Member_BAO_MembershipType::getMemberOfContactByMemTypes($cMemTypes);
be2fb01f 244 $mems_by_org = [];
2e8b13d1 245 foreach ($contactMemberships as $mem) {
4256ea25 246 $mem['member_of_contact_id'] = CRM_Utils_Array::value($mem['membership_type_id'], $memberorgs);
a7488080 247 if (!empty($mem['membership_end_date'])) {
4256ea25
AH
248 $mem['membership_end_date'] = CRM_Utils_Date::customformat($mem['membership_end_date']);
249 }
250 $mem['membership_type'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
251 $mem['membership_type_id'],
252 'name', 'id'
6a488035 253 );
4256ea25
AH
254 $mem['membership_status'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus',
255 $mem['status_id'],
256 'label', 'id'
6a488035 257 );
74dd0d90
CW
258 $mem['renewUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership',
259 "reset=1&action=renew&cid={$this->_contactID}&id={$mem['id']}&context=membership&selectedChild=member"
260 . ($this->_mode ? '&mode=live' : '')
261 );
4256ea25
AH
262 $mem['membershipTab'] = CRM_Utils_System::url('civicrm/contact/view',
263 "reset=1&force=1&cid={$this->_contactID}&selectedChild=member"
264 );
265 $mems_by_org[$mem['member_of_contact_id']] = $mem;
6a488035 266 }
74dd0d90 267 $this->assign('existingContactMemberships', $mems_by_org);
6a488035
TO
268 }
269 }
1001e556 270 else {
74dd0d90 271 // In standalone mode we don't have a contact id yet so lookup will be done client-side with this script:
d292601b
AH
272 $resources = CRM_Core_Resources::singleton();
273 $resources->addScriptFile('civicrm', 'templates/CRM/Member/Form/MembershipStandalone.js');
be2fb01f 274 $passthru = [
d292601b 275 'typeorgs' => CRM_Member_BAO_MembershipType::getMembershipTypeOrganization(),
74dd0d90
CW
276 'memtypes' => CRM_Core_PseudoConstant::get('CRM_Member_BAO_Membership', 'membership_type_id'),
277 'statuses' => CRM_Core_PseudoConstant::get('CRM_Member_BAO_Membership', 'status_id'),
be2fb01f
CW
278 ];
279 $resources->addSetting(['existingMems' => $passthru]);
d292601b 280 }
6a488035
TO
281 }
282
97ae4877 283 if (!$this->_memType) {
284 $params = CRM_Utils_Request::exportValues();
06470609 285 if (!empty($params['membership_type_id'][1])) {
71098424 286 $this->_memType = $params['membership_type_id'][1];
287 }
97ae4877 288 }
6452294d
MW
289
290 // Add custom data to form
291 CRM_Custom_Form_CustomData::addToForm($this, $this->_memType);
6a488035
TO
292
293 // CRM-4395, get the online pending contribution id.
294 $this->_onlinePendingContributionId = NULL;
295 if (!$this->_mode && $this->_id && ($this->_action & CRM_Core_Action::UPDATE)) {
296 $this->_onlinePendingContributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($this->_id,
297 'Membership'
298 );
299 }
300 $this->assign('onlinePendingContributionId', $this->_onlinePendingContributionId);
6a488035 301
e2046b33 302 $this->setPageTitle(ts('Membership'));
6a488035
TO
303 }
304
305 /**
5e56c7a5 306 * Set default values for the form.
6a488035
TO
307 */
308 public function setDefaultValues() {
6a488035
TO
309
310 if ($this->_priceSetId) {
9da8dc8c 311 return CRM_Price_BAO_PriceSet::setDefaultPriceSet($this, $defaults);
6a488035
TO
312 }
313
314 $defaults = parent::setDefaultValues();
315
316 //setting default join date and receive date
6a488035 317 if ($this->_action == CRM_Core_Action::ADD) {
9eb6085d 318 $defaults['receive_date'] = date('Y-m-d H:i:s');
6a488035
TO
319 }
320
6a488035
TO
321 $defaults['num_terms'] = 1;
322
a7488080 323 if (!empty($defaults['id'])) {
6a488035
TO
324 if ($this->_onlinePendingContributionId) {
325 $defaults['record_contribution'] = $this->_onlinePendingContributionId;
326 }
327 else {
328 $contributionId = CRM_Core_DAO::singleValueQuery("
329 SELECT contribution_id
330 FROM civicrm_membership_payment
331 WHERE membership_id = $this->_id
332 ORDER BY contribution_id
333 DESC limit 1");
334
335 if ($contributionId) {
336 $defaults['record_contribution'] = $contributionId;
337 }
338 }
339 }
133e2c99 340
374a4dd6 341 //set Soft Credit Type to Gift by default
342 $scTypes = CRM_Core_OptionGroup::values("soft_credit_type");
343 $defaults['soft_credit_type_id'] = CRM_Utils_Array::value(ts('Gift'), array_flip($scTypes));
344
a7488080 345 if (!empty($defaults['record_contribution']) && !$this->_mode) {
be2fb01f
CW
346 $contributionParams = ['id' => $defaults['record_contribution']];
347 $contributionIds = [];
6a488035
TO
348
349 //keep main object campaign in hand.
350 $memberCampaignId = CRM_Utils_Array::value('campaign_id', $defaults);
351
352 CRM_Contribute_BAO_Contribution::getValues($contributionParams, $defaults, $contributionIds);
353
354 //get back original object campaign id.
355 $defaults['campaign_id'] = $memberCampaignId;
356
6a488035 357 // Contribution::getValues() over-writes the membership record's source field value - so we need to restore it.
a7488080 358 if (!empty($defaults['membership_source'])) {
6a488035
TO
359 $defaults['source'] = $defaults['membership_source'];
360 }
361 }
d96cf288 362 //CRM-13420
a7488080 363 if (empty($defaults['payment_instrument_id'])) {
d96cf288
DG
364 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
365 }
6a488035
TO
366
367 // User must explicitly choose to send a receipt in both add and update mode.
368 $defaults['send_receipt'] = 0;
369
370 if ($this->_action & CRM_Core_Action::UPDATE) {
371 // in this mode by default uncheck this checkbox
372 unset($defaults['record_contribution']);
373 }
374
2e8b13d1 375 $subscriptionCancelled = FALSE;
a7488080 376 if (!empty($defaults['id'])) {
6a488035
TO
377 $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled($this->_id);
378 }
379
380 $alreadyAutoRenew = FALSE;
a7488080 381 if (!empty($defaults['contribution_recur_id']) && !$subscriptionCancelled) {
6a488035
TO
382 $defaults['auto_renew'] = 1;
383 $alreadyAutoRenew = TRUE;
384 }
385 $this->assign('alreadyAutoRenew', $alreadyAutoRenew);
386
387 $this->assign('member_is_test', CRM_Utils_Array::value('member_is_test', $defaults));
388
389 $this->assign('membership_status_id', CRM_Utils_Array::value('status_id', $defaults));
390
a7488080 391 if (!empty($defaults['is_pay_later'])) {
6a488035
TO
392 $this->assign('is_pay_later', TRUE);
393 }
394 if ($this->_mode) {
3b8e6c3f 395 $defaults = $this->getBillingDefaults($defaults);
5e56c7a5 396 // hack to simplify credit card entry for testing
397 // $defaults['credit_card_type'] = 'Visa';
398 // $defaults['credit_card_number'] = '4807731747657838';
399 // $defaults['cvv2'] = '000';
400 // $defaults['credit_card_exp_date'] = array( 'Y' => '2012', 'M' => '05' );
6a488035
TO
401 }
402
6a488035 403 //setting default join date if there is no join date
a7488080 404 if (empty($defaults['join_date'])) {
b5876030 405 $defaults['join_date'] = date('Y-m-d');
6a488035
TO
406 }
407
a7488080 408 if (!empty($defaults['membership_end_date'])) {
6a488035
TO
409 $this->assign('endDate', $defaults['membership_end_date']);
410 }
411
412 return $defaults;
413 }
414
415 /**
fe482240 416 * Build the form object.
6a488035
TO
417 */
418 public function buildQuickForm() {
6a488035 419
59c798c9 420 $this->buildQuickEntityForm();
a5dfa653 421 $this->assign('currency', CRM_Core_BAO_Country::defaultCurrencySymbol());
53d0a906 422 $isUpdateToExistingRecurringMembership = $this->isUpdateToExistingRecurringMembership();
6a488035
TO
423 // build price set form.
424 $buildPriceSet = FALSE;
8cc574cf 425 if ($this->_priceSetId || !empty($_POST['price_set_id'])) {
a7488080 426 if (!empty($_POST['price_set_id'])) {
6a488035
TO
427 $buildPriceSet = TRUE;
428 }
429 $getOnlyPriceSetElements = TRUE;
430 if (!$this->_priceSetId) {
431 $this->_priceSetId = $_POST['price_set_id'];
432 $getOnlyPriceSetElements = FALSE;
433 }
434
435 $this->set('priceSetId', $this->_priceSetId);
9da8dc8c 436 CRM_Price_BAO_PriceSet::buildPriceSet($this);
6a488035 437
be2fb01f 438 $optionsMembershipTypes = [];
6a488035
TO
439 foreach ($this->_priceSet['fields'] as $pField) {
440 if (empty($pField['options'])) {
441 continue;
442 }
443 foreach ($pField['options'] as $opId => $opValues) {
444 $optionsMembershipTypes[$opId] = CRM_Utils_Array::value('membership_type_id', $opValues, 0);
445 }
446 }
447
9da8dc8c 448 $this->assign('autoRenewOption', CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId));
6a488035
TO
449
450 $this->assign('optionsMembershipTypes', $optionsMembershipTypes);
5a9c4d4a 451 $this->assign('contributionType', CRM_Utils_Array::value('financial_type_id', $this->_priceSet));
6a488035
TO
452
453 // get only price set form elements.
454 if ($getOnlyPriceSetElements) {
455 return;
456 }
457 }
458
459 // use to build form during form rule.
460 $this->assign('buildPriceSet', $buildPriceSet);
461
462 if ($this->_action & CRM_Core_Action::ADD) {
463 $buildPriceSet = FALSE;
9da8dc8c 464 $priceSets = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviMember');
6a488035
TO
465 if (!empty($priceSets)) {
466 $buildPriceSet = TRUE;
467 }
468
469 if ($buildPriceSet) {
470 $this->add('select', 'price_set_id', ts('Choose price set'),
be2fb01f 471 [
21dfd5f5 472 '' => ts('Choose price set'),
be2fb01f
CW
473 ] + $priceSets,
474 NULL, ['onchange' => "buildAmount( this.value );"]
6a488035
TO
475 );
476 }
477 $this->assign('hasPriceSets', $buildPriceSet);
478 }
479
6a488035 480 if ($this->_action & CRM_Core_Action::DELETE) {
be2fb01f
CW
481 $this->addButtons([
482 [
c5c263ca
AH
483 'type' => 'next',
484 'name' => ts('Delete'),
485 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
486 'isDefault' => TRUE,
be2fb01f
CW
487 ],
488 [
c5c263ca
AH
489 'type' => 'cancel',
490 'name' => ts('Cancel'),
be2fb01f
CW
491 ],
492 ]);
6a488035
TO
493 return;
494 }
495
496 if ($this->_context == 'standalone') {
be2fb01f 497 $this->addEntityRef('contact_id', ts('Contact'), [
c5c263ca 498 'create' => TRUE,
be2fb01f
CW
499 'api' => ['extra' => ['email']],
500 ], TRUE);
6a488035
TO
501 }
502
503 $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -');
504
c60d2e2c 505 // Throw status bounce when no Membership type or priceset is present
7284a1a4
PN
506 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()
507 && empty($this->allMembershipTypeDetails) && empty($priceSets)
508 ) {
c60d2e2c
PN
509 CRM_Core_Error::statusBounce(ts('You do not have all the permissions needed for this page.'));
510 }
6a488035 511 // retrieve all memberships
be2fb01f 512 $allMembershipInfo = [];
ab30e033
EM
513 foreach ($this->allMembershipTypeDetails as $key => $values) {
514 if ($this->_mode && empty($values['minimum_fee'])) {
515 continue;
516 }
517 else {
518 $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values);
519 if (empty($selMemTypeOrg[$memberOfContactId])) {
520 $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
521 $memberOfContactId,
522 'display_name',
523 'id'
524 );
6a488035 525
ab30e033 526 $selOrgMemType[$memberOfContactId][0] = ts('- select -');
6a488035 527 }
ab30e033
EM
528 if (empty($selOrgMemType[$memberOfContactId][$key])) {
529 $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values);
6a488035 530 }
6a488035 531 }
decbd3d0 532 $totalAmount = CRM_Utils_Array::value('minimum_fee', $values);
533 //CRM-18827 - override the default value if total_amount is submitted
534 if (!empty($this->_submitValues['total_amount'])) {
535 $totalAmount = $this->_submitValues['total_amount'];
536 }
ab30e033
EM
537 // build membership info array, which is used when membership type is selected to:
538 // - set the payment information block
539 // - set the max related block
be2fb01f 540 $allMembershipInfo[$key] = [
ab30e033 541 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values),
decbd3d0 542 'total_amount' => CRM_Utils_Money::format($totalAmount, NULL, '%a'),
543 'total_amount_numeric' => $totalAmount,
ab30e033
EM
544 'auto_renew' => CRM_Utils_Array::value('auto_renew', $values),
545 'has_related' => isset($values['relationship_type_id']),
546 'max_related' => CRM_Utils_Array::value('max_related', $values),
be2fb01f 547 ];
6a488035
TO
548 }
549
550 $this->assign('allMembershipInfo', json_encode($allMembershipInfo));
551
552 // show organization by default, if only one organization in
553 // the list
554 if (count($selMemTypeOrg) == 2) {
555 unset($selMemTypeOrg[0], $selOrgMemType[0][0]);
556 }
557 //sort membership organization and type, CRM-6099
558 natcasesort($selMemTypeOrg);
559 foreach ($selOrgMemType as $index => $orgMembershipType) {
560 natcasesort($orgMembershipType);
561 $selOrgMemType[$index] = $orgMembershipType;
562 }
563
be2fb01f 564 $memTypeJs = [
ab30e033 565 'onChange' => "buildMaxRelated(this.value,true); CRM.buildCustomData('Membership', this.value);",
be2fb01f 566 ];
8deb20a3 567
ab30e033 568 if (!empty($this->_recurPaymentProcessors)) {
51a6bf4f 569 $memTypeJs['onChange'] = "" . $memTypeJs['onChange'] . " buildAutoRenew(this.value, null, '{$this->_mode}');";
ab30e033 570 }
6a488035 571
6a488035
TO
572 $this->add('text', 'max_related', ts('Max related'),
573 CRM_Core_DAO::getAttribute('CRM_Member_DAO_Membership', 'max_related')
574 );
575
353ffa53 576 $sel = &$this->addElement('hierselect',
6a488035
TO
577 'membership_type_id',
578 ts('Membership Organization and Type'),
579 $memTypeJs
580 );
581
be2fb01f 582 $sel->setOptions([$selMemTypeOrg, $selOrgMemType]);
53d0a906 583 if ($isUpdateToExistingRecurringMembership) {
584 $sel->freeze();
6a488035
TO
585 }
586
6a488035 587 if ($this->_action & CRM_Core_Action::ADD) {
be2fb01f 588 $this->add('number', 'num_terms', ts('Number of Terms'), ['size' => 6]);
6a488035
TO
589 }
590
6a488035
TO
591 $this->add('text', 'source', ts('Source'),
592 CRM_Core_DAO::getAttribute('CRM_Member_DAO_Membership', 'source')
593 );
594
595 //CRM-7362 --add campaigns.
596 $campaignId = NULL;
597 if ($this->_id) {
598 $campaignId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'campaign_id');
599 }
600 CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId);
601
602 if (!$this->_mode) {
603 $this->add('select', 'status_id', ts('Membership Status'),
be2fb01f 604 ['' => ts('- select -')] + CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label')
6a488035 605 );
e136f704
O
606
607 $statusOverride = $this->addElement('select', 'is_override', ts('Status Override?'),
608 CRM_Member_StatusOverrideTypes::getSelectOptions()
6a488035 609 );
53d0a906 610 if ($statusOverride && $isUpdateToExistingRecurringMembership) {
611 $statusOverride->freeze();
6a488035
TO
612 }
613
be2fb01f 614 $this->add('datepicker', 'status_override_end_date', ts('Status Override End Date'), '', FALSE, ['minDate' => time(), 'time' => FALSE]);
e136f704 615
6a488035
TO
616 $this->addElement('checkbox', 'record_contribution', ts('Record Membership Payment?'));
617
6a488035
TO
618 $this->add('text', 'total_amount', ts('Amount'));
619 $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');
620
9eb6085d 621 $this->add('datepicker', 'receive_date', ts('Received'), [], FALSE, ['time' => TRUE]);
6a488035
TO
622
623 $this->add('select', 'payment_instrument_id',
4db803dd 624 ts('Payment Method'),
be2fb01f
CW
625 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::paymentInstrument(),
626 FALSE, ['onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);"]
6a488035
TO
627 );
628 $this->add('text', 'trxn_id', ts('Transaction ID'));
629 $this->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
be2fb01f 630 'objectExists', [
5e56c7a5 631 'CRM_Contribute_DAO_Contribution',
632 $this->_id,
633 'trxn_id',
be2fb01f 634 ]
6a488035
TO
635 );
636
6a488035 637 $this->add('select', 'contribution_status_id',
7fe7b63d 638 ts('Payment Status'), CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses('membership')
6a488035
TO
639 );
640 $this->add('text', 'check_number', ts('Check Number'),
641 CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
642 );
643 }
644 else {
645 //add field for amount to allow an amount to be entered that differs from minimum
646 $this->add('text', 'total_amount', ts('Amount'));
647 }
5a9c4d4a
PN
648 $this->add('select', 'financial_type_id',
649 ts('Financial Type'),
be2fb01f 650 ['' => ts('- select -')] + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $this->_action)
5a9c4d4a 651 );
d80dbc14 652
d80dbc14 653 $this->addElement('checkbox', 'is_different_contribution_contact', ts('Record Payment from a Different Contact?'));
186a737c 654
be2fb01f
CW
655 $this->addSelect('soft_credit_type_id', ['entity' => 'contribution_soft']);
656 $this->addEntityRef('soft_credit_contact_id', ts('Payment From'), ['create' => TRUE]);
d80dbc14 657
6a488035
TO
658 $this->addElement('checkbox',
659 'send_receipt',
660 ts('Send Confirmation and Receipt?'), NULL,
be2fb01f 661 ['onclick' => "showEmailOptions()"]
6a488035
TO
662 );
663
664 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails);
665
186a737c 666 $this->add('textarea', 'receipt_text', ts('Receipt Message'));
6a488035
TO
667
668 // Retrieve the name and email of the contact - this will be the TO for receipt email
669 if ($this->_contactID) {
670 list($this->_memberDisplayName,
671 $this->_memberEmail
b11c92be 672 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
6a488035
TO
673
674 $this->assign('emailExists', $this->_memberEmail);
675 $this->assign('displayName', $this->_memberDisplayName);
676 }
677
50b85bf9 678 if ($isUpdateToExistingRecurringMembership && CRM_Member_BAO_Membership::isCancelSubscriptionSupported($this->_id)) {
679 $this->assign('cancelAutoRenew',
680 CRM_Utils_System::url('civicrm/contribute/unsubscribe', "reset=1&mid={$this->_id}")
681 );
682 }
53d0a906 683
50b85bf9 684 $this->assign('isRecur', $isUpdateToExistingRecurringMembership);
6a488035 685
be2fb01f 686 $this->addFormRule(['CRM_Member_Form_Membership', 'formRule'], $this);
8c80f3f9 687 $mailingInfo = Civi::settings()->get('mailing_backend');
688 $this->assign('isEmailEnabledForSite', ($mailingInfo['outBound_option'] != 2));
6a488035
TO
689
690 parent::buildQuickForm();
691 }
692
693 /**
fe482240 694 * Validation.
6a488035 695 *
b2363ea8
TO
696 * @param array $params
697 * (ref.) an assoc array of name/value pairs.
6a488035 698 *
5e56c7a5 699 * @param array $files
700 * @param CRM_Member_Form_Membership $self
2a6da8d7
EM
701 *
702 * @throws CiviCRM_API3_Exception
72b3a70c
CW
703 * @return bool|array
704 * mixed true or array of errors
6a488035 705 */
00be9182 706 public static function formRule($params, $files, $self) {
be2fb01f 707 $errors = [];
6a488035 708
ccb02c2d 709 $priceSetId = self::getPriceSetID($params);
710 $priceSetDetails = self::getPriceSetDetails($params);
6a488035 711
e4a6290d 712 $selectedMemberships = self::getSelectedMemberships($priceSetDetails[$priceSetId], $params);
ccb02c2d 713
714 if (!empty($params['price_set_id'])) {
9da8dc8c 715 CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $params, $errors);
6a488035 716
15724d0a 717 $priceFieldIDS = self::getPriceFieldIDs($params, $priceSetDetails[$priceSetId]);
6a488035
TO
718
719 if (!empty($priceFieldIDS)) {
720 $ids = implode(',', $priceFieldIDS);
721
9da8dc8c 722 $count = CRM_Price_BAO_PriceSet::getMembershipCount($ids);
2e8b13d1 723 foreach ($count as $occurrence) {
b44e3f84 724 if ($occurrence > 1) {
6a488035
TO
725 $errors['_qf_default'] = ts('Select at most one option associated with the same membership type.');
726 }
727 }
6a488035 728 }
867047cd 729 // Return error if empty $self->_memTypeSelected
730 if (empty($errors) && empty($selectedMemberships)) {
731 $errors['_qf_default'] = ts('Select at least one membership option.');
732 }
733 if (!$self->_mode && empty($params['record_contribution'])) {
734 $errors['record_contribution'] = ts('Record Membership Payment is required when you use a price set.');
735 }
6a488035 736 }
867047cd 737 else {
738 if (empty($params['membership_type_id'][1])) {
739 $errors['membership_type_id'] = ts('Please select a membership type.');
740 }
6a488035
TO
741 $numterms = CRM_Utils_Array::value('num_terms', $params);
742 if ($numterms && intval($numterms) != $numterms) {
743 $errors['num_terms'] = ts('Please enter an integer for the number of terms.');
744 }
6a488035 745
356b5786 746 if (($self->_mode || isset($params['record_contribution'])) && empty($params['financial_type_id'])) {
867047cd 747 $errors['financial_type_id'] = ts('Please enter the financial Type.');
748 }
6a488035
TO
749 }
750
18aa3e1e
EM
751 if (!empty($errors) && (count($selectedMemberships) > 1)) {
752 $memberOfContacts = CRM_Member_BAO_MembershipType::getMemberOfContactByMemTypes($selectedMemberships);
6a488035
TO
753 $duplicateMemberOfContacts = array_count_values($memberOfContacts);
754 foreach ($duplicateMemberOfContacts as $countDuplicate) {
755 if ($countDuplicate > 1) {
756 $errors['_qf_default'] = ts('Please do not select more than one membership associated with the same organization.');
757 }
758 }
759 }
760
6a488035
TO
761 if (!empty($errors)) {
762 return $errors;
763 }
764
8cc574cf 765 if (!empty($params['record_contribution']) && empty($params['payment_instrument_id'])) {
4db803dd 766 $errors['payment_instrument_id'] = ts('Payment Method is a required field.');
d96cf288 767 }
6a488035 768
a7488080
CW
769 if (!empty($params['is_different_contribution_contact'])) {
770 if (empty($params['soft_credit_type_id'])) {
133e2c99 771 $errors['soft_credit_type_id'] = ts('Please Select a Soft Credit Type');
772 }
d80dbc14 773 if (empty($params['soft_credit_contact_id'])) {
774 $errors['soft_credit_contact_id'] = ts('Please select a contact');
133e2c99 775 }
776 }
777
a7488080 778 if (!empty($params['payment_processor_id'])) {
a479fe60 779 // validate payment instrument (e.g. credit card number)
f48e6cf7 780 CRM_Core_Payment_Form::validatePaymentInstrument($params['payment_processor_id'], $params, $errors, NULL);
6a488035
TO
781 }
782
783 $joinDate = NULL;
a7488080 784 if (!empty($params['join_date'])) {
6a488035
TO
785
786 $joinDate = CRM_Utils_Date::processDate($params['join_date']);
787
18aa3e1e 788 foreach ($selectedMemberships as $memType) {
6a488035 789 $startDate = NULL;
a7488080 790 if (!empty($params['start_date'])) {
6a488035
TO
791 $startDate = CRM_Utils_Date::processDate($params['start_date']);
792 }
793
794 // if end date is set, ensure that start date is also set
795 // and that end date is later than start date
6a488035 796 $endDate = NULL;
a7488080 797 if (!empty($params['end_date'])) {
6a488035
TO
798 $endDate = CRM_Utils_Date::processDate($params['end_date']);
799 }
800
801 $membershipDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($memType);
802
803 if ($startDate && CRM_Utils_Array::value('period_type', $membershipDetails) == 'rolling') {
804 if ($startDate < $joinDate) {
805 $errors['start_date'] = ts('Start date must be the same or later than Member since.');
806 }
807 }
808
809 if ($endDate) {
810 if ($membershipDetails['duration_unit'] == 'lifetime') {
f476dde8 811 // Check if status is NOT cancelled or similar. For lifetime memberships, there is no automated
ae3d69ec 812 // process to update status based on end-date. The user must change the status now.
be2fb01f 813 $result = civicrm_api3('MembershipStatus', 'get', [
ae3d69ec
SG
814 'sequential' => 1,
815 'is_current_member' => 0,
be2fb01f 816 ]);
f476dde8 817 $tmp_statuses = $result['values'];
be2fb01f 818 $status_ids = [];
22e263ad 819 foreach ($tmp_statuses as $cur_stat) {
8efea814 820 $status_ids[] = $cur_stat['id'];
f476dde8 821 }
e136f704 822
481a74f4 823 if (empty($params['status_id']) || in_array($params['status_id'], $status_ids) == FALSE) {
f476dde8 824 $errors['status_id'] = ts('Please enter a status that does NOT represent a current membership status.');
e136f704
O
825 }
826
827 if (!empty($params['is_override']) && !CRM_Member_StatusOverrideTypes::isPermanent($params['is_override'])) {
828 $errors['is_override'] = ts('Because you set an End Date for a lifetime membership, This must be set to "Override Permanently"');
f476dde8 829 }
6a488035
TO
830 }
831 else {
832 if (!$startDate) {
833 $errors['start_date'] = ts('Start date must be set if end date is set.');
834 }
835 if ($endDate < $startDate) {
836 $errors['end_date'] = ts('End date must be the same or later than start date.');
837 }
838 }
839 }
840
5e56c7a5 841 // Default values for start and end dates if not supplied on the form.
6a488035
TO
842 $defaultDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($memType,
843 $joinDate,
844 $startDate,
845 $endDate
846 );
847
848 if (!$startDate) {
849 $startDate = CRM_Utils_Array::value('start_date',
850 $defaultDates
851 );
852 }
853 if (!$endDate) {
854 $endDate = CRM_Utils_Array::value('end_date',
855 $defaultDates
856 );
857 }
858
859 //CRM-3724, check for availability of valid membership status.
e136f704 860 if ((empty($params['is_override']) || CRM_Member_StatusOverrideTypes::isNo($params['is_override'])) && !isset($errors['_qf_default'])) {
6a488035
TO
861 $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($startDate,
862 $endDate,
863 $joinDate,
864 'today',
5f11bbcc
EM
865 TRUE,
866 $memType,
867 $params
6a488035
TO
868 );
869 if (empty($calcStatus)) {
870 $url = CRM_Utils_System::url('civicrm/admin/member/membershipStatus', 'reset=1&action=browse');
871 $errors['_qf_default'] = ts('There is no valid Membership Status available for selected membership dates.');
be2fb01f 872 $status = ts('Oops, it looks like there is no valid membership status available for the given membership dates. You can <a href="%1">Configure Membership Status Rules</a>.', [1 => $url]);
6a488035 873 if (!$self->_mode) {
e136f704 874 $status .= ' ' . ts('OR You can sign up by setting Status Override? to something other than "NO".');
6a488035
TO
875 }
876 CRM_Core_Session::setStatus($status, ts('Membership Status Error'), 'error');
877 }
878 }
879 }
880 }
881 else {
882 $errors['join_date'] = ts('Please enter the Member Since.');
883 }
884
e136f704
O
885 if (!empty($params['is_override']) && CRM_Member_StatusOverrideTypes::isOverridden($params['is_override']) && empty($params['status_id'])) {
886 $errors['status_id'] = ts('Please enter the Membership status.');
887 }
888
889 if (!empty($params['is_override']) && CRM_Member_StatusOverrideTypes::isUntilDate($params['is_override'])) {
890 if (empty($params['status_override_end_date'])) {
891 $errors['status_override_end_date'] = ts('Please enter the Membership override end date.');
892 }
6a488035
TO
893 }
894
895 //total amount condition arise when membership type having no
896 //minimum fee
897 if (isset($params['record_contribution'])) {
6a488035
TO
898 if (CRM_Utils_System::isNull($params['total_amount'])) {
899 $errors['total_amount'] = ts('Please enter the contribution.');
900 }
901 }
902
903 // validate contribution status for 'Failed'.
8cc574cf 904 if ($self->_onlinePendingContributionId && !empty($params['record_contribution']) &&
6a488035
TO
905 (CRM_Utils_Array::value('contribution_status_id', $params) ==
906 array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'))
907 )
908 ) {
909 $errors['contribution_status_id'] = ts('Please select a valid payment status before updating.');
910 }
911
912 return empty($errors) ? TRUE : $errors;
913 }
914
915 /**
fe482240 916 * Process the form submission.
6a488035
TO
917 */
918 public function postProcess() {
919 if ($this->_action & CRM_Core_Action::DELETE) {
3506b6cd 920 CRM_Member_BAO_Membership::del($this->_id);
6a488035
TO
921 return;
922 }
7865d848
EM
923 // get the submitted form values.
924 $this->_params = $this->controller->exportValues($this->_name);
e9fe9519 925 $this->prepareStatusOverrideValues();
6a488035 926
09108d7d 927 $this->submit();
7865d848
EM
928
929 $this->setUserContext();
930 }
931
e9fe9519
O
932 /**
933 * Prepares the values related to status override.
934 */
935 private function prepareStatusOverrideValues() {
936 $this->setOverrideDateValue();
937 $this->convertIsOverrideValue();
938 }
939
940 /**
941 * Sets status override end date to empty value if
942 * the selected override option is not 'until date'.
943 */
944 private function setOverrideDateValue() {
393f1657 945 if (!CRM_Member_StatusOverrideTypes::isUntilDate(CRM_Utils_Array::value('is_override', $this->_params))) {
e9fe9519
O
946 $this->_params['status_override_end_date'] = '';
947 }
948 }
949
e136f704
O
950 /**
951 * Convert the value of selected (status override?)
952 * option to TRUE if it indicate an overridden status
953 * or FALSE otherwise.
954 */
955 private function convertIsOverrideValue() {
956 $this->_params['is_override'] = CRM_Member_StatusOverrideTypes::isOverridden($this->_params['is_override']);
957 }
958
7865d848
EM
959 /**
960 * Send email receipt.
961 *
829df55e 962 * @deprecated
963 * This function is shared with Batch_Entry which has limited overlap
964 * & needs rationalising.
965 *
7865d848
EM
966 * @param CRM_Core_Form $form
967 * Form object.
968 * @param array $formValues
969 * @param object $membership
970 * Object.
efc0e24a 971 * @param array $customValues
7865d848
EM
972 *
973 * @return bool
974 * true if mail was sent successfully
975 */
efc0e24a 976 public static function emailReceipt(&$form, &$formValues, &$membership, $customValues = NULL) {
7865d848 977 // retrieve 'from email id' for acknowledgement
beac1417 978 $receiptFrom = CRM_Utils_Array::value('from_email_address', $formValues);
7865d848 979
efc0e24a 980 // @todo figure out how much of the stuff below is genuinely shared with the batch form & a logical shared place.
7865d848
EM
981 if (!empty($formValues['payment_instrument_id'])) {
982 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
983 $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']];
984 }
985
0816949d 986 $form->assign('customValues', $customValues);
7865d848
EM
987
988 if ($form->_mode) {
efc0e24a 989 // @todo move this outside shared code as Batch entry just doesn't
5103bd18 990 $form->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters(
0b50eca0 991 $form->_params,
992 $form->_bltID
993 ));
7d193e45 994
7865d848
EM
995 $date = CRM_Utils_Date::format($form->_params['credit_card_exp_date']);
996 $date = CRM_Utils_Date::mysqlToIso($date);
997 $form->assign('credit_card_exp_date', $date);
998 $form->assign('credit_card_number',
999 CRM_Utils_System::mungeCreditCard($form->_params['credit_card_number'])
1000 );
1001 $form->assign('credit_card_type', $form->_params['credit_card_type']);
1002 $form->assign('contributeMode', 'direct');
1003 $form->assign('isAmountzero', 0);
1004 $form->assign('is_pay_later', 0);
1005 $form->assign('isPrimary', 1);
1006 }
1007
1008 $form->assign('module', 'Membership');
1009 $form->assign('contactID', $formValues['contact_id']);
1010
1011 $form->assign('membershipID', CRM_Utils_Array::value('membership_id', $form->_params, CRM_Utils_Array::value('membership_id', $form->_defaultValues)));
1012
1013 if (!empty($formValues['contribution_id'])) {
1014 $form->assign('contributionID', $formValues['contribution_id']);
1015 }
1016 elseif (isset($form->_onlinePendingContributionId)) {
1017 $form->assign('contributionID', $form->_onlinePendingContributionId);
1018 }
1019
1020 if (!empty($formValues['contribution_status_id'])) {
1021 $form->assign('contributionStatusID', $formValues['contribution_status_id']);
1022 $form->assign('contributionStatus', CRM_Contribute_PseudoConstant::contributionStatus($formValues['contribution_status_id'], 'name'));
1023 }
1024
1025 if (!empty($formValues['is_renew'])) {
1026 $form->assign('receiptType', 'membership renewal');
1027 }
1028 else {
1029 $form->assign('receiptType', 'membership signup');
1030 }
9eb6085d 1031 $form->assign('receive_date', CRM_Utils_Array::value('receive_date', $formValues));
7865d848
EM
1032 $form->assign('formValues', $formValues);
1033
1034 if (empty($lineItem)) {
1035 $form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y'));
1036 if (!CRM_Utils_System::isNull($membership->end_date)) {
1037 $form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y'));
1038 }
1039 $form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership->membership_type_id));
1040 }
1041
efc0e24a 1042 // @todo - if we have to figure out if this is for batch processing it doesn't belong in the shared function.
7865d848
EM
1043 $isBatchProcess = is_a($form, 'CRM_Batch_Form_Entry');
1044 if ((empty($form->_contributorDisplayName) || empty($form->_contributorEmail)) || $isBatchProcess) {
1045 // in this case the form is being called statically from the batch editing screen
1046 // having one class in the form layer call another statically is not greate
1047 // & we should aim to move this function to the BAO layer in future.
1048 // however, we can assume that the contact_id passed in by the batch
1049 // function will be the recipient
1050 list($form->_contributorDisplayName, $form->_contributorEmail)
1051 = CRM_Contact_BAO_Contact_Location::getEmailDetails($formValues['contact_id']);
1052 if (empty($form->_receiptContactId) || $isBatchProcess) {
1053 $form->_receiptContactId = $formValues['contact_id'];
1054 }
1055 }
efc0e24a 1056 // @todo determine isEmailPdf in calling function.
7865d848
EM
1057 $template = CRM_Core_Smarty::singleton();
1058 $taxAmt = $template->get_template_vars('dataArray');
1059 $eventTaxAmt = $template->get_template_vars('totalTaxAmount');
aaffa79f 1060 $prefixValue = Civi::settings()->get('contribution_invoice_settings');
7865d848
EM
1061 $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue);
1062 if ((!empty($taxAmt) || isset($eventTaxAmt)) && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) {
1063 $isEmailPdf = TRUE;
1064 }
1065 else {
1066 $isEmailPdf = FALSE;
1067 }
1068
1069 list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
be2fb01f 1070 [
7865d848
EM
1071 'groupName' => 'msg_tpl_workflow_membership',
1072 'valueName' => 'membership_offline_receipt',
1073 'contactId' => $form->_receiptContactId,
1074 'from' => $receiptFrom,
1075 'toName' => $form->_contributorDisplayName,
1076 'toEmail' => $form->_contributorEmail,
1077 'PDFFilename' => ts('receipt') . '.pdf',
1078 'isEmailPdf' => $isEmailPdf,
1079 'contributionId' => $formValues['contribution_id'],
1080 'isTest' => (bool) ($form->_action & CRM_Core_Action::PREVIEW),
be2fb01f 1081 ]
7865d848
EM
1082 );
1083
1084 return TRUE;
1085 }
1086
1087 /**
5e56c7a5 1088 * Submit function.
1089 *
1090 * This is also accessed by unit tests.
7865d848 1091 */
09108d7d 1092 public function submit() {
4bd318e0 1093 $isTest = ($this->_mode == 'test') ? 1 : 0;
09108d7d 1094 $this->storeContactFields($this->_params);
ba2f3f65 1095 $this->beginPostProcess();
7865d848 1096 $joinDate = $startDate = $endDate = NULL;
be2fb01f 1097 $membershipTypes = $membership = $calcDate = [];
41709813 1098 $membershipType = NULL;
18135422 1099 $paymentInstrumentID = $this->_paymentProcessor['object']->getPaymentInstrumentID();
be2fb01f 1100 $params = $softParams = $ids = [];
7865d848 1101
64412b4e 1102 $mailSend = FALSE;
09108d7d 1103 $this->processBillingAddress();
64412b4e
MWMC
1104 $formValues = $this->_params;
1105 $formValues = $this->setPriceSetParameters($formValues);
7865d848 1106
08fd4b45
EM
1107 if ($this->_id) {
1108 $ids['membership'] = $params['id'] = $this->_id;
1109 }
1110 $ids['userId'] = CRM_Core_Session::singleton()->get('userID');
1111
7865d848
EM
1112 // Set variables that we normally get from context.
1113 // In form mode these are set in preProcess.
1114 //TODO: set memberships, fixme
1115 $this->setContextVariables($formValues);
ccb02c2d 1116
e4a6290d 1117 $this->_memTypeSelected = self::getSelectedMemberships(
ccb02c2d 1118 $this->_priceSet,
e4a6290d 1119 $formValues
1120 );
867047cd 1121 if (empty($formValues['financial_type_id'])) {
ccb02c2d 1122 $formValues['financial_type_id'] = $this->_priceSet['financial_type_id'];
867047cd 1123 }
7865d848 1124
6a488035 1125 $config = CRM_Core_Config::singleton();
6a488035 1126
be2fb01f 1127 $membershipTypeValues = [];
6a488035
TO
1128 foreach ($this->_memTypeSelected as $memType) {
1129 $membershipTypeValues[$memType]['membership_type_id'] = $memType;
1130 }
1131
1132 //take the required membership recur values.
7865d848 1133 if ($this->_mode && !empty($formValues['auto_renew'])) {
ccb02c2d 1134 $params['is_recur'] = $formValues['is_recur'] = TRUE;
6a488035
TO
1135
1136 $count = 0;
1137 foreach ($this->_memTypeSelected as $memType) {
1138 $recurMembershipTypeValues = CRM_Utils_Array::value($memType,
ec7af55f 1139 $this->allMembershipTypeDetails, []
6a488035 1140 );
ec7af55f 1141 if (!$recurMembershipTypeValues['auto_renew']) {
1142 continue;
1143 }
1144 foreach ([
1145 'frequency_interval' => 'duration_interval',
1146 'frequency_unit' => 'duration_unit',
1147 ] as $mapVal => $mapParam) {
1148 $membershipTypeValues[$memType][$mapVal] = $recurMembershipTypeValues[$mapParam];
1149
6a488035 1150 if (!$count) {
ccb02c2d 1151 $formValues[$mapVal] = CRM_Utils_Array::value($mapParam,
6a488035
TO
1152 $recurMembershipTypeValues
1153 );
1154 }
1155 }
1156 $count++;
1157 }
6a488035
TO
1158 }
1159
ccb02c2d 1160 $isQuickConfig = $this->_priceSet['is_quick_config'];
1161
be2fb01f 1162 $termsByType = [];
9f1bc5dc 1163
be2fb01f 1164 $lineItem = [$this->_priceSetId => []];
ccb02c2d 1165
b764bfd0 1166 // BEGIN Fix for dev/core/issues/860
63b45b1d
DJ
1167 // Prepare fee block and call buildAmount hook - based on CRM_Price_BAO_PriceSet::buildPriceSet().
1168 CRM_Price_BAO_PriceSet::applyACLFinancialTypeStatusToFeeBlock($this->_priceSet['fields']);
1169 CRM_Utils_Hook::buildAmount('membership', $this, $this->_priceSet['fields']);
b764bfd0
DJ
1170 // END Fix for dev/core/issues/860
1171
08fd4b45 1172 CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'],
1a1a2f4f 1173 $formValues, $lineItem[$this->_priceSetId], NULL, $this->_priceSetId);
ccb02c2d 1174
de6c59ca 1175 if (!empty($formValues['tax_amount'])) {
ccb02c2d 1176 $params['tax_amount'] = $formValues['tax_amount'];
08fd4b45 1177 }
ccb02c2d 1178 $params['total_amount'] = CRM_Utils_Array::value('amount', $formValues);
ccb02c2d 1179 if (!empty($lineItem[$this->_priceSetId])) {
1180 foreach ($lineItem[$this->_priceSetId] as &$li) {
08fd4b45
EM
1181 if (!empty($li['membership_type_id'])) {
1182 if (!empty($li['membership_num_terms'])) {
1183 $termsByType[$li['membership_type_id']] = $li['membership_num_terms'];
6a488035 1184 }
08fd4b45 1185 }
6a488035 1186
08fd4b45
EM
1187 ///CRM-11529 for quick config backoffice transactions
1188 //when financial_type_id is passed in form, update the
1189 //lineitems with the financial type selected in form
c26225d2 1190 $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $formValues);
08fd4b45
EM
1191 if ($isQuickConfig && $submittedFinancialType) {
1192 $li['financial_type_id'] = $submittedFinancialType;
6a488035
TO
1193 }
1194 }
1195 }
1196
6a488035
TO
1197 $params['contact_id'] = $this->_contactID;
1198
be2fb01f 1199 $fields = [
6a488035
TO
1200 'status_id',
1201 'source',
1202 'is_override',
e136f704 1203 'status_override_end_date',
6a488035 1204 'campaign_id',
be2fb01f 1205 ];
6a488035
TO
1206
1207 foreach ($fields as $f) {
1208 $params[$f] = CRM_Utils_Array::value($f, $formValues);
1209 }
1210
1211 // fix for CRM-3724
1212 // when is_override false ignore is_admin statuses during membership
1213 // status calculation. similarly we did fix for import in CRM-3570.
a7488080 1214 if (empty($params['is_override'])) {
6a488035
TO
1215 $params['exclude_is_admin'] = TRUE;
1216 }
1217
1218 // process date params to mysql date format.
be2fb01f 1219 $dateTypes = [
6a488035
TO
1220 'join_date' => 'joinDate',
1221 'start_date' => 'startDate',
1222 'end_date' => 'endDate',
be2fb01f 1223 ];
6a488035
TO
1224 foreach ($dateTypes as $dateField => $dateVariable) {
1225 $$dateVariable = CRM_Utils_Date::processDate($formValues[$dateField]);
1226 }
1227
b09fe5ed 1228 $memTypeNumTerms = empty($termsByType) ? CRM_Utils_Array::value('num_terms', $formValues) : NULL;
6a488035 1229
be2fb01f 1230 $calcDates = [];
6a488035 1231 foreach ($this->_memTypeSelected as $memType) {
1693f081 1232 if (empty($memTypeNumTerms)) {
1233 $memTypeNumTerms = CRM_Utils_Array::value($memType, $termsByType, 1);
1234 }
6a488035
TO
1235 $calcDates[$memType] = CRM_Member_BAO_MembershipType::getDatesForMembershipType($memType,
1236 $joinDate, $startDate, $endDate, $memTypeNumTerms
1237 );
1238 }
1239
1240 foreach ($calcDates as $memType => $calcDate) {
5d86176b 1241 foreach (array_keys($dateTypes) as $d) {
6a488035
TO
1242 //first give priority to form values then calDates.
1243 $date = CRM_Utils_Array::value($d, $formValues);
1244 if (!$date) {
1245 $date = CRM_Utils_Array::value($d, $calcDate);
1246 }
1247
1248 $membershipTypeValues[$memType][$d] = CRM_Utils_Date::processDate($date);
6a488035
TO
1249 }
1250 }
1251
6a488035
TO
1252 foreach ($this->_memTypeSelected as $memType) {
1253 if (array_key_exists('max_related', $formValues)) {
c26225d2 1254 // max related memberships - take from form or inherit from membership type
6a488035
TO
1255 $membershipTypeValues[$memType]['max_related'] = CRM_Utils_Array::value('max_related', $formValues);
1256 }
6a488035 1257 $membershipTypeValues[$memType]['custom'] = CRM_Core_BAO_CustomField::postProcess($formValues,
6a488035
TO
1258 $this->_id,
1259 'Membership'
1260 );
6a488035
TO
1261 $membershipTypes[$memType] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
1262 $memType
1263 );
1264 }
1265
1266 $membershipType = implode(', ', $membershipTypes);
1267
1268 // Retrieve the name and email of the current user - this will be the FROM for the receipt email
ab30e033 1269 list($userName) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']);
6a488035 1270
d80dbc14 1271 //CRM-13981, allow different person as a soft-contributor of chosen type
b11c92be 1272 if ($this->_contributorContactID != $this->_contactID) {
91ef9be0 1273 $params['contribution_contact_id'] = $this->_contributorContactID;
ccb02c2d 1274 if (!empty($formValues['soft_credit_type_id'])) {
1275 $softParams['soft_credit_type_id'] = $formValues['soft_credit_type_id'];
91ef9be0 1276 $softParams['contact_id'] = $this->_contactID;
6a488035
TO
1277 }
1278 }
c26225d2
MWMC
1279
1280 $pendingMembershipStatusId = CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Pending');
1281
a7488080 1282 if (!empty($formValues['record_contribution'])) {
be2fb01f 1283 $recordContribution = [
b11c92be 1284 'total_amount',
b11c92be 1285 'financial_type_id',
1286 'payment_instrument_id',
1287 'trxn_id',
1288 'contribution_status_id',
1289 'check_number',
1290 'campaign_id',
1291 'receive_date',
a55e39e9 1292 'card_type_id',
1293 'pan_truncation',
be2fb01f 1294 ];
6a488035
TO
1295
1296 foreach ($recordContribution as $f) {
1297 $params[$f] = CRM_Utils_Array::value($f, $formValues);
1298 }
1299
1300 if (!$this->_onlinePendingContributionId) {
2286d173 1301 if (empty($formValues['source'])) {
be2fb01f 1302 $params['contribution_source'] = ts('%1 Membership: Offline signup (by %2)', [
7865d848
EM
1303 1 => $membershipType,
1304 2 => $userName,
be2fb01f 1305 ]);
2286d173
PD
1306 }
1307 else {
0e81467c 1308 $params['contribution_source'] = $formValues['source'];
2286d173 1309 }
0e81467c 1310 }
6a488035 1311
c26225d2 1312 $completedContributionStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
a7488080 1313 if (empty($params['is_override']) &&
c26225d2 1314 CRM_Utils_Array::value('contribution_status_id', $params) != $completedContributionStatusId
6a488035 1315 ) {
c26225d2 1316 $params['status_id'] = $pendingMembershipStatusId;
6a488035
TO
1317 $params['skipStatusCal'] = TRUE;
1318 $params['is_pay_later'] = 1;
1319 $this->assign('is_pay_later', 1);
1320 }
1321
a7488080 1322 if (!empty($formValues['send_receipt'])) {
5d86176b 1323 $params['receipt_date'] = CRM_Utils_Array::value('receive_date', $formValues);
6a488035
TO
1324 }
1325
1326 //insert financial type name in receipt.
1327 $formValues['contributionType_name'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
1328 $formValues['financial_type_id']
1329 );
1330 }
1331
1332 // process line items, until no previous line items.
1333 if (!empty($lineItem)) {
1334 $params['lineItems'] = $lineItem;
1335 $params['processPriceSet'] = TRUE;
1336 }
be2fb01f 1337 $createdMemberships = [];
6a488035 1338 if ($this->_mode) {
ccb02c2d 1339 $params['total_amount'] = CRM_Utils_Array::value('total_amount', $formValues, 0);
a7f2d5fd 1340
1341 //CRM-20264 : Store CC type and number (last 4 digit) during backoffice or online payment
1342 $params['card_type_id'] = CRM_Utils_Array::value('card_type_id', $this->_params);
1343 $params['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $this->_params);
a8d4ff25 1344
ccb02c2d 1345 if (!$isQuickConfig) {
b11c92be 1346 $params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet',
ccb02c2d 1347 $this->_priceSetId,
6a488035
TO
1348 'financial_type_id'
1349 );
1350 }
1351 else {
5a9c4d4a 1352 $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $formValues);
6a488035
TO
1353 }
1354
ba2f3f65 1355 //get the payment processor id as per mode. Try removing in favour of beginPostProcess.
ccb02c2d 1356 $params['payment_processor_id'] = $formValues['payment_processor_id'] = $this->_paymentProcessor['id'];
09108d7d 1357 $params['register_date'] = date('YmdHis');
6a488035 1358
a1a94e61 1359 // add all the additional payment params we need
ccb02c2d 1360 $formValues['amount'] = $params['total_amount'];
ba2f3f65 1361 // @todo this is a candidate for beginPostProcessFunction.
ccb02c2d 1362 $formValues['currencyID'] = $config->defaultCurrency;
1363 $formValues['description'] = ts("Contribution submitted by a staff person using member's credit card for signup");
1364 $formValues['invoiceID'] = md5(uniqid(rand(), TRUE));
1365 $formValues['financial_type_id'] = $params['financial_type_id'];
6a488035
TO
1366
1367 // at this point we've created a contact and stored its address etc
1368 // all the payment processors expect the name and address to be in the
1369 // so we copy stuff over to first_name etc.
ccb02c2d 1370 $paymentParams = $formValues;
6a488035
TO
1371 $paymentParams['contactID'] = $this->_contributorContactID;
1372 //CRM-10377 if payment is by an alternate contact then we need to set that person
1373 // as the contact in the payment params
b11c92be 1374 if ($this->_contributorContactID != $this->_contactID) {
ccb02c2d 1375 if (!empty($formValues['soft_credit_type_id'])) {
133e2c99 1376 $softParams['contact_id'] = $params['contact_id'];
ccb02c2d 1377 $softParams['soft_credit_type_id'] = $formValues['soft_credit_type_id'];
6a488035
TO
1378 }
1379 }
ccb02c2d 1380 if (!empty($formValues['send_receipt'])) {
6a488035
TO
1381 $paymentParams['email'] = $this->_contributorEmail;
1382 }
1383
ba2f3f65 1384 // This is a candidate for shared beginPostProcess function.
64412b4e 1385 // @todo Do we need this now we have $this->formatParamsForPaymentProcessor() ?
ccb02c2d 1386 CRM_Core_Payment_Form::mapParams($this->_bltID, $formValues, $paymentParams, TRUE);
6a488035 1387 // CRM-7137 -for recurring membership,
b44e3f84 1388 // we do need contribution and recurring records.
6a488035 1389 $result = NULL;
a7488080 1390 if (!empty($paymentParams['is_recur'])) {
8a7b41d1
EM
1391 $financialType = new CRM_Financial_DAO_FinancialType();
1392 $financialType->id = $params['financial_type_id'];
1393 $financialType->find(TRUE);
ccb02c2d 1394 $this->_params = $formValues;
18135422 1395
ba013eea 1396 $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($this,
6a488035 1397 $paymentParams,
3febe800 1398 NULL,
be2fb01f 1399 [
f6261e9d 1400 'contact_id' => $this->_contributorContactID,
9b581f1d 1401 'line_item' => $lineItem,
f6261e9d 1402 'is_test' => $isTest,
1403 'campaign_id' => CRM_Utils_Array::value('campaign_id', $paymentParams),
ccb02c2d 1404 'contribution_page_id' => CRM_Utils_Array::value('contribution_page_id', $formValues),
3febe800 1405 'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)),
1406 'thankyou_date' => CRM_Utils_Array::value('thankyou_date', $paymentParams),
18135422 1407 'payment_instrument_id' => $paymentInstrumentID,
be2fb01f 1408 ],
8a7b41d1 1409 $financialType,
4bd318e0 1410 FALSE,
449f4c90 1411 $this->_bltID,
1412 TRUE
6a488035 1413 );
133e2c99 1414
1415 //create new soft-credit record, CRM-13981
00c1cd97
CW
1416 if ($softParams) {
1417 $softParams['contribution_id'] = $contribution->id;
1418 $softParams['currency'] = $contribution->currency;
1419 $softParams['amount'] = $contribution->total_amount;
1420 CRM_Contribute_BAO_ContributionSoft::add($softParams);
1421 }
133e2c99 1422
a22bd791 1423 $paymentParams['contactID'] = $this->_contactID;
6a488035 1424 $paymentParams['contributionID'] = $contribution->id;
b11c92be 1425 $paymentParams['contributionTypeID'] = $contribution->financial_type_id;
6a488035
TO
1426 $paymentParams['contributionPageID'] = $contribution->contribution_page_id;
1427 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id;
f57cb50c 1428 $params['contribution_id'] = $paymentParams['contributionID'];
6a488035 1429 $params['contribution_recur_id'] = $paymentParams['contributionRecurID'];
6a488035 1430 }
c26225d2 1431 $paymentStatus = NULL;
6a488035
TO
1432
1433 if ($params['total_amount'] > 0.0) {
ab30e033 1434 $payment = $this->_paymentProcessor['object'];
06d062ce
EM
1435 try {
1436 $result = $payment->doPayment($paymentParams);
ccb02c2d 1437 $formValues = array_merge($formValues, $result);
c26225d2 1438 $paymentStatus = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $formValues['payment_status_id']);
ab30e033
EM
1439 // Assign amount to template if payment was successful.
1440 $this->assign('amount', $params['total_amount']);
6a488035 1441 }
31176a73 1442 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
06d062ce 1443 if (!empty($paymentParams['contributionID'])) {
ab30e033
EM
1444 CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'], $this->_contactID,
1445 $e->getMessage());
06d062ce
EM
1446 }
1447 if (!empty($paymentParams['contributionRecurID'])) {
1448 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']);
1449 }
1450
63dd64f2 1451 CRM_Core_Session::singleton()->setStatus($e->getMessage());
06d062ce 1452 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership',
31176a73 1453 "reset=1&action=add&cid={$this->_contactID}&context=membership&mode={$this->_mode}"
06d062ce 1454 ));
6a488035 1455
06d062ce 1456 }
6a488035
TO
1457 }
1458
c26225d2
MWMC
1459 if ($paymentStatus !== 'Completed') {
1460 $params['status_id'] = $pendingMembershipStatusId;
7d193e45
LS
1461 $params['skipStatusCal'] = TRUE;
1462 // unset send-receipt option, since receipt will be sent when ipn is received.
ccb02c2d 1463 unset($formValues['send_receipt'], $formValues['send_receipt']);
7d193e45 1464 //as membership is pending set dates to null.
be2fb01f 1465 $memberDates = [
7d193e45
LS
1466 'join_date' => 'joinDate',
1467 'start_date' => 'startDate',
1468 'end_date' => 'endDate',
be2fb01f 1469 ];
2e8b13d1 1470 foreach ($memberDates as $dv) {
7d193e45
LS
1471 $$dv = NULL;
1472 foreach ($this->_memTypeSelected as $memType) {
1473 $membershipTypeValues[$memType][$dv] = NULL;
1474 }
1475 }
1476 }
09108d7d 1477 $now = date('YmdHis');
553842be 1478 $params['receive_date'] = date('Y-m-d H:i:s');
ccb02c2d 1479 $params['invoice_id'] = $formValues['invoiceID'];
6a488035 1480 $params['contribution_source'] = ts('%1 Membership Signup: Credit card or direct debit (by %2)',
be2fb01f 1481 [1 => $membershipType, 2 => $userName]
6a488035
TO
1482 );
1483 $params['source'] = $formValues['source'] ? $formValues['source'] : $params['contribution_source'];
1484 $params['trxn_id'] = CRM_Utils_Array::value('trxn_id', $result);
6a488035 1485 $params['is_test'] = ($this->_mode == 'live') ? 0 : 1;
ccb02c2d 1486 if (!empty($formValues['send_receipt'])) {
6a488035
TO
1487 $params['receipt_date'] = $now;
1488 }
1489 else {
1490 $params['receipt_date'] = NULL;
1491 }
1492
ccb02c2d 1493 $this->set('params', $formValues);
6a488035
TO
1494 $this->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $result));
1495 $this->assign('receive_date',
1496 CRM_Utils_Date::mysqlToIso($params['receive_date'])
1497 );
1498
1499 // required for creating membership for related contacts
1500 $params['action'] = $this->_action;
1501
1502 //create membership record.
1503 $count = 0;
1504 foreach ($this->_memTypeSelected as $memType) {
1505 if ($count &&
1506 ($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id))
1507 ) {
1508 $membershipTypeValues[$memType]['relate_contribution_id'] = $relateContribution;
1509 }
1510
1511 $membershipParams = array_merge($membershipTypeValues[$memType], $params);
87d0f881 1512 //CRM-15366
396e62d8 1513 if (!empty($softParams) && empty($paymentParams['is_recur'])) {
1514 $membershipParams['soft_credit'] = $softParams;
1515 }
77623a96 1516 if (isset($result['fee_amount'])) {
1517 $membershipParams['fee_amount'] = $result['fee_amount'];
1518 }
8a7b41d1
EM
1519 // This is required to trigger the recording of the membership contribution in the
1520 // CRM_Member_BAO_Membership::Create function.
1521 // @todo stop setting this & 'teach' the create function to respond to something
1522 // appropriate as part of our 2-step always create the pending contribution & then finally add the payment
1523 // process -
1524 // @see http://wiki.civicrm.org/confluence/pages/viewpage.action?pageId=261062657#Payments&AccountsRoadmap-Movetowardsalwaysusinga2-steppaymentprocess
1525 $membershipParams['contribution_status_id'] = CRM_Utils_Array::value('payment_status_id', $result);
ccb02c2d 1526 if (!empty($paymentParams['is_recur'])) {
1527 // The earlier process created the line items (although we want to get rid of the earlier one in favour
1528 // of a single path!
1529 unset($membershipParams['lineItems']);
1530 }
18135422 1531 $membershipParams['payment_instrument_id'] = $paymentInstrumentID;
f57cb50c 1532 // @todo stop passing $ids (membership and userId only are set above)
6a488035 1533 $membership = CRM_Member_BAO_Membership::create($membershipParams, $ids);
3e228d81
PN
1534 $params['contribution'] = CRM_Utils_Array::value('contribution', $membershipParams);
1535 unset($params['lineItems']);
6a488035
TO
1536 $this->_membershipIDs[] = $membership->id;
1537 $createdMemberships[$memType] = $membership;
1538 $count++;
1539 }
1540
6a488035
TO
1541 }
1542 else {
1543 $params['action'] = $this->_action;
8cc574cf 1544 if ($this->_onlinePendingContributionId && !empty($formValues['record_contribution'])) {
6a488035
TO
1545
1546 // update membership as well as contribution object, CRM-4395
1547 $params['contribution_id'] = $this->_onlinePendingContributionId;
1548 $params['componentId'] = $params['id'];
1549 $params['componentName'] = 'contribute';
1550 $result = CRM_Contribute_BAO_Contribution::transitionComponents($params, TRUE);
8cc574cf 1551 if (!empty($result) && !empty($params['contribution_id'])) {
be2fb01f 1552 $lineItem = [];
77dbdcbc 1553 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['contribution_id']);
b11c92be 1554 $itemId = key($lineItems);
1555 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
4aa7d844 1556
6a488035
TO
1557 $lineItems[$itemId]['unit_price'] = $params['total_amount'];
1558 $lineItems[$itemId]['line_total'] = $params['total_amount'];
1559 $lineItems[$itemId]['id'] = $itemId;
1560 $lineItem[$priceSetId] = $lineItems;
8aa7457a
EM
1561 $contributionBAO = new CRM_Contribute_BAO_Contribution();
1562 $contributionBAO->id = $params['contribution_id'];
7524682e 1563 $contributionBAO->contact_id = $params['contact_id'];
8aa7457a
EM
1564 $contributionBAO->find();
1565 CRM_Price_BAO_LineItem::processPriceSet($params['contribution_id'], $lineItem, $contributionBAO, 'civicrm_membership');
133e2c99 1566
1567 //create new soft-credit record, CRM-13981
00c1cd97
CW
1568 if ($softParams) {
1569 $softParams['contribution_id'] = $params['contribution_id'];
1570 while ($contributionBAO->fetch()) {
1571 $softParams['currency'] = $contributionBAO->currency;
1572 $softParams['amount'] = $contributionBAO->total_amount;
1573 }
1574 CRM_Contribute_BAO_ContributionSoft::add($softParams);
133e2c99 1575 }
6a488035
TO
1576 }
1577
1578 //carry updated membership object.
1579 $membership = new CRM_Member_DAO_Membership();
1580 $membership->id = $this->_id;
1581 $membership->find(TRUE);
1582
1583 $cancelled = TRUE;
1584 if ($membership->end_date) {
1585 //display end date w/ status message.
1586 $endDate = $membership->end_date;
1587
be2fb01f 1588 if (!in_array($membership->status_id, [
7ff60806
PN
1589 // CRM-15475
1590 array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus(NULL, " name = 'Cancelled' ", 'name', FALSE, TRUE)),
1591 array_search('Expired', CRM_Member_PseudoConstant::membershipStatus()),
be2fb01f 1592 ])
b11c92be 1593 ) {
6a488035
TO
1594 $cancelled = FALSE;
1595 }
1596 }
1597 // suppress form values in template.
1598 $this->assign('cancelled', $cancelled);
1599
6a488035
TO
1600 $createdMemberships[] = $membership;
1601 }
1602 else {
1603 $count = 0;
1604 foreach ($this->_memTypeSelected as $memType) {
8cc574cf 1605 if ($count && !empty($formValues['record_contribution']) &&
6a488035
TO
1606 ($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id))
1607 ) {
1608 $membershipTypeValues[$memType]['relate_contribution_id'] = $relateContribution;
1609 }
1610
553842be
SL
1611 // @todo figure out why recieve_date isn't being set right here.
1612 if (empty($params['receive_date'])) {
1613 $params['receive_date'] = date('Y-m-d H:i:s');
1614 }
6a488035 1615 $membershipParams = array_merge($params, $membershipTypeValues[$memType]);
a7488080 1616 if (!empty($formValues['int_amount'])) {
be2fb01f 1617 $init_amount = [];
b11c92be 1618 foreach ($formValues as $key => $value) {
1619 if (strstr($key, 'txt-price')) {
6a488035
TO
1620 $init_amount[$key] = $value;
1621 }
1622 }
1623 $membershipParams['init_amount'] = $init_amount;
1624 }
d80dbc14 1625
1626 if (!empty($softParams)) {
1627 $membershipParams['soft_credit'] = $softParams;
1628 }
f57cb50c 1629 // @todo stop passing $ids (membership and userId only are set above)
6a488035 1630 $membership = CRM_Member_BAO_Membership::create($membershipParams, $ids);
3e228d81 1631 $params['contribution'] = CRM_Utils_Array::value('contribution', $membershipParams);
d5b95619 1632 unset($params['lineItems']);
fd706baa
PN
1633 // skip line item creation for next interation since line item(s) are already created.
1634 $params['skipLineItem'] = TRUE;
6a488035
TO
1635
1636 $this->_membershipIDs[] = $membership->id;
1637 $createdMemberships[$memType] = $membership;
1638 $count++;
1639 }
1640 }
1641 }
1bd1288b 1642 $isRecur = CRM_Utils_Array::value('is_recur', $params);
1643 if (($this->_action & CRM_Core_Action::UPDATE)) {
1644 $this->addStatusMessage($this->getStatusMessageForUpdate($membership, $endDate));
1645 }
1646 elseif (($this->_action & CRM_Core_Action::ADD)) {
1647 $this->addStatusMessage($this->getStatusMessageForCreate($endDate, $membershipTypes, $createdMemberships,
1648 $isRecur, $calcDates));
1649 }
6a488035 1650
ccb02c2d 1651 if (!empty($lineItem[$this->_priceSetId])) {
aaffa79f 1652 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
03b412ae 1653 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
01604562 1654 $taxAmount = FALSE;
79d001a2 1655 $totalTaxAmount = 0;
ccb02c2d 1656 foreach ($lineItem[$this->_priceSetId] as & $priceFieldOp) {
a7488080 1657 if (!empty($priceFieldOp['membership_type_id'])) {
3b85fc04
PN
1658 $priceFieldOp['start_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'], '%B %E%f, %Y') : '-';
1659 $priceFieldOp['end_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'], '%B %E%f, %Y') : '-';
6a488035
TO
1660 }
1661 else {
1662 $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A';
1663 }
03b412ae 1664 if ($invoicing && isset($priceFieldOp['tax_amount'])) {
01604562 1665 $taxAmount = TRUE;
79d001a2
PB
1666 $totalTaxAmount += $priceFieldOp['tax_amount'];
1667 }
1668 }
03b412ae 1669 if ($invoicing) {
be2fb01f 1670 $dataArray = [];
ccb02c2d 1671 foreach ($lineItem[$this->_priceSetId] as $key => $value) {
03b412ae
PB
1672 if (isset($value['tax_amount']) && isset($value['tax_rate'])) {
1673 if (isset($dataArray[$value['tax_rate']])) {
1674 $dataArray[$value['tax_rate']] = $dataArray[$value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value);
0db6c3e1
TO
1675 }
1676 else {
03b412ae
PB
1677 $dataArray[$value['tax_rate']] = CRM_Utils_Array::value('tax_amount', $value);
1678 }
0e81467c 1679 }
03b412ae 1680 }
01604562
PB
1681 if ($taxAmount) {
1682 $this->assign('totalTaxAmount', $totalTaxAmount);
a6e29c95 1683 // Not sure why would need this on Submit.... unless it's being used when sending mails in which case this is the wrong place
1684 $this->assign('taxTerm', $this->getSalesTaxTerm());
01604562 1685 }
03b412ae 1686 $this->assign('dataArray', $dataArray);
6a488035
TO
1687 }
1688 }
1689 $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE);
1690
1691 $receiptSend = FALSE;
7d193e45
LS
1692 $contributionId = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id);
1693 $membershipIds = $this->_membershipIDs;
1694 if ($contributionId && !empty($membershipIds)) {
1695 $contributionDetails = CRM_Contribute_BAO_Contribution::getContributionDetails(
1696 CRM_Export_Form_Select::MEMBER_EXPORT, $this->_membershipIDs);
1697 if ($contributionDetails[$membership->id]['contribution_status'] == 'Completed') {
aadd21c2 1698 $receiptSend = TRUE;
7d193e45
LS
1699 }
1700 }
6a488035 1701
7e0c2ccd 1702 $receiptSent = FALSE;
7d193e45 1703 if (!empty($formValues['send_receipt']) && $receiptSend) {
b11c92be 1704 $formValues['contact_id'] = $this->_contactID;
7d193e45 1705 $formValues['contribution_id'] = $contributionId;
186a737c
EM
1706 // We really don't need a distinct receipt_text_signup vs receipt_text_renewal as they are
1707 // handled in the receipt. But by setting one we avoid breaking templates for now
1708 // although at some point we should switch in the templates.
1709 $formValues['receipt_text_signup'] = $formValues['receipt_text'];
6a488035 1710 // send email receipt
09108d7d 1711 $this->assignBillingName();
efc0e24a 1712 $mailSend = $this->emailMembershipReceipt($formValues, $membership);
7e0c2ccd 1713 $receiptSent = TRUE;
6a488035 1714 }
6a488035 1715
7865d848
EM
1716 // finally set membership id if already not set
1717 if (!$this->_id) {
1718 $this->_id = $membership->id;
6a488035 1719 }
6a488035 1720
268a84f2 1721 $this->updateContributionOnMembershipTypeChange($params, $membership);
5ce8b943 1722 if ($receiptSent && $mailSend) {
be2fb01f 1723 $this->addStatusMessage(ts('A membership confirmation and receipt has been sent to %1.', [1 => $this->_contributorEmail]));
5ce8b943 1724 }
1725
1726 CRM_Core_Session::setStatus($this->getStatusMessage(), ts('Complete'), 'success');
1727 $this->setStatusMessage($membership);
7865d848
EM
1728 }
1729
268a84f2 1730 /**
1731 * Update related contribution of a membership if update_contribution_on_membership_type_change
1732 * contribution setting is enabled and type is changed on edit
1733 *
1734 * @param array $inputParams
1735 * submitted form values
1736 * @param CRM_Member_DAO_Membership $membership
1737 * Updated membership object
1738 *
1739 */
1740 protected function updateContributionOnMembershipTypeChange($inputParams, $membership) {
1741 if (Civi::settings()->get('update_contribution_on_membership_type_change') &&
971e129b
SL
1742 // on update
1743 ($this->_action & CRM_Core_Action::UPDATE) &&
1744 // if ID is present
1745 $this->_id &&
1746 // if selected membership doesn't match with earlier membership
1747 !in_array($this->_memType, $this->_memTypeSelected)
268a84f2 1748 ) {
de6c59ca 1749 if (!empty($inputParams['is_recur'])) {
268a84f2 1750 CRM_Core_Session::setStatus(ts('Associated recurring contribution cannot be updated on membership type change.', ts('Error'), 'error'));
1751 return;
1752 }
1753
1754 // fetch lineitems by updated membership ID
1755 $lineItems = CRM_Price_BAO_LineItem::getLineItems($membership->id, 'membership');
1756 // retrieve the related contribution ID
1757 $contributionID = CRM_Core_DAO::getFieldValue(
1758 'CRM_Member_DAO_MembershipPayment',
1759 $membership->id,
1760 'contribution_id',
1761 'membership_id'
1762 );
1763 // get price fields of chosen price-set
1764 $priceSetDetails = CRM_Utils_Array::value(
1765 $this->_priceSetId,
1766 CRM_Price_BAO_PriceSet::getSetDetail(
1767 $this->_priceSetId,
1768 TRUE,
1769 TRUE
1770 )
1771 );
1772
1773 // add price field information in $inputParams
1774 self::addPriceFieldByMembershipType($inputParams, $priceSetDetails['fields'], $membership->membership_type_id);
6dde7f04 1775
268a84f2 1776 // update related contribution and financial records
1777 CRM_Price_BAO_LineItem::changeFeeSelections(
1778 $inputParams,
1779 $membership->id,
1780 'membership',
1781 $contributionID,
1782 $priceSetDetails['fields'],
6dde7f04 1783 $lineItems
268a84f2 1784 );
1785 CRM_Core_Session::setStatus(ts('Associated contribution is updated on membership type change.'), ts('Success'), 'success');
1786 }
1787 }
1788
1789 /**
1790 * Add selected price field information in $formValues
1791 *
1792 * @param array $formValues
1793 * submitted form values
1794 * @param array $priceFields
1795 * Price fields of selected Priceset ID
1796 * @param int $membershipTypeID
1797 * Selected membership type ID
1798 *
1799 */
1800 public static function addPriceFieldByMembershipType(&$formValues, $priceFields, $membershipTypeID) {
1801 foreach ($priceFields as $priceFieldID => $priceField) {
1802 if (isset($priceField['options']) && count($priceField['options'])) {
1803 foreach ($priceField['options'] as $option) {
1804 if ($option['membership_type_id'] == $membershipTypeID) {
1805 $formValues["price_{$priceFieldID}"] = $option['id'];
1806 break;
1807 }
1808 }
1809 }
1810 }
1811 }
971e129b 1812
5e56c7a5 1813 /**
1814 * Set context in session.
1815 */
7865d848 1816 protected function setUserContext() {
6a488035 1817 $buttonName = $this->controller->getButtonName();
7865d848
EM
1818 $session = CRM_Core_Session::singleton();
1819
6a488035
TO
1820 if ($this->_context == 'standalone') {
1821 if ($buttonName == $this->getButtonName('upload', 'new')) {
1822 $session->replaceUserContext(CRM_Utils_System::url('civicrm/member/add',
b11c92be 1823 'reset=1&action=add&context=standalone'
1824 ));
6a488035
TO
1825 }
1826 else {
1827 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
b11c92be 1828 "reset=1&cid={$this->_contactID}&selectedChild=member"
1829 ));
6a488035
TO
1830 }
1831 }
1832 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
1833 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/membership',
b11c92be 1834 "reset=1&action=add&context=membership&cid={$this->_contactID}"
1835 ));
6a488035
TO
1836 }
1837 }
1838
1839 /**
5e56c7a5 1840 * Get status message for updating membership.
1841 *
1842 * @param CRM_Member_BAO_Membership $membership
1843 * @param string $endDate
5e56c7a5 1844 *
7865d848 1845 * @return string
6a488035 1846 */
6d5b9c63 1847 protected function getStatusMessageForUpdate($membership, $endDate) {
5e56c7a5 1848 // End date can be modified by hooks, so if end date is set then use it.
7865d848 1849 $endDate = ($membership->end_date) ? $membership->end_date : $endDate;
6a488035 1850
be2fb01f 1851 $statusMsg = ts('Membership for %1 has been updated.', [1 => $this->_memberDisplayName]);
7865d848
EM
1852 if ($endDate && $endDate !== 'null') {
1853 $endDate = CRM_Utils_Date::customFormat($endDate);
be2fb01f 1854 $statusMsg .= ' ' . ts('The membership End Date is %1.', [1 => $endDate]);
6a488035 1855 }
7865d848
EM
1856 return $statusMsg;
1857 }
6a488035 1858
7865d848 1859 /**
5e56c7a5 1860 * Get status message for create action.
1861 *
1862 * @param string $endDate
5e56c7a5 1863 * @param array $membershipTypes
1864 * @param array $createdMemberships
5b217d3f 1865 * @param bool $isRecur
5e56c7a5 1866 * @param array $calcDates
5e56c7a5 1867 *
7865d848
EM
1868 * @return array|string
1869 */
6d5b9c63 1870 protected function getStatusMessageForCreate($endDate, $membershipTypes, $createdMemberships,
1871 $isRecur, $calcDates) {
7865d848
EM
1872 // FIX ME: fix status messages
1873
be2fb01f 1874 $statusMsg = [];
7865d848 1875 foreach ($membershipTypes as $memType => $membershipType) {
be2fb01f 1876 $statusMsg[$memType] = ts('%1 membership for %2 has been added.', [
7865d848
EM
1877 1 => $membershipType,
1878 2 => $this->_memberDisplayName,
be2fb01f 1879 ]);
6a488035 1880
7865d848
EM
1881 $membership = $createdMemberships[$memType];
1882 $memEndDate = ($membership->end_date) ? $membership->end_date : $endDate;
6a488035 1883
7865d848 1884 //get the end date from calculated dates.
5b217d3f 1885 if (!$memEndDate && !$isRecur) {
7865d848 1886 $memEndDate = CRM_Utils_Array::value('end_date', $calcDates[$memType]);
35fa23f8 1887 }
6a488035 1888
7865d848
EM
1889 if ($memEndDate && $memEndDate !== 'null') {
1890 $memEndDate = CRM_Utils_Date::customFormat($memEndDate);
be2fb01f 1891 $statusMsg[$memType] .= ' ' . ts('The new membership End Date is %1.', [1 => $memEndDate]);
b11c92be 1892 }
6a488035 1893 }
7865d848 1894 $statusMsg = implode('<br/>', $statusMsg);
7865d848 1895 return $statusMsg;
6a488035 1896 }
96025800 1897
5b217d3f 1898 /**
1899 * @param $membership
5b217d3f 1900 */
5ce8b943 1901 protected function setStatusMessage($membership) {
5b217d3f 1902 //CRM-15187
1903 // display message when membership type is changed
1904 if (($this->_action & CRM_Core_Action::UPDATE) && $this->_id && !in_array($this->_memType, $this->_memTypeSelected)) {
0fedbc88
PN
1905 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'membership');
1906 $maxID = max(array_keys($lineItem));
1907 $lineItem = $lineItem[$maxID];
1908 $membershipTypeDetails = $this->allMembershipTypeDetails[$membership->membership_type_id];
1909 if ($membershipTypeDetails['financial_type_id'] != $lineItem['financial_type_id']) {
1910 CRM_Core_Session::setStatus(
1911 ts('The financial types associated with the old and new membership types are different. You may want to edit the contribution associated with this membership to adjust its financial type.'),
1912 ts('Warning')
1913 );
1914 }
1915 if ($membershipTypeDetails['minimum_fee'] != $lineItem['line_total']) {
1916 CRM_Core_Session::setStatus(
1917 ts('The cost of the old and new membership types are different. You may want to edit the contribution associated with this membership to adjust its amount.'),
1918 ts('Warning')
1919 );
1920 }
5b217d3f 1921 }
1922 }
1923
50b85bf9 1924 /**
1925 * @return bool
1926 */
1927 protected function isUpdateToExistingRecurringMembership() {
1928 $isRecur = FALSE;
1929 if ($this->_action & CRM_Core_Action::UPDATE
59c798c9 1930 && CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->getEntityId(),
50b85bf9 1931 'contribution_recur_id')
59c798c9 1932 && !CRM_Member_BAO_Membership::isSubscriptionCancelled($this->getEntityId())) {
50b85bf9 1933
1934 $isRecur = TRUE;
1935 }
1936 return $isRecur;
1937 }
1938
829df55e 1939 /**
1940 * Send a receipt for the membership.
1941 *
1942 * @param array $formValues
efc0e24a 1943 * @param \CRM_Member_BAO_Membership $membership
1944 *
1945 * @return bool
829df55e 1946 */
1947 protected function emailMembershipReceipt($formValues, $membership) {
efc0e24a 1948 $customValues = $this->getCustomValuesForReceipt($formValues, $membership);
1949
1950 return self::emailReceipt($this, $formValues, $membership, $customValues);
1951 }
1952
1953 /**
1954 * Filter the custom values from the input parameters (for display in the email).
1955 *
1956 * @todo figure out why the scary code this calls does & document.
1957 *
1958 * @param array $formValues
1959 * @param \CRM_Member_BAO_Membership $membership
1960 * @return array
1961 */
1962 protected function getCustomValuesForReceipt($formValues, $membership) {
1963 $customFields = $customValues = [];
1964 if (property_exists($this, '_groupTree')
1965 && !empty($this->_groupTree)
1966 ) {
1967 foreach ($this->_groupTree as $groupID => $group) {
1968 if ($groupID == 'info') {
1969 continue;
1970 }
1971 foreach ($group['fields'] as $k => $field) {
1972 $field['title'] = $field['label'];
1973 $customFields["custom_{$k}"] = $field;
1974 }
1975 }
1976 }
1977
1978 $members = [['member_id', '=', $membership->id, 0, 0]];
1979 // check whether its a test drive
1980 if ($this->_mode == 'test') {
1981 $members[] = ['member_test', '=', 1, 0, 0];
1982 }
1983
1984 CRM_Core_BAO_UFGroup::getValues($formValues['contact_id'], $customFields, $customValues, FALSE, $members);
1985 return $customValues;
829df55e 1986 }
1987
6a488035 1988}