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