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