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