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