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