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