Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for offline membership form | |
38 | * | |
39 | */ | |
40 | class CRM_Member_Form_Membership extends CRM_Member_Form { | |
41 | ||
42 | protected $_memType = NULL; | |
43 | ||
44 | protected $_onlinePendingContributionId; | |
45 | ||
46 | public $_mode; | |
47 | ||
48 | public $_contributeMode = 'direct'; | |
49 | ||
50 | protected $_recurMembershipTypes; | |
51 | ||
52 | protected $_memTypeSelected; | |
53 | ||
54 | /* | |
55 | * Display name of the member | |
56 | */ | |
57 | protected $_memberDisplayName = null; | |
58 | ||
59 | /* | |
60 | * email of the person paying for the membership (used for receipts) | |
61 | */ | |
62 | protected $_memberEmail = null; | |
63 | ||
64 | /* | |
65 | * Contact ID of the member | |
66 | */ | |
67 | protected $_contactID = null; | |
68 | ||
69 | /* | |
70 | * Display name of the person paying for the membership (used for receipts) | |
71 | */ | |
72 | protected $_contributorDisplayName = null; | |
73 | ||
74 | /* | |
75 | * email of the person paying for the membership (used for receipts) | |
76 | */ | |
77 | protected $_contributorEmail = null; | |
78 | ||
79 | /* | |
80 | * email of the person paying for the membership (used for receipts) | |
81 | */ | |
82 | protected $_contributorContactID = null; | |
83 | ||
84 | /* | |
85 | * ID of the person the receipt is to go to | |
86 | */ | |
87 | protected $_receiptContactId = null; | |
88 | ||
89 | /* | |
90 | * Keep a class variable for ALL membeshipID's so | |
91 | * postProcess hook function can do something with it | |
92 | */ | |
93 | protected $_membershipIDs = array( ); | |
94 | ||
95 | public function preProcess() { | |
96 | //custom data related code | |
97 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
98 | $this->assign('cdType', FALSE); | |
99 | if ($this->_cdType) { | |
100 | $this->assign('cdType', TRUE); | |
101 | return CRM_Custom_Form_CustomData::preProcess($this); | |
102 | } | |
103 | ||
104 | // get price set id. | |
105 | $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET); | |
106 | $this->set('priceSetId', $this->_priceSetId); | |
107 | $this->assign('priceSetId', $this->_priceSetId); | |
108 | ||
109 | // action | |
110 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
111 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
112 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
113 | $this->_processors = array(); | |
114 | ||
115 | ||
116 | // check for edit permission | |
117 | if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) { | |
118 | CRM_Core_Error::fatal(ts('You do not have permission to access this page')); | |
119 | } | |
120 | ||
121 | if ($this->_action & CRM_Core_Action::DELETE) { | |
122 | $contributionID = CRM_Member_BAO_Membership::getMembershipContributionId($this->_id); | |
123 | // check delete permission for contribution | |
124 | if ($this->_id && $contributionID && !CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { | |
125 | CRM_Core_Error::fatal(ts("This Membership is linked to a contribution. You must have 'delete in CiviContribute' permission in order to delete this record.")); | |
126 | } | |
127 | } | |
128 | ||
129 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
130 | $this->assign('context', $this->_context); | |
131 | ||
132 | if ($this->_id) { | |
133 | $this->_memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'membership_type_id'); | |
134 | $this->_membershipIDs[] = $this->_id; | |
135 | } | |
136 | ||
137 | $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); | |
138 | $this->assign('membershipMode', $this->_mode); | |
139 | ||
140 | if ($this->_mode) { | |
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 | |
b2b0530a | 170 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
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 | ||
189 | if ($this->_action & CRM_Core_Action::ADD) { | |
190 | //check whether any active membership statuses are available - redirects back to contact summary if not | |
a8d4ff25 | 191 | CRM_Member_BAO_Membership::statusAvailabilty($this->_contactID); |
6a488035 TO |
192 | |
193 | if ($this->_contactID) { | |
194 | //check whether contact has a current membership so we can alert user that they may want to do a renewal instead | |
4256ea25 AH |
195 | $contactMemberships = array(); |
196 | $memParams = array('contact_id' => $this->_contactID); | |
197 | CRM_Member_BAO_Membership::getValues($memParams, $contactMemberships, TRUE); | |
198 | $cMemTypes = array(); | |
199 | foreach ($contactMemberships as $mem) { | |
200 | $cMemTypes[] = $mem['membership_type_id']; | |
201 | } | |
202 | if (count($cMemTypes) > 0) { | |
203 | $memberorgs = CRM_Member_BAO_MembershipType::getMemberOfContactByMemTypes($cMemTypes); | |
204 | $mems_by_org = array(); | |
205 | foreach ($contactMemberships as $memid => $mem) { | |
206 | $mem['member_of_contact_id'] = CRM_Utils_Array::value($mem['membership_type_id'], $memberorgs); | |
207 | if (CRM_Utils_Array::value('membership_end_date', $mem)) { | |
208 | $mem['membership_end_date'] = CRM_Utils_Date::customformat($mem['membership_end_date']); | |
209 | } | |
210 | $mem['membership_type'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
211 | $mem['membership_type_id'], | |
212 | 'name', 'id' | |
6a488035 | 213 | ); |
4256ea25 AH |
214 | $mem['membership_status'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', |
215 | $mem['status_id'], | |
216 | 'label', 'id' | |
6a488035 | 217 | ); |
4256ea25 AH |
218 | if ($this->_mode) { |
219 | $mem['renewUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', | |
220 | "reset=1&action=renew&cid={$this->_contactID}&id={$mem['id']}&context=membership&selectedChild=member&mode=live" | |
221 | ); | |
222 | } | |
223 | else { | |
224 | $mem['renewUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', | |
225 | "reset=1&action=renew&cid={$this->_contactID}&id={$mem['id']}&context=membership&selectedChild=member" | |
226 | ); | |
227 | } | |
228 | $mem['membershipTab'] = CRM_Utils_System::url('civicrm/contact/view', | |
229 | "reset=1&force=1&cid={$this->_contactID}&selectedChild=member" | |
230 | ); | |
231 | $mems_by_org[$mem['member_of_contact_id']] = $mem; | |
6a488035 | 232 | } |
4256ea25 AH |
233 | $resources = CRM_Core_Resources::singleton(); |
234 | $resources->addSetting(array('existingMems' => array('memberorgs' => $mems_by_org))); | |
235 | $resources->addScriptFile('civicrm', 'templates/CRM/Member/Form/Membership.js'); | |
6a488035 TO |
236 | } |
237 | } | |
1001e556 | 238 | else { |
d292601b AH |
239 | $resources = CRM_Core_Resources::singleton(); |
240 | $resources->addScriptFile('civicrm', 'templates/CRM/Member/Form/MembershipStandalone.js'); | |
241 | $statuses = array(); | |
242 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
243 | $membershipStatus->is_current_member = 1; | |
244 | $membershipStatus->find(); | |
245 | $membershipStatus->selectAdd(); | |
246 | $membershipStatus->selectAdd('id'); | |
247 | while ($membershipStatus->fetch()) { | |
248 | $statuses[$membershipStatus->id] = $membershipStatus->label; | |
249 | } | |
250 | $membershipStatus->free(); | |
251 | $passthru = array( | |
252 | 'typeorgs' => CRM_Member_BAO_MembershipType::getMembershipTypeOrganization(), | |
253 | 'memtypes' => CRM_Member_BAO_MembershipType::getMembershipTypes(false), | |
254 | 'statuses' => $statuses, | |
255 | ); | |
256 | $resources->addSetting(array('existingMems' => $passthru)); | |
257 | } | |
6a488035 TO |
258 | } |
259 | ||
260 | // when custom data is included in this page | |
261 | if (CRM_Utils_Array::value('hidden_custom', $_POST)) { | |
262 | CRM_Custom_Form_CustomData::preProcess($this); | |
263 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
264 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
265 | } | |
266 | ||
267 | // CRM-4395, get the online pending contribution id. | |
268 | $this->_onlinePendingContributionId = NULL; | |
269 | if (!$this->_mode && $this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { | |
270 | $this->_onlinePendingContributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($this->_id, | |
271 | 'Membership' | |
272 | ); | |
273 | } | |
274 | $this->assign('onlinePendingContributionId', $this->_onlinePendingContributionId); | |
275 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); | |
276 | ||
277 | // Set title | |
278 | if ($this->_contactID) { | |
279 | $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactID); | |
280 | ||
281 | // Check if this is default domain contact CRM-10482 | |
282 | if (CRM_Contact_BAO_Contact::checkDomainContact($this->_contactID)) { | |
283 | $displayName .= ' (' . ts('default organization') . ')'; | |
284 | } | |
285 | ||
286 | // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container | |
287 | CRM_Utils_System::setTitle(ts('Membership for') . ' ' . $displayName); | |
288 | } | |
289 | parent::preProcess(); | |
290 | } | |
291 | ||
292 | /** | |
293 | * This function sets the default values for the form. MobileProvider that in edit/view mode | |
294 | * the default values are retrieved from the database | |
295 | * | |
296 | * @access public | |
297 | * | |
298 | * @return None | |
299 | */ | |
300 | public function setDefaultValues() { | |
301 | if ($this->_cdType) { | |
302 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
303 | } | |
304 | ||
305 | if ($this->_priceSetId) { | |
306 | return CRM_Price_BAO_Set::setDefaultPriceSet($this, $defaults); | |
307 | } | |
308 | ||
309 | $defaults = parent::setDefaultValues(); | |
310 | ||
311 | //setting default join date and receive date | |
312 | list($now) = CRM_Utils_Date::setDateDefaults(); | |
313 | if ($this->_action == CRM_Core_Action::ADD) { | |
314 | $defaults['receive_date'] = $now; | |
315 | } | |
316 | ||
317 | if (is_numeric($this->_memType)) { | |
318 | $defaults['membership_type_id'] = array(); | |
319 | $defaults['membership_type_id'][0] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
320 | $this->_memType, | |
321 | 'member_of_contact_id', | |
322 | 'id' | |
323 | ); | |
324 | $defaults['membership_type_id'][1] = $this->_memType; | |
325 | } | |
326 | else { | |
327 | $defaults['membership_type_id'] = $this->_memType; | |
328 | } | |
329 | ||
330 | $defaults['num_terms'] = 1; | |
331 | ||
332 | if (CRM_Utils_Array::value('id', $defaults)) { | |
333 | if ($this->_onlinePendingContributionId) { | |
334 | $defaults['record_contribution'] = $this->_onlinePendingContributionId; | |
335 | } | |
336 | else { | |
337 | $contributionId = CRM_Core_DAO::singleValueQuery(" | |
338 | SELECT contribution_id | |
339 | FROM civicrm_membership_payment | |
340 | WHERE membership_id = $this->_id | |
341 | ORDER BY contribution_id | |
342 | DESC limit 1"); | |
343 | ||
344 | if ($contributionId) { | |
345 | $defaults['record_contribution'] = $contributionId; | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | if (CRM_Utils_Array::value('record_contribution', $defaults) && !$this->_mode) { | |
351 | $contributionParams = array('id' => $defaults['record_contribution']); | |
352 | $contributionIds = array(); | |
353 | ||
354 | //keep main object campaign in hand. | |
355 | $memberCampaignId = CRM_Utils_Array::value('campaign_id', $defaults); | |
356 | ||
357 | CRM_Contribute_BAO_Contribution::getValues($contributionParams, $defaults, $contributionIds); | |
358 | ||
359 | //get back original object campaign id. | |
360 | $defaults['campaign_id'] = $memberCampaignId; | |
361 | ||
362 | if (CRM_Utils_Array::value('receive_date', $defaults)) { | |
363 | list($defaults['receive_date']) = CRM_Utils_Date::setDateDefaults($defaults['receive_date']); | |
364 | } | |
365 | ||
366 | // Contribution::getValues() over-writes the membership record's source field value - so we need to restore it. | |
367 | if (CRM_Utils_Array::value('membership_source', $defaults)) { | |
368 | $defaults['source'] = $defaults['membership_source']; | |
369 | } | |
370 | } | |
371 | ||
372 | // User must explicitly choose to send a receipt in both add and update mode. | |
373 | $defaults['send_receipt'] = 0; | |
374 | ||
375 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
376 | // in this mode by default uncheck this checkbox | |
377 | unset($defaults['record_contribution']); | |
378 | } | |
379 | ||
380 | if (CRM_Utils_Array::value('id', $defaults)) { | |
381 | $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled($this->_id); | |
382 | } | |
383 | ||
384 | $alreadyAutoRenew = FALSE; | |
385 | if (CRM_Utils_Array::value('contribution_recur_id', $defaults) && !$subscriptionCancelled) { | |
386 | $defaults['auto_renew'] = 1; | |
387 | $alreadyAutoRenew = TRUE; | |
388 | } | |
389 | $this->assign('alreadyAutoRenew', $alreadyAutoRenew); | |
390 | ||
391 | $this->assign('member_is_test', CRM_Utils_Array::value('member_is_test', $defaults)); | |
392 | ||
393 | $this->assign('membership_status_id', CRM_Utils_Array::value('status_id', $defaults)); | |
394 | ||
395 | if (CRM_Utils_Array::value('is_pay_later', $defaults)) { | |
396 | $this->assign('is_pay_later', TRUE); | |
397 | } | |
398 | if ($this->_mode) { | |
399 | $fields = array(); | |
400 | ||
401 | foreach ($this->_fields as $name => $dontCare) { | |
402 | $fields[$name] = 1; | |
403 | } | |
404 | $names = array( | |
405 | 'first_name', 'middle_name', 'last_name', "street_address-{$this->_bltID}", | |
406 | "city-{$this->_bltID}", "postal_code-{$this->_bltID}", "country_id-{$this->_bltID}", | |
407 | "state_province_id-{$this->_bltID}", | |
408 | ); | |
409 | foreach ($names as $name) { | |
410 | $fields[$name] = 1; | |
411 | } | |
412 | $fields["state_province-{$this->_bltID}"] = 1; | |
413 | $fields["country-{$this->_bltID}"] = 1; | |
414 | $fields["email-{$this->_bltID}"] = 1; | |
415 | $fields['email-Primary'] = 1; | |
416 | ||
417 | if ($this->_contactID) { | |
418 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults); | |
419 | } | |
420 | ||
421 | // use primary email address if billing email address is empty | |
422 | if (empty($this->_defaults["email-{$this->_bltID}"]) && | |
423 | !empty($this->_defaults['email-Primary']) | |
424 | ) { | |
425 | $defaults["email-{$this->_bltID}"] = $this->_defaults['email-Primary']; | |
426 | } | |
427 | ||
428 | foreach ($names as $name) { | |
429 | if (!empty($this->_defaults[$name])) { | |
430 | $defaults['billing_' . $name] = $this->_defaults[$name]; | |
431 | } | |
432 | } | |
433 | ||
434 | // // hack to simplify credit card entry for testing | |
435 | // $defaults['credit_card_type'] = 'Visa'; | |
436 | // $defaults['credit_card_number'] = '4807731747657838'; | |
437 | // $defaults['cvv2'] = '000'; | |
438 | // $defaults['credit_card_exp_date'] = array( 'Y' => '2012', 'M' => '05' ); | |
439 | } | |
440 | ||
441 | $dates = array('join_date', 'start_date', 'end_date'); | |
442 | foreach ($dates as $key) { | |
443 | if (CRM_Utils_Array::value($key, $defaults)) { | |
444 | list($defaults[$key]) = CRM_Utils_Date::setDateDefaults(CRM_Utils_Array::value($key, $defaults)); | |
445 | } | |
446 | } | |
447 | ||
448 | //setting default join date if there is no join date | |
449 | if (!CRM_Utils_Array::value('join_date', $defaults)) { | |
450 | $defaults['join_date'] = $now; | |
451 | } | |
452 | ||
453 | if (CRM_Utils_Array::value('membership_end_date', $defaults)) { | |
454 | $this->assign('endDate', $defaults['membership_end_date']); | |
455 | } | |
456 | ||
457 | return $defaults; | |
458 | } | |
459 | ||
460 | /** | |
461 | * Function to build the form | |
462 | * | |
463 | * @return None | |
464 | * @access public | |
465 | */ | |
466 | public function buildQuickForm() { | |
467 | if ($this->_cdType) { | |
468 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
469 | } | |
470 | ||
471 | // build price set form. | |
472 | $buildPriceSet = FALSE; | |
473 | if ($this->_priceSetId || CRM_Utils_Array::value('price_set_id', $_POST)) { | |
474 | if (CRM_Utils_Array::value('price_set_id', $_POST)) { | |
475 | $buildPriceSet = TRUE; | |
476 | } | |
477 | $getOnlyPriceSetElements = TRUE; | |
478 | if (!$this->_priceSetId) { | |
479 | $this->_priceSetId = $_POST['price_set_id']; | |
480 | $getOnlyPriceSetElements = FALSE; | |
481 | } | |
482 | ||
483 | $this->set('priceSetId', $this->_priceSetId); | |
484 | CRM_Price_BAO_Set::buildPriceSet($this); | |
485 | ||
486 | $optionsMembershipTypes = array(); | |
487 | foreach ($this->_priceSet['fields'] as $pField) { | |
488 | if (empty($pField['options'])) { | |
489 | continue; | |
490 | } | |
491 | foreach ($pField['options'] as $opId => $opValues) { | |
492 | $optionsMembershipTypes[$opId] = CRM_Utils_Array::value('membership_type_id', $opValues, 0); | |
493 | } | |
494 | } | |
495 | ||
496 | $this->assign('autoRenewOption', CRM_Price_BAO_Set::checkAutoRenewForPriceSet($this->_priceSetId)); | |
497 | ||
498 | $this->assign('optionsMembershipTypes', $optionsMembershipTypes); | |
5a9c4d4a | 499 | $this->assign('contributionType', CRM_Utils_Array::value('financial_type_id', $this->_priceSet)); |
6a488035 TO |
500 | |
501 | // get only price set form elements. | |
502 | if ($getOnlyPriceSetElements) { | |
503 | return; | |
504 | } | |
505 | } | |
506 | ||
507 | // use to build form during form rule. | |
508 | $this->assign('buildPriceSet', $buildPriceSet); | |
509 | ||
510 | if ($this->_action & CRM_Core_Action::ADD) { | |
511 | $buildPriceSet = FALSE; | |
512 | $priceSets = CRM_Price_BAO_Set::getAssoc(FALSE, 'CiviMember'); | |
513 | if (!empty($priceSets)) { | |
514 | $buildPriceSet = TRUE; | |
515 | } | |
516 | ||
517 | if ($buildPriceSet) { | |
518 | $this->add('select', 'price_set_id', ts('Choose price set'), | |
519 | array( | |
520 | '' => ts('Choose price set')) + $priceSets, | |
521 | NULL, array('onchange' => "buildAmount( this.value );") | |
522 | ); | |
523 | } | |
524 | $this->assign('hasPriceSets', $buildPriceSet); | |
525 | } | |
526 | ||
527 | //need to assign custom data type and subtype to the template | |
528 | $this->assign('customDataType', 'Membership'); | |
529 | $this->assign('customDataSubType', $this->_memType); | |
530 | $this->assign('entityID', $this->_id); | |
531 | ||
532 | if ($this->_action & CRM_Core_Action::DELETE) { | |
533 | $this->addButtons(array( | |
534 | array( | |
535 | 'type' => 'next', | |
536 | 'name' => ts('Delete'), | |
537 | 'spacing' => ' ', | |
538 | 'isDefault' => TRUE, | |
539 | ), | |
540 | array( | |
541 | 'type' => 'cancel', | |
542 | 'name' => ts('Cancel'), | |
543 | ), | |
544 | ) | |
545 | ); | |
546 | return; | |
547 | } | |
548 | ||
549 | if ($this->_context == 'standalone') { | |
550 | CRM_Contact_Form_NewContact::buildQuickForm($this); | |
551 | } | |
552 | ||
553 | $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -'); | |
554 | ||
555 | $dao = new CRM_Member_DAO_MembershipType(); | |
556 | $dao->domain_id = CRM_Core_Config::domainID(); | |
557 | $dao->find(); | |
558 | ||
559 | // retrieve all memberships | |
560 | $allMemberships = CRM_Member_BAO_Membership::buildMembershipTypeValues($this); | |
561 | ||
562 | $allMembershipInfo = $membershipType = array(); | |
563 | foreach( $allMemberships as $key => $values ) { | |
564 | if (CRM_Utils_Array::value('is_active', $values) ) { | |
565 | $membershipType[$key] = CRM_Utils_Array::value('name', $values); | |
566 | if ($this->_mode && !CRM_Utils_Array::value('minimum_fee', $values)) { | |
567 | continue; | |
568 | } | |
569 | else { | |
570 | $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values); | |
571 | if (!CRM_Utils_Array::value($memberOfContactId, $selMemTypeOrg)) { | |
572 | $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
573 | $memberOfContactId, | |
574 | 'display_name', | |
575 | 'id' | |
576 | ); | |
577 | ||
578 | $selOrgMemType[$memberOfContactId][0] = ts('- select -'); | |
579 | } | |
580 | if (!CRM_Utils_Array::value($key, $selOrgMemType[$memberOfContactId])) { | |
581 | $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values); | |
582 | } | |
583 | } | |
584 | ||
585 | // build membership info array, which is used when membership type is selected to: | |
586 | // - set the payment information block | |
587 | // - set the max related block | |
588 | $allMembershipInfo[$key] = array( | |
589 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values), | |
590 | 'total_amount' => CRM_Utils_Money::format($values['minimum_fee'], NULL, '%a'), | |
591 | 'total_amount_numeric' => CRM_Utils_Array::value('minimum_fee', $values), | |
592 | 'auto_renew' => CRM_Utils_Array::value('auto_renew', $values), | |
593 | 'has_related' => isset($values['relationship_type_id']), | |
594 | 'max_related' => CRM_Utils_Array::value('max_related', $values), | |
595 | ); | |
596 | } | |
597 | } | |
598 | ||
599 | $this->assign('allMembershipInfo', json_encode($allMembershipInfo)); | |
600 | ||
601 | // show organization by default, if only one organization in | |
602 | // the list | |
603 | if (count($selMemTypeOrg) == 2) { | |
604 | unset($selMemTypeOrg[0], $selOrgMemType[0][0]); | |
605 | } | |
606 | //sort membership organization and type, CRM-6099 | |
607 | natcasesort($selMemTypeOrg); | |
608 | foreach ($selOrgMemType as $index => $orgMembershipType) { | |
609 | natcasesort($orgMembershipType); | |
610 | $selOrgMemType[$index] = $orgMembershipType; | |
611 | } | |
612 | ||
613 | $memTypeJs = array('onChange' => "CRM.buildCustomData( 'Membership', this.value );"); | |
614 | ||
615 | //build the form for auto renew. | |
616 | $recurProcessor = $autoRenew = array(); | |
617 | if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) { | |
618 | $autoRenewElement = $this->addElement('checkbox', | |
619 | 'auto_renew', | |
620 | ts('Membership renewed automatically'), | |
621 | NULL, | |
622 | array('onclick' => "buildReceiptANDNotice( );") | |
623 | ); | |
624 | ||
625 | if ($this->_mode) { | |
626 | //get the valid recurring processors. | |
627 | $recurring = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1'); | |
628 | $recurProcessor = array_intersect_assoc($this->_processors, $recurring); | |
629 | $autoRenew = array(); | |
630 | if (!empty($recurProcessor)) { | |
631 | if (!empty($membershipType)) { | |
632 | $sql = ' | |
633 | SELECT id, | |
634 | auto_renew, | |
635 | duration_unit, | |
636 | duration_interval | |
637 | FROM civicrm_membership_type | |
638 | WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; | |
639 | $recurMembershipTypes = CRM_Core_DAO::executeQuery($sql); | |
640 | while ($recurMembershipTypes->fetch()) { | |
641 | $autoRenew[$recurMembershipTypes->id] = $recurMembershipTypes->auto_renew; | |
642 | foreach (array( | |
643 | 'id', 'auto_renew', 'duration_unit', 'duration_interval') as $fld) { | |
644 | $this->_recurMembershipTypes[$recurMembershipTypes->id][$fld] = $recurMembershipTypes->$fld; | |
645 | } | |
646 | } | |
647 | } | |
648 | $memTypeJs = array( | |
649 | 'onChange' => | |
650 | "CRM.buildCustomData( 'Membership', this.value ); buildAutoRenew(this.value, null );", | |
651 | ); | |
652 | } | |
653 | } | |
654 | } | |
655 | $allowAutoRenew = FALSE; | |
656 | if ($this->_mode && !empty($recurProcessor)) { | |
657 | $allowAutoRenew = TRUE; | |
658 | } | |
659 | $this->assign('allowAutoRenew', $allowAutoRenew); | |
660 | $this->assign('autoRenewOptions', json_encode($autoRenew)); | |
661 | $this->assign('recurProcessor', json_encode($recurProcessor)); | |
662 | ||
663 | // for max_related: a little JS to show/hide & set default value | |
664 | $memTypeJs['onChange'] = "buildMaxRelated(this.value,true); " . $memTypeJs['onChange']; | |
665 | $this->add('text', 'max_related', ts('Max related'), | |
666 | CRM_Core_DAO::getAttribute('CRM_Member_DAO_Membership', 'max_related') | |
667 | ); | |
668 | ||
669 | $sel = &$this->addElement('hierselect', | |
670 | 'membership_type_id', | |
671 | ts('Membership Organization and Type'), | |
672 | $memTypeJs | |
673 | ); | |
674 | ||
675 | $sel->setOptions(array($selMemTypeOrg, $selOrgMemType)); | |
676 | $elements = array(); | |
677 | if ($sel) { | |
678 | $elements[] = $sel; | |
679 | } | |
680 | ||
681 | $this->applyFilter('__ALL__', 'trim'); | |
682 | ||
683 | if ($this->_action & CRM_Core_Action::ADD) { | |
684 | $this->add('text', 'num_terms', ts('Number of Terms'), array('size' => 6)); | |
685 | } | |
686 | ||
687 | $this->addDate('join_date', ts('Member Since'), FALSE, array('formatType' => 'activityDate')); | |
688 | $this->addDate('start_date', ts('Start Date'), FALSE, array('formatType' => 'activityDate')); | |
689 | $endDate = $this->addDate('end_date', ts('End Date'), FALSE, array('formatType' => 'activityDate')); | |
690 | if ($endDate) { | |
691 | $elements[] = $endDate; | |
692 | } | |
693 | ||
694 | $this->add('text', 'source', ts('Source'), | |
695 | CRM_Core_DAO::getAttribute('CRM_Member_DAO_Membership', 'source') | |
696 | ); | |
697 | ||
698 | //CRM-7362 --add campaigns. | |
699 | $campaignId = NULL; | |
700 | if ($this->_id) { | |
701 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, 'campaign_id'); | |
702 | } | |
703 | CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId); | |
704 | ||
705 | if (!$this->_mode) { | |
706 | $this->add('select', 'status_id', ts('Membership Status'), | |
707 | array('' => ts('- select -')) + CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label') | |
708 | ); | |
709 | $statusOverride = $this->addElement('checkbox', 'is_override', | |
710 | ts('Status Override?'), NULL, | |
711 | array('onClick' => 'showHideMemberStatus()') | |
712 | ); | |
713 | if ($statusOverride) { | |
714 | $elements[] = $statusOverride; | |
715 | } | |
716 | ||
717 | $this->addElement('checkbox', 'record_contribution', ts('Record Membership Payment?')); | |
718 | ||
6a488035 TO |
719 | $this->add('text', 'total_amount', ts('Amount')); |
720 | $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money'); | |
721 | ||
722 | $this->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDate')); | |
723 | ||
724 | $this->add('select', 'payment_instrument_id', | |
725 | ts('Paid By'), | |
726 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), | |
727 | FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);") | |
728 | ); | |
729 | $this->add('text', 'trxn_id', ts('Transaction ID')); | |
730 | $this->addRule('trxn_id', ts('Transaction ID already exists in Database.'), | |
731 | 'objectExists', array('CRM_Contribute_DAO_Contribution', $this->_id, 'trxn_id') | |
732 | ); | |
733 | ||
734 | $allowStatuses = array(); | |
735 | $statuses = CRM_Contribute_PseudoConstant::contributionStatus(); | |
736 | if ($this->_onlinePendingContributionId) { | |
737 | $statusNames = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
738 | foreach ($statusNames as $val => $name) { | |
739 | if (in_array($name, array( | |
740 | 'In Progress', 'Overdue'))) { | |
741 | continue; | |
742 | } | |
743 | $allowStatuses[$val] = $statuses[$val]; | |
744 | } | |
745 | } | |
746 | else { | |
747 | $allowStatuses = $statuses; | |
748 | } | |
749 | $this->add('select', 'contribution_status_id', | |
750 | ts('Payment Status'), $allowStatuses | |
751 | ); | |
752 | $this->add('text', 'check_number', ts('Check Number'), | |
753 | CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number') | |
754 | ); | |
755 | } | |
756 | else { | |
757 | //add field for amount to allow an amount to be entered that differs from minimum | |
758 | $this->add('text', 'total_amount', ts('Amount')); | |
759 | } | |
5a9c4d4a PN |
760 | $this->add('select', 'financial_type_id', |
761 | ts('Financial Type'), | |
762 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType() | |
763 | ); | |
6a488035 TO |
764 | if ($this->_context != 'standalone') { |
765 | //CRM-10223 - allow contribution to be recorded against different contact | |
766 | // causes a conflict in standalone mode so skip in standalone for now | |
767 | $this->addElement('checkbox', 'is_different_contribution_contact', ts('Record Payment from a Different Contact?')); | |
768 | $this->add( 'select', 'honor_type_id', ts('Membership payment is : '), | |
cbf48754 | 769 | array( '' => ts( '-') ) + CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'honor_type_id') ); |
6a488035 TO |
770 | CRM_Contact_Form_NewContact::buildQuickForm($this,1, null, false,'contribution_'); |
771 | } | |
772 | ||
773 | $this->addElement('checkbox', | |
774 | 'send_receipt', | |
775 | ts('Send Confirmation and Receipt?'), NULL, | |
776 | array('onclick' => "showHideByValue( 'send_receipt', '', 'notice', 'table-row', 'radio', false); showHideByValue( 'send_receipt', '', 'fromEmail', 'table-row', 'radio', false);") | |
777 | ); | |
778 | ||
779 | $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails); | |
780 | ||
781 | $this->add('textarea', 'receipt_text_signup', ts('Receipt Message')); | |
782 | if ($this->_mode) { | |
783 | ||
784 | $this->add('select', 'payment_processor_id', | |
785 | ts('Payment Processor'), | |
786 | $this->_processors, TRUE, | |
787 | array('onChange' => "buildAutoRenew( null, this.value );") | |
788 | ); | |
789 | CRM_Core_Payment_Form::buildCreditCard($this, TRUE); | |
790 | } | |
791 | ||
792 | // Retrieve the name and email of the contact - this will be the TO for receipt email | |
793 | if ($this->_contactID) { | |
794 | list($this->_memberDisplayName, | |
795 | $this->_memberEmail | |
796 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
797 | ||
798 | $this->assign('emailExists', $this->_memberEmail); | |
799 | $this->assign('displayName', $this->_memberDisplayName); | |
800 | } | |
801 | ||
802 | $isRecur = FALSE; | |
803 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
804 | $recurContributionId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, | |
805 | 'contribution_recur_id' | |
806 | ); | |
807 | if ($recurContributionId && !CRM_Member_BAO_Membership::isSubscriptionCancelled($this->_id)) { | |
808 | $isRecur = TRUE; | |
809 | if (CRM_Member_BAO_Membership::isCancelSubscriptionSupported($this->_id)) { | |
810 | $this->assign('cancelAutoRenew', | |
811 | CRM_Utils_System::url('civicrm/contribute/unsubscribe', "reset=1&mid={$this->_id}") | |
812 | ); | |
813 | } | |
814 | foreach ($elements as $elem) { | |
815 | $elem->freeze(); | |
816 | } | |
817 | } | |
818 | } | |
819 | $this->assign('isRecur', $isRecur); | |
820 | ||
821 | $this->addFormRule(array('CRM_Member_Form_Membership', 'formRule'), $this); | |
822 | ||
823 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
824 | 'mailing_backend' | |
825 | ); | |
826 | $this->assign('outBound_option', $mailingInfo['outBound_option']); | |
827 | ||
828 | parent::buildQuickForm(); | |
829 | } | |
830 | ||
831 | /** | |
832 | * Function for validation | |
833 | * | |
834 | * @param array $params (ref.) an assoc array of name/value pairs | |
835 | * | |
836 | * @return mixed true or array of errors | |
837 | * @access public | |
838 | * @static | |
839 | */ | |
840 | static function formRule($params, $files, $self) { | |
841 | $errors = array(); | |
842 | ||
843 | $priceSetId = CRM_Utils_Array::value('price_set_id', $params); | |
844 | ||
845 | if ($priceSetId) { | |
846 | CRM_Price_BAO_Field::priceSetValidation($priceSetId, $params, $errors); | |
847 | ||
848 | $priceFieldIDS = array(); | |
849 | foreach ($self->_priceSet['fields'] as $priceIds => $dontCare) { | |
850 | ||
851 | if (!empty($params['price_' . $priceIds])) { | |
852 | if (is_array($params['price_' . $priceIds])) { | |
853 | foreach ($params['price_' . $priceIds] as $priceFldVal => $isSet) { | |
854 | if ($isSet) { | |
855 | $priceFieldIDS[] = $priceFldVal; | |
856 | } | |
857 | } | |
858 | } | |
859 | else { | |
860 | $priceFieldIDS[] = $params['price_' . $priceIds]; | |
861 | } | |
862 | } | |
863 | } | |
864 | ||
865 | if (!empty($priceFieldIDS)) { | |
866 | $ids = implode(',', $priceFieldIDS); | |
867 | ||
868 | $count = CRM_Price_BAO_Set::getMembershipCount($ids); | |
869 | foreach ($count as $id => $occurance) { | |
870 | if ($occurance > 1) { | |
871 | $errors['_qf_default'] = ts('Select at most one option associated with the same membership type.'); | |
872 | } | |
873 | } | |
874 | ||
875 | foreach ($priceFieldIDS as $priceFieldId) { | |
876 | if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_FieldValue', $priceFieldId, 'membership_type_id')) { | |
877 | $self->_memTypeSelected[$id] = $id; | |
878 | } | |
879 | } | |
880 | } | |
881 | } | |
882 | elseif (!CRM_Utils_Array::value(1, $params['membership_type_id'])) { | |
883 | $errors['membership_type_id'] = ts('Please select a membership type.'); | |
884 | } | |
885 | else { | |
886 | $self->_memTypeSelected[] = $params['membership_type_id'][1]; | |
887 | } | |
888 | ||
889 | if (!$priceSetId) { | |
890 | $numterms = CRM_Utils_Array::value('num_terms', $params); | |
891 | if ($numterms && intval($numterms) != $numterms) { | |
892 | $errors['num_terms'] = ts('Please enter an integer for the number of terms.'); | |
893 | } | |
894 | } | |
895 | ||
896 | // Return error if empty $self->_memTypeSelected | |
897 | if ($priceSetId && empty($errors) && empty($self->_memTypeSelected)) { | |
898 | $errors['_qf_default'] = ts('Select at least one membership option.'); | |
899 | } | |
900 | ||
901 | if (!empty($errors) && (count($self->_memTypeSelected) > 1)) { | |
902 | $memberOfContacts = CRM_Member_BAO_MembershipType::getMemberOfContactByMemTypes($self->_memTypeSelected); | |
903 | $duplicateMemberOfContacts = array_count_values($memberOfContacts); | |
904 | foreach ($duplicateMemberOfContacts as $countDuplicate) { | |
905 | if ($countDuplicate > 1) { | |
906 | $errors['_qf_default'] = ts('Please do not select more than one membership associated with the same organization.'); | |
907 | } | |
908 | } | |
909 | } | |
910 | ||
911 | //check if contact is selected in standalone mode | |
912 | if (isset($params['contact_select_id'][1]) && !$params['contact_select_id'][1]) { | |
913 | $errors['contact[1]'] = ts('Please select a contact or create new contact'); | |
914 | } | |
915 | ||
916 | if (!empty($errors)) { | |
917 | return $errors; | |
918 | } | |
919 | ||
920 | if ($priceSetId && !$self->_mode && !CRM_Utils_Array::value('record_contribution', $params)) { | |
921 | $errors['record_contribution'] = ts('Record Membership Payment is required when you using price set.'); | |
922 | } | |
a8d4ff25 | 923 | |
5a9c4d4a PN |
924 | if (!$priceSetId && $self->_mode && !CRM_Utils_Array::value('financial_type_id', $params)) { |
925 | $errors['financial_type_id'] = ts('Please enter the financial Type.'); | |
926 | } | |
6a488035 TO |
927 | |
928 | if (CRM_Utils_Array::value('payment_processor_id', $params)) { | |
929 | // make sure that credit card number and cvv are valid | |
7cb3d4f0 | 930 | CRM_Core_Payment_Form::validateCreditCard($params, $errors); |
6a488035 TO |
931 | } |
932 | ||
933 | $joinDate = NULL; | |
934 | if (CRM_Utils_Array::value('join_date', $params)) { | |
935 | ||
936 | $joinDate = CRM_Utils_Date::processDate($params['join_date']); | |
937 | ||
938 | foreach ($self->_memTypeSelected as $memType) { | |
939 | $startDate = NULL; | |
940 | if (CRM_Utils_Array::value('start_date', $params)) { | |
941 | $startDate = CRM_Utils_Date::processDate($params['start_date']); | |
942 | } | |
943 | ||
944 | // if end date is set, ensure that start date is also set | |
945 | // and that end date is later than start date | |
946 | // If selected membership type has duration unit as 'lifetime' | |
947 | // and end date is set, then give error | |
948 | $endDate = NULL; | |
949 | if (CRM_Utils_Array::value('end_date', $params)) { | |
950 | $endDate = CRM_Utils_Date::processDate($params['end_date']); | |
951 | } | |
952 | ||
953 | $membershipDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($memType); | |
954 | ||
955 | if ($startDate && CRM_Utils_Array::value('period_type', $membershipDetails) == 'rolling') { | |
956 | if ($startDate < $joinDate) { | |
957 | $errors['start_date'] = ts('Start date must be the same or later than Member since.'); | |
958 | } | |
959 | } | |
960 | ||
961 | if ($endDate) { | |
962 | if ($membershipDetails['duration_unit'] == 'lifetime') { | |
963 | $errors['end_date'] = ts('The selected Membership Type has a lifetime duration. You cannot specify an End Date for lifetime memberships. Please clear the End Date OR select a different Membership Type.'); | |
964 | } | |
965 | else { | |
966 | if (!$startDate) { | |
967 | $errors['start_date'] = ts('Start date must be set if end date is set.'); | |
968 | } | |
969 | if ($endDate < $startDate) { | |
970 | $errors['end_date'] = ts('End date must be the same or later than start date.'); | |
971 | } | |
972 | } | |
973 | } | |
974 | ||
975 | // Default values for start and end dates if not supplied | |
976 | // on the form | |
977 | $defaultDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($memType, | |
978 | $joinDate, | |
979 | $startDate, | |
980 | $endDate | |
981 | ); | |
982 | ||
983 | if (!$startDate) { | |
984 | $startDate = CRM_Utils_Array::value('start_date', | |
985 | $defaultDates | |
986 | ); | |
987 | } | |
988 | if (!$endDate) { | |
989 | $endDate = CRM_Utils_Array::value('end_date', | |
990 | $defaultDates | |
991 | ); | |
992 | } | |
993 | ||
994 | //CRM-3724, check for availability of valid membership status. | |
995 | if (!CRM_Utils_Array::value('is_override', $params) && !isset($errors['_qf_default'])) { | |
996 | $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($startDate, | |
997 | $endDate, | |
998 | $joinDate, | |
999 | 'today', | |
1000 | TRUE | |
1001 | ); | |
1002 | if (empty($calcStatus)) { | |
1003 | $url = CRM_Utils_System::url('civicrm/admin/member/membershipStatus', 'reset=1&action=browse'); | |
1004 | $errors['_qf_default'] = ts('There is no valid Membership Status available for selected membership dates.'); | |
1005 | $status = ts('Oops, it looks like there is no valid membership status available for the given membership dates. You can <a href="%1">Configure Membership Status Rules</a>.', array(1 => $url)); | |
1006 | if (!$self->_mode) { | |
1007 | $status .= ' ' . ts('OR You can sign up by setting Status Override? to true.'); | |
1008 | } | |
1009 | CRM_Core_Session::setStatus($status, ts('Membership Status Error'), 'error'); | |
1010 | } | |
1011 | } | |
1012 | } | |
1013 | } | |
1014 | else { | |
1015 | $errors['join_date'] = ts('Please enter the Member Since.'); | |
1016 | } | |
1017 | ||
1018 | if (isset($params['is_override']) && | |
1019 | $params['is_override'] && | |
1020 | !CRM_Utils_Array::value('status_id', $params) | |
1021 | ) { | |
1022 | $errors['status_id'] = ts('Please enter the status.'); | |
1023 | } | |
1024 | ||
1025 | //total amount condition arise when membership type having no | |
1026 | //minimum fee | |
1027 | if (isset($params['record_contribution'])) { | |
5a9c4d4a PN |
1028 | if (!$params['financial_type_id']) { |
1029 | $errors['financial_type_id'] = ts('Please enter the financial Type.'); | |
6a488035 TO |
1030 | } |
1031 | if (CRM_Utils_System::isNull($params['total_amount'])) { | |
1032 | $errors['total_amount'] = ts('Please enter the contribution.'); | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | // validate contribution status for 'Failed'. | |
1037 | if ($self->_onlinePendingContributionId && | |
1038 | CRM_Utils_Array::value('record_contribution', $params) && | |
1039 | (CRM_Utils_Array::value('contribution_status_id', $params) == | |
1040 | array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')) | |
1041 | ) | |
1042 | ) { | |
1043 | $errors['contribution_status_id'] = ts('Please select a valid payment status before updating.'); | |
1044 | } | |
1045 | ||
1046 | return empty($errors) ? TRUE : $errors; | |
1047 | } | |
1048 | ||
1049 | /** | |
1050 | * Function to process the form | |
1051 | * | |
1052 | * @access public | |
1053 | * | |
1054 | * @return None | |
1055 | */ | |
1056 | public function postProcess() { | |
1057 | if ($this->_action & CRM_Core_Action::DELETE) { | |
1058 | CRM_Member_BAO_Membership::deleteRelatedMemberships($this->_id); | |
1059 | CRM_Member_BAO_Membership::deleteMembership($this->_id); | |
1060 | return; | |
1061 | } | |
1062 | ||
1063 | $config = CRM_Core_Config::singleton(); | |
1064 | // get the submitted form values. | |
1065 | $this->_params = $formValues = $this->controller->exportValues($this->_name); | |
1066 | ||
1067 | $params = $ids = array(); | |
1068 | ||
1069 | $membershipTypeValues = array(); | |
1070 | foreach ($this->_memTypeSelected as $memType) { | |
1071 | $membershipTypeValues[$memType]['membership_type_id'] = $memType; | |
1072 | } | |
1073 | ||
1074 | //take the required membership recur values. | |
1075 | if ($this->_mode && CRM_Utils_Array::value('auto_renew', $this->_params)) { | |
1076 | $params['is_recur'] = $this->_params['is_recur'] = $formValues['is_recur'] = TRUE; | |
1077 | $mapping = array( | |
1078 | 'frequency_interval' => 'duration_interval', | |
1079 | 'frequency_unit' => 'duration_unit', | |
1080 | ); | |
1081 | ||
1082 | $count = 0; | |
1083 | foreach ($this->_memTypeSelected as $memType) { | |
1084 | $recurMembershipTypeValues = CRM_Utils_Array::value($memType, | |
1085 | $this->_recurMembershipTypes, array() | |
1086 | ); | |
1087 | foreach ($mapping as $mapVal => $mapParam) { | |
1088 | $membershipTypeValues[$memType][$mapVal] = CRM_Utils_Array::value($mapParam, | |
1089 | $recurMembershipTypeValues | |
1090 | ); | |
1091 | if (!$count) { | |
1092 | $this->_params[$mapVal] = $formValues[$mapVal] = CRM_Utils_Array::value($mapParam, | |
1093 | $recurMembershipTypeValues | |
1094 | ); | |
1095 | } | |
1096 | } | |
1097 | $count++; | |
1098 | } | |
1099 | ||
1100 | // unset send-receipt option, since receipt will be sent when ipn is received. | |
1101 | unset($this->_params['send_receipt'], $formValues['send_receipt']); | |
1102 | } | |
1103 | ||
1104 | // process price set and get total amount and line items. | |
1105 | $lineItem = array(); | |
1106 | $priceSetId = NULL; | |
1107 | if (!$priceSetId = CRM_Utils_Array::value('price_set_id', $formValues)) { | |
1108 | CRM_Member_BAO_Membership::createLineItems($this, $formValues['membership_type_id'], $priceSetId); | |
1109 | } | |
1110 | $isQuickConfig = 0; | |
1111 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $this->_priceSetId, 'is_quick_config')) { | |
1112 | $isQuickConfig = 1; | |
1113 | } | |
1114 | ||
1115 | $termsByType = array(); | |
1116 | if ($priceSetId) { | |
1117 | CRM_Price_BAO_Set::processAmount($this->_priceSet['fields'], | |
1118 | $this->_params, $lineItem[$priceSetId]); | |
1119 | $params['total_amount'] = CRM_Utils_Array::value('amount', $this->_params); | |
1120 | $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $formValues); | |
6a488035 TO |
1121 | if (!empty($lineItem[$priceSetId])) { |
1122 | foreach ($lineItem[$priceSetId] as &$li) { | |
1123 | if (CRM_Utils_Array::value('membership_type_id', $li)) { | |
1124 | if (CRM_Utils_Array::value('membership_num_terms', $li)) { | |
1125 | $termsByType[$li['membership_type_id']] = $li['membership_num_terms']; | |
1126 | } | |
1127 | } | |
1128 | ||
1129 | ///CRM-11529 for quick config backoffice transactions | |
1130 | //when financial_type_id is passed in form, update the | |
1131 | //lineitems with the financial type selected in form | |
5a9c4d4a | 1132 | if ($isQuickConfig && $submittedFinancialType) { |
6a488035 TO |
1133 | $li['financial_type_id'] = $submittedFinancialType; |
1134 | } | |
1135 | } | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | $this->storeContactFields($formValues); | |
1140 | ||
1141 | $params['contact_id'] = $this->_contactID; | |
1142 | ||
1143 | $fields = array( | |
1144 | 'status_id', | |
1145 | 'source', | |
1146 | 'is_override', | |
1147 | 'campaign_id', | |
1148 | ); | |
1149 | ||
1150 | foreach ($fields as $f) { | |
1151 | $params[$f] = CRM_Utils_Array::value($f, $formValues); | |
1152 | } | |
1153 | ||
1154 | // fix for CRM-3724 | |
1155 | // when is_override false ignore is_admin statuses during membership | |
1156 | // status calculation. similarly we did fix for import in CRM-3570. | |
1157 | if (!CRM_Utils_Array::value('is_override', $params)) { | |
1158 | $params['exclude_is_admin'] = TRUE; | |
1159 | } | |
1160 | ||
1161 | // process date params to mysql date format. | |
1162 | $dateTypes = array( | |
1163 | 'join_date' => 'joinDate', | |
1164 | 'start_date' => 'startDate', | |
1165 | 'end_date' => 'endDate', | |
1166 | ); | |
1167 | foreach ($dateTypes as $dateField => $dateVariable) { | |
1168 | $$dateVariable = CRM_Utils_Date::processDate($formValues[$dateField]); | |
1169 | } | |
1170 | ||
1171 | $dates = array( | |
1172 | 'join_date', | |
1173 | 'start_date', | |
1174 | 'end_date', | |
1175 | ); | |
1176 | ||
1177 | $num_terms = CRM_Utils_Array::value('num_terms', $formValues, 1); | |
1178 | ||
1179 | $calcDates = array(); | |
1180 | foreach ($this->_memTypeSelected as $memType) { | |
1181 | $memTypeNumTerms = CRM_Utils_Array::value($memType, $termsByType, $num_terms); | |
1182 | $calcDates[$memType] = CRM_Member_BAO_MembershipType::getDatesForMembershipType($memType, | |
1183 | $joinDate, $startDate, $endDate, $memTypeNumTerms | |
1184 | ); | |
1185 | } | |
1186 | ||
1187 | foreach ($calcDates as $memType => $calcDate) { | |
1188 | foreach ($dates as $d) { | |
1189 | //first give priority to form values then calDates. | |
1190 | $date = CRM_Utils_Array::value($d, $formValues); | |
1191 | if (!$date) { | |
1192 | $date = CRM_Utils_Array::value($d, $calcDate); | |
1193 | } | |
1194 | ||
1195 | $membershipTypeValues[$memType][$d] = CRM_Utils_Date::processDate($date); | |
1196 | //$params[$d] = CRM_Utils_Date::processDate( $date ); | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | // max related memberships - take from form or inherit from membership type | |
1201 | foreach ($this->_memTypeSelected as $memType) { | |
1202 | if (array_key_exists('max_related', $formValues)) { | |
1203 | $membershipTypeValues[$memType]['max_related'] = CRM_Utils_Array::value('max_related', $formValues); | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | if ($this->_id) { | |
1208 | $ids['membership'] = $params['id'] = $this->_id; | |
1209 | } | |
1210 | ||
1211 | $session = CRM_Core_Session::singleton(); | |
1212 | $ids['userId'] = $session->get('userID'); | |
1213 | ||
1214 | // membership type custom data | |
1215 | foreach ($this->_memTypeSelected as $memType) { | |
1216 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, | |
1217 | $memType | |
1218 | ); | |
1219 | ||
1220 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, | |
1221 | CRM_Core_BAO_CustomField::getFields('Membership', | |
1222 | FALSE, FALSE, | |
1223 | NULL, NULL, TRUE | |
1224 | ) | |
1225 | ); | |
1226 | ||
1227 | $membershipTypeValues[$memType]['custom'] = CRM_Core_BAO_CustomField::postProcess($formValues, | |
1228 | $customFields, | |
1229 | $this->_id, | |
1230 | 'Membership' | |
1231 | ); | |
1232 | } | |
1233 | ||
1234 | foreach ($this->_memTypeSelected as $memType) { | |
1235 | $membershipTypes[$memType] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
1236 | $memType | |
1237 | ); | |
1238 | } | |
1239 | ||
1240 | $membershipType = implode(', ', $membershipTypes); | |
1241 | ||
1242 | // Retrieve the name and email of the current user - this will be the FROM for the receipt email | |
1243 | list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']); | |
1244 | ||
1245 | //CRM-10223 - allow contribution to be recorded against different contact | |
1246 | if($this->_contributorContactID != $this->_contactID){ | |
1247 | $params['contribution_contact_id'] = $this->_contributorContactID; | |
1248 | if(CRM_Utils_Array::value('honor_type_id', $this->_params)){ | |
1249 | $params['honor_type_id'] = $this->_params['honor_type_id']; | |
1250 | $params['honor_contact_id'] = $params['contact_id']; | |
1251 | } | |
1252 | } | |
1253 | if (CRM_Utils_Array::value('record_contribution', $formValues)) { | |
1254 | $recordContribution = array( | |
1255 | 'total_amount', 'honor_type_id','financial_type_id', 'payment_instrument_id', | |
1256 | 'trxn_id', 'contribution_status_id', 'check_number', 'campaign_id', 'receive_date', | |
1257 | ); | |
1258 | ||
1259 | foreach ($recordContribution as $f) { | |
1260 | $params[$f] = CRM_Utils_Array::value($f, $formValues); | |
1261 | } | |
1262 | ||
1263 | if (!$this->_onlinePendingContributionId) { | |
1264 | $params['contribution_source'] = ts('%1 Membership: Offline signup (by %2)', | |
1265 | array(1 => $membershipType, 2 => $userName) | |
1266 | ); | |
1267 | } | |
1268 | ||
1269 | if (!CRM_Utils_Array::value('is_override', $params) && | |
1270 | CRM_Utils_Array::value('contribution_status_id', $params) == array_search('Pending', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')) | |
1271 | ) { | |
1272 | $allStatus = CRM_Member_PseudoConstant::membershipStatus(); | |
1273 | $params['status_id'] = array_search('Pending', $allStatus); | |
1274 | $params['skipStatusCal'] = TRUE; | |
1275 | $params['is_pay_later'] = 1; | |
1276 | $this->assign('is_pay_later', 1); | |
1277 | } | |
1278 | ||
1279 | if (CRM_Utils_Array::value('receive_date', $params)) { | |
1280 | $formValues['receive_date'] = $params['receive_date'] = CRM_Utils_Date::processDate($params['receive_date']); | |
1281 | } | |
1282 | if (CRM_Utils_Array::value('send_receipt', $formValues)) { | |
1283 | $params['receipt_date'] = CRM_Utils_Array::value('receive_date', $params); | |
1284 | } | |
1285 | ||
1286 | //insert financial type name in receipt. | |
1287 | $formValues['contributionType_name'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', | |
1288 | $formValues['financial_type_id'] | |
1289 | ); | |
1290 | } | |
1291 | ||
1292 | // process line items, until no previous line items. | |
1293 | if (!empty($lineItem)) { | |
1294 | $params['lineItems'] = $lineItem; | |
1295 | $params['processPriceSet'] = TRUE; | |
1296 | } | |
1297 | $createdMemberships = array(); | |
1298 | if ($this->_mode) { | |
1299 | if (empty($formValues['total_amount']) && !$priceSetId) { | |
1300 | // if total amount not provided minimum for membership type is used | |
1301 | $params['total_amount'] = $formValues['total_amount'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
1302 | $formValues['membership_type_id'][1], 'minimum_fee' | |
1303 | ); | |
1304 | } | |
1305 | else { | |
1306 | $params['total_amount'] = CRM_Utils_Array::value('total_amount', $formValues, 0); | |
1307 | } | |
a8d4ff25 | 1308 | |
5a9c4d4a | 1309 | if ($priceSetId && !$isQuickConfig) { |
6a488035 TO |
1310 | $params['financial_type_id'] = CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_Set', |
1311 | $priceSetId, | |
1312 | 'financial_type_id' | |
1313 | ); | |
1314 | } | |
1315 | else { | |
5a9c4d4a | 1316 | $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $formValues); |
6a488035 TO |
1317 | } |
1318 | ||
1319 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($formValues['payment_processor_id'], | |
1320 | $this->_mode | |
1321 | ); | |
1322 | ||
1323 | //get the payment processor id as per mode. | |
1324 | $params['payment_processor_id'] = $this->_params['payment_processor_id'] = $formValues['payment_processor_id'] = $this->_paymentProcessor['id']; | |
1325 | ||
1326 | ||
1327 | $now = date('YmdHis'); | |
1328 | $fields = array(); | |
1329 | ||
1330 | // set email for primary location. | |
1331 | $fields['email-Primary'] = 1; | |
1332 | $formValues['email-5'] = $formValues['email-Primary'] = $this->_memberEmail; | |
1333 | $params['register_date'] = $now; | |
1334 | ||
1335 | // now set the values for the billing location. | |
1336 | foreach ($this->_fields as $name => $dontCare) { | |
1337 | $fields[$name] = 1; | |
1338 | } | |
1339 | ||
1340 | // also add location name to the array | |
1341 | $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); | |
1342 | ||
1343 | $formValues["address_name-{$this->_bltID}"] = trim($formValues["address_name-{$this->_bltID}"]); | |
1344 | ||
1345 | $fields["address_name-{$this->_bltID}"] = 1; | |
1346 | ||
1347 | $fields["email-{$this->_bltID}"] = 1; | |
1348 | ||
1349 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type'); | |
1350 | ||
1351 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
1352 | ||
1353 | foreach ($nameFields as $name) { | |
1354 | $fields[$name] = 1; | |
1355 | if (array_key_exists("billing_$name", $formValues)) { | |
1356 | $formValues[$name] = $formValues["billing_{$name}"]; | |
1357 | $formValues['preserveDBName'] = TRUE; | |
1358 | } | |
1359 | } | |
1360 | //here we are setting up the billing contact - if different from the member they are already created | |
1361 | // but they will get billing details assigned | |
1362 | CRM_Contact_BAO_Contact::createProfileContact($formValues, $fields, | |
1363 | $this->_contributorContactID, NULL, NULL, $ctype | |
1364 | ); | |
1365 | ||
1366 | ||
1367 | // add all the additioanl payment params we need | |
1368 | $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}"]); | |
1369 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
1370 | ||
1371 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
1372 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
1373 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); | |
1374 | $this->_params['amount'] = $params['total_amount']; | |
1375 | $this->_params['currencyID'] = $config->defaultCurrency; | |
1376 | $this->_params['payment_action'] = 'Sale'; | |
1377 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1378 | $this->_params['financial_type_id'] = $params['financial_type_id']; | |
1379 | ||
1380 | // at this point we've created a contact and stored its address etc | |
1381 | // all the payment processors expect the name and address to be in the | |
1382 | // so we copy stuff over to first_name etc. | |
1383 | $paymentParams = $this->_params; | |
1384 | $paymentParams['contactID'] = $this->_contributorContactID; | |
1385 | //CRM-10377 if payment is by an alternate contact then we need to set that person | |
1386 | // as the contact in the payment params | |
1387 | if($this->_contributorContactID != $this->_contactID){ | |
1388 | if(CRM_Utils_Array::value('honor_type_id', $this->_params)){ | |
1389 | $paymentParams['honor_contact_id'] = $this->_contactID; | |
1390 | $paymentParams['honor_type_id'] = $this->_params['honor_type_id']; | |
1391 | } | |
1392 | } | |
1393 | if (CRM_Utils_Array::value('send_receipt', $this->_params)) { | |
1394 | $paymentParams['email'] = $this->_contributorEmail; | |
1395 | } | |
1396 | ||
1397 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); | |
1398 | ||
1399 | // CRM-7137 -for recurring membership, | |
1400 | // we do need contribution and recuring records. | |
1401 | $result = NULL; | |
1402 | if (CRM_Utils_Array::value('is_recur', $paymentParams)) { | |
1403 | $allStatus = CRM_Member_PseudoConstant::membershipStatus(); | |
1404 | ||
1405 | $contributionType = new CRM_Financial_DAO_FinancialType(); | |
1406 | $contributionType->id = $params['financial_type_id']; | |
1407 | if (!$contributionType->find(TRUE)) { | |
1408 | CRM_Core_Error::fatal('Could not find a system table'); | |
1409 | } | |
1410 | ||
1411 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($this, | |
1412 | $paymentParams, | |
1413 | $result, | |
1414 | $this->_contributorContactID, | |
1415 | $contributionType, | |
1416 | FALSE, | |
1417 | TRUE, | |
1418 | FALSE | |
1419 | ); | |
1420 | $paymentParams['contactID'] = $contactID; | |
1421 | $paymentParams['contributionID'] = $contribution->id; | |
1422 | $paymentParams['contributionTypeID'] = $contribution->financial_type_id; | |
1423 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; | |
1424 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; | |
1425 | $ids['contribution'] = $contribution->id; | |
1426 | $params['contribution_recur_id'] = $paymentParams['contributionRecurID']; | |
1427 | $params['status_id'] = array_search('Pending', $allStatus); | |
1428 | $params['skipStatusCal'] = TRUE; | |
1429 | ||
1430 | //as membership is pending set dates to null. | |
1431 | $memberDates = array( | |
1432 | 'join_date' => 'joinDate', | |
1433 | 'start_date' => 'startDate', | |
1434 | 'end_date' => 'endDate', | |
1435 | ); | |
1436 | ||
1437 | foreach ($memberDates as $dp => $dv) { | |
1438 | $$dv = NULL; | |
1439 | foreach ($this->_memTypeSelected as $memType) { | |
1440 | $membershipTypeValues[$memType][$dv] = NULL; | |
1441 | } | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | if ($params['total_amount'] > 0.0) { | |
1446 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); | |
1447 | $result = &$payment->doDirectPayment($paymentParams); | |
1448 | } | |
1449 | ||
1450 | if (is_a($result, 'CRM_Core_Error')) { | |
1451 | //make sure to cleanup db for recurring case. | |
1452 | if (CRM_Utils_Array::value('contributionID', $paymentParams)) { | |
1453 | CRM_Contribute_BAO_Contribution::deleteContribution($paymentParams['contributionID']); | |
1454 | } | |
1455 | if (CRM_Utils_Array::value('contributionRecurID', $paymentParams)) { | |
1456 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']); | |
1457 | } | |
1458 | ||
1459 | CRM_Core_Error::displaySessionError($result); | |
1460 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', | |
1461 | "reset=1&action=add&cid={$this->_contactID}&context=&mode={$this->_mode}" | |
1462 | )); | |
1463 | } | |
1464 | ||
1465 | if ($result) { | |
1466 | $this->_params = array_merge($this->_params, $result); | |
1467 | //assign amount to template if payment was successful | |
1468 | $this->assign('amount', $params['total_amount']); | |
1469 | } | |
1470 | ||
1471 | $params['contribution_status_id'] = CRM_Utils_Array::value('is_recur', $paymentParams) ? 2 : 1; | |
1472 | $params['receive_date'] = $now; | |
1473 | $params['invoice_id'] = $this->_params['invoiceID']; | |
1474 | $params['contribution_source'] = ts('%1 Membership Signup: Credit card or direct debit (by %2)', | |
1475 | array(1 => $membershipType, 2 => $userName) | |
1476 | ); | |
1477 | $params['source'] = $formValues['source'] ? $formValues['source'] : $params['contribution_source']; | |
1478 | $params['trxn_id'] = CRM_Utils_Array::value('trxn_id', $result); | |
1479 | $params['payment_instrument_id'] = 1; | |
1480 | $params['is_test'] = ($this->_mode == 'live') ? 0 : 1; | |
1481 | if (CRM_Utils_Array::value('send_receipt', $this->_params)) { | |
1482 | $params['receipt_date'] = $now; | |
1483 | } | |
1484 | else { | |
1485 | $params['receipt_date'] = NULL; | |
1486 | } | |
1487 | ||
1488 | $this->set('params', $this->_params); | |
1489 | $this->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $result)); | |
1490 | $this->assign('receive_date', | |
1491 | CRM_Utils_Date::mysqlToIso($params['receive_date']) | |
1492 | ); | |
1493 | ||
1494 | // required for creating membership for related contacts | |
1495 | $params['action'] = $this->_action; | |
1496 | ||
1497 | //create membership record. | |
1498 | $count = 0; | |
1499 | foreach ($this->_memTypeSelected as $memType) { | |
1500 | if ($count && | |
1501 | ($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id)) | |
1502 | ) { | |
1503 | $membershipTypeValues[$memType]['relate_contribution_id'] = $relateContribution; | |
1504 | } | |
1505 | ||
1506 | $membershipParams = array_merge($membershipTypeValues[$memType], $params); | |
1507 | $membership = CRM_Member_BAO_Membership::create($membershipParams, $ids); | |
1508 | ||
1509 | $this->_membershipIDs[] = $membership->id; | |
1510 | $createdMemberships[$memType] = $membership; | |
1511 | $count++; | |
1512 | } | |
1513 | ||
1514 | if (!CRM_Utils_Array::value('is_recur', $params) && $params['total_amount'] > 0.0) { | |
1515 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
1516 | $contribution->trxn_id = CRM_Utils_Array::value('trxn_id', $result); | |
1517 | if ($contribution->find(TRUE)) { | |
1518 | // next create the transaction record | |
1519 | $trxnParams = array( | |
1520 | 'contribution_id' => $contribution->id, | |
1521 | 'trxn_date' => $now, | |
1522 | 'trxn_type' => 'Debit', | |
1523 | 'total_amount' => $params['total_amount'], | |
1524 | 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), | |
1525 | 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['total_amount']), | |
1526 | 'currency' => $config->defaultCurrency, | |
1527 | 'payment_processor' => $this->_paymentProcessor['payment_processor_type'], | |
1528 | 'trxn_id' => CRM_Utils_Array::value('trxn_id', $result), | |
1529 | ); | |
1530 | $trxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams); | |
1531 | } | |
1532 | } | |
1533 | } | |
1534 | else { | |
1535 | $params['action'] = $this->_action; | |
1536 | if ($this->_onlinePendingContributionId && | |
1537 | CRM_Utils_Array::value('record_contribution', $formValues) | |
1538 | ) { | |
1539 | ||
1540 | // update membership as well as contribution object, CRM-4395 | |
1541 | $params['contribution_id'] = $this->_onlinePendingContributionId; | |
1542 | $params['componentId'] = $params['id']; | |
1543 | $params['componentName'] = 'contribute'; | |
1544 | $result = CRM_Contribute_BAO_Contribution::transitionComponents($params, TRUE); | |
1545 | if (!empty($result) && CRM_Utils_Array::value('contribution_id', $params)) { | |
1546 | $lineItem = array(); | |
1547 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($params['contribution_id'], 'contribution'); | |
1548 | $itemId = key($lineItems); | |
1549 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $lineItems[$itemId]['price_field_id'], 'price_set_id'); | |
1550 | $fieldType = NULL; | |
1551 | if ($itemId && CRM_Utils_Array::value('price_field_id', $lineItems[$itemId])) { | |
1552 | $fieldType = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $lineItems[$itemId]['price_field_id'], 'html_type'); | |
1553 | } | |
1554 | $lineItems[$itemId]['unit_price'] = $params['total_amount']; | |
1555 | $lineItems[$itemId]['line_total'] = $params['total_amount']; | |
1556 | $lineItems[$itemId]['id'] = $itemId; | |
1557 | $lineItem[$priceSetId] = $lineItems; | |
1558 | CRM_Price_BAO_LineItem::processPriceSet($params['contribution_id'], $lineItem); | |
1559 | } | |
1560 | ||
1561 | //carry updated membership object. | |
1562 | $membership = new CRM_Member_DAO_Membership(); | |
1563 | $membership->id = $this->_id; | |
1564 | $membership->find(TRUE); | |
1565 | ||
1566 | $cancelled = TRUE; | |
1567 | if ($membership->end_date) { | |
1568 | //display end date w/ status message. | |
1569 | $endDate = $membership->end_date; | |
1570 | ||
1571 | $membershipStatues = CRM_Member_PseudoConstant::membershipStatus(); | |
1572 | if (!in_array($membership->status_id, array(array_search('Cancelled', $membershipStatues), | |
1573 | array_search('Expired', $membershipStatues), | |
1574 | ))) { | |
1575 | $cancelled = FALSE; | |
1576 | } | |
1577 | } | |
1578 | // suppress form values in template. | |
1579 | $this->assign('cancelled', $cancelled); | |
1580 | ||
1581 | // FIX ME: need to recheck this | |
1582 | // here we might updated dates, so get from object. | |
1583 | foreach ($calcDates[$membership->membership_type_id] as $date => & $val) { | |
1584 | if ($membership->$date) { | |
1585 | $val = $membership->$date; | |
1586 | } | |
1587 | } | |
1588 | ||
1589 | $createdMemberships[] = $membership; | |
1590 | } | |
1591 | else { | |
1592 | $count = 0; | |
1593 | foreach ($this->_memTypeSelected as $memType) { | |
1594 | if ($count && | |
1595 | CRM_Utils_Array::value('record_contribution', $formValues) && | |
1596 | ($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id)) | |
1597 | ) { | |
1598 | $membershipTypeValues[$memType]['relate_contribution_id'] = $relateContribution; | |
1599 | } | |
1600 | ||
1601 | $membershipParams = array_merge($params, $membershipTypeValues[$memType]); | |
1602 | if (CRM_Utils_Array::value('int_amount', $formValues)) { | |
1603 | $init_amount = array(); | |
1604 | foreach($formValues as $key => $value){ | |
1605 | if (strstr($key,'txt-price')){ | |
1606 | $init_amount[$key] = $value; | |
1607 | } | |
1608 | } | |
1609 | $membershipParams['init_amount'] = $init_amount; | |
1610 | } | |
1611 | $membership = CRM_Member_BAO_Membership::create($membershipParams, $ids); | |
1612 | ||
1613 | $this->_membershipIDs[] = $membership->id; | |
1614 | $createdMemberships[$memType] = $membership; | |
1615 | $count++; | |
1616 | } | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | if (!empty($lineItem[$priceSetId])) { | |
1621 | foreach ($lineItem[$priceSetId] as & $priceFieldOp) { | |
1622 | if (CRM_Utils_Array::value('membership_type_id', $priceFieldOp)) { | |
1623 | $priceFieldOp['start_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'], '%d%f %b, %Y') : '-'; | |
1624 | ||
1625 | $priceFieldOp['end_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'], '%d%f %b, %Y') : '-'; | |
1626 | } | |
1627 | else { | |
1628 | $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A'; | |
1629 | } | |
1630 | } | |
1631 | } | |
1632 | $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE); | |
1633 | ||
1634 | $receiptSend = FALSE; | |
1635 | if (CRM_Utils_Array::value('send_receipt', $formValues)) { | |
1636 | $receiptSend = TRUE; | |
1637 | ||
1638 | $formValues['contact_id'] = $this->_contactID; | |
1639 | ||
1640 | // send email receipt | |
1641 | $mailSend = self::emailReceipt( $this, $formValues, $membership ); | |
1642 | } | |
1643 | ||
1644 | if (($this->_action & CRM_Core_Action::UPDATE)) { | |
1645 | //end date can be modified by hooks, so if end date is set then use it. | |
1646 | $endDate = ($membership->end_date) ? $membership->end_date : $endDate; | |
1647 | ||
1648 | $statusMsg = ts('Membership for %1 has been updated.', array(1 => $this->_memberDisplayName)); | |
1649 | if ($endDate && $endDate !== 'null') { | |
1650 | $endDate = CRM_Utils_Date::customFormat($endDate); | |
1651 | $statusMsg .= ' ' . ts('The membership End Date is %1.', array(1 => $endDate)); | |
1652 | } | |
1653 | if ($receiptSend) { | |
1654 | $statusMsg .= ' ' . ts('A confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail)); | |
1655 | } | |
1656 | } | |
1657 | elseif (($this->_action & CRM_Core_Action::ADD)) { | |
1658 | // FIX ME: fix status messages | |
1659 | ||
1660 | $statusMsg = array(); | |
1661 | foreach ($membershipTypes as $memType => $membershipType) { | |
1662 | $statusMsg[$memType] = ts('%1 membership for %2 has been added.', array(1 => $membershipType, 2 => $this->_memberDisplayName)); | |
1663 | ||
1664 | $membership = $createdMemberships[$memType]; | |
1665 | $memEndDate = ($membership->end_date) ? $membership->end_date : $endDate; | |
1666 | ||
1667 | //get the end date from calculated dates. | |
1668 | if (!$memEndDate && !CRM_Utils_Array::value('is_recur', $params)) { | |
1669 | $memEndDate = CRM_Utils_Array::value('end_date', $calcDates[$memType]); | |
1670 | } | |
1671 | ||
1672 | if ($memEndDate && $memEndDate !== 'null') { | |
1673 | $memEndDate = CRM_Utils_Date::customFormat($memEndDate); | |
1674 | $statusMsg[$memType] .= ' ' . ts('The new membership End Date is %1.', array(1 => $memEndDate)); | |
1675 | } | |
1676 | } | |
1677 | $statusMsg = implode('<br/>', $statusMsg); | |
1678 | if ($receiptSend && $mailSend) { | |
71447ba5 | 1679 | $statusMsg .= ' ' . ts('A membership confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail)); |
6a488035 TO |
1680 | } |
1681 | } | |
1682 | ||
1683 | // finally set membership id if already not set | |
1684 | if (!$this->_id) { | |
1685 | $this->_id = $membership->id; | |
1686 | } | |
1687 | ||
1688 | CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success'); | |
1689 | ||
1690 | $buttonName = $this->controller->getButtonName(); | |
1691 | if ($this->_context == 'standalone') { | |
1692 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
1693 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/member/add', | |
1694 | 'reset=1&action=add&context=standalone' | |
1695 | )); | |
1696 | } | |
1697 | else { | |
1698 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
1699 | "reset=1&cid={$this->_contactID}&selectedChild=member" | |
1700 | )); | |
1701 | } | |
1702 | } | |
1703 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
1704 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/membership', | |
1705 | "reset=1&action=add&context=membership&cid={$this->_contactID}" | |
1706 | )); | |
1707 | } | |
1708 | } | |
1709 | ||
1710 | /** | |
1711 | * Function to send email receipt | |
1712 | * | |
1713 | * @param object $form form object | |
1714 | * @param array $values submitted values | |
1715 | * @param object $membership object | |
1716 | * | |
1717 | * @return boolean true if mail was sent successfully | |
1718 | * @static | |
1719 | */ | |
1720 | static function emailReceipt( &$form, &$formValues, &$membership ) { | |
1721 | // retrieve 'from email id' for acknowledgement | |
1722 | $receiptFrom = $formValues['from_email_address']; | |
1723 | ||
1724 | if (CRM_Utils_Array::value('payment_instrument_id', $formValues)) { | |
1725 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
1726 | $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']]; | |
1727 | } | |
1728 | ||
1729 | // retrieve custom data | |
1730 | $customFields = $customValues = array(); | |
1731 | if ( property_exists( $form, '_groupTree' ) | |
1732 | && !empty( $form->_groupTree ) ) { | |
1733 | foreach ($form->_groupTree as $groupID => $group) { | |
1734 | if ($groupID == 'info') { | |
1735 | continue; | |
1736 | } | |
1737 | foreach ($group['fields'] as $k => $field) { | |
1738 | $field['title'] = $field['label']; | |
1739 | $customFields["custom_{$k}"] = $field; | |
1740 | } | |
1741 | } | |
1742 | } | |
1743 | ||
1744 | $members = array(array('member_id', '=', $membership->id, 0, 0)); | |
1745 | // check whether its a test drive | |
1746 | if ($form->_mode == 'test') { | |
1747 | $members[] = array('member_test', '=', 1, 0, 0); | |
1748 | } | |
1749 | ||
1750 | CRM_Core_BAO_UFGroup::getValues($formValues['contact_id'], $customFields, $customValues, FALSE, $members); | |
1751 | ||
1752 | if ($form->_mode) { | |
1753 | if (CRM_Utils_Array::value('billing_first_name', $form->_params)) { | |
1754 | $name = $form->_params['billing_first_name']; | |
1755 | } | |
1756 | ||
1757 | if (CRM_Utils_Array::value('billing_middle_name', $form->_params)) { | |
1758 | $name .= " {$form->_params['billing_middle_name']}"; | |
1759 | } | |
1760 | ||
1761 | if (CRM_Utils_Array::value('billing_last_name', $form->_params)) { | |
1762 | $name .= " {$form->_params['billing_last_name']}"; | |
1763 | } | |
1764 | ||
1765 | $form->assign('billingName', $name); | |
1766 | ||
1767 | // assign the address formatted up for display | |
1768 | $addressParts = array( | |
1769 | "street_address-{$form->_bltID}", | |
1770 | "city-{$form->_bltID}", | |
1771 | "postal_code-{$form->_bltID}", | |
1772 | "state_province-{$form->_bltID}", | |
1773 | "country-{$form->_bltID}", | |
1774 | ); | |
1775 | $addressFields = array(); | |
1776 | foreach ($addressParts as $part) { | |
1777 | list($n, $id) = explode('-', $part); | |
1778 | if (isset($form->_params['billing_' . $part])) { | |
1779 | $addressFields[$n] = $form->_params['billing_' . $part]; | |
1780 | } | |
1781 | } | |
1782 | $form->assign('address', CRM_Utils_Address::format($addressFields)); | |
1783 | ||
1784 | $date = CRM_Utils_Date::format($form->_params['credit_card_exp_date']); | |
1785 | $date = CRM_Utils_Date::mysqlToIso($date); | |
1786 | $form->assign('credit_card_exp_date', $date); | |
1787 | $form->assign('credit_card_number', | |
1788 | CRM_Utils_System::mungeCreditCard($form->_params['credit_card_number']) | |
1789 | ); | |
1790 | $form->assign('credit_card_type', $form->_params['credit_card_type']); | |
1791 | $form->assign('contributeMode', 'direct'); | |
1792 | $form->assign('isAmountzero', 0); | |
1793 | $form->assign('is_pay_later', 0); | |
1794 | $form->assign('isPrimary', 1); | |
1795 | } | |
1796 | ||
1797 | $form->assign('module', 'Membership'); | |
1798 | $form->assign('contactID', $formValues['contact_id'] ); | |
1799 | ||
1800 | $form->assign('membershipID', CRM_Utils_Array::value('membership_id', $form->_params, CRM_Utils_Array::value('membership_id', $form->_defaultValues))); | |
1801 | ||
1802 | if ( CRM_Utils_Array::value('contribution_id', $formValues ) ) { | |
1803 | $form->assign( 'contributionID', $formValues['contribution_id'] ); | |
1804 | } | |
1805 | else { | |
1806 | $form->assign( 'contributionID', $form->_onlinePendingContributionId ); | |
1807 | } | |
1808 | ||
1809 | if (CRM_Utils_Array::value('contribution_status_id', $formValues )) { | |
1810 | $form->assign('contributionStatusID', $formValues['contribution_status_id']); | |
1811 | $form->assign('contributionStatus', CRM_Contribute_PseudoConstant::contributionStatus($formValues['contribution_status_id'], 'name')); | |
1812 | } | |
1813 | ||
1814 | if ( CRM_Utils_Array::value( 'is_renew', $formValues ) ) { | |
1815 | $form->assign('receiptType', 'membership renewal'); | |
1816 | } | |
1817 | else { | |
1818 | $form->assign('receiptType', 'membership signup'); | |
1819 | } | |
1820 | $form->assign('receive_date', CRM_Utils_Array::value('receive_date', $formValues ) ); | |
1821 | $form->assign('formValues', $formValues); | |
1822 | ||
1823 | if (empty($lineItem)) { | |
1824 | $form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y' ) ); | |
1825 | $form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y' ) ); | |
1826 | $form->assign('membership_name', CRM_Member_PseudoConstant::membershipType( $membership->membership_type_id ) ); | |
1827 | } | |
1828 | ||
1829 | $form->assign('customValues', $customValues); | |
1830 | $isBatchProcess = is_a($form, 'CRM_Batch_Form_Entry'); | |
1831 | if((empty($form->_contributorDisplayName) || empty($form->_contributorEmail)) || $isBatchProcess){ | |
1832 | // in this case the form is being called statically from the batch editing screen | |
1833 | // having one class in the form layer call another statically is not greate | |
1834 | // & we should aim to move this function to the BAO layer in future. | |
1835 | // however, we can assume that the contact_id passed in by the batch | |
1836 | // function will be the recipient | |
1837 | list( | |
1838 | $form->_contributorDisplayName, | |
1839 | $form->_contributorEmail | |
1840 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails( | |
1841 | $formValues['contact_id'] | |
1842 | ); | |
1843 | if(empty($form->_receiptContactId) || $isBatchProcess){ | |
1844 | $form->_receiptContactId = $formValues['contact_id']; | |
1845 | } | |
1846 | } | |
1847 | ||
1848 | list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate( | |
1849 | array( | |
1850 | 'groupName' => 'msg_tpl_workflow_membership', | |
1851 | 'valueName' => 'membership_offline_receipt', | |
1852 | 'contactId' => $form->_receiptContactId, | |
1853 | 'from' => $receiptFrom, | |
1854 | 'toName' => $form->_contributorDisplayName, | |
1855 | 'toEmail' => $form->_contributorEmail, | |
1856 | 'isTest' => (bool)($form->_action & CRM_Core_Action::PREVIEW) | |
1857 | ) | |
1858 | ); | |
1859 | ||
1860 | return true; | |
1861 | } | |
1862 | } | |
1863 |