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 | * form to process actions on the group aspect of Custom Data | |
38 | */ | |
39 | class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_ContributionBase { | |
40 | ||
41 | /** | |
42 | * the id of the contact associated with this contribution | |
43 | * | |
44 | * @var int | |
45 | * @public | |
46 | */ | |
47 | public $_contactID; | |
48 | ||
1f1740fe DL |
49 | |
50 | /** | |
51 | * The id of the contribution object that is created when the form is submitted | |
52 | * | |
53 | * @var int | |
54 | * @public | |
55 | */ | |
56 | public $_contributionID; | |
57 | ||
c8bde7ea EM |
58 | /** |
59 | * Set the parameters to be passed to contribution create function | |
60 | * | |
61 | * @param array $params | |
62 | * @param integer $contactID | |
63 | * @param $financialTypeID | |
64 | * @param $online | |
65 | * @param $contributionPageId | |
66 | * @param $nonDeductibleAmount | |
67 | * @param $campaignId | |
68 | * | |
69 | * @param $isMonetary | |
70 | * | |
71 | * @param $pending | |
72 | * @param $paymentProcessorOutcome | |
73 | * @param $receiptDate | |
74 | * | |
75 | * @param $recurringContributionID | |
76 | * @param $isTest | |
77 | * | |
78 | * @param $addressID | |
79 | * | |
80 | * @param $softCreditToID | |
81 | * | |
82 | * @param $lineItems | |
83 | * | |
84 | * @internal param $financialType | |
85 | * @return array | |
86 | */ | |
87 | public static function getContributionParams($params, $contactID, $financialTypeID, $online, $contributionPageId, $nonDeductibleAmount, $campaignId, $isMonetary, $pending, | |
88 | $paymentProcessorOutcome, $receiptDate, $recurringContributionID, $isTest, $addressID, $softCreditToID, $lineItems) | |
89 | { | |
90 | $contributionParams = array( | |
91 | 'contact_id' => $contactID, | |
92 | 'financial_type_id' => $financialTypeID, | |
93 | 'contribution_page_id' => $contributionPageId, | |
94 | 'receive_date' => (CRM_Utils_Array::value('receive_date', $params)) ? CRM_Utils_Date::processDate($params['receive_date']) : date('YmdHis'), | |
95 | 'non_deductible_amount' => $nonDeductibleAmount, | |
96 | 'total_amount' => $params['amount'], | |
97 | 'amount_level' => CRM_Utils_Array::value('amount_level', $params), | |
98 | 'invoice_id' => $params['invoiceID'], | |
99 | 'currency' => $params['currencyID'], | |
100 | 'source' => | |
101 | (!$online || !empty($params['source'])) ? | |
102 | CRM_Utils_Array::value('source', $params) : | |
103 | CRM_Utils_Array::value('description', $params), | |
104 | 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), | |
105 | //configure cancel reason, cancel date and thankyou date | |
106 | //from 'contribution' type profile if included | |
107 | 'cancel_reason' => CRM_Utils_Array::value('cancel_reason', $params, 0), | |
108 | 'cancel_date' => | |
109 | isset($params['cancel_date']) ? | |
110 | CRM_Utils_Date::format($params['cancel_date']) : | |
111 | NULL, | |
112 | 'thankyou_date' => | |
113 | isset($params['thankyou_date']) ? | |
114 | CRM_Utils_Date::format($params['thankyou_date']) : | |
115 | NULL, | |
116 | 'campaign_id' => $campaignId, | |
117 | 'is_test' => $isTest, | |
118 | 'address_id' => $addressID, | |
119 | //setting to make available to hook - although seems wrong to set on form for BAO hook availability | |
120 | 'soft_credit_to' => $softCreditToID, | |
b205ce09 PN |
121 | 'line_item' => $lineItems, |
122 | 'skipLineItem' => CRM_Utils_Array::value('skipLineItem', $params, 0) | |
c8bde7ea EM |
123 | ); |
124 | if (!$online && isset($params['thankyou_date'])) { | |
125 | $contributionParam['thankyou_date'] = $params['thankyou_date']; | |
126 | } | |
127 | if (!$online || $isMonetary) { | |
128 | if (empty($params['is_pay_later'])) { | |
129 | $contributionParams['payment_instrument_id'] = 1; | |
130 | } | |
131 | } | |
132 | if (!$pending && $paymentProcessorOutcome) { | |
133 | $contributionParams += array( | |
134 | 'fee_amount' => CRM_Utils_Array::value('fee_amount', $paymentProcessorOutcome), | |
135 | 'net_amount' => CRM_Utils_Array::value('net_amount', $paymentProcessorOutcome, $params['amount']), | |
136 | 'trxn_id' => $paymentProcessorOutcome['trxn_id'], | |
137 | 'receipt_date' => $receiptDate, | |
138 | // also add financial_trxn details as part of fix for CRM-4724 | |
139 | 'trxn_result_code' => CRM_Utils_Array::value('trxn_result_code', $paymentProcessorOutcome), | |
140 | 'payment_processor' => CRM_Utils_Array::value('payment_processor', $paymentProcessorOutcome), | |
141 | ); | |
142 | } | |
143 | ||
144 | // CRM-4038: for non-en_US locales, CRM_Contribute_BAO_Contribution::add() expects localised amounts | |
145 | $contributionParams['non_deductible_amount'] = trim(CRM_Utils_Money::format($contributionParams['non_deductible_amount'], ' ')); | |
146 | $contributionParams['total_amount'] = trim(CRM_Utils_Money::format($contributionParams['total_amount'], ' ')); | |
147 | ||
148 | if ($recurringContributionID) { | |
149 | $contributionParams['contribution_recur_id'] = $recurringContributionID; | |
150 | } | |
151 | ||
152 | $contributionParams['contribution_status_id'] = $pending ? 2 : 1; | |
153 | if (isset($contributionParams['invoice_id'])) { | |
154 | $contributionParams['id'] = CRM_Core_DAO::getFieldValue( | |
155 | 'CRM_Contribute_DAO_Contribution', | |
156 | $contributionParams['invoice_id'], | |
157 | 'id', | |
158 | 'invoice_id' | |
159 | ); | |
160 | } | |
161 | ||
162 | return $contributionParams; | |
163 | } | |
164 | ||
6a488035 TO |
165 | /** |
166 | * Function to set variables up before form is built | |
167 | * | |
168 | * @return void | |
169 | * @access public | |
170 | */ | |
171 | public function preProcess() { | |
172 | $config = CRM_Core_Config::singleton(); | |
173 | parent::preProcess(); | |
cde484fd | 174 | |
6a488035 TO |
175 | // lineItem isn't set until Register postProcess |
176 | $this->_lineItem = $this->get('lineItem'); | |
177 | $this->_paymentProcessor = $this->get('paymentProcessor'); | |
cde484fd | 178 | |
6a488035 TO |
179 | if ($this->_contributeMode == 'express') { |
180 | // rfp == redirect from paypal | |
181 | $rfp = CRM_Utils_Request::retrieve('rfp', 'Boolean', | |
182 | CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET' | |
183 | ); | |
184 | if ($rfp) { | |
185 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); | |
186 | $expressParams = $payment->getExpressCheckoutDetails($this->get('token')); | |
187 | ||
188 | $this->_params['payer'] = $expressParams['payer']; | |
189 | $this->_params['payer_id'] = $expressParams['payer_id']; | |
190 | $this->_params['payer_status'] = $expressParams['payer_status']; | |
191 | ||
192 | CRM_Core_Payment_Form::mapParams($this->_bltID, $expressParams, $this->_params, FALSE); | |
cde484fd | 193 | |
6a488035 | 194 | // fix state and country id if present |
f0bf6c15 | 195 | if (!empty($this->_params["billing_state_province_id-{$this->_bltID}"])) { |
6a488035 TO |
196 | $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); |
197 | } | |
198 | if (!empty($this->_params["billing_country_id-{$this->_bltID}"]) && $this->_params["billing_country_id-{$this->_bltID}"]) { | |
199 | $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
200 | } | |
201 | ||
202 | // set a few other parameters for PayPal | |
203 | $this->_params['token'] = $this->get('token'); | |
204 | ||
205 | $this->_params['amount'] = $this->get('amount'); | |
206 | ||
207 | if (!empty($this->_membershipBlock)){ | |
208 | $this->_params['selectMembership'] = $this->get('selectMembership'); | |
209 | } | |
210 | // we use this here to incorporate any changes made by folks in hooks | |
211 | $this->_params['currencyID'] = $config->defaultCurrency; | |
212 | ||
213 | $this->_params['payment_action'] = 'Sale'; | |
214 | ||
215 | // also merge all the other values from the profile fields | |
216 | $values = $this->controller->exportValues('Main'); | |
217 | $skipFields = array( | |
218 | 'amount', 'amount_other', | |
219 | "billing_street_address-{$this->_bltID}", | |
220 | "billing_city-{$this->_bltID}", | |
221 | "billing_state_province_id-{$this->_bltID}", | |
222 | "billing_postal_code-{$this->_bltID}", | |
223 | "billing_country_id-{$this->_bltID}", | |
224 | ); | |
225 | foreach ($values as $name => $value) { | |
226 | // skip amount field | |
227 | if (!in_array($name, $skipFields)) { | |
228 | $this->_params[$name] = $value; | |
229 | } | |
230 | } | |
231 | $this->set('getExpressCheckoutDetails', $this->_params); | |
232 | } | |
233 | else { | |
234 | $this->_params = $this->get('getExpressCheckoutDetails'); | |
235 | } | |
236 | } | |
237 | else { | |
238 | $this->_params = $this->controller->exportValues('Main'); | |
239 | ||
240 | if (!empty($this->_params["billing_state_province_id-{$this->_bltID}"])) { | |
241 | $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); | |
242 | } | |
243 | if (!empty($this->_params["billing_country_id-{$this->_bltID}"])) { | |
244 | $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
245 | } | |
246 | ||
247 | if (isset($this->_params['credit_card_exp_date'])) { | |
248 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
249 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
250 | } | |
5df36634 PJ |
251 | |
252 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); | |
6a488035 TO |
253 | $this->_params['amount'] = $this->get('amount'); |
254 | ||
255 | $this->_useForMember = $this->get('useForMember'); | |
cde484fd | 256 | |
6a488035 | 257 | if (isset($this->_params['amount'])) { |
df6c4f28 | 258 | $this->setFormAmountFields($this->_params['priceSetId']); |
6a488035 TO |
259 | } |
260 | $this->_params['currencyID'] = $config->defaultCurrency; | |
261 | $this->_params['payment_action'] = 'Sale'; | |
262 | } | |
263 | ||
264 | $this->_params['is_pay_later'] = $this->get('is_pay_later'); | |
265 | $this->assign('is_pay_later', $this->_params['is_pay_later']); | |
266 | if ($this->_params['is_pay_later']) { | |
267 | $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']); | |
268 | } | |
269 | // if onbehalf-of-organization | |
a7488080 | 270 | if (!empty($this->_params['hidden_onbehalf_profile'])) { |
8cc574cf | 271 | if (!empty($this->_params['org_option']) && !empty($this->_params['organization_id'])) { |
a7488080 | 272 | if (!empty($this->_params['onbehalfof_id'])) { |
6a488035 TO |
273 | $this->_params['organization_id'] = $this->_params['onbehalfof_id']; |
274 | } | |
275 | } | |
276 | ||
277 | $this->_params['organization_name'] = $this->_params['onbehalf']['organization_name']; | |
278 | $addressBlocks = array( | |
279 | 'street_address', 'city', 'state_province', | |
280 | 'postal_code', 'country', 'supplemental_address_1', | |
281 | 'supplemental_address_2', 'supplemental_address_3', | |
282 | 'postal_code_suffix', 'geo_code_1', 'geo_code_2', 'address_name', | |
283 | ); | |
284 | ||
285 | $blocks = array('email', 'phone', 'im', 'url', 'openid'); | |
286 | foreach ($this->_params['onbehalf'] as $loc => $value) { | |
287 | $field = $typeId = NULL; | |
288 | if (strstr($loc, '-')) { | |
289 | list($field, $locType) = explode('-', $loc); | |
290 | } | |
291 | ||
292 | if (in_array($field, $addressBlocks)) { | |
293 | if ($locType == 'Primary') { | |
294 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
295 | $locType = $defaultLocationType->id; | |
cde484fd DL |
296 | } |
297 | ||
6a488035 TO |
298 | if ($field == 'country') { |
299 | $value = CRM_Core_PseudoConstant::countryIsoCode($value); | |
300 | } | |
301 | elseif ($field == 'state_province') { | |
302 | $value = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); | |
303 | } | |
cde484fd | 304 | |
6a488035 TO |
305 | $isPrimary = 1; |
306 | if (isset($this->_params['onbehalf_location']['address']) | |
307 | && count($this->_params['onbehalf_location']['address']) > 0) { | |
308 | $isPrimary = 0; | |
309 | } | |
cde484fd | 310 | |
6a488035 | 311 | $this->_params['onbehalf_location']['address'][$locType][$field] = $value; |
a7488080 | 312 | if (empty($this->_params['onbehalf_location']['address'][$locType]['is_primary'])) { |
6a488035 TO |
313 | $this->_params['onbehalf_location']['address'][$locType]['is_primary'] = $isPrimary; |
314 | } | |
315 | $this->_params['onbehalf_location']['address'][$locType]['location_type_id'] = $locType; | |
316 | } | |
317 | elseif (in_array($field, $blocks)) { | |
318 | if (!$typeId || is_numeric($typeId)) { | |
319 | $blockName = $fieldName = $field; | |
320 | $locationType = 'location_type_id'; | |
321 | if ( $locType == 'Primary' ) { | |
322 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
323 | $locationValue = $defaultLocationType->id; | |
324 | } | |
325 | else { | |
326 | $locationValue = $locType; | |
327 | } | |
328 | $locTypeId = ''; | |
329 | $phoneExtField = array(); | |
330 | ||
331 | if ($field == 'url') { | |
332 | $blockName = 'website'; | |
887e764d PN |
333 | $locationType = 'website_type_id'; |
334 | list($field, $locationValue) = explode('-', $loc); | |
6a488035 TO |
335 | } |
336 | elseif ($field == 'im') { | |
337 | $fieldName = 'name'; | |
338 | $locTypeId = 'provider_id'; | |
339 | $typeId = $this->_params['onbehalf']["{$loc}-provider_id"]; | |
340 | } | |
341 | elseif ($field == 'phone') { | |
342 | list($field, $locType, $typeId) = explode('-', $loc); | |
343 | $locTypeId = 'phone_type_id'; | |
344 | ||
345 | //check if extension field exists | |
346 | $extField = str_replace('phone','phone_ext', $loc); | |
347 | if (isset($this->_params['onbehalf'][$extField])) { | |
348 | $phoneExtField = array('phone_ext' => $this->_params['onbehalf'][$extField]); | |
349 | } | |
350 | } | |
351 | ||
352 | $isPrimary = 1; | |
353 | if ( isset ($this->_params['onbehalf_location'][$blockName] ) | |
354 | && count( $this->_params['onbehalf_location'][$blockName] ) > 0 ) { | |
355 | $isPrimary = 0; | |
356 | } | |
357 | if ($locationValue) { | |
358 | $blockValues = array( | |
359 | $fieldName => $value, | |
360 | $locationType => $locationValue, | |
361 | 'is_primary' => $isPrimary, | |
362 | ); | |
363 | ||
364 | if ($locTypeId) { | |
365 | $blockValues = array_merge($blockValues, array($locTypeId => $typeId)); | |
366 | } | |
367 | if (!empty($phoneExtField)) { | |
368 | $blockValues = array_merge($blockValues, $phoneExtField); | |
369 | } | |
370 | ||
371 | $this->_params['onbehalf_location'][$blockName][] = $blockValues; | |
372 | } | |
373 | } | |
374 | } | |
375 | elseif (strstr($loc, 'custom')) { | |
376 | if ($value && isset($this->_params['onbehalf']["{$loc}_id"])) { | |
377 | $value = $this->_params['onbehalf']["{$loc}_id"]; | |
378 | } | |
379 | $this->_params['onbehalf_location']["{$loc}"] = $value; | |
380 | } | |
381 | else { | |
382 | if ($loc == 'contact_sub_type') { | |
383 | $this->_params['onbehalf_location'][$loc] = $value; | |
384 | } | |
385 | else { | |
386 | $this->_params['onbehalf_location'][$field] = $value; | |
387 | } | |
388 | } | |
389 | } | |
390 | } | |
a7488080 | 391 | elseif (!empty($this->_values['is_for_organization'])) { |
6a488035 TO |
392 | // no on behalf of an organization, CRM-5519 |
393 | // so reset loc blocks from main params. | |
394 | foreach (array( | |
395 | 'phone', 'email', 'address') as $blk) { | |
396 | if (isset($this->_params[$blk])) { | |
397 | unset($this->_params[$blk]); | |
398 | } | |
399 | } | |
400 | } | |
401 | ||
402 | // if auto renew checkbox is set, initiate a open-ended recurring membership | |
8cc574cf CW |
403 | if ((!empty($this->_params['selectMembership']) || !empty($this->_params['priceSetId'])) && !empty($this->_paymentProcessor['is_recur']) && |
404 | CRM_Utils_Array::value('auto_renew', $this->_params) && empty($this->_params['is_recur']) && empty($this->_params['frequency_interval'])) { | |
6a488035 TO |
405 | |
406 | $this->_params['is_recur'] = $this->_values['is_recur'] = 1; | |
407 | // check if price set is not quick config | |
d80f2618 | 408 | if (!empty($this->_params['priceSetId']) && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config')) { |
9da8dc8c | 409 | list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_PriceSet::getRecurDetails($this->_params['priceSetId']); |
6a488035 TO |
410 | } |
411 | else { | |
412 | // FIXME: set interval and unit based on selected membership type | |
413 | $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
414 | $this->_params['selectMembership'], 'duration_interval' | |
415 | ); | |
416 | $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
417 | $this->_params['selectMembership'], 'duration_unit' | |
418 | ); | |
419 | } | |
420 | } | |
421 | ||
422 | if ($this->_pcpId) { | |
423 | $params = $this->processPcp($this, $this->_params); | |
424 | $this->_params = $params; | |
425 | } | |
426 | $this->_params['invoiceID'] = $this->get('invoiceID'); | |
427 | ||
428 | //carry campaign from profile. | |
429 | if (array_key_exists('contribution_campaign_id', $this->_params)) { | |
430 | $this->_params['campaign_id'] = $this->_params['contribution_campaign_id']; | |
431 | } | |
432 | ||
569df8ec DG |
433 | // assign contribution page id to the template so we can add css class for it |
434 | $this->assign('contributionPageID', $this->_id); | |
435 | ||
6a488035 TO |
436 | $this->set('params', $this->_params); |
437 | } | |
438 | ||
439 | /** | |
440 | * Function to actually build the form | |
441 | * | |
442 | * @return void | |
443 | * @access public | |
444 | */ | |
445 | public function buildQuickForm() { | |
446 | $this->assignToTemplate(); | |
447 | ||
448 | $params = $this->_params; | |
6a488035 | 449 | // make sure we have values for it |
8af73472 | 450 | if ($this->_honor_block_is_active && !empty($params['soft_credit_type_id'])) { |
133e2c99 | 451 | $honorName = null; |
452 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
453 | ||
8af73472 | 454 | $this->assign('honor_block_is_active', $this->_honor_block_is_active); |
133e2c99 | 455 | $this->assign('soft_credit_type', $softCreditTypes[$params['soft_credit_type_id']]); |
1421174e | 456 | CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields($this, $params['honor'], $params['honoree_profile_id']); |
6a488035 | 457 | |
133e2c99 | 458 | $fieldTypes = array('Contact'); |
459 | $fieldTypes[] = CRM_Core_BAO_UFGroup::getContactType($params['honoree_profile_id']); | |
460 | $this->buildCustom($params['honoree_profile_id'], 'honoreeProfileFields', TRUE, 'honor', $fieldTypes); | |
6a488035 TO |
461 | } |
462 | $this->assign('receiptFromEmail', CRM_Utils_Array::value('receipt_from_email', $this->_values)); | |
463 | $amount_block_is_active = $this->get('amount_block_is_active'); | |
464 | $this->assign('amount_block_is_active', $amount_block_is_active); | |
465 | ||
a7488080 | 466 | if (!empty($params['selectProduct']) && $params['selectProduct'] != 'no_thanks') { |
6a488035 TO |
467 | $option = CRM_Utils_Array::value('options_' . $params['selectProduct'], $params); |
468 | $productID = $params['selectProduct']; | |
469 | CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, FALSE, | |
470 | $productID, $option | |
471 | ); | |
472 | $this->set('productID', $productID); | |
473 | $this->set('option', $option); | |
474 | } | |
475 | $config = CRM_Core_Config::singleton(); | |
476 | if (in_array('CiviMember', $config->enableComponents)) { | |
477 | if (isset($params['selectMembership']) && | |
478 | $params['selectMembership'] != 'no_thanks' | |
479 | ) { | |
480 | CRM_Member_BAO_Membership::buildMembershipBlock($this, | |
481 | $this->_id, | |
5b757295 | 482 | $this->_membershipContactID, |
6a488035 TO |
483 | FALSE, |
484 | $params['selectMembership'], | |
5b757295 | 485 | FALSE |
6a488035 TO |
486 | ); |
487 | } | |
488 | else { | |
489 | $this->assign('membershipBlock', FALSE); | |
490 | } | |
491 | } | |
492 | $this->buildCustom($this->_values['custom_pre_id'], 'customPre', TRUE); | |
493 | $this->buildCustom($this->_values['custom_post_id'], 'customPost', TRUE); | |
494 | ||
a7488080 | 495 | if (!empty($params['hidden_onbehalf_profile'])) { |
6a488035 TO |
496 | $ufJoinParams = array( |
497 | 'module' => 'onBehalf', | |
498 | 'entity_table' => 'civicrm_contribution_page', | |
499 | 'entity_id' => $this->_id, | |
500 | ); | |
501 | $OnBehalfProfile = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
502 | $profileId = $OnBehalfProfile[0]; | |
503 | ||
504 | $fieldTypes = array('Contact', 'Organization'); | |
505 | $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); | |
506 | $fieldTypes = array_merge($fieldTypes, $contactSubType); | |
507 | if (is_array($this->_membershipBlock) && !empty($this->_membershipBlock)) { | |
508 | $fieldTypes = array_merge($fieldTypes, array('Membership')); | |
509 | } | |
510 | else { | |
511 | $fieldTypes = array_merge($fieldTypes, array('Contribution')); | |
512 | } | |
513 | ||
133e2c99 | 514 | $this->buildCustom($profileId, 'onbehalfProfile', TRUE, 'onbehalf', $fieldTypes); |
6a488035 TO |
515 | } |
516 | ||
517 | $this->_separateMembershipPayment = $this->get('separateMembershipPayment'); | |
518 | $this->assign('is_separate_payment', $this->_separateMembershipPayment); | |
9da8dc8c | 519 | if ($this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
6a488035 TO |
520 | $this->assign('lineItem', $this->_lineItem); |
521 | } else { | |
522 | $this->assign('is_quick_config', 1); | |
523 | $this->_params['is_quick_config'] = 1; | |
524 | } | |
525 | $this->assign('priceSetID', $this->_priceSetId); | |
526 | $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(false, null, 'name'); | |
f92fc7eb CW |
527 | if ($this->_paymentProcessor && |
528 | $this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('Google_Checkout', $paymentProcessorType) | |
6a488035 TO |
529 | && !$this->_params['is_pay_later'] && !($this->_amount == 0) |
530 | ) { | |
531 | $this->_checkoutButtonName = $this->getButtonName('next', 'checkout'); | |
532 | $this->add('image', | |
533 | $this->_checkoutButtonName, | |
534 | $this->_paymentProcessor['url_button'], | |
97e557d7 | 535 | array('class' => 'crm-form-submit') |
6a488035 TO |
536 | ); |
537 | ||
538 | $this->addButtons(array( | |
539 | array( | |
540 | 'type' => 'back', | |
541 | 'name' => ts('<< Go Back'), | |
542 | ), | |
543 | ) | |
544 | ); | |
545 | } | |
546 | else { | |
547 | if ($this->_contributeMode == 'notify' || !$this->_values['is_monetary'] || | |
548 | $this->_amount <= 0.0 || $this->_params['is_pay_later'] || | |
549 | ($this->_separateMembershipPayment && $this->_amount <= 0.0) | |
550 | ) { | |
551 | $contribButton = ts('Continue >>'); | |
552 | $this->assign('button', ts('Continue')); | |
553 | } | |
554 | else { | |
555 | $contribButton = ts('Make Contribution'); | |
556 | $this->assign('button', ts('Make Contribution')); | |
557 | } | |
558 | $this->addButtons(array( | |
559 | array( | |
560 | 'type' => 'next', | |
561 | 'name' => $contribButton, | |
562 | 'spacing' => ' ', | |
563 | 'isDefault' => TRUE, | |
c6edd786 | 564 | 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"), |
6a488035 TO |
565 | ), |
566 | array( | |
567 | 'type' => 'back', | |
568 | 'name' => ts('Go Back'), | |
569 | ), | |
570 | ) | |
571 | ); | |
572 | } | |
573 | ||
574 | $defaults = array(); | |
575 | $fields = array(); | |
576 | foreach ($this->_fields as $name => $dontCare) { | |
133e2c99 | 577 | if ($name != 'onbehalf' || $name != 'honor') { |
6a488035 TO |
578 | $fields[$name] = 1; |
579 | } | |
580 | } | |
581 | $fields["billing_state_province-{$this->_bltID}"] = $fields["billing_country-{$this->_bltID}"] = $fields["email-{$this->_bltID}"] = 1; | |
582 | ||
583 | $contact = $this->_params; | |
584 | foreach ($fields as $name => $dontCare) { | |
133e2c99 | 585 | if (isset($contact[$name])) { |
6a488035 TO |
586 | $defaults[$name] = $contact[$name]; |
587 | if (substr($name, 0, 7) == 'custom_') { | |
588 | $timeField = "{$name}_time"; | |
589 | if (isset($contact[$timeField])) { | |
590 | $defaults[$timeField] = $contact[$timeField]; | |
591 | } | |
592 | if (isset($contact["{$name}_id"])) { | |
593 | $defaults["{$name}_id"] = $contact["{$name}_id"]; | |
594 | } | |
595 | } | |
8cc574cf | 596 | elseif (in_array($name, array('addressee', 'email_greeting', 'postal_greeting')) && !empty($contact[$name . '_custom'])) { |
6a488035 TO |
597 | $defaults[$name . '_custom'] = $contact[$name . '_custom']; |
598 | } | |
599 | } | |
600 | } | |
601 | ||
602 | $this->assign('useForMember', $this->get('useForMember')); | |
603 | ||
6a488035 TO |
604 | $this->setDefaults($defaults); |
605 | ||
606 | $this->freeze(); | |
607 | } | |
608 | ||
609 | /** | |
610 | * overwrite action, since we are only showing elements in frozen mode | |
611 | * no help display needed | |
612 | * | |
613 | * @return int | |
614 | * @access public | |
615 | */ | |
616 | function getAction() { | |
617 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
618 | return CRM_Core_Action::VIEW | CRM_Core_Action::PREVIEW; | |
619 | } | |
620 | else { | |
621 | return CRM_Core_Action::VIEW; | |
622 | } | |
623 | } | |
624 | ||
625 | /** | |
626 | * This function sets the default values for the form. Note that in edit/view mode | |
627 | * the default values are retrieved from the database | |
628 | * | |
629 | * @access public | |
630 | * | |
631 | * @return void | |
632 | */ | |
633 | function setDefaultValues() {} | |
634 | ||
635 | /** | |
636 | * Process the form | |
637 | * | |
638 | * @return void | |
639 | * @access public | |
640 | */ | |
641 | public function postProcess() { | |
da8d9879 | 642 | $contactID = $this->getContactID(); |
6a488035 TO |
643 | |
644 | // add a description field at the very beginning | |
645 | $this->_params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']); | |
646 | ||
647 | // also add accounting code | |
648 | $this->_params['accountingCode'] = CRM_Utils_Array::value('accountingCode', | |
649 | $this->_values | |
650 | ); | |
651 | ||
652 | // fix currency ID | |
dbddfb08 | 653 | $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency; |
6a488035 | 654 | |
6a488035 TO |
655 | //carry payment processor id. |
656 | if ($paymentProcessorId = CRM_Utils_Array::value('id', $this->_paymentProcessor)) { | |
657 | $this->_params['payment_processor_id'] = $paymentProcessorId; | |
6a488035 | 658 | } |
a7488080 | 659 | if (!empty($params['image_URL'])) { |
6a488035 TO |
660 | CRM_Contact_BAO_Contact::processImageParams($params); |
661 | } | |
e9599dfa | 662 | $premiumParams = $membershipParams = $params = $this->_params; |
99fffb1f | 663 | $fields = array('email-Primary' => 1); |
6a488035 TO |
664 | |
665 | // get the add to groups | |
666 | $addToGroups = array(); | |
667 | ||
668 | // now set the values for the billing location. | |
669 | foreach ($this->_fields as $name => $value) { | |
670 | $fields[$name] = 1; | |
671 | ||
672 | // get the add to groups for uf fields | |
a7488080 | 673 | if (!empty($value['add_to_group_id'])) { |
6a488035 TO |
674 | $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id']; |
675 | } | |
676 | } | |
677 | ||
678 | if (!array_key_exists('first_name', $fields)) { | |
679 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
680 | foreach ($nameFields as $name) { | |
681 | $fields[$name] = 1; | |
682 | if (array_key_exists("billing_$name", $params)) { | |
683 | $params[$name] = $params["billing_{$name}"]; | |
684 | $params['preserveDBName'] = TRUE; | |
685 | } | |
686 | } | |
687 | } | |
688 | ||
689 | // billing email address | |
690 | $fields["email-{$this->_bltID}"] = 1; | |
691 | ||
692 | //unset the billing parameters if it is pay later mode | |
693 | //to avoid creation of billing location | |
694 | if ($params['is_pay_later']) { | |
695 | $billingFields = array( | |
696 | 'billing_first_name', | |
697 | 'billing_middle_name', | |
698 | 'billing_last_name', | |
699 | "billing_street_address-{$this->_bltID}", | |
700 | "billing_city-{$this->_bltID}", | |
701 | "billing_state_province-{$this->_bltID}", | |
702 | "billing_state_province_id-{$this->_bltID}", | |
703 | "billing_postal_code-{$this->_bltID}", | |
704 | "billing_country-{$this->_bltID}", | |
705 | "billing_country_id-{$this->_bltID}", | |
706 | ); | |
707 | ||
708 | foreach ($billingFields as $value) { | |
709 | unset($params[$value]); | |
710 | unset($fields[$value]); | |
711 | } | |
712 | } | |
713 | ||
714 | // if onbehalf-of-organization contribution, take out | |
715 | // organization params in a separate variable, to make sure | |
716 | // normal behavior is continued. And use that variable to | |
717 | // process on-behalf-of functionality. | |
a7488080 | 718 | if (!empty($this->_params['hidden_onbehalf_profile'])) { |
6a488035 TO |
719 | $behalfOrganization = array(); |
720 | $orgFields = array('organization_name', 'organization_id', 'org_option'); | |
721 | foreach ($orgFields as $fld) { | |
722 | if (array_key_exists($fld, $params)) { | |
723 | $behalfOrganization[$fld] = $params[$fld]; | |
724 | unset($params[$fld]); | |
725 | } | |
726 | } | |
727 | ||
728 | if (is_array($params['onbehalf']) && !empty($params['onbehalf'])) { | |
729 | foreach ($params['onbehalf'] as $fld => $values) { | |
730 | if (strstr($fld, 'custom_')) { | |
731 | $behalfOrganization[$fld] = $values; | |
cde484fd | 732 | } |
6a488035 TO |
733 | elseif (!(strstr($fld, '-'))) { |
734 | if (in_array($fld, array( | |
735 | 'contribution_campaign_id', 'member_campaign_id'))) { | |
736 | $fld = 'campaign_id'; | |
737 | } | |
738 | else { | |
739 | $behalfOrganization[$fld] = $values; | |
cde484fd | 740 | } |
6a488035 TO |
741 | $this->_params[$fld] = $values; |
742 | } | |
743 | } | |
744 | } | |
745 | ||
746 | if (array_key_exists('onbehalf_location', $params) && is_array($params['onbehalf_location'])) { | |
747 | foreach ($params['onbehalf_location'] as $block => $vals) { | |
748 | //fix for custom data (of type checkbox, multi-select) | |
749 | if ( substr($block, 0, 7) == 'custom_' ) { | |
750 | continue; | |
751 | } | |
752 | // fix the index of block elements | |
753 | if (is_array($vals) ) { | |
754 | foreach ( $vals as $key => $val ) { | |
755 | //dont adjust the index of address block as | |
756 | //it's index is WRT to location type | |
757 | $newKey = ($block == 'address') ? $key : ++$key; | |
758 | $behalfOrganization[$block][$newKey] = $val; | |
759 | } | |
760 | } | |
761 | } | |
762 | unset($params['onbehalf_location']); | |
763 | } | |
a7488080 | 764 | if (!empty($params['onbehalf[image_URL]'])) { |
6a488035 TO |
765 | $behalfOrganization['image_URL'] = $params['onbehalf[image_URL]']; |
766 | } | |
767 | } | |
768 | ||
769 | // check for profile double opt-in and get groups to be subscribed | |
770 | $subscribeGroupIds = CRM_Core_BAO_UFGroup::getDoubleOptInGroupIds($params, $contactID); | |
771 | ||
772 | // since we are directly adding contact to group lets unset it from mailing | |
773 | if (!empty($addToGroups)) { | |
774 | foreach ($addToGroups as $groupId) { | |
775 | if (isset($subscribeGroupIds[$groupId])) { | |
776 | unset($subscribeGroupIds[$groupId]); | |
777 | } | |
778 | } | |
779 | } | |
780 | ||
781 | foreach ($addToGroups as $k) { | |
782 | if (array_key_exists($k, $subscribeGroupIds)) { | |
783 | unset($addToGroups[$k]); | |
784 | } | |
785 | } | |
786 | ||
da8d9879 | 787 | if (empty($contactID)) { |
6a488035 | 788 | $dupeParams = $params; |
a7488080 | 789 | if (!empty($dupeParams['onbehalf'])) { |
6a488035 TO |
790 | unset($dupeParams['onbehalf']); |
791 | } | |
792 | ||
793 | $dedupeParams = CRM_Dedupe_Finder::formatParams($dupeParams, 'Individual'); | |
794 | $dedupeParams['check_permission'] = FALSE; | |
795 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); | |
796 | ||
797 | // if we find more than one contact, use the first one | |
99fffb1f | 798 | $contactID = CRM_Utils_Array::value(0, $ids); |
6a488035 TO |
799 | |
800 | // Fetch default greeting id's if creating a contact | |
99fffb1f | 801 | if (!$contactID) { |
6a488035 TO |
802 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { |
803 | if (!isset($params[$greeting])) { | |
804 | $params[$greeting] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting); | |
805 | } | |
806 | } | |
807 | } | |
99fffb1f | 808 | $contactType = NULL; |
6a488035 TO |
809 | } |
810 | else { | |
99fffb1f | 811 | $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'contact_type'); |
6a488035 | 812 | } |
99fffb1f EM |
813 | $contactID = CRM_Contact_BAO_Contact::createProfileContact( |
814 | $params, | |
815 | $fields, | |
816 | $contactID, | |
817 | $addToGroups, | |
818 | NULL, | |
819 | $contactType, | |
820 | TRUE | |
821 | ); | |
6a488035 TO |
822 | |
823 | // Make the contact ID associated with the contribution available at the Class level. | |
824 | // Also make available to the session. | |
596bff78 | 825 | //@todo consider handling this in $this->getContactID(); |
6a488035 TO |
826 | $this->set('contactID', $contactID); |
827 | $this->_contactID = $contactID; | |
828 | ||
829 | //get email primary first if exist | |
b288b9c2 EM |
830 | $subscriptionEmail = array('email' => CRM_Utils_Array::value('email-Primary', $params)); |
831 | if (!$subscriptionEmail['email']) { | |
832 | $subscriptionEmail['email'] = CRM_Utils_Array::value("email-{$this->_bltID}", $params); | |
6a488035 TO |
833 | } |
834 | // subscribing contact to groups | |
b288b9c2 EM |
835 | if (!empty($subscribeGroupIds) && $subscriptionEmail['email']) { |
836 | CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($subscribeGroupIds, $subscriptionEmail, $contactID); | |
6a488035 TO |
837 | } |
838 | ||
839 | // If onbehalf-of-organization contribution / signup, add organization | |
840 | // and it's location. | |
841 | if (isset($params['hidden_onbehalf_profile']) && isset($behalfOrganization['organization_name'])) { | |
842 | $ufFields = array(); | |
843 | foreach ($this->_fields['onbehalf'] as $name => $value) { | |
844 | $ufFields[$name] = 1; | |
845 | } | |
846 | self::processOnBehalfOrganization($behalfOrganization, $contactID, $this->_values, | |
847 | $this->_params, $ufFields | |
848 | ); | |
6fe8deba | 849 | } else if (!empty($this->_membershipContactID) && $contactID != $this->_membershipContactID) { |
51fa20cb | 850 | // this is an onbehalf renew case for inherited membership. For e.g a permissioned member of household, |
51e89def DS |
851 | // store current user id as related contact for later use for mailing / activity.. |
852 | $this->_values['related_contact'] = $contactID; | |
853 | $this->_params['related_contact'] = $contactID; | |
854 | // swap contact like we do for on-behalf-org case, so parent/primary membership is affected | |
855 | $contactID = $this->_membershipContactID; | |
856 | } | |
857 | ||
6a488035 TO |
858 | // lets store the contactID in the session |
859 | // for things like tell a friend | |
860 | $session = CRM_Core_Session::singleton(); | |
861 | if (!$session->get('userID')) { | |
862 | $session->set('transaction.userID', $contactID); | |
863 | } | |
864 | else { | |
865 | $session->set('transaction.userID', NULL); | |
866 | } | |
867 | ||
868 | $this->_useForMember = $this->get('useForMember'); | |
869 | ||
870 | // store the fact that this is a membership and membership type is selected | |
871 | $processMembership = FALSE; | |
8cc574cf | 872 | if ((!empty($membershipParams['selectMembership']) && |
6a488035 TO |
873 | $membershipParams['selectMembership'] != 'no_thanks' |
874 | ) || | |
875 | $this->_useForMember | |
876 | ) { | |
877 | $processMembership = TRUE; | |
878 | ||
879 | if (!$this->_useForMember) { | |
880 | $this->assign('membership_assign', TRUE); | |
881 | $this->set('membershipTypeID', $this->_params['selectMembership']); | |
882 | } | |
883 | ||
884 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
885 | $membershipParams['is_test'] = 1; | |
886 | } | |
887 | if ($this->_params['is_pay_later']) { | |
888 | $membershipParams['is_pay_later'] = 1; | |
889 | } | |
890 | } | |
891 | ||
892 | if ($processMembership) { | |
893 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $membershipParams, TRUE); | |
894 | ||
895 | // added new parameter for cms user contact id, needed to distinguish behaviour for on behalf of sign-ups | |
896 | if (isset($this->_params['related_contact'])) { | |
897 | $membershipParams['cms_contactID'] = $this->_params['related_contact']; | |
898 | } | |
899 | else { | |
900 | $membershipParams['cms_contactID'] = $contactID; | |
901 | } | |
902 | ||
f64a217a | 903 | //inherit campaign from contribution page. |
6a488035 TO |
904 | if (!array_key_exists('campaign_id', $membershipParams)) { |
905 | $membershipParams['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values); | |
906 | } | |
907 | ||
908 | if (!empty($membershipParams['onbehalf']) && | |
8cc574cf | 909 | is_array($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) { |
6a488035 TO |
910 | $this->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id']; |
911 | } | |
912 | ||
913 | $customFieldsFormatted = $fieldTypes = array(); | |
914 | if (!empty($membershipParams['onbehalf']) && | |
915 | is_array($membershipParams['onbehalf'])) { | |
916 | foreach ($membershipParams['onbehalf'] as $key => $value) { | |
917 | if (strstr($key, 'custom_')) { | |
918 | $customFieldId = explode('_', $key); | |
919 | CRM_Core_BAO_CustomField::formatCustomField( | |
920 | $customFieldId[1], | |
921 | $customFieldsFormatted, | |
922 | $value, | |
923 | 'Membership', | |
924 | NULL, | |
925 | $contactID | |
926 | ); | |
927 | } | |
928 | } | |
929 | $fieldTypes = array('Contact', 'Organization', 'Membership'); | |
930 | } | |
931 | ||
932 | $priceFieldIds = $this->get('memberPriceFieldIDS'); | |
933 | ||
934 | if (!empty($priceFieldIds)) { | |
9da8dc8c | 935 | $contributionTypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceFieldIds['id'], 'financial_type_id'); |
6a488035 TO |
936 | unset($priceFieldIds['id']); |
937 | $membershipTypeIds = array(); | |
938 | $membershipTypeTerms = array(); | |
939 | foreach ($priceFieldIds as $priceFieldId) { | |
9da8dc8c | 940 | if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) { |
6a488035 | 941 | $membershipTypeIds[] = $id; |
dbddfb08 EM |
942 | //@todo the value for $term is immediately overwritten. It is unclear from the code whether it was intentional to |
943 | // do this or a double = was intended (this ambiguity is the reason many IDEs complain about 'assignment in condition' | |
6a488035 | 944 | $term = 1; |
9da8dc8c | 945 | if ($term = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_num_terms')) { |
6a488035 TO |
946 | $membershipTypeTerms[$id] = ($term > 1) ? $term : 1; |
947 | } | |
948 | else { | |
949 | $membershipTypeTerms[$id] = 1; | |
950 | } | |
951 | } | |
952 | } | |
953 | $membershipParams['selectMembership'] = $membershipTypeIds; | |
9ce8bcfc | 954 | $membershipParams['financial_type_id'] = $contributionTypeID; |
6a488035 TO |
955 | $membershipParams['types_terms'] = $membershipTypeTerms; |
956 | } | |
a7488080 | 957 | if (!empty($membershipParams['selectMembership'])) { |
9ce8bcfc | 958 | // CRM-12233 |
5624f515 | 959 | $membershipLineItems = array(); |
9ce8bcfc PN |
960 | if ($this->_separateMembershipPayment && $this->_values['amount_block_is_active']) { |
961 | foreach ($this->_values['fee'] as $key => $feeValues) { | |
5624f515 | 962 | if ($feeValues['name'] == 'membership_amount') { |
9ce8bcfc | 963 | $fieldId = $this->_params['price_' . $key]; |
5624f515 | 964 | $membershipLineItems[$this->_priceSetId][$fieldId] = $this->_lineItem[$this->_priceSetId][$fieldId]; |
9ce8bcfc PN |
965 | unset($this->_lineItem[$this->_priceSetId][$fieldId]); |
966 | break; | |
967 | } | |
968 | } | |
969 | } | |
5624f515 | 970 | $this->processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, $membershipLineItems); |
fa9d0451 EM |
971 | if (!$this->_amount > 0.0 || !$membershipParams['amount']) { |
972 | // we need to explicitly create a CMS user in case of free memberships | |
973 | // since it is done under processConfirm for paid memberships | |
974 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($membershipParams, | |
975 | $membershipParams['cms_contactID'], | |
976 | 'email-' . $this->_bltID | |
977 | ); | |
978 | } | |
9ce8bcfc | 979 | } |
6a488035 TO |
980 | } |
981 | else { | |
982 | // at this point we've created a contact and stored its address etc | |
983 | // all the payment processors expect the name and address to be in the | |
984 | // so we copy stuff over to first_name etc. | |
985 | $paymentParams = $this->_params; | |
157b21d8 | 986 | $contributionTypeId = $this->_values['financial_type_id']; |
6a488035 TO |
987 | |
988 | $fieldTypes = array(); | |
989 | if (!empty($paymentParams['onbehalf']) && | |
990 | is_array($paymentParams['onbehalf']) | |
991 | ) { | |
992 | foreach ($paymentParams['onbehalf'] as $key => $value) { | |
993 | if (strstr($key, 'custom_')) { | |
994 | $this->_params[$key] = $value; | |
995 | } | |
996 | } | |
997 | $fieldTypes = array('Contact', 'Organization', 'Contribution'); | |
998 | } | |
999 | ||
1000 | CRM_Contribute_BAO_Contribution_Utils::processConfirm($this, $paymentParams, | |
1001 | $premiumParams, $contactID, | |
1002 | $contributionTypeId, | |
1003 | 'contribution', | |
1004 | $fieldTypes | |
1005 | ); | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | /** | |
1010 | * Process the form | |
1011 | * | |
cd6994fc E |
1012 | * @param $premiumParams |
1013 | * @param $contribution | |
1014 | * | |
6a488035 TO |
1015 | * @return void |
1016 | * @access public | |
1017 | */ | |
1018 | public function postProcessPremium($premiumParams, $contribution) { | |
62c4420e | 1019 | $hour = $minute = $second = 0; |
6a488035 TO |
1020 | // assigning Premium information to receipt tpl |
1021 | $selectProduct = CRM_Utils_Array::value('selectProduct', $premiumParams); | |
1022 | if ($selectProduct && | |
1023 | $selectProduct != 'no_thanks' | |
1024 | ) { | |
1025 | $startDate = $endDate = ""; | |
1026 | $this->assign('selectPremium', TRUE); | |
1027 | $productDAO = new CRM_Contribute_DAO_Product(); | |
1028 | $productDAO->id = $selectProduct; | |
1029 | $productDAO->find(TRUE); | |
1030 | $this->assign('product_name', $productDAO->name); | |
1031 | $this->assign('price', $productDAO->price); | |
1032 | $this->assign('sku', $productDAO->sku); | |
1033 | $this->assign('option', CRM_Utils_Array::value('options_' . $premiumParams['selectProduct'], $premiumParams)); | |
1034 | ||
1035 | $periodType = $productDAO->period_type; | |
cde484fd | 1036 | |
6a488035 TO |
1037 | if ($periodType) { |
1038 | $fixed_period_start_day = $productDAO->fixed_period_start_day; | |
1039 | $duration_unit = $productDAO->duration_unit; | |
1040 | $duration_interval = $productDAO->duration_interval; | |
1041 | if ($periodType == 'rolling') { | |
1042 | $startDate = date('Y-m-d'); | |
1043 | } | |
1044 | elseif ($periodType == 'fixed') { | |
1045 | if ($fixed_period_start_day) { | |
1046 | $date = explode('-', date('Y-m-d')); | |
1047 | $month = substr($fixed_period_start_day, 0, strlen($fixed_period_start_day) - 2); | |
1048 | $day = substr($fixed_period_start_day, -2) . "<br>"; | |
1049 | $year = $date[0]; | |
1050 | $startDate = $year . '-' . $month . '-' . $day; | |
1051 | } | |
1052 | else { | |
1053 | $startDate = date('Y-m-d'); | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | $date = explode('-', $startDate); | |
1058 | $year = $date[0]; | |
1059 | $month = $date[1]; | |
1060 | $day = $date[2]; | |
1061 | ||
1062 | switch ($duration_unit) { | |
1063 | case 'year': | |
1064 | $year = $year + $duration_interval; | |
1065 | break; | |
1066 | ||
1067 | case 'month': | |
1068 | $month = $month + $duration_interval; | |
1069 | break; | |
1070 | ||
1071 | case 'day': | |
1072 | $day = $day + $duration_interval; | |
1073 | break; | |
1074 | ||
1075 | case 'week': | |
1076 | $day = $day + ($duration_interval * 7); | |
1077 | } | |
1078 | $endDate = date('Y-m-d H:i:s', mktime($hour, $minute, $second, $month, $day, $year)); | |
1079 | $this->assign('start_date', $startDate); | |
1080 | $this->assign('end_date', $endDate); | |
1081 | } | |
1082 | ||
1083 | $dao = new CRM_Contribute_DAO_Premium(); | |
1084 | $dao->entity_table = 'civicrm_contribution_page'; | |
1085 | $dao->entity_id = $this->_id; | |
1086 | $dao->find(TRUE); | |
1087 | $this->assign('contact_phone', $dao->premiums_contact_phone); | |
1088 | $this->assign('contact_email', $dao->premiums_contact_email); | |
1089 | ||
1090 | //create Premium record | |
1091 | $params = array( | |
1092 | 'product_id' => $premiumParams['selectProduct'], | |
1093 | 'contribution_id' => $contribution->id, | |
1094 | 'product_option' => CRM_Utils_Array::value('options_' . $premiumParams['selectProduct'], $premiumParams), | |
1095 | 'quantity' => 1, | |
1096 | 'start_date' => CRM_Utils_Date::customFormat($startDate, '%Y%m%d'), | |
1097 | 'end_date' => CRM_Utils_Date::customFormat($endDate, '%Y%m%d'), | |
1098 | ); | |
a7488080 | 1099 | if (!empty($premiumParams['selectProduct'])){ |
6a488035 TO |
1100 | $daoPremiumsProduct = new CRM_Contribute_DAO_PremiumsProduct(); |
1101 | $daoPremiumsProduct->product_id = $premiumParams['selectProduct']; | |
1102 | $daoPremiumsProduct->premiums_id = $dao->id; | |
1103 | $daoPremiumsProduct->find(true); | |
1104 | $params['financial_type_id'] = $daoPremiumsProduct->financial_type_id; | |
1105 | } | |
1106 | //Fixed For CRM-3901 | |
1107 | $daoContrProd = new CRM_Contribute_DAO_ContributionProduct(); | |
1108 | $daoContrProd->contribution_id = $contribution->id; | |
1109 | if ($daoContrProd->find(TRUE)) { | |
1110 | $params['id'] = $daoContrProd->id; | |
1111 | } | |
cde484fd | 1112 | |
6a488035 | 1113 | CRM_Contribute_BAO_Contribution::addPremium($params); |
8cc574cf | 1114 | if ($productDAO->cost && !empty($params['financial_type_id'])) { |
6a488035 TO |
1115 | $trxnParams = array( |
1116 | 'cost' => $productDAO->cost, | |
1117 | 'currency' => $productDAO->currency, | |
1118 | 'financial_type_id' => $params['financial_type_id'], | |
1119 | 'contributionId' => $contribution->id | |
1120 | ); | |
1121 | CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($trxnParams); | |
1122 | } | |
1123 | } | |
1124 | elseif ($selectProduct == 'no_thanks') { | |
1125 | //Fixed For CRM-3901 | |
1126 | $daoContrProd = new CRM_Contribute_DAO_ContributionProduct(); | |
1127 | $daoContrProd->contribution_id = $contribution->id; | |
1128 | if ($daoContrProd->find(TRUE)) { | |
1129 | $daoContrProd->delete(); | |
1130 | } | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | /** | |
1135 | * Process the contribution | |
1136 | * | |
cd6994fc | 1137 | * @param $form |
4ce4a928 EM |
1138 | * @param array $params |
1139 | * @param array $result | |
1140 | * @param integer $contactID | |
a1a94e61 | 1141 | * @param CRM_Financial_DAO_FinancialType $financialType |
cd6994fc E |
1142 | * @param bool $pending |
1143 | * @param bool $online | |
1144 | * | |
a1a94e61 EM |
1145 | * @param bool $isTest |
1146 | * @param array $lineItems | |
1147 | * | |
4ce4a928 EM |
1148 | * @throws Exception |
1149 | * @internal param bool $deductibleMode | |
6a488035 TO |
1150 | * @return CRM_Contribute_DAO_Contribution |
1151 | * @access public | |
1152 | */ | |
1f1740fe DL |
1153 | static function processContribution( |
1154 | &$form, | |
6a488035 TO |
1155 | $params, |
1156 | $result, | |
1157 | $contactID, | |
637b4757 | 1158 | $financialType, |
4ce4a928 | 1159 | $pending, |
4bd318e0 EM |
1160 | $online, |
1161 | $isTest, | |
1162 | $lineItems | |
6a488035 TO |
1163 | ) { |
1164 | $transaction = new CRM_Core_Transaction(); | |
4625312e | 1165 | $contribSoftContactId = $addressID = NULL; |
6a488035 TO |
1166 | |
1167 | // add these values for the recurringContrib function ,CRM-10188 | |
637b4757 | 1168 | $params['financial_type_id'] = $financialType->id; |
c8bde7ea EM |
1169 | |
1170 | //create an contribution address | |
5785931e | 1171 | if ($form->_contributeMode != 'notify' && empty($params['is_pay_later']) && !empty($form->_values['is_monetary'])) { |
c8bde7ea EM |
1172 | $addressID = CRM_Contribute_BAO_Contribution::createAddress($params, $form->_bltID); |
1173 | } | |
1174 | ||
e34dd94b | 1175 | //@todo - this is being set from the form to resolve CRM-10188 - an |
1176 | // eNotice caused by it not being set @ the front end | |
1177 | // however, we then get it being over-written with null for backend contributions | |
1178 | // a better fix would be to set the values in the respective forms rather than require | |
1179 | // a function being shared by two forms to deal with their respective values | |
1180 | // moving it to the BAO & not taking the $form as a param would make sense here. | |
1181 | if(!isset($params['is_email_receipt'])){ | |
1182 | $params['is_email_receipt'] = CRM_Utils_Array::value( 'is_email_receipt', $form->_values ); | |
1183 | } | |
637b4757 | 1184 | $recurringContributionID = self::processRecurringContribution($form, $params, $contactID, $financialType, $online); |
6a488035 | 1185 | |
6a488035 TO |
1186 | // CRM-11885 |
1187 | // if non_deductible_amount exists i.e. Additional Details fieldset was opened [and staff typed something] -> keep it. | |
1188 | if (isset($params['non_deductible_amount']) && (!empty($params['non_deductible_amount']))) { | |
1189 | $nonDeductibleAmount = $params['non_deductible_amount']; | |
1190 | } | |
1191 | // if non_deductible_amount does NOT exist - then calculate it depending on: | |
1192 | // $contributionType->is_deductible and whether there is a product (premium). | |
1193 | else { | |
1194 | //if ($contributionType->is_deductible && $deductibleMode) { | |
637b4757 | 1195 | if ($financialType->is_deductible) { |
6a488035 TO |
1196 | if ($online && isset($params['selectProduct'])) { |
1197 | $selectProduct = CRM_Utils_Array::value('selectProduct', $params); | |
1198 | } | |
1199 | if (!$online && isset($params['product_name'][0])) { | |
1200 | $selectProduct = $params['product_name'][0]; | |
1201 | } | |
1202 | // if there is a product - compare the value to the contribution amount | |
1203 | if (isset($selectProduct) && | |
1204 | $selectProduct != 'no_thanks' | |
1205 | ) { | |
1206 | $productDAO = new CRM_Contribute_DAO_Product(); | |
1207 | $productDAO->id = $selectProduct; | |
1208 | $productDAO->find(TRUE); | |
1209 | // product value exceeds contribution amount | |
1210 | if ($params['amount'] < $productDAO->price) { | |
1211 | $nonDeductibleAmount = $params['amount']; | |
1212 | } | |
1213 | // product value does NOT exceed contribution amount | |
1214 | else { | |
1215 | $nonDeductibleAmount = $productDAO->price; | |
1216 | } | |
1217 | } | |
1218 | // contribution is deductible - but there is no product | |
1219 | else { | |
1220 | $nonDeductibleAmount = '0.00'; | |
1221 | } | |
1222 | } | |
1223 | // contribution is NOT deductible | |
1224 | else { | |
1225 | $nonDeductibleAmount = $params['amount']; | |
1226 | } | |
1227 | } | |
1228 | ||
1229 | $now = date('YmdHis'); | |
1230 | $receiptDate = CRM_Utils_Array::value('receipt_date', $params); | |
a7488080 | 1231 | if (!empty($form->_values['is_email_receipt'])) { |
6a488035 TO |
1232 | $receiptDate = $now; |
1233 | } | |
1234 | ||
1235 | //get the contrib page id. | |
dbddfb08 | 1236 | $contributionPageId = NULL; |
6a488035 TO |
1237 | if ($online) { |
1238 | $contributionPageId = $form->_id; | |
1239 | $campaignId = CRM_Utils_Array::value('campaign_id', $params); | |
1240 | if (!array_key_exists('campaign_id', $params)) { | |
1241 | $campaignId = CRM_Utils_Array::value('campaign_id', $form->_values); | |
1242 | } | |
1243 | } | |
1244 | else { | |
1245 | //also for offline we do support - CRM-7290 | |
1246 | $contributionPageId = CRM_Utils_Array::value('contribution_page_id', $params); | |
1247 | $campaignId = CRM_Utils_Array::value('campaign_id', $params); | |
1248 | } | |
1249 | ||
6a488035 | 1250 | // Prepare soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin. |
8cc574cf | 1251 | if (!empty($params['pcp_made_through_id']) || !empty($params['soft_credit_to'])) { |
6a488035 | 1252 | // if its due to pcp |
a7488080 | 1253 | if (!empty($params['pcp_made_through_id'])) { |
6a488035 TO |
1254 | $contribSoftContactId = CRM_Core_DAO::getFieldValue( |
1255 | 'CRM_PCP_DAO_PCP', | |
1256 | $params['pcp_made_through_id'], | |
1257 | 'contact_id' | |
1258 | ); | |
1259 | } | |
1260 | else { | |
1261 | $contribSoftContactId = CRM_Utils_Array::value('soft_credit_to', $params); | |
1262 | } | |
1263 | ||
1264 | // Pass these details onto with the contribution to make them | |
1265 | // available at hook_post_process, CRM-8908 | |
c8bde7ea | 1266 | $params['soft_credit_to'] = $contribSoftContactId; |
6a488035 TO |
1267 | } |
1268 | ||
92acf33d | 1269 | if (isset($params['amount'])) { |
c8bde7ea EM |
1270 | $contribParams = self::getContributionParams( |
1271 | $params, $contactID, $financialType->id, $online, $contributionPageId, $nonDeductibleAmount, $campaignId, $form->_values['is_monetary'], $pending, $result, $receiptDate, | |
1272 | $recurringContributionID, $isTest, $addressID, $contribSoftContactId, $lineItems | |
1273 | ); | |
1274 | $contribution = CRM_Contribute_BAO_Contribution::add($contribParams); | |
9879aeb8 DL |
1275 | if (is_a($contribution, 'CRM_Core_Error')) { |
1276 | $message = CRM_Core_Error::getMessages($contribution); | |
1277 | CRM_Core_Error::fatal($message); | |
1278 | } | |
1f1740fe DL |
1279 | |
1280 | // lets store it in the form variable so postProcess hook can get to this and use it | |
1281 | $form->_contributionID = $contribution->id; | |
6a488035 | 1282 | } |
cde484fd | 1283 | |
1421174e | 1284 | //CRM-13981, processing honor contact into soft-credit contribution |
1285 | CRM_Contact_Form_ProfileContact::postProcess($form); | |
1286 | ||
6a488035 TO |
1287 | // process soft credit / pcp pages |
1288 | CRM_Contribute_Form_Contribution_Confirm::processPcpSoft($params, $contribution); | |
cde484fd | 1289 | |
6a488035 | 1290 | //handle pledge stuff. |
8cc574cf CW |
1291 | if (empty($form->_params['separate_membership_payment']) && !empty($form->_values['pledge_block_id']) && |
1292 | (!empty($form->_params['is_pledge']) || !empty($form->_values['pledge_id'])) | |
6a488035 TO |
1293 | ) { |
1294 | ||
a7488080 | 1295 | if (!empty($form->_values['pledge_id'])) { |
6a488035 TO |
1296 | |
1297 | //when user doing pledge payments. | |
1298 | //update the schedule when payment(s) are made | |
1299 | foreach ($form->_params['pledge_amount'] as $paymentId => $dontCare) { | |
1300 | $scheduledAmount = CRM_Core_DAO::getFieldValue( | |
1301 | 'CRM_Pledge_DAO_PledgePayment', | |
1302 | $paymentId, | |
1303 | 'scheduled_amount', | |
1304 | 'id' | |
1305 | ); | |
1306 | ||
1307 | $pledgePaymentParams = array( | |
1308 | 'id' => $paymentId, | |
1309 | 'contribution_id' => $contribution->id, | |
1310 | 'status_id' => $contribution->contribution_status_id, | |
1311 | 'actual_amount' => $scheduledAmount, | |
1312 | ); | |
1313 | ||
1314 | ||
1315 | CRM_Pledge_BAO_PledgePayment::add($pledgePaymentParams); | |
1316 | } | |
1317 | ||
1318 | //update pledge status according to the new payment statuses | |
1319 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($form->_values['pledge_id']); | |
1320 | } | |
1321 | else { | |
1322 | //when user creating pledge record. | |
1323 | $pledgeParams = array(); | |
1324 | $pledgeParams['contact_id'] = $contribution->contact_id; | |
1325 | $pledgeParams['installment_amount'] = $pledgeParams['actual_amount'] = $contribution->total_amount; | |
1326 | $pledgeParams['contribution_id'] = $contribution->id; | |
1327 | $pledgeParams['contribution_page_id'] = $contribution->contribution_page_id; | |
1328 | $pledgeParams['financial_type_id'] = $contribution->financial_type_id; | |
1329 | $pledgeParams['frequency_interval'] = $params['pledge_frequency_interval']; | |
1330 | $pledgeParams['installments'] = $params['pledge_installments']; | |
1331 | $pledgeParams['frequency_unit'] = $params['pledge_frequency_unit']; | |
1332 | if ($pledgeParams['frequency_unit'] == 'month') { | |
1333 | $pledgeParams['frequency_day'] = intval(date("d")); | |
1334 | } | |
1335 | else { | |
1336 | $pledgeParams['frequency_day'] = 1; | |
1337 | } | |
1338 | $pledgeParams['create_date'] = $pledgeParams['start_date'] = $pledgeParams['scheduled_date'] = date("Ymd"); | |
1339 | $pledgeParams['status_id'] = $contribution->contribution_status_id; | |
1340 | $pledgeParams['max_reminders'] = $form->_values['max_reminders']; | |
1341 | $pledgeParams['initial_reminder_day'] = $form->_values['initial_reminder_day']; | |
1342 | $pledgeParams['additional_reminder_day'] = $form->_values['additional_reminder_day']; | |
1343 | $pledgeParams['is_test'] = $contribution->is_test; | |
1344 | $pledgeParams['acknowledge_date'] = date('Ymd'); | |
1345 | $pledgeParams['original_installment_amount'] = $pledgeParams['installment_amount']; | |
1346 | ||
1347 | //inherit campaign from contirb page. | |
1348 | $pledgeParams['campaign_id'] = $campaignId; | |
1349 | ||
1350 | $pledge = CRM_Pledge_BAO_Pledge::create($pledgeParams); | |
1351 | ||
1352 | $form->_params['pledge_id'] = $pledge->id; | |
1353 | ||
1354 | //send acknowledgment email. only when pledge is created | |
1355 | if ($pledge->id) { | |
1356 | //build params to send acknowledgment. | |
1357 | $pledgeParams['id'] = $pledge->id; | |
1358 | $pledgeParams['receipt_from_name'] = $form->_values['receipt_from_name']; | |
1359 | $pledgeParams['receipt_from_email'] = $form->_values['receipt_from_email']; | |
1360 | ||
1361 | //scheduled amount will be same as installment_amount. | |
1362 | $pledgeParams['scheduled_amount'] = $pledgeParams['installment_amount']; | |
1363 | ||
1364 | //get total pledge amount. | |
1365 | $pledgeParams['total_pledge_amount'] = $pledge->amount; | |
1366 | ||
1367 | CRM_Pledge_BAO_Pledge::sendAcknowledgment($form, $pledgeParams); | |
1368 | } | |
1369 | } | |
1370 | } | |
1371 | ||
1372 | if ($online && $contribution) { | |
1373 | CRM_Core_BAO_CustomValueTable::postProcess($form->_params, | |
1374 | CRM_Core_DAO::$_nullArray, | |
1375 | 'civicrm_contribution', | |
1376 | $contribution->id, | |
1377 | 'Contribution' | |
1378 | ); | |
1379 | } | |
1380 | elseif ($contribution) { | |
1381 | //handle custom data. | |
1382 | $params['contribution_id'] = $contribution->id; | |
a7488080 | 1383 | if (!empty($params['custom']) && |
6a488035 TO |
1384 | is_array($params['custom']) && |
1385 | !is_a($contribution, 'CRM_Core_Error') | |
1386 | ) { | |
1387 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contribution', $contribution->id); | |
1388 | } | |
1389 | } | |
1390 | // Save note | |
1391 | if ($contribution && !empty($params['contribution_note'])) { | |
1392 | $noteParams = array( | |
1393 | 'entity_table' => 'civicrm_contribution', | |
1394 | 'note' => $params['contribution_note'], | |
1395 | 'entity_id' => $contribution->id, | |
1396 | 'contact_id' => $contribution->contact_id, | |
1397 | 'modified_date' => date('Ymd'), | |
1398 | ); | |
1399 | ||
1400 | CRM_Core_BAO_Note::add($noteParams, array()); | |
1401 | } | |
1402 | ||
1403 | ||
1404 | if (isset($params['related_contact'])) { | |
1405 | $contactID = $params['related_contact']; | |
1406 | } | |
1407 | elseif (isset($params['cms_contactID'])) { | |
1408 | $contactID = $params['cms_contactID']; | |
1409 | } | |
cde484fd | 1410 | |
6a488035 TO |
1411 | //create contribution activity w/ individual and target |
1412 | //activity w/ organisation contact id when onbelf, CRM-4027 | |
1413 | $targetContactID = NULL; | |
a7488080 | 1414 | if (!empty($params['hidden_onbehalf_profile'])) { |
6a488035 TO |
1415 | $targetContactID = $contribution->contact_id; |
1416 | $contribution->contact_id = $contactID; | |
1417 | } | |
1418 | ||
1419 | // create an activity record | |
1420 | if ($contribution) { | |
9879aeb8 | 1421 | CRM_Activity_BAO_Activity::addActivity($contribution, NULL, $targetContactID); |
6a488035 TO |
1422 | } |
1423 | ||
1424 | $transaction->commit(); | |
4ab1a3a3 | 1425 | // CRM-13074 - create the CMSUser after the transaction is completed as it |
1426 | // is not appropriate to delete a valid contribution if a user create problem occurs | |
1427 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, | |
1428 | $contactID, | |
1429 | 'email-' . $form->_bltID | |
1430 | ); | |
6a488035 TO |
1431 | return $contribution; |
1432 | } | |
1433 | ||
1434 | /** | |
1435 | * Create the recurring contribution record | |
1436 | * | |
1437 | */ | |
cde484fd | 1438 | static function processRecurringContribution(&$form, &$params, $contactID, $contributionType, $online = TRUE) { |
6a488035 TO |
1439 | // return if this page is not set for recurring |
1440 | // or the user has not chosen the recurring option | |
1441 | ||
1442 | //this is online case validation. | |
8cc574cf | 1443 | if ((empty($form->_values['is_recur']) && $online) || empty($params['is_recur'])) { |
6a488035 TO |
1444 | return NULL; |
1445 | } | |
1446 | ||
dbddfb08 | 1447 | $recurParams = array('contact_id' => $contactID); |
6a488035 TO |
1448 | $recurParams['amount'] = CRM_Utils_Array::value('amount', $params); |
1449 | $recurParams['auto_renew'] = CRM_Utils_Array::value('auto_renew', $params); | |
1450 | $recurParams['frequency_unit'] = CRM_Utils_Array::value('frequency_unit', $params); | |
1451 | $recurParams['frequency_interval'] = CRM_Utils_Array::value('frequency_interval', $params); | |
1452 | $recurParams['installments'] = CRM_Utils_Array::value('installments', $params); | |
1453 | $recurParams['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params); | |
1454 | ||
d8a20b17 FG |
1455 | // CRM-14354: For an auto-renewing membership with an additional contribution, |
1456 | // if separate payments is not enabled, make sure only the membership fee recurs | |
1457 | if ($form->_membershipBlock['is_separate_payment'] === '0' | |
1458 | && isset($params['selectMembership']) | |
1459 | && $form->_values['is_allow_other_amount'] == '1' | |
1460 | ) { | |
1461 | $recurParams['amount'] = $form->_membershipTypeValues[$params['selectMembership']]['minimum_fee']; | |
1462 | } | |
1463 | ||
6a488035 TO |
1464 | $recurParams['is_test'] = 0; |
1465 | if (($form->_action & CRM_Core_Action::PREVIEW) || | |
1466 | (isset($form->_mode) && ($form->_mode == 'test')) | |
1467 | ) { | |
1468 | $recurParams['is_test'] = 1; | |
1469 | } | |
1470 | ||
1471 | $recurParams['start_date'] = $recurParams['create_date'] = $recurParams['modified_date'] = date('YmdHis'); | |
a7488080 | 1472 | if (!empty($params['receive_date'])) { |
6a488035 TO |
1473 | $recurParams['start_date'] = $params['receive_date']; |
1474 | } | |
1475 | $recurParams['invoice_id'] = CRM_Utils_Array::value('invoiceID', $params); | |
4f8dce1b | 1476 | $recurParams['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); |
6a488035 TO |
1477 | $recurParams['payment_processor_id'] = CRM_Utils_Array::value('payment_processor_id', $params); |
1478 | $recurParams['is_email_receipt'] = CRM_Utils_Array::value('is_email_receipt', $params); | |
1479 | // we need to add a unique trxn_id to avoid a unique key error | |
1480 | // in paypal IPN we reset this when paypal sends us the real trxn id, CRM-2991 | |
1481 | $recurParams['trxn_id'] = CRM_Utils_Array::value('trxn_id', $params, $params['invoiceID']); | |
1482 | $recurParams['financial_type_id'] = $contributionType->id; | |
1483 | ||
1484 | if (!$online || $form->_values['is_monetary']) { | |
1485 | $recurParams['payment_instrument_id'] = 1; | |
1486 | } | |
1487 | ||
1488 | $campaignId = CRM_Utils_Array::value('campaign_id', $params); | |
1489 | if ($online) { | |
1490 | if (!array_key_exists('campaign_id', $params)) { | |
1491 | $campaignId = CRM_Utils_Array::value('campaign_id', $form->_values); | |
1492 | } | |
1493 | } | |
1494 | $recurParams['campaign_id'] = $campaignId; | |
1495 | ||
1496 | $recurring = CRM_Contribute_BAO_ContributionRecur::add($recurParams); | |
1497 | if (is_a($recurring, 'CRM_Core_Error')) { | |
bd0bfdec | 1498 | CRM_Core_Error::displaySessionError($recurring); |
6a488035 TO |
1499 | $urlString = 'civicrm/contribute/transact'; |
1500 | $urlParams = '_qf_Main_display=true'; | |
e8ab1184 | 1501 | if (get_class($form) == 'CRM_Contribute_Form_Contribution') { |
6a488035 TO |
1502 | $urlString = 'civicrm/contact/view/contribution'; |
1503 | $urlParams = "action=add&cid={$form->_contactID}"; | |
1504 | if ($form->_mode) { | |
1505 | $urlParams .= "&mode={$form->_mode}"; | |
1506 | } | |
1507 | } | |
1508 | CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams)); | |
1509 | } | |
1510 | ||
1511 | return $recurring->id; | |
1512 | } | |
1513 | ||
6a488035 TO |
1514 | /** |
1515 | * Function to add on behalf of organization and it's location | |
1516 | * | |
1517 | * @param $behalfOrganization array array of organization info | |
6a488035 TO |
1518 | * @param $contactID int individual contact id. One |
1519 | * who is doing the process of signup / contribution. | |
1520 | * | |
cd6994fc E |
1521 | * @param $values array form values array |
1522 | * @param $params | |
1523 | * @param null $fields | |
1524 | * | |
6a488035 TO |
1525 | * @return void |
1526 | * @access public | |
1527 | */ | |
1528 | static function processOnBehalfOrganization(&$behalfOrganization, &$contactID, &$values, &$params, $fields = NULL) { | |
1529 | $isCurrentEmployer = FALSE; | |
dbddfb08 | 1530 | $dupeIDs = array(); |
6a488035 | 1531 | $orgID = NULL; |
8cc574cf | 1532 | if (!empty($behalfOrganization['organization_id']) && !empty($behalfOrganization['org_option'])) { |
6a488035 TO |
1533 | $orgID = $behalfOrganization['organization_id']; |
1534 | unset($behalfOrganization['organization_id']); | |
1535 | $isCurrentEmployer = TRUE; | |
1536 | } | |
1537 | ||
1538 | // formalities for creating / editing organization. | |
1539 | $behalfOrganization['contact_type'] = 'Organization'; | |
1540 | ||
1541 | // get the relationship type id | |
1542 | $relType = new CRM_Contact_DAO_RelationshipType(); | |
1543 | $relType->name_a_b = 'Employee of'; | |
1544 | $relType->find(TRUE); | |
1545 | $relTypeId = $relType->id; | |
1546 | ||
1547 | // keep relationship params ready | |
1548 | $relParams['relationship_type_id'] = $relTypeId . '_a_b'; | |
1549 | $relParams['is_permission_a_b'] = 1; | |
1550 | $relParams['is_active'] = 1; | |
1551 | ||
1552 | if (!$orgID) { | |
1553 | // check if matching organization contact exists | |
1554 | $dedupeParams = CRM_Dedupe_Finder::formatParams($behalfOrganization, 'Organization'); | |
1555 | $dedupeParams['check_permission'] = FALSE; | |
1556 | $dupeIDs = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Organization', 'Unsupervised'); | |
1557 | ||
1558 | // CRM-6243 says to pick the first org even if more than one match | |
1559 | if (count($dupeIDs) >= 1) { | |
34eea47c | 1560 | $behalfOrganization['contact_id'] = $orgID = $dupeIDs[0]; |
6a488035 TO |
1561 | // don't allow name edit |
1562 | unset($behalfOrganization['organization_name']); | |
1563 | } | |
1564 | } | |
1565 | else { | |
1566 | // if found permissioned related organization, allow location edit | |
1567 | $behalfOrganization['contact_id'] = $orgID; | |
1568 | // don't allow name edit | |
1569 | unset($behalfOrganization['organization_name']); | |
1570 | } | |
1571 | ||
1572 | // handling for image url | |
a7488080 | 1573 | if (!empty($behalfOrganization['image_URL'])) { |
6a488035 TO |
1574 | CRM_Contact_BAO_Contact::processImageParams($behalfOrganization); |
1575 | } | |
1576 | ||
1577 | // create organization, add location | |
1578 | $orgID = CRM_Contact_BAO_Contact::createProfileContact($behalfOrganization, $fields, $orgID, | |
1579 | NULL, NULL, 'Organization' | |
1580 | ); | |
1581 | // create relationship | |
1582 | $relParams['contact_check'][$orgID] = 1; | |
1583 | $cid = array('contact' => $contactID); | |
1584 | CRM_Contact_BAO_Relationship::create($relParams, $cid); | |
1585 | ||
1586 | // if multiple match - send a duplicate alert | |
1587 | if ($dupeIDs && (count($dupeIDs) > 1)) { | |
1588 | $values['onbehalf_dupe_alert'] = 1; | |
1589 | // required for IPN | |
1590 | $params['onbehalf_dupe_alert'] = 1; | |
1591 | } | |
1592 | ||
1593 | // make sure organization-contact-id is considered for recording | |
1594 | // contribution/membership etc.. | |
1595 | if ($contactID != $orgID) { | |
1596 | // take a note of contact-id, so we can send the | |
1597 | // receipt to individual contact as well. | |
1598 | ||
1599 | // required for mailing/template display ..etc | |
1600 | $values['related_contact'] = $contactID; | |
1601 | // required for IPN | |
1602 | $params['related_contact'] = $contactID; | |
1603 | ||
1604 | //make this employee of relationship as current | |
1605 | //employer / employee relationship, CRM-3532 | |
1606 | if ($isCurrentEmployer && | |
1607 | ($orgID != CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id')) | |
1608 | ) { | |
1609 | $isCurrentEmployer = FALSE; | |
1610 | } | |
1611 | ||
1612 | if (!$isCurrentEmployer && $orgID) { | |
1613 | //build current employer params | |
1614 | $currentEmpParams[$contactID] = $orgID; | |
1615 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); | |
1616 | } | |
1617 | ||
1618 | // contribution / signup will be done using this | |
1619 | // organization id. | |
1620 | $contactID = $orgID; | |
1621 | } | |
1622 | } | |
1623 | ||
1624 | /** | |
1625 | * Function used to save pcp / soft credit entry | |
1626 | * This is used by contribution and also event pcps | |
1627 | * | |
1628 | * @param array $params associated array | |
1629 | * @param object $contribution contribution object | |
1630 | * | |
1631 | * @static | |
1632 | * @access public | |
1633 | */ | |
1634 | static function processPcpSoft(&$params, &$contribution) { | |
1635 | //add soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin. | |
a7488080 | 1636 | if (!empty($params['soft_credit_to'])) { |
dbddfb08 | 1637 | $contributionSoftParams = array(); |
6a488035 TO |
1638 | foreach (array( |
1639 | 'pcp_display_in_roll', 'pcp_roll_nickname', 'pcp_personal_note', 'amount') as $val) { | |
a7488080 | 1640 | if (!empty($params[$val])) { |
dbddfb08 | 1641 | $contributionSoftParams[$val] = $params[$val]; |
6a488035 TO |
1642 | } |
1643 | } | |
1644 | ||
dbddfb08 | 1645 | $contributionSoftParams['contact_id'] = $params['soft_credit_to']; |
6a488035 | 1646 | // add contribution id |
dbddfb08 | 1647 | $contributionSoftParams['contribution_id'] = $contribution->id; |
6a488035 | 1648 | // add pcp id |
dbddfb08 | 1649 | $contributionSoftParams['pcp_id'] = $params['pcp_made_through_id']; |
6a488035 | 1650 | |
dbddfb08 | 1651 | $contributionSoftParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name'); |
51fa20cb | 1652 | |
dbddfb08 | 1653 | CRM_Contribute_BAO_ContributionSoft::add($contributionSoftParams); |
6a488035 TO |
1654 | } |
1655 | } | |
1656 | ||
1657 | /** | |
1658 | * Function used to se pcp related defaults / params | |
1659 | * This is used by contribution and also event pcps | |
1660 | * | |
1661 | * @param object $page form object | |
1662 | * @param array $params associated array | |
1663 | * | |
cd6994fc | 1664 | * @return array |
6a488035 TO |
1665 | * @static |
1666 | * @access public | |
1667 | */ | |
1668 | static function processPcp(&$page, $params) { | |
547206bc | 1669 | $params['pcp_made_through_id'] = $page->_pcpId; |
6a488035 | 1670 | $page->assign('pcpBlock', TRUE); |
8cc574cf | 1671 | if (!empty($params['pcp_display_in_roll']) && empty($params['pcp_roll_nickname'])) { |
6a488035 TO |
1672 | $params['pcp_roll_nickname'] = ts('Anonymous'); |
1673 | $params['pcp_is_anonymous'] = 1; | |
1674 | } | |
1675 | else { | |
1676 | $params['pcp_is_anonymous'] = 0; | |
1677 | } | |
1678 | foreach (array( | |
547206bc | 1679 | 'pcp_display_in_roll', |
1680 | 'pcp_is_anonymous', | |
1681 | 'pcp_roll_nickname', | |
1682 | 'pcp_personal_note' | |
1683 | ) as $val) { | |
a7488080 | 1684 | if (!empty($params[$val])) { |
6a488035 TO |
1685 | $page->assign($val, $params[$val]); |
1686 | } | |
1687 | } | |
1688 | ||
1689 | return $params; | |
1690 | } | |
705b4205 EM |
1691 | |
1692 | /** | |
5624f515 EM |
1693 | * @param array $membershipParams |
1694 | * @param integer $contactID | |
1695 | * @param array $customFieldsFormatted | |
1696 | * @param array $fieldTypes | |
1697 | * @param array $premiumParams | |
1698 | * @param array $membershipLineItems line items specifically relating to memberships | |
705b4205 | 1699 | */ |
5624f515 | 1700 | public function processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, $membershipLineItems) { |
705b4205 | 1701 | try { |
05990684 EM |
1702 | $membershipTypeIDs = (array) $membershipParams['selectMembership']; |
1703 | $membershipTypes = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIDs); | |
5624f515 EM |
1704 | $membershipType = empty($membershipTypes) ? array() : reset($membershipTypes); |
1705 | $this->assign('membership_name', CRM_Utils_Array::value('name', $membershipType)); | |
1706 | ||
fa9d0451 | 1707 | $isPaidMembership = FALSE; |
553991dd | 1708 | if($this->_amount >= 0.0 && isset($membershipParams['amount'])) { |
fa9d0451 EM |
1709 | //amount must be greater than zero for |
1710 | //adding contribution record to contribution table. | |
1711 | //this condition arises when separate membership payment is | |
1712 | //enabled and contribution amount is not selected. fix for CRM-3010 | |
1713 | $isPaidMembership = TRUE; | |
1714 | } | |
fb49fa48 | 1715 | $isProcessSeparateMembershipTransaction = $this->isSeparateMembershipTransaction($this->_id, $this->_values['amount_block_is_active']); |
fa9d0451 | 1716 | |
c98997b9 | 1717 | if ($this->_values['amount_block_is_active']) { |
246e7293 | 1718 | $contributionTypeId = $this->_values['financial_type_id']; |
236a2274 EM |
1719 | } |
1720 | else { | |
5624f515 | 1721 | $contributionTypeId = CRM_Utils_Array::value('financial_type_id', $membershipType, CRM_Utils_Array::value('financial_type_id' ,$membershipParams)); |
236a2274 EM |
1722 | } |
1723 | ||
705b4205 | 1724 | CRM_Member_BAO_Membership::postProcessMembership($membershipParams, $contactID, |
05990684 | 1725 | $this, $premiumParams, $customFieldsFormatted, $fieldTypes, $membershipType, $membershipTypeIDs, $isPaidMembership, $this->_membershipId, $isProcessSeparateMembershipTransaction, $contributionTypeId, |
5624f515 | 1726 | $membershipLineItems |
705b4205 EM |
1727 | ); |
1728 | $this->assign('membership_assign', TRUE); | |
1729 | $this->set('membershipTypeID', $membershipParams['selectMembership']); | |
1730 | } | |
1731 | catch (CRM_Core_Exception $e) { | |
1732 | CRM_Core_Session::singleton()->setStatus($e->getMessage()); | |
1733 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=true&qfKey={$this->_params['qfKey']}")); | |
1734 | } | |
1735 | } | |
fb49fa48 EM |
1736 | |
1737 | /** | |
1738 | * Are we going to do 2 financial transactions? | |
1739 | * ie the membership block supports a separate transactions AND the contribution form has been configured for a contribution | |
1740 | * transaction AND a membership transaction AND the payment processor supports double financial transactions (ie. NOT doTransferPayment style) | |
1741 | * | |
1742 | * | |
1743 | * @param integer $formID | |
1744 | * @param bool $amountBlockActiveOnForm | |
1745 | * | |
1746 | * @return bool | |
1747 | */ | |
1748 | public function isSeparateMembershipTransaction($formID, $amountBlockActiveOnForm) { | |
1749 | $memBlockDetails = CRM_Member_BAO_Membership::getMembershipBlock($formID); | |
1750 | if (!empty($memBlockDetails['is_separate_payment']) && $amountBlockActiveOnForm) { | |
1751 | return TRUE; | |
1752 | } | |
1753 | return FALSE; | |
1754 | } | |
df6c4f28 EM |
1755 | |
1756 | /** | |
1757 | * This function sets the fields | |
1758 | * - $this->_params['amount_level'] | |
1759 | * - $this->_params['selectMembership'] | |
1760 | * And under certain circumstances sets | |
1761 | * $this->_params['amount'] = null; | |
1762 | * | |
1763 | * @param $priceSetID | |
1764 | * | |
1765 | * @internal param $isQuickConfig | |
1766 | * @internal param $priceField | |
1767 | */ | |
1768 | public function setFormAmountFields($priceSetID) { | |
1769 | $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_params['priceSetId'], 'is_quick_config'); | |
1770 | $priceField = new CRM_Price_DAO_PriceField(); | |
1771 | $priceField->price_set_id = $priceSetID; | |
1772 | $priceField->orderBy('weight'); | |
1773 | $priceField->find(); | |
1774 | ||
1775 | while ($priceField->fetch()) { | |
1776 | $paramWeDoNotUnderstand = NULL; | |
1777 | if ($priceField->name == "contribution_amount") { | |
1778 | $paramWeDoNotUnderstand = $priceField->id; | |
1779 | } | |
1780 | if ($isQuickConfig && !empty($this->_params["price_{$priceField->id}"])) { | |
1781 | if ($this->_values['fee'][$priceField->id]['html_type'] != 'Text') { | |
1782 | $this->_params['amount_level'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', | |
1783 | $this->_params["price_{$priceField->id}"], 'label'); | |
1784 | } | |
1785 | if ($priceField->name == "membership_amount") { | |
1786 | $this->_params['selectMembership'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', | |
1787 | $this->_params["price_{$priceField->id}"], 'membership_type_id'); | |
1788 | } | |
1789 | } // if separate payment we set contribution amount to be null, so that it will not show contribution amount same as membership amount. | |
1790 | // @todo - this needs more documentation - it appears the setting to null is tied up with separate membership payments | |
1791 | // but the circumstances are very confusing. Many of these conditions are repeated in the next conditional | |
1792 | // so we should merge them together | |
1793 | // the quick config seems like a red-herring - if this is about a separate membership payment then there | |
f64a217a | 1794 | // are 2 types of line items - membership ones & non-membership ones - regardless of whether quick config is set |
df6c4f28 EM |
1795 | elseif ( |
1796 | CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) | |
1797 | && !empty($this->_values['fee'][$priceField->id]) | |
1798 | && ($this->_values['fee'][$priceField->id]['name'] == "other_amount") | |
1799 | && CRM_Utils_Array::value("price_{$paramWeDoNotUnderstand}", $this->_params) < 1 | |
1800 | && empty($this->_params["price_{$priceField->id}"]) | |
1801 | ) { | |
1802 | $this->_params['amount'] = null; | |
1803 | } | |
1804 | ||
1805 | // Fix for CRM-14375 - If we are using separate payments and "no | |
1806 | // thank you" is selected for the additional contribution, set | |
1807 | // contribution amount to be null, so that it will not show | |
1808 | // contribution amount same as membership amount. | |
1809 | //@todo - merge with section above | |
1810 | if ($this->_membershipBlock['is_separate_payment'] | |
1811 | && CRM_Utils_Array::value('name', $this->_values['fee'][$priceField->id]) == 'contribution_amount' | |
1812 | && CRM_Utils_Array::value("price_{$priceField->id}", $this->_params) == '-1' | |
1813 | ) { | |
1814 | $this->_params['amount'] = null; | |
1815 | } | |
1816 | } | |
1817 | } | |
be26f3e0 EM |
1818 | |
1819 | /** | |
1820 | * Static submit function allowing tests (& api access although this is being built slowly) | |
1821 | * @param $params | |
1822 | */ | |
1823 | static function submit($params) { | |
1824 | $form = new CRM_Contribute_Form_Contribution_Confirm(); | |
1825 | $form->_id = $params['id']; | |
f64a217a EM |
1826 | CRM_Contribute_BAO_ContributionPage::setValues($form->_id, $form->_values); |
1827 | $form->_separateMembershipPayment = CRM_Contribute_BAO_ContributionPage::getIsMembershipPayment($form->_id); | |
be26f3e0 EM |
1828 | //this way the mocked up controller ignores the session stuff |
1829 | $_SERVER['REQUEST_METHOD'] = 'GET'; | |
1830 | $form->controller = new CRM_Contribute_Controller_Contribution(); | |
1831 | $params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1832 | $paramsProcessedForForm = $form->_params = self::getFormParams($params['id'], $params); | |
f64a217a EM |
1833 | $form->_amount = $params['amount']; |
1834 | ||
be26f3e0 EM |
1835 | $priceSetID = $form->_params['priceSetId'] = $paramsProcessedForForm['price_set_id']; |
1836 | $priceFields = CRM_Price_BAO_PriceSet::getSetDetail($priceSetID); | |
f64a217a EM |
1837 | $priceSetFields = reset($priceFields); |
1838 | $form->_values['fee'] = $priceSetFields['fields']; | |
5624f515 | 1839 | $form->_priceSetId = $priceSetID; |
be26f3e0 | 1840 | $form->setFormAmountFields($priceSetID); |
8ca611b7 EM |
1841 | if (!empty($params['payment_processor'])) { |
1842 | $form->_paymentProcessor = civicrm_api3('payment_processor', 'getsingle', array('id' => $params['payment_processor'])); | |
1843 | } | |
be26f3e0 EM |
1844 | $priceFields = $priceFields[$priceSetID]['fields']; |
1845 | CRM_Price_BAO_PriceSet::processAmount($priceFields, $paramsProcessedForForm, $lineItems, 'civicrm_contribution'); | |
1846 | $form->_lineItem = array($priceSetID => $lineItems); | |
1847 | $form->postProcess(); | |
1848 | } | |
1849 | ||
1850 | /** | |
1851 | * Helper function for static submit function - set relevant params - help us to build up an array that we can pass in | |
1852 | * @param $id | |
1853 | * @param array $params | |
1854 | * | |
1855 | * @return array | |
1856 | * @throws CiviCRM_API3_Exception | |
1857 | */ | |
1858 | static function getFormParams($id, array $params) { | |
1859 | if(!isset($params['is_pay_later'])) { | |
8ca611b7 EM |
1860 | if (!empty($params['payment_processor'])) { |
1861 | $params['is_pay_later'] = 0; | |
1862 | } | |
1863 | else { | |
1864 | $params['is_pay_later'] = civicrm_api3('contribution_page', 'getvalue', array( | |
1865 | 'id' => $id, | |
1866 | 'return' => 'is_pay_later' | |
1867 | )); | |
1868 | } | |
be26f3e0 EM |
1869 | } |
1870 | if(empty($params['price_set_id'])) { | |
1871 | $params['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id']); | |
1872 | } | |
1873 | return $params; | |
1874 | } | |
6a488035 | 1875 | } |