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