Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Membership Renewal | |
38 | * | |
39 | */ | |
40 | class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { | |
d424ffde CW |
41 | |
42 | /** | |
fe482240 | 43 | * Display name of the member. |
d424ffde CW |
44 | * |
45 | * @var string | |
6a488035 | 46 | */ |
b09fe5ed | 47 | protected $_memberDisplayName = NULL; |
d424ffde CW |
48 | |
49 | /** | |
50 | * email of the person paying for the membership (used for receipts) | |
51 | */ | |
b09fe5ed | 52 | protected $_memberEmail = NULL; |
d424ffde CW |
53 | |
54 | /** | |
fe482240 | 55 | * Contact ID of the member. |
d424ffde CW |
56 | * |
57 | * | |
58 | * @var int | |
59 | */ | |
b09fe5ed | 60 | public $_contactID = NULL; |
d424ffde CW |
61 | |
62 | /** | |
63 | * Display name of the person paying for the membership (used for receipts) | |
64 | * | |
65 | * @var string | |
66 | */ | |
b09fe5ed | 67 | protected $_contributorDisplayName = NULL; |
d424ffde CW |
68 | |
69 | /** | |
70 | * email of the person paying for the membership (used for receipts) | |
71 | */ | |
b09fe5ed | 72 | protected $_contributorEmail = NULL; |
d424ffde CW |
73 | |
74 | /** | |
75 | * email of the person paying for the membership (used for receipts) | |
76 | * | |
77 | * @var int | |
78 | */ | |
b09fe5ed | 79 | protected $_contributorContactID = NULL; |
d424ffde CW |
80 | |
81 | /** | |
82 | * ID of the person the receipt is to go to | |
83 | * | |
84 | * @var int | |
85 | */ | |
b09fe5ed | 86 | protected $_receiptContactId = NULL; |
d424ffde CW |
87 | |
88 | /** | |
6a488035 TO |
89 | * context would be set to standalone if the contact is use is being selected from |
90 | * the form rather than in the URL | |
91 | */ | |
dda27a57 | 92 | public $_context; |
6a488035 | 93 | |
5d86176b | 94 | /** |
95 | * An array to hold a list of datefields on the form | |
96 | * so that they can be converted to ISO in a consistent manner | |
97 | * | |
98 | * @var array | |
99 | */ | |
100 | protected $_dateFields = array( | |
101 | 'receive_date' => array('default' => 'now'), | |
102 | ); | |
103 | ||
6a488035 TO |
104 | public function preProcess() { |
105 | //custom data related code | |
106 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
107 | $this->assign('cdType', FALSE); | |
108 | if ($this->_cdType) { | |
109 | $this->assign('cdType', TRUE); | |
110 | return CRM_Custom_Form_CustomData::preProcess($this); | |
111 | } | |
112 | ||
a6513ad5 | 113 | parent::preProcess(); |
6a488035 TO |
114 | // check for edit permission |
115 | if (!CRM_Core_Permission::check('edit memberships')) { | |
0499b0ad | 116 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 | 117 | } |
6a488035 TO |
118 | |
119 | $this->assign('endDate', CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
353ffa53 TO |
120 | $this->_id, 'end_date' |
121 | ) | |
122 | )); | |
6a488035 TO |
123 | $this->assign('membershipStatus', |
124 | CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', | |
125 | CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
126 | $this->_id, 'status_id' | |
127 | ), | |
128 | 'name' | |
129 | ) | |
130 | ); | |
131 | ||
6a488035 TO |
132 | if ($this->_mode) { |
133 | $membershipFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee'); | |
134 | if (!$membershipFee) { | |
a6513ad5 | 135 | $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 |
136 | CRM_Core_Session::setStatus($statusMsg, '', 'info'); |
137 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', | |
353ffa53 TO |
138 | "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership" |
139 | )); | |
6a488035 | 140 | } |
6a488035 TO |
141 | } |
142 | ||
143 | // when custom data is included in this page | |
a7488080 | 144 | if (!empty($_POST['hidden_custom'])) { |
6a488035 TO |
145 | CRM_Custom_Form_CustomData::preProcess($this); |
146 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
147 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
148 | } | |
149 | ||
e2046b33 CW |
150 | CRM_Utils_System::setTitle(ts('Renew Membership')); |
151 | ||
6a488035 TO |
152 | parent::preProcess(); |
153 | } | |
154 | ||
155 | /** | |
c490a46a | 156 | * Set default values for the form. |
6a488035 TO |
157 | * the default values are retrieved from the database |
158 | * | |
6a488035 | 159 | * |
355ba699 | 160 | * @return void |
6a488035 TO |
161 | */ |
162 | public function setDefaultValues() { | |
163 | if ($this->_cdType) { | |
164 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
165 | } | |
a6513ad5 | 166 | |
353ffa53 | 167 | $defaults = parent::setDefaultValues(); |
6a488035 TO |
168 | $this->_memType = $defaults['membership_type_id']; |
169 | ||
170 | // set renewal_date and receive_date to today in correct input format (setDateDefaults uses today if no value passed) | |
a9c72cdb | 171 | list($now, $currentTime) = CRM_Utils_Date::setDateDefaults(); |
6a488035 TO |
172 | $defaults['renewal_date'] = $now; |
173 | $defaults['receive_date'] = $now; | |
a9c72cdb | 174 | $defaults['receive_date_time'] = $currentTime; |
6a488035 TO |
175 | |
176 | if ($defaults['id']) { | |
177 | $defaults['record_contribution'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', | |
178 | $defaults['id'], | |
179 | 'contribution_id', | |
180 | 'membership_id' | |
181 | ); | |
182 | } | |
183 | ||
184 | if (is_numeric($this->_memType)) { | |
185 | $defaults['membership_type_id'] = array(); | |
186 | $defaults['membership_type_id'][0] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
187 | $this->_memType, | |
188 | 'member_of_contact_id', | |
189 | 'id' | |
190 | ); | |
191 | $defaults['membership_type_id'][1] = $this->_memType; | |
192 | } | |
193 | else { | |
194 | $defaults['membership_type_id'] = $this->_memType; | |
195 | } | |
196 | ||
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 | ||
6a488035 TO |
217 | $renewalDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('renewal_date', $defaults), |
218 | NULL, NULL, 'Y-m-d' | |
219 | ); | |
220 | $this->assign('renewalDate', $renewalDate); | |
221 | $this->assign('member_is_test', CRM_Utils_Array::value('member_is_test', $defaults)); | |
222 | ||
223 | if ($this->_mode) { | |
224 | $fields = array(); | |
225 | ||
226 | foreach ($this->_fields as $name => $dontCare) { | |
227 | $fields[$name] = 1; | |
228 | } | |
229 | ||
230 | $names = array( | |
353ffa53 TO |
231 | 'first_name', |
232 | 'middle_name', | |
233 | 'last_name', | |
234 | "street_address-{$this->_bltID}", | |
235 | "city-{$this->_bltID}", | |
236 | "postal_code-{$this->_bltID}", | |
237 | "country_id-{$this->_bltID}", | |
6a488035 TO |
238 | "state_province_id-{$this->_bltID}", |
239 | ); | |
240 | foreach ($names as $name) { | |
241 | $fields[$name] = 1; | |
242 | } | |
243 | ||
244 | $fields["state_province-{$this->_bltID}"] = 1; | |
245 | $fields["country-{$this->_bltID}"] = 1; | |
246 | $fields["email-{$this->_bltID}"] = 1; | |
247 | $fields['email-Primary'] = 1; | |
248 | ||
249 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults); | |
250 | ||
251 | // use primary email address if billing email address is empty | |
252 | if (empty($this->_defaults["email-{$this->_bltID}"]) && | |
253 | !empty($this->_defaults['email-Primary']) | |
254 | ) { | |
255 | $defaults["email-{$this->_bltID}"] = $this->_defaults['email-Primary']; | |
256 | } | |
257 | ||
258 | foreach ($names as $name) { | |
259 | if (!empty($this->_defaults[$name])) { | |
260 | $defaults['billing_' . $name] = $this->_defaults[$name]; | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | return $defaults; | |
266 | } | |
267 | ||
268 | /** | |
fe482240 | 269 | * Build the form object. |
6a488035 | 270 | * |
355ba699 | 271 | * @return void |
6a488035 TO |
272 | */ |
273 | public function buildQuickForm() { | |
274 | if ($this->_cdType) { | |
275 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
276 | } | |
277 | ||
278 | parent::buildQuickForm(); | |
279 | ||
353ffa53 | 280 | $defaults = parent::setDefaultValues(); |
6a488035 TO |
281 | $this->_memType = $defaults['membership_type_id']; |
282 | $this->assign('customDataType', 'Membership'); | |
283 | $this->assign('customDataSubType', $this->_memType); | |
284 | $this->assign('entityID', $this->_id); | |
285 | $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -'); | |
286 | ||
287 | $allMemberships = CRM_Member_BAO_Membership::buildMembershipTypeValues($this); | |
288 | ||
289 | $allMembershipInfo = $membershipType = array(); | |
290 | ||
291 | // auto renew options if enabled for the membership | |
dbd82592 | 292 | $options = CRM_Core_SelectValues::memberAutoRenew(); |
6a488035 | 293 | |
481a74f4 | 294 | foreach ($allMemberships as $key => $values) { |
a7488080 | 295 | if (!empty($values['is_active'])) { |
6a488035 | 296 | $membershipType[$key] = CRM_Utils_Array::value('name', $values); |
8cc574cf | 297 | if ($this->_mode && empty($values['minimum_fee'])) { |
6a488035 TO |
298 | continue; |
299 | } | |
300 | else { | |
301 | $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values); | |
a7488080 | 302 | if (empty($selMemTypeOrg[$memberOfContactId])) { |
6a488035 TO |
303 | $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
304 | $memberOfContactId, | |
305 | 'display_name', | |
306 | 'id' | |
307 | ); | |
308 | ||
309 | $selOrgMemType[$memberOfContactId][0] = ts('- select -'); | |
310 | } | |
a7488080 | 311 | if (empty($selOrgMemType[$memberOfContactId][$key])) { |
6a488035 TO |
312 | $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values); |
313 | } | |
314 | } | |
315 | ||
316 | // build membership info array, which is used to set the payment information block when | |
317 | // membership type is selected. | |
318 | $allMembershipInfo[$key] = array( | |
319 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values), | |
353ffa53 | 320 | 'total_amount' => CRM_Utils_Money::format($values['minimum_fee'], NULL, '%a'), |
21dfd5f5 | 321 | 'total_amount_numeric' => CRM_Utils_Array::value('minimum_fee', $values), |
6a488035 TO |
322 | ); |
323 | ||
a7488080 | 324 | if (!empty($values['auto_renew'])) { |
6a488035 | 325 | $allMembershipInfo[$key]['auto_renew'] = $options[$values['auto_renew']]; |
b09fe5ed | 326 | } |
6a488035 TO |
327 | } |
328 | } | |
6a488035 TO |
329 | |
330 | $this->assign('allMembershipInfo', json_encode($allMembershipInfo)); | |
331 | ||
332 | if ($this->_memType) { | |
333 | $this->assign('orgName', $selMemTypeOrg[$allMemberships[$this->_memType]['member_of_contact_id']]); | |
334 | $this->assign('memType', $allMemberships[$this->_memType]['name']); | |
335 | } | |
336 | ||
337 | // force select of organization by default, if only one organization in | |
338 | // the list | |
339 | if (count($selMemTypeOrg) == 2) { | |
340 | unset($selMemTypeOrg[0], $selOrgMemType[0][0]); | |
341 | } | |
342 | //sort membership organization and type, CRM-6099 | |
343 | natcasesort($selMemTypeOrg); | |
344 | foreach ($selOrgMemType as $index => $orgMembershipType) { | |
345 | natcasesort($orgMembershipType); | |
346 | $selOrgMemType[$index] = $orgMembershipType; | |
347 | } | |
348 | ||
349 | $js = array('onChange' => "setPaymentBlock( ); CRM.buildCustomData( 'Membership', this.value );"); | |
350 | ||
351 | //build the form for auto renew. | |
352 | $recurProcessor = array(); | |
353 | if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) { | |
354 | //get the valid recurring processors. | |
355 | $recurring = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1'); | |
356 | $recurProcessor = array_intersect_assoc($this->_processors, $recurring); | |
357 | if (!empty($recurProcessor)) { | |
358 | $autoRenew = array(); | |
359 | if (!empty($membershipType)) { | |
360 | $sql = ' | |
03e04002 | 361 | SELECT id, |
6a488035 TO |
362 | auto_renew, |
363 | duration_unit, | |
364 | duration_interval | |
365 | FROM civicrm_membership_type | |
366 | WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; | |
367 | $recurMembershipTypes = CRM_Core_DAO::executeQuery($sql); | |
368 | while ($recurMembershipTypes->fetch()) { | |
369 | $autoRenew[$recurMembershipTypes->id] = $recurMembershipTypes->auto_renew; | |
370 | foreach (array( | |
353ffa53 TO |
371 | 'id', |
372 | 'auto_renew', | |
373 | 'duration_unit', | |
af9b09df | 374 | 'duration_interval', |
353ffa53 | 375 | ) as $fld) { |
6a488035 TO |
376 | $this->_recurMembershipTypes[$recurMembershipTypes->id][$fld] = $recurMembershipTypes->$fld; |
377 | } | |
378 | } | |
379 | } | |
380 | $js = array('onChange' => "setPaymentBlock(); CRM.buildCustomData( 'Membership', this.value );"); | |
381 | $this->assign('autoRenew', json_encode($autoRenew)); | |
382 | } | |
383 | $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'), | |
384 | NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );") | |
385 | ); | |
386 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
387 | $autoRenewElement->freeze(); | |
388 | } | |
389 | } | |
390 | $this->assign('recurProcessor', json_encode($recurProcessor)); | |
391 | ||
392 | $sel = &$this->addElement('hierselect', | |
393 | 'membership_type_id', | |
394 | ts('Renewal Membership Organization and Type'), $js | |
395 | ); | |
396 | ||
397 | $sel->setOptions(array($selMemTypeOrg, $selOrgMemType)); | |
398 | $elements = array(); | |
399 | if ($sel) { | |
400 | $elements[] = $sel; | |
401 | } | |
402 | ||
403 | $this->applyFilter('__ALL__', 'trim'); | |
404 | ||
405 | $this->addDate('renewal_date', ts('Date Renewal Entered'), FALSE, array('formatType' => 'activityDate')); | |
406 | ||
03e04002 | 407 | $this->add('select', 'financial_type_id', ts('Financial Type'), |
6a488035 TO |
408 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType() |
409 | ); | |
410 | if (CRM_Core_Permission::access('CiviContribute') && !$this->_mode) { | |
411 | $this->addElement('checkbox', 'record_contribution', ts('Record Renewal Payment?'), NULL, array('onclick' => "checkPayment();")); | |
412 | ||
413 | $this->add('text', 'total_amount', ts('Amount')); | |
414 | $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money'); | |
415 | ||
5d86176b | 416 | $this->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime')); |
6a488035 TO |
417 | |
418 | $this->add('text', 'num_terms', ts('Extend Membership by'), array('onchange' => "setPaymentBlock();"), TRUE); | |
419 | $this->addRule('num_terms', ts('Please enter a whole number for how many periods to renew.'), 'integer'); | |
420 | ||
421 | $this->add('select', 'payment_instrument_id', ts('Paid By'), | |
422 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), | |
423 | FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);") | |
424 | ); | |
425 | ||
426 | $this->add('text', 'trxn_id', ts('Transaction ID')); | |
427 | $this->addRule('trxn_id', ts('Transaction ID already exists in Database.'), | |
428 | 'objectExists', array('CRM_Contribute_DAO_Contribution', $this->_id, 'trxn_id') | |
429 | ); | |
430 | ||
431 | $this->add('select', 'contribution_status_id', ts('Payment Status'), | |
432 | CRM_Contribute_PseudoConstant::contributionStatus() | |
433 | ); | |
434 | ||
435 | $this->add('text', 'check_number', ts('Check Number'), | |
436 | CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number') | |
437 | ); | |
438 | } | |
439 | else { | |
440 | $this->add('text', 'total_amount', ts('Amount')); | |
441 | $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money'); | |
442 | } | |
443 | $this->addElement('checkbox', 'send_receipt', ts('Send Confirmation and Receipt?'), NULL, | |
444 | array('onclick' => "showHideByValue( 'send_receipt', '', 'notice', 'table-row', 'radio', false ); showHideByValue( 'send_receipt', '', 'fromEmail', 'table-row', 'radio',false);") | |
445 | ); | |
446 | ||
447 | $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails); | |
448 | ||
449 | $this->add('textarea', 'receipt_text_renewal', ts('Renewal Message')); | |
450 | ||
6a488035 TO |
451 | // Retrieve the name and email of the contact - this will be the TO for receipt email |
452 | list($this->_contributorDisplayName, | |
453 | $this->_contributorEmail | |
353ffa53 | 454 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); |
6a488035 TO |
455 | $this->assign('email', $this->_contributorEmail); |
456 | ||
457 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
458 | 'mailing_backend' | |
459 | ); | |
460 | $this->assign('outBound_option', $mailingInfo['outBound_option']); | |
461 | ||
462 | if (CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'contribution_recur_id')) { | |
463 | if (CRM_Member_BAO_Membership::isCancelSubscriptionSupported($this->_id)) { | |
464 | $this->assign('cancelAutoRenew', | |
465 | CRM_Utils_System::url('civicrm/contribute/unsubscribe', "reset=1&mid={$this->_id}") | |
466 | ); | |
467 | } | |
468 | } | |
469 | $this->addFormRule(array('CRM_Member_Form_MembershipRenewal', 'formRule')); | |
470 | if ($this->_context != 'standalone') { | |
471 | //CRM-10223 - allow contribution to be recorded against different contact | |
472 | // causes a conflict in standalone mode so skip in standalone for now | |
473 | $this->addElement('checkbox', 'contribution_contact', ts('Record Payment from a Different Contact?')); | |
4c7aa1f7 CW |
474 | $this->addSelect('soft_credit_type_id', array('entity' => 'contribution_soft')); |
475 | $this->addEntityRef('soft_credit_contact_id', ts('Payment From'), array('create' => TRUE)); | |
6a488035 | 476 | } |
b09fe5ed | 477 | } |
6a488035 TO |
478 | |
479 | /** | |
fe482240 | 480 | * Validation. |
6a488035 | 481 | * |
b2363ea8 TO |
482 | * @param array $params |
483 | * (ref.) an assoc array of name/value pairs. | |
6a488035 | 484 | * |
72b3a70c CW |
485 | * @return bool|array |
486 | * mixed true or array of errors | |
6a488035 | 487 | */ |
00be9182 | 488 | public static function formRule($params) { |
6a488035 TO |
489 | $errors = array(); |
490 | if ($params['membership_type_id'][0] == 0) { | |
491 | $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.'); | |
492 | } | |
493 | if ($params['membership_type_id'][1] == 0) { | |
494 | $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.'); | |
495 | } | |
496 | ||
497 | //total amount condition arise when membership type having no | |
498 | //minimum fee | |
499 | if (isset($params['record_contribution'])) { | |
500 | if (!$params['financial_type_id']) { | |
501 | $errors['financial_type_id'] = ts('Please select a Financial Type.'); | |
502 | } | |
503 | if (!$params['total_amount']) { | |
504 | $errors['total_amount'] = ts('Please enter a Contribution Amount.'); | |
505 | } | |
a7488080 | 506 | if (empty($params['payment_instrument_id'])) { |
d96cf288 DG |
507 | $errors['payment_instrument_id'] = ts('Paid By is a required field.'); |
508 | } | |
6a488035 TO |
509 | } |
510 | return empty($errors) ? TRUE : $errors; | |
511 | } | |
512 | ||
513 | /** | |
fe482240 | 514 | * Process the renewal form. |
6a488035 | 515 | * |
6a488035 | 516 | * |
355ba699 | 517 | * @return void |
6a488035 TO |
518 | */ |
519 | public function postProcess() { | |
520 | ||
521 | $ids = array(); | |
522 | $config = CRM_Core_Config::singleton(); | |
523 | ||
524 | // get the submitted form values. | |
525 | $this->_params = $formValues = $this->controller->exportValues($this->_name); | |
526 | ||
527 | $this->storeContactFields($formValues); | |
528 | // use values from screen | |
529 | ||
530 | if ($formValues['membership_type_id'][1] <> 0) { | |
531 | $defaults['receipt_text_renewal'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
532 | $formValues['membership_type_id'][1], | |
533 | 'receipt_text_renewal' | |
534 | ); | |
535 | } | |
536 | ||
481a74f4 | 537 | $now = CRM_Utils_Date::getToday(NULL, 'YmdHis'); |
5d86176b | 538 | $this->convertDateFieldsToMySQL($formValues); |
6a488035 TO |
539 | $this->assign('receive_date', $formValues['receive_date']); |
540 | ||
a7488080 | 541 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
542 | $formValues['receipt_date'] = $now; |
543 | $this->assign('receipt_date', CRM_Utils_Date::mysqlToIso($formValues['receipt_date'])); | |
544 | } | |
545 | else { | |
546 | $formValues['receipt_date'] = NULL; | |
547 | } | |
548 | ||
549 | if ($this->_mode) { | |
550 | $formValues['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_params, CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
353ffa53 TO |
551 | $this->_memType, 'minimum_fee' |
552 | )); | |
a7488080 | 553 | if (empty($formValues['financial_type_id'])) { |
b09fe5ed | 554 | $formValues['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'financial_type_id'); |
6a488035 TO |
555 | } |
556 | ||
557 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($formValues['payment_processor_id'], | |
558 | $this->_mode | |
559 | ); | |
03e04002 | 560 | |
6a488035 TO |
561 | $fields = array(); |
562 | ||
563 | // set email for primary location. | |
564 | $fields['email-Primary'] = 1; | |
565 | $formValues['email-5'] = $formValues['email-Primary'] = $this->_contributorEmail; | |
566 | $formValues['register_date'] = $now; | |
567 | ||
568 | // now set the values for the billing location. | |
569 | foreach ($this->_fields as $name => $dontCare) { | |
570 | $fields[$name] = 1; | |
571 | } | |
572 | ||
573 | // also add location name to the array | |
574 | $formValues["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $formValues) . ' ' . CRM_Utils_Array::value('billing_middle_name', $formValues) . ' ' . CRM_Utils_Array::value('billing_last_name', $formValues); | |
575 | ||
576 | $formValues["address_name-{$this->_bltID}"] = trim($formValues["address_name-{$this->_bltID}"]); | |
577 | ||
578 | $fields["address_name-{$this->_bltID}"] = 1; | |
579 | ||
580 | $fields["email-{$this->_bltID}"] = 1; | |
581 | ||
582 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type'); | |
583 | ||
584 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
585 | ||
586 | foreach ($nameFields as $name) { | |
587 | $fields[$name] = 1; | |
588 | if (array_key_exists("billing_$name", $formValues)) { | |
589 | $formValues[$name] = $formValues["billing_{$name}"]; | |
590 | $formValues['preserveDBName'] = TRUE; | |
591 | } | |
592 | } | |
593 | ||
594 | //here we are setting up the billing contact - if different from the member they are already created | |
595 | // but they will get billing details assigned | |
596 | CRM_Contact_BAO_Contact::createProfileContact($formValues, $fields, | |
597 | $this->_contributorContactID, NULL, NULL, $ctype | |
598 | ); | |
599 | ||
600 | // add all the additional payment params we need | |
601 | $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); | |
602 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
603 | ||
604 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
605 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
cafb3b7b | 606 | $this->_params['description'] = ts('Office Credit Card Membership Renewal Contribution'); |
6a488035 TO |
607 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); |
608 | $this->_params['amount'] = $formValues['total_amount']; | |
609 | $this->_params['currencyID'] = $config->defaultCurrency; | |
610 | $this->_params['payment_action'] = 'Sale'; | |
611 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
612 | ||
613 | // at this point we've created a contact and stored its address etc | |
614 | // all the payment processors expect the name and address to be in the passed params | |
615 | // so we copy stuff over to first_name etc. | |
616 | $paymentParams = $this->_params; | |
a7488080 | 617 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
618 | $paymentParams['email'] = $this->_contributorEmail; |
619 | } | |
620 | ||
621 | $paymentParams['contactID'] = $this->_contributorContactID; | |
6a488035 TO |
622 | |
623 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); | |
624 | ||
625 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); | |
626 | ||
627 | $result = &$payment->doDirectPayment($paymentParams); | |
628 | ||
629 | if (is_a($result, 'CRM_Core_Error')) { | |
630 | CRM_Core_Error::displaySessionError($result); | |
631 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', | |
353ffa53 TO |
632 | "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership&mode={$this->_mode}" |
633 | )); | |
6a488035 TO |
634 | } |
635 | ||
636 | if ($result) { | |
637 | $this->_params = array_merge($this->_params, $result); | |
638 | } | |
639 | $formValues['contribution_status_id'] = 1; | |
640 | $formValues['invoice_id'] = $this->_params['invoiceID']; | |
641 | $formValues['trxn_id'] = $result['trxn_id']; | |
642 | $formValues['payment_instrument_id'] = 1; | |
643 | $formValues['is_test'] = ($this->_mode == 'live') ? 0 : 1; | |
644 | $this->set('params', $this->_params); | |
645 | $this->assign('trxn_id', $result['trxn_id']); | |
646 | } | |
647 | ||
648 | $renewalDate = NULL; | |
649 | ||
650 | if ($formValues['renewal_date']) { | |
651 | $this->set('renewalDate', CRM_Utils_Date::processDate($formValues['renewal_date'])); | |
652 | } | |
653 | $this->_membershipId = $this->_id; | |
654 | ||
655 | // membership type custom data | |
656 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, | |
657 | $formValues['membership_type_id'][1] | |
658 | ); | |
659 | ||
660 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, | |
661 | CRM_Core_BAO_CustomField::getFields('Membership', | |
662 | FALSE, FALSE, | |
663 | NULL, NULL, TRUE | |
664 | ) | |
665 | ); | |
666 | ||
667 | $customFieldsFormatted = CRM_Core_BAO_CustomField::postProcess($formValues, | |
668 | $customFields, | |
669 | $this->_id, | |
670 | 'Membership' | |
671 | ); | |
672 | ||
673 | // check for test membership. | |
674 | $isTestMembership = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_membershipId, 'is_test'); | |
675 | ||
676 | // chk for renewal for multiple terms CRM-8750 | |
677 | $numRenewTerms = 1; | |
678 | if (is_numeric(CRM_Utils_Array::value('num_terms', $formValues))) { | |
679 | $numRenewTerms = $formValues['num_terms']; | |
680 | } | |
681 | ||
682 | //if contribution status is pending then set pay later | |
683 | if ($formValues['contribution_status_id'] == array_search('Pending', CRM_Contribute_PseudoConstant::contributionStatus())) { | |
684 | $this->_params['is_pay_later'] = 1; | |
685 | } | |
c98997b9 | 686 | $renewMembership = CRM_Member_BAO_Membership::renewMembershipFormWrapper($this->_contactID, |
6a488035 TO |
687 | $formValues['membership_type_id'][1], |
688 | $isTestMembership, $this, NULL, NULL, | |
c98997b9 EM |
689 | $customFieldsFormatted, $numRenewTerms, |
690 | $this->_membershipId | |
6a488035 TO |
691 | ); |
692 | ||
693 | $endDate = CRM_Utils_Date::processDate($renewMembership->end_date); | |
694 | ||
695 | // Retrieve the name and email of the current user - this will be the FROM for the receipt email | |
696 | $session = CRM_Core_Session::singleton(); | |
697 | $userID = $session->get('userID'); | |
698 | ||
699 | list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
700 | ||
701 | $memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $renewMembership->membership_type_id, 'name'); | |
702 | ||
a7488080 | 703 | if (!empty($formValues['record_contribution']) || $this->_mode) { |
6a488035 TO |
704 | // set the source |
705 | $formValues['contribution_source'] = "{$memType} Membership: Offline membership renewal (by {$userName})"; | |
03e04002 | 706 | |
6a488035 TO |
707 | //create line items |
708 | $lineItem = array(); | |
b09fe5ed | 709 | $priceSetId = NULL; |
6a488035 | 710 | CRM_Member_BAO_Membership::createLineItems($this, $formValues['membership_type_id'], $priceSetId); |
9da8dc8c | 711 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], |
6a488035 TO |
712 | $this->_params, $lineItem[$priceSetId] |
713 | ); | |
03e04002 | 714 | //CRM-11529 for quick config backoffice transactions |
715 | //when financial_type_id is passed in form, update the | |
1f676e92 | 716 | //line items with the financial type selected in form |
6a488035 TO |
717 | if ($submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $formValues)) { |
718 | foreach ($lineItem[$priceSetId] as &$li) { | |
719 | $li['financial_type_id'] = $submittedFinancialType; | |
720 | } | |
721 | } | |
722 | $formValues['total_amount'] = CRM_Utils_Array::value('amount', $this->_params); | |
723 | if (!empty($lineItem)) { | |
724 | $formValues['lineItems'] = $lineItem; | |
725 | $formValues['processPriceSet'] = TRUE; | |
726 | } | |
727 | ||
728 | //assign contribution contact id to the field expected by recordMembershipContribution | |
9b873358 | 729 | if ($this->_contributorContactID != $this->_contactID) { |
91ef9be0 | 730 | $formValues['contribution_contact_id'] = $this->_contributorContactID; |
9b873358 | 731 | if (!empty($this->_params['soft_credit_type_id'])) { |
d80dbc14 | 732 | $formValues['soft_credit'] = array( |
133e2c99 | 733 | 'soft_credit_type_id' => $this->_params['soft_credit_type_id'], |
91ef9be0 | 734 | 'contact_id' => $this->_contactID, |
133e2c99 | 735 | ); |
6a488035 TO |
736 | } |
737 | } | |
738 | $formValues['contact_id'] = $this->_contactID; | |
1acc24d5 EM |
739 | //recordMembershipContribution receives params as a reference & adds one variable. This is |
740 | // not a great pattern & ideally it would not receive as a reference. We assign our params as a | |
741 | // temporary variable to avoid e-notice & to make it clear to future refactorer that | |
742 | // this function is NOT reliant on that var being set | |
743 | $temporaryParams = array_merge($formValues, array('membership_id' => $renewMembership->id)); | |
744 | CRM_Member_BAO_Membership::recordMembershipContribution($temporaryParams); | |
6a488035 TO |
745 | } |
746 | ||
6a488035 | 747 | $receiptSend = FALSE; |
a7488080 | 748 | if (!empty($formValues['send_receipt'])) { |
6a488035 TO |
749 | $receiptSend = TRUE; |
750 | ||
751 | $receiptFrom = $formValues['from_email_address']; | |
752 | ||
a7488080 | 753 | if (!empty($formValues['payment_instrument_id'])) { |
6a488035 TO |
754 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); |
755 | $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']]; | |
756 | } | |
757 | //get the group Tree | |
758 | $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree('Membership', $this, $this->_id, FALSE, $this->_memType); | |
759 | ||
760 | // retrieve custom data | |
761 | $customFields = $customValues = $fo = array(); | |
762 | foreach ($this->_groupTree as $groupID => $group) { | |
763 | if ($groupID == 'info') { | |
764 | continue; | |
765 | } | |
766 | foreach ($group['fields'] as $k => $field) { | |
767 | $field['title'] = $field['label']; | |
768 | $customFields["custom_{$k}"] = $field; | |
769 | } | |
770 | } | |
771 | $members = array(array('member_id', '=', $this->_membershipId, 0, 0)); | |
772 | // check whether its a test drive | |
773 | if ($this->_mode == 'test') { | |
774 | $members[] = array('member_test', '=', 1, 0, 0); | |
775 | } | |
776 | CRM_Core_BAO_UFGroup::getValues($this->_contactID, $customFields, $customValues, FALSE, $members); | |
777 | ||
778 | $this->assign_by_ref('formValues', $formValues); | |
a7488080 | 779 | if (!empty($formValues['contribution_id'])) { |
6a488035 TO |
780 | $this->assign('contributionID', $formValues['contribution_id']); |
781 | } | |
782 | $this->assign('membershipID', $this->_id); | |
783 | $this->assign('contactID', $this->_contactID); | |
784 | $this->assign('module', 'Membership'); | |
785 | $this->assign('receiptType', 'membership renewal'); | |
786 | $this->assign('mem_start_date', CRM_Utils_Date::customFormat($renewMembership->start_date)); | |
787 | $this->assign('mem_end_date', CRM_Utils_Date::customFormat($renewMembership->end_date)); | |
788 | $this->assign('membership_name', CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
353ffa53 TO |
789 | $renewMembership->membership_type_id |
790 | )); | |
6a488035 TO |
791 | $this->assign('customValues', $customValues); |
792 | if ($this->_mode) { | |
a7488080 | 793 | if (!empty($this->_params['billing_first_name'])) { |
6a488035 TO |
794 | $name = $this->_params['billing_first_name']; |
795 | } | |
796 | ||
a7488080 | 797 | if (!empty($this->_params['billing_middle_name'])) { |
6a488035 TO |
798 | $name .= " {$this->_params['billing_middle_name']}"; |
799 | } | |
800 | ||
a7488080 | 801 | if (!empty($this->_params['billing_last_name'])) { |
6a488035 TO |
802 | $name .= " {$this->_params['billing_last_name']}"; |
803 | } | |
804 | $this->assign('billingName', $name); | |
805 | ||
806 | // assign the address formatted up for display | |
807 | $addressParts = array( | |
808 | "street_address-{$this->_bltID}", | |
809 | "city-{$this->_bltID}", | |
810 | "postal_code-{$this->_bltID}", | |
811 | "state_province-{$this->_bltID}", | |
812 | "country-{$this->_bltID}", | |
813 | ); | |
814 | $addressFields = array(); | |
815 | foreach ($addressParts as $part) { | |
816 | list($n, $id) = explode('-', $part); | |
817 | if (isset($this->_params['billing_' . $part])) { | |
818 | $addressFields[$n] = $this->_params['billing_' . $part]; | |
819 | } | |
820 | } | |
821 | $this->assign('address', CRM_Utils_Address::format($addressFields)); | |
822 | $date = CRM_Utils_Date::format($this->_params['credit_card_exp_date']); | |
823 | $date = CRM_Utils_Date::mysqlToIso($date); | |
824 | $this->assign('credit_card_exp_date', $date); | |
825 | $this->assign('credit_card_number', | |
826 | CRM_Utils_System::mungeCreditCard($this->_params['credit_card_number']) | |
827 | ); | |
828 | $this->assign('credit_card_type', $this->_params['credit_card_type']); | |
829 | $this->assign('contributeMode', 'direct'); | |
830 | $this->assign('isAmountzero', 0); | |
831 | $this->assign('is_pay_later', 0); | |
832 | $this->assign('isPrimary', 1); | |
833 | if ($this->_mode == 'test') { | |
834 | $this->assign('action', '1024'); | |
835 | } | |
836 | } | |
837 | ||
c6327d7d | 838 | list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
839 | array( |
840 | 'groupName' => 'msg_tpl_workflow_membership', | |
841 | 'valueName' => 'membership_offline_receipt', | |
842 | 'contactId' => $this->_receiptContactId, | |
843 | 'from' => $receiptFrom, | |
844 | 'toName' => $this->_contributorDisplayName, | |
845 | 'toEmail' => $this->_contributorEmail, | |
846 | 'isTest' => $this->_mode == 'test', | |
847 | ) | |
848 | ); | |
849 | } | |
850 | ||
851 | $statusMsg = ts('%1 membership for %2 has been renewed.', array(1 => $memType, 2 => $this->_memberDisplayName)); | |
852 | ||
853 | if ($endDate) { | |
b09fe5ed | 854 | $statusMsg .= ' ' . ts('The new membership End Date is %1.', array(1 => CRM_Utils_Date::customFormat(substr($endDate, 0, 8)))); |
6a488035 TO |
855 | } |
856 | ||
857 | if ($receiptSend && $mailSend) { | |
858 | $statusMsg .= ' ' . ts('A renewal confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail)); | |
859 | } | |
860 | ||
861 | CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success'); | |
862 | } | |
96025800 | 863 | |
6a488035 | 864 | } |