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) | |
971e129b | 32 | * @var string |
d424ffde | 33 | */ |
b09fe5ed | 34 | protected $_memberEmail = NULL; |
d424ffde CW |
35 | |
36 | /** | |
fe482240 | 37 | * Contact ID of the member. |
d424ffde CW |
38 | * |
39 | * | |
40 | * @var int | |
41 | */ | |
b09fe5ed | 42 | public $_contactID = NULL; |
d424ffde CW |
43 | |
44 | /** | |
45 | * Display name of the person paying for the membership (used for receipts) | |
46 | * | |
47 | * @var string | |
48 | */ | |
b09fe5ed | 49 | protected $_contributorDisplayName = NULL; |
d424ffde CW |
50 | |
51 | /** | |
52 | * email of the person paying for the membership (used for receipts) | |
971e129b | 53 | * @var string |
d424ffde | 54 | */ |
b09fe5ed | 55 | protected $_contributorEmail = NULL; |
d424ffde CW |
56 | |
57 | /** | |
58 | * email of the person paying for the membership (used for receipts) | |
59 | * | |
60 | * @var int | |
61 | */ | |
b09fe5ed | 62 | protected $_contributorContactID = NULL; |
d424ffde CW |
63 | |
64 | /** | |
65 | * ID of the person the receipt is to go to | |
66 | * | |
67 | * @var int | |
68 | */ | |
b09fe5ed | 69 | protected $_receiptContactId = NULL; |
d424ffde CW |
70 | |
71 | /** | |
6a488035 TO |
72 | * context would be set to standalone if the contact is use is being selected from |
73 | * the form rather than in the URL | |
971e129b | 74 | * @var string |
6a488035 | 75 | */ |
dda27a57 | 76 | public $_context; |
6a488035 | 77 | |
af990df6 EM |
78 | /** |
79 | * End date of renewed membership. | |
80 | * | |
81 | * @var string | |
82 | */ | |
83 | protected $endDate = NULL; | |
84 | ||
85 | /** | |
86 | * Has an email been sent. | |
87 | * | |
88 | * @var string | |
89 | */ | |
90 | protected $isMailSent = FALSE; | |
91 | ||
92 | /** | |
93 | * The name of the renewed membership type. | |
94 | * | |
95 | * @var string | |
96 | */ | |
97 | protected $membershipTypeName = ''; | |
98 | ||
59c798c9 | 99 | /** |
100 | * Set entity fields to be assigned to the form. | |
101 | */ | |
102 | protected function setEntityFields() {} | |
103 | ||
104 | /** | |
105 | * Set the delete message. | |
106 | * | |
107 | * We do this from the constructor in order to do a translation. | |
108 | */ | |
109 | public function setDeleteMessage() {} | |
110 | ||
c73f0c56 | 111 | /** |
112 | * Pre-process form. | |
113 | * | |
114 | * @throws \Exception | |
115 | */ | |
24d9c528 JG |
116 | |
117 | /** | |
118 | * Set the renewal notification status message. | |
119 | */ | |
120 | public function setRenewalMessage() { | |
121 | $statusMsg = ts('%1 membership for %2 has been renewed.', array(1 => $this->membershipTypeName, 2 => $this->_memberDisplayName)); | |
122 | ||
123 | if ($this->isMailSent) { | |
124 | $statusMsg .= ' ' . ts('A renewal confirmation and receipt has been sent to %1.', array( | |
125 | 1 => $this->_contributorEmail, | |
126 | )); | |
127 | } | |
128 | CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success'); | |
129 | } | |
130 | ||
6a488035 | 131 | public function preProcess() { |
6a488035 | 132 | |
186a737c EM |
133 | // This string makes up part of the class names, differentiating them (not sure why) from the membership fields. |
134 | $this->assign('formClass', 'membershiprenew'); | |
a6513ad5 | 135 | parent::preProcess(); |
6a488035 TO |
136 | |
137 | $this->assign('endDate', CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
353ffa53 TO |
138 | $this->_id, 'end_date' |
139 | ) | |
140 | )); | |
6a488035 TO |
141 | $this->assign('membershipStatus', |
142 | CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', | |
143 | CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
144 | $this->_id, 'status_id' | |
145 | ), | |
146 | 'name' | |
147 | ) | |
148 | ); | |
149 | ||
6a488035 TO |
150 | if ($this->_mode) { |
151 | $membershipFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee'); | |
152 | if (!$membershipFee) { | |
a6513ad5 | 153 | $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 |
154 | CRM_Core_Session::setStatus($statusMsg, '', 'info'); |
155 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', | |
353ffa53 TO |
156 | "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership" |
157 | )); | |
6a488035 | 158 | } |
6a488035 TO |
159 | } |
160 | ||
161 | // when custom data is included in this page | |
a7488080 | 162 | if (!empty($_POST['hidden_custom'])) { |
9ac2fef4 | 163 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_memType, 1, 'Membership', $this->_id); |
6a488035 TO |
164 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
165 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
166 | } | |
167 | ||
e2046b33 | 168 | CRM_Utils_System::setTitle(ts('Renew Membership')); |
af990df6 EM |
169 | |
170 | parent::preProcess(); | |
6a488035 TO |
171 | } |
172 | ||
173 | /** | |
c490a46a | 174 | * Set default values for the form. |
6a488035 TO |
175 | * the default values are retrieved from the database |
176 | * | |
2f9f578f EM |
177 | * @return array |
178 | * Default values. | |
6a488035 TO |
179 | */ |
180 | public function setDefaultValues() { | |
a6513ad5 | 181 | |
353ffa53 | 182 | $defaults = parent::setDefaultValues(); |
6a488035 TO |
183 | |
184 | // set renewal_date and receive_date to today in correct input format (setDateDefaults uses today if no value passed) | |
9eb6085d | 185 | $now = date('Y-m-d'); |
6a488035 | 186 | $defaults['renewal_date'] = $now; |
9eb6085d | 187 | $defaults['receive_date'] = $now . ' ' . date('H:i:s'); |
6a488035 TO |
188 | |
189 | if ($defaults['id']) { | |
190 | $defaults['record_contribution'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', | |
191 | $defaults['id'], | |
192 | 'contribution_id', | |
193 | 'membership_id' | |
194 | ); | |
195 | } | |
196 | ||
6a488035 | 197 | $defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'financial_type_id'); |
133e2c99 | 198 | |
d96cf288 | 199 | //CRM-13420 |
a7488080 | 200 | if (empty($defaults['payment_instrument_id'])) { |
d96cf288 DG |
201 | $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')); |
202 | } | |
6a488035 TO |
203 | |
204 | $defaults['total_amount'] = CRM_Utils_Money::format(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
353ffa53 TO |
205 | $this->_memType, |
206 | 'minimum_fee' | |
207 | ), NULL, '%a'); | |
6a488035 | 208 | |
6a488035 TO |
209 | $defaults['record_contribution'] = 0; |
210 | $defaults['num_terms'] = 1; | |
211 | $defaults['send_receipt'] = 0; | |
212 | ||
374a4dd6 | 213 | //set Soft Credit Type to Gift by default |
214 | $scTypes = CRM_Core_OptionGroup::values("soft_credit_type"); | |
215 | $defaults['soft_credit_type_id'] = CRM_Utils_Array::value(ts('Gift'), array_flip($scTypes)); | |
216 | ||
9eb6085d | 217 | $renewalDate = CRM_Utils_Array::value('renewal_date', $defaults); |
6a488035 TO |
218 | $this->assign('renewalDate', $renewalDate); |
219 | $this->assign('member_is_test', CRM_Utils_Array::value('member_is_test', $defaults)); | |
220 | ||
221 | if ($this->_mode) { | |
3b8e6c3f | 222 | $defaults = $this->getBillingDefaults($defaults); |
6a488035 | 223 | } |
6a488035 TO |
224 | return $defaults; |
225 | } | |
226 | ||
227 | /** | |
fe482240 | 228 | * Build the form object. |
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 { | |
262 | $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values); | |
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])) { |
6a488035 TO |
273 | $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values); |
274 | } | |
275 | } | |
276 | ||
414368d7 | 277 | //CRM-16950 |
805eecf8 | 278 | $taxAmount = NULL; |
414368d7 | 279 | $totalAmount = CRM_Utils_Array::value('minimum_fee', $values); |
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, |
414368d7 BS |
290 | 'total_amount' => CRM_Utils_Money::format($totalAmount, NULL, '%a'), |
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'), | |
7fe7b63d | 362 | CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses('membership') |
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 SL |
410 | * @param $files |
411 | * @param $self | |
6a488035 | 412 | * |
72b3a70c CW |
413 | * @return bool|array |
414 | * mixed true or array of errors | |
6a488035 | 415 | */ |
9ca6b140 | 416 | public static function formRule($params, $files, $self) { |
be2fb01f | 417 | $errors = []; |
6a488035 TO |
418 | if ($params['membership_type_id'][0] == 0) { |
419 | $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.'); | |
420 | } | |
421 | if ($params['membership_type_id'][1] == 0) { | |
422 | $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.'); | |
423 | } | |
424 | ||
9ca6b140 A |
425 | // CRM-20571 |
426 | // Get the Join Date from Membership info as it is not available in the Renewal form | |
427 | $joinDate = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $self->_id, 'join_date'); | |
428 | ||
bb7def85 | 429 | // CRM-20571: Check if the renewal date is not before Join Date, if it is then add to 'errors' array |
9ca6b140 | 430 | // The fields in Renewal form come into this routine in $params array. 'renewal_date' is in the form |
bb7def85 A |
431 | // We process both the dates before comparison using CRM utils so that they are in same date format |
432 | if (isset($params['renewal_date'])) { | |
9eb6085d | 433 | if ($params['renewal_date'] < $joinDate) { |
bb7def85 A |
434 | $errors['renewal_date'] = ts('Renewal date must be the same or later than Member since (Join Date).'); |
435 | } | |
436 | } | |
437 | ||
6a488035 TO |
438 | //total amount condition arise when membership type having no |
439 | //minimum fee | |
440 | if (isset($params['record_contribution'])) { | |
441 | if (!$params['financial_type_id']) { | |
442 | $errors['financial_type_id'] = ts('Please select a Financial Type.'); | |
443 | } | |
444 | if (!$params['total_amount']) { | |
445 | $errors['total_amount'] = ts('Please enter a Contribution Amount.'); | |
446 | } | |
a7488080 | 447 | if (empty($params['payment_instrument_id'])) { |
4db803dd | 448 | $errors['payment_instrument_id'] = ts('Payment Method is a required field.'); |
d96cf288 | 449 | } |
6a488035 TO |
450 | } |
451 | return empty($errors) ? TRUE : $errors; | |
452 | } | |
453 | ||
454 | /** | |
fe482240 | 455 | * Process the renewal form. |
6a488035 TO |
456 | */ |
457 | public function postProcess() { | |
6a488035 | 458 | // get the submitted form values. |
b37f1131 | 459 | $this->_params = $this->controller->exportValues($this->_name); |
1a00ea3c | 460 | $this->assignBillingName(); |
6a488035 | 461 | |
1a00ea3c | 462 | try { |
af990df6 | 463 | $this->submit(); |
24d9c528 | 464 | $this->setRenewalMessage(); |
1a00ea3c EM |
465 | } |
466 | catch (\Civi\Payment\Exception\PaymentProcessorException $e) { | |
63dd64f2 | 467 | CRM_Core_Session::singleton()->setStatus($e->getMessage()); |
1a00ea3c EM |
468 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', |
469 | "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership&mode={$this->_mode}" | |
470 | )); | |
471 | } | |
4187b12f EM |
472 | } |
473 | ||
4187b12f EM |
474 | /** |
475 | * Process form submission. | |
476 | * | |
477 | * This function is also accessed by a unit test. | |
f2f20028 | 478 | * |
479 | * @throws \CRM_Core_Exception | |
90530b87 | 480 | * @throws \CiviCRM_API3_Exception |
4187b12f | 481 | */ |
b37f1131 EM |
482 | protected function submit() { |
483 | $this->storeContactFields($this->_params); | |
ba2f3f65 | 484 | $this->beginPostProcess(); |
481a74f4 | 485 | $now = CRM_Utils_Date::getToday(NULL, 'YmdHis'); |
9eb6085d | 486 | $this->assign('receive_date', CRM_Utils_Array::value('receive_date', $this->_params, date('Y-m-d H:i:s'))); |
09108d7d | 487 | $this->processBillingAddress(); |
af990df6 | 488 | list($userName) = CRM_Contact_BAO_Contact_Location::getEmailDetails(CRM_Core_Session::singleton()->get('userID')); |
0816949d EM |
489 | $this->_params['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_params, |
490 | CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee') | |
491 | ); | |
492 | $this->_membershipId = $this->_id; | |
493 | $customFieldsFormatted = CRM_Core_BAO_CustomField::postProcess($this->_params, | |
494 | $this->_id, | |
495 | 'Membership' | |
496 | ); | |
497 | if (empty($this->_params['financial_type_id'])) { | |
498 | $this->_params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'financial_type_id'); | |
499 | } | |
14065266 | 500 | $contributionRecurID = NULL; |
0816949d EM |
501 | $this->assign('membershipID', $this->_id); |
502 | $this->assign('contactID', $this->_contactID); | |
503 | $this->assign('module', 'Membership'); | |
504 | $this->assign('receiptType', 'membership renewal'); | |
505 | $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency; | |
506 | $this->_params['invoice_id'] = $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
6a488035 | 507 | |
a7488080 | 508 | if (!empty($this->_params['send_receipt'])) { |
b37f1131 EM |
509 | $this->_params['receipt_date'] = $now; |
510 | $this->assign('receipt_date', CRM_Utils_Date::mysqlToIso($this->_params['receipt_date'])); | |
6a488035 TO |
511 | } |
512 | else { | |
b37f1131 | 513 | $this->_params['receipt_date'] = NULL; |
6a488035 TO |
514 | } |
515 | ||
516 | if ($this->_mode) { | |
b37f1131 | 517 | $this->_params['register_date'] = $now; |
e47238e5 | 518 | $this->_params['description'] = ts("Contribution submitted by a staff person using member's credit card for renewal"); |
b37f1131 | 519 | $this->_params['amount'] = $this->_params['total_amount']; |
b1b2796c | 520 | $this->_params['payment_instrument_id'] = $this->_paymentProcessor['payment_instrument_id']; |
392bb607 | 521 | $this->_params['receive_date'] = $now; |
6a488035 TO |
522 | |
523 | // at this point we've created a contact and stored its address etc | |
524 | // all the payment processors expect the name and address to be in the passed params | |
525 | // so we copy stuff over to first_name etc. | |
526 | $paymentParams = $this->_params; | |
a7488080 | 527 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
528 | $paymentParams['email'] = $this->_contributorEmail; |
529 | } | |
b1b2796c | 530 | $paymentParams['is_email_receipt'] = !empty($this->_params['send_receipt']); |
6a488035 TO |
531 | |
532 | $paymentParams['contactID'] = $this->_contributorContactID; | |
6a488035 TO |
533 | |
534 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); | |
535 | ||
0816949d | 536 | $payment = $this->_paymentProcessor['object']; |
6a488035 | 537 | |
b37f1131 | 538 | if (!empty($this->_params['auto_renew'])) { |
a70418a7 | 539 | $contributionRecurParams = $this->processRecurringContribution($paymentParams); |
14065266 | 540 | $contributionRecurID = $contributionRecurParams['contributionRecurID']; |
a70418a7 EM |
541 | $paymentParams = array_merge($paymentParams, $contributionRecurParams); |
542 | } | |
0816949d | 543 | |
1a00ea3c | 544 | $result = $payment->doPayment($paymentParams); |
0816949d | 545 | $this->_params = array_merge($this->_params, $result); |
6a488035 | 546 | |
0816949d | 547 | $this->_params['contribution_status_id'] = $result['payment_status_id']; |
b37f1131 | 548 | $this->_params['trxn_id'] = $result['trxn_id']; |
b37f1131 | 549 | $this->_params['is_test'] = ($this->_mode == 'live') ? 0 : 1; |
6a488035 TO |
550 | $this->set('params', $this->_params); |
551 | $this->assign('trxn_id', $result['trxn_id']); | |
552 | } | |
553 | ||
9eb6085d | 554 | $renewalDate = !empty($this->_params['renewal_date']) ? $renewalDate = $this->_params['renewal_date'] : NULL; |
4187b12f | 555 | |
6a488035 TO |
556 | // check for test membership. |
557 | $isTestMembership = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_membershipId, 'is_test'); | |
558 | ||
559 | // chk for renewal for multiple terms CRM-8750 | |
560 | $numRenewTerms = 1; | |
b37f1131 EM |
561 | if (is_numeric(CRM_Utils_Array::value('num_terms', $this->_params))) { |
562 | $numRenewTerms = $this->_params['num_terms']; | |
6a488035 TO |
563 | } |
564 | ||
565 | //if contribution status is pending then set pay later | |
0816949d | 566 | $this->_params['is_pay_later'] = FALSE; |
c3b82060 | 567 | if ($this->_params['contribution_status_id'] == array_search('Pending', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label'))) { |
6a488035 TO |
568 | $this->_params['is_pay_later'] = 1; |
569 | } | |
8bc79dfc | 570 | |
f7e6f614 | 571 | // These variable sets prior to membership may not be required for this form. They were in |
d87103d2 | 572 | // a function this form shared with other forms. |
61767a1d | 573 | $membershipSource = NULL; |
d87103d2 EM |
574 | if (!empty($this->_params['membership_source'])) { |
575 | $membershipSource = $this->_params['membership_source']; | |
61767a1d | 576 | } |
0816949d | 577 | |
5b9d3ce8 | 578 | // @todo Move this into CRM_Member_BAO_Membership::processMembership |
af9ea4e0 | 579 | $pending = ($this->_params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending')); |
f7e6f614 | 580 | list($membership) = CRM_Member_BAO_Membership::processMembership( |
b37f1131 | 581 | $this->_contactID, $this->_params['membership_type_id'][1], $isTestMembership, |
dfc9c0c7 | 582 | $renewalDate, NULL, $customFieldsFormatted, $numRenewTerms, $this->_membershipId, |
5b9d3ce8 | 583 | $pending, |
0816949d EM |
584 | $contributionRecurID, $membershipSource, $this->_params['is_pay_later'], CRM_Utils_Array::value('campaign_id', |
585 | $this->_params) | |
6a488035 TO |
586 | ); |
587 | ||
f7e6f614 | 588 | $this->endDate = CRM_Utils_Date::processDate($membership->end_date); |
6a488035 | 589 | |
f7e6f614 | 590 | $this->membershipTypeName = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membership->membership_type_id, |
af990df6 | 591 | 'name'); |
6a488035 | 592 | |
b37f1131 | 593 | if (!empty($this->_params['record_contribution']) || $this->_mode) { |
6a488035 | 594 | // set the source |
af990df6 | 595 | $this->_params['contribution_source'] = "{$this->membershipTypeName} Membership: Offline membership renewal (by {$userName})"; |
03e04002 | 596 | |
6a488035 | 597 | //create line items |
be2fb01f | 598 | $lineItem = []; |
ccb02c2d | 599 | $this->_params = $this->setPriceSetParameters($this->_params); |
9da8dc8c | 600 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], |
bdcbbfea | 601 | $this->_params, $lineItem[$this->_priceSetId], $this->_priceSetId |
6a488035 | 602 | ); |
03e04002 | 603 | //CRM-11529 for quick config backoffice transactions |
604 | //when financial_type_id is passed in form, update the | |
1f676e92 | 605 | //line items with the financial type selected in form |
b37f1131 | 606 | if ($submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $this->_params)) { |
ccb02c2d | 607 | foreach ($lineItem[$this->_priceSetId] as &$li) { |
6a488035 TO |
608 | $li['financial_type_id'] = $submittedFinancialType; |
609 | } | |
610 | } | |
b3f6108a | 611 | |
6a488035 | 612 | if (!empty($lineItem)) { |
b37f1131 EM |
613 | $this->_params['lineItems'] = $lineItem; |
614 | $this->_params['processPriceSet'] = TRUE; | |
6a488035 TO |
615 | } |
616 | ||
617 | //assign contribution contact id to the field expected by recordMembershipContribution | |
9b873358 | 618 | if ($this->_contributorContactID != $this->_contactID) { |
b37f1131 | 619 | $this->_params['contribution_contact_id'] = $this->_contributorContactID; |
9b873358 | 620 | if (!empty($this->_params['soft_credit_type_id'])) { |
be2fb01f | 621 | $this->_params['soft_credit'] = [ |
133e2c99 | 622 | 'soft_credit_type_id' => $this->_params['soft_credit_type_id'], |
91ef9be0 | 623 | 'contact_id' => $this->_contactID, |
be2fb01f | 624 | ]; |
6a488035 TO |
625 | } |
626 | } | |
b37f1131 | 627 | $this->_params['contact_id'] = $this->_contactID; |
1acc24d5 EM |
628 | //recordMembershipContribution receives params as a reference & adds one variable. This is |
629 | // not a great pattern & ideally it would not receive as a reference. We assign our params as a | |
630 | // temporary variable to avoid e-notice & to make it clear to future refactorer that | |
631 | // this function is NOT reliant on that var being set | |
be2fb01f | 632 | $temporaryParams = array_merge($this->_params, [ |
f7e6f614 | 633 | 'membership_id' => $membership->id, |
e136edcf | 634 | 'contribution_recur_id' => $contributionRecurID, |
be2fb01f | 635 | ]); |
85ade0ae | 636 | //Remove `tax_amount` if it is not calculated. |
637 | if (CRM_Utils_Array::value('tax_amount', $temporaryParams) === 0) { | |
638 | unset($temporaryParams['tax_amount']); | |
639 | } | |
1acc24d5 | 640 | CRM_Member_BAO_Membership::recordMembershipContribution($temporaryParams); |
6a488035 TO |
641 | } |
642 | ||
b37f1131 | 643 | if (!empty($this->_params['send_receipt'])) { |
f2f20028 | 644 | $this->sendReceipt($membership); |
645 | } | |
646 | } | |
6a488035 | 647 | |
f2f20028 | 648 | /** |
649 | * Send a receipt. | |
650 | * | |
651 | * @param array $membership | |
652 | * | |
653 | * @throws \CRM_Core_Exception | |
654 | */ | |
655 | protected function sendReceipt($membership) { | |
656 | $receiptFrom = $this->_params['from_email_address']; | |
6a488035 | 657 | |
f2f20028 | 658 | if (!empty($this->_params['payment_instrument_id'])) { |
659 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
660 | $this->_params['paidBy'] = $paymentInstrument[$this->_params['payment_instrument_id']]; | |
661 | } | |
662 | //get the group Tree | |
663 | $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree('Membership', NULL, $this->_id, FALSE, $this->_memType); | |
664 | ||
665 | // retrieve custom data | |
666 | $customFields = $customValues = $fo = []; | |
667 | foreach ($this->_groupTree as $groupID => $group) { | |
668 | if ($groupID == 'info') { | |
669 | continue; | |
6a488035 | 670 | } |
f2f20028 | 671 | foreach ($group['fields'] as $k => $field) { |
672 | $field['title'] = $field['label']; | |
673 | $customFields["custom_{$k}"] = $field; | |
6a488035 | 674 | } |
f2f20028 | 675 | } |
676 | $members = [['member_id', '=', $this->_membershipId, 0, 0]]; | |
677 | // check whether its a test drive | |
678 | if ($this->_mode == 'test') { | |
679 | $members[] = ['member_test', '=', 1, 0, 0]; | |
680 | } | |
681 | CRM_Core_BAO_UFGroup::getValues($this->_contactID, $customFields, $customValues, FALSE, $members); | |
6a488035 | 682 | |
f2f20028 | 683 | $this->assign_by_ref('formValues', $this->_params); |
684 | if (!empty($this->_params['contribution_id'])) { | |
685 | $this->assign('contributionID', $this->_params['contribution_id']); | |
686 | } | |
0816949d | 687 | |
f2f20028 | 688 | $this->assign('membership_name', CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', |
689 | $membership->membership_type_id | |
690 | )); | |
691 | $this->assign('customValues', $customValues); | |
692 | $this->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date)); | |
693 | $this->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date)); | |
694 | if ($this->_mode) { | |
695 | $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( | |
696 | $this->_params, | |
697 | $this->_bltID | |
353ffa53 | 698 | )); |
f2f20028 | 699 | $this->assign('contributeMode', 'direct'); |
700 | $this->assign('isAmountzero', 0); | |
701 | $this->assign('is_pay_later', 0); | |
702 | $this->assign('isPrimary', 1); | |
703 | $this->assign('receipt_text_renewal', $this->_params['receipt_text']); | |
704 | if ($this->_mode == 'test') { | |
705 | $this->assign('action', '1024'); | |
6a488035 | 706 | } |
6a488035 | 707 | } |
f2f20028 | 708 | |
709 | list($this->isMailSent) = CRM_Core_BAO_MessageTemplate::sendTemplate( | |
710 | [ | |
711 | 'groupName' => 'msg_tpl_workflow_membership', | |
712 | 'valueName' => 'membership_offline_receipt', | |
713 | 'contactId' => $this->_receiptContactId, | |
714 | 'from' => $receiptFrom, | |
715 | 'toName' => $this->_contributorDisplayName, | |
716 | 'toEmail' => $this->_contributorEmail, | |
717 | 'isTest' => $this->_mode == 'test', | |
718 | ] | |
719 | ); | |
6a488035 | 720 | } |
96025800 | 721 | |
6a488035 | 722 | } |