Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 usefusul, 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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
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 processing a contribution | |
38 | * | |
39 | */ | |
40 | class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditPayment { | |
6a488035 | 41 | /** |
100fef9d | 42 | * The id of the contribution that we are processing |
6a488035 TO |
43 | * |
44 | * @var int | |
6a488035 TO |
45 | */ |
46 | public $_id; | |
47 | ||
48 | /** | |
100fef9d | 49 | * The id of the premium that we are processing |
6a488035 TO |
50 | * |
51 | * @var int | |
6a488035 TO |
52 | */ |
53 | public $_premiumID = NULL; | |
4691b077 EM |
54 | |
55 | /** | |
56 | * @var CRM_Contribute_DAO_ContributionProduct | |
57 | */ | |
6a488035 TO |
58 | public $_productDAO = NULL; |
59 | ||
60 | /** | |
100fef9d | 61 | * The id of the note |
6a488035 TO |
62 | * |
63 | * @var int | |
6a488035 TO |
64 | */ |
65 | public $_noteID; | |
66 | ||
67 | /** | |
100fef9d | 68 | * The id of the contact associated with this contribution |
6a488035 TO |
69 | * |
70 | * @var int | |
6a488035 TO |
71 | */ |
72 | public $_contactID; | |
73 | ||
74 | /** | |
100fef9d | 75 | * The id of the pledge payment that we are processing |
6a488035 TO |
76 | * |
77 | * @var int | |
6a488035 TO |
78 | */ |
79 | public $_ppID; | |
80 | ||
81 | /** | |
100fef9d | 82 | * The id of the pledge that we are processing |
6a488035 TO |
83 | * |
84 | * @var int | |
6a488035 TO |
85 | */ |
86 | public $_pledgeID; | |
87 | ||
88 | /** | |
100fef9d | 89 | * Is this contribution associated with an online |
6a488035 TO |
90 | * financial transaction |
91 | * | |
92 | * @var boolean | |
6a488035 TO |
93 | */ |
94 | public $_online = FALSE; | |
95 | ||
96 | /** | |
97 | * Stores all product option | |
98 | * | |
99 | * @var array | |
6a488035 TO |
100 | */ |
101 | public $_options; | |
102 | ||
4691b077 EM |
103 | /** |
104 | * Storage of parameters from form | |
105 | * | |
106 | * @var array | |
4691b077 EM |
107 | */ |
108 | public $_params; | |
109 | ||
6a488035 TO |
110 | /** |
111 | * Store the contribution Type ID | |
112 | * | |
113 | * @var array | |
114 | */ | |
115 | public $_contributionType; | |
116 | ||
117 | /** | |
118 | * The contribution values if an existing contribution | |
119 | */ | |
120 | public $_values; | |
121 | ||
122 | /** | |
123 | * The pledge values if this contribution is associated with pledge | |
124 | */ | |
125 | public $_pledgeValues; | |
126 | ||
127 | public $_contributeMode = 'direct'; | |
128 | ||
129 | public $_context; | |
130 | ||
5032bf07 EM |
131 | /** |
132 | * Parameter with confusing name | |
133 | * @todo what is it? | |
134 | * @var string | |
135 | */ | |
136 | public $_compContext; | |
137 | ||
6a488035 TO |
138 | public $_compId; |
139 | ||
5032bf07 EM |
140 | /** |
141 | * Possible From email addresses | |
142 | * @var array | |
143 | */ | |
144 | public $_fromEmails; | |
d77a0a58 EM |
145 | |
146 | /** | |
147 | * ID of from email | |
148 | * @var integer | |
149 | */ | |
150 | public $fromEmailId; | |
d424ffde CW |
151 | |
152 | /** | |
6a488035 TO |
153 | * Store the line items if price set used. |
154 | */ | |
155 | public $_lineItems; | |
156 | ||
d77a0a58 | 157 | /** |
100fef9d | 158 | * Line item |
d77a0a58 EM |
159 | * @todo explain why we use lineItem & lineItems |
160 | * @var array | |
161 | */ | |
162 | public $_lineItem; | |
163 | ||
17db9f82 | 164 | /** |
efe42cdc | 165 | * @var array soft credit info |
17db9f82 KJ |
166 | */ |
167 | public $_softCreditInfo; | |
168 | ||
6a488035 | 169 | protected $_formType; |
efe42cdc EM |
170 | |
171 | /** | |
172 | * @todo what on earth does cdType stand for???? | |
173 | * @var | |
174 | */ | |
6a488035 | 175 | protected $_cdType; |
1421174e | 176 | public $_honoreeProfileType; |
6a488035 | 177 | |
459091e1 | 178 | /** |
100fef9d | 179 | * Array of billing panes to be displayed by billingBlock.tpl - currently this is likely to look like |
459091e1 EM |
180 | * array('Credit Card' => ts('Credit Card') or array('Direct Debit => ts('Direct Debit') |
181 | * @todo billing details (address stuff) to be added when we stop hard coding the panes in billingBlock.tpl | |
182 | * | |
183 | * @var array | |
184 | */ | |
185 | public $billingPane = array(); | |
186 | ||
187 | /** | |
100fef9d | 188 | * Array of the payment fields to be displayed in the payment fieldset (pane) in billingBlock.tpl |
459091e1 EM |
189 | * this contains all the information to describe these fields from quickform. See CRM_Core_Form_Payment getPaymentFormFieldsMetadata |
190 | * | |
191 | * @var array | |
192 | */ | |
353ffa53 | 193 | public $_paymentFields = array(); |
4691b077 | 194 | /** |
100fef9d | 195 | * Logged in user's email |
4691b077 EM |
196 | * @var string |
197 | */ | |
198 | public $userEmail; | |
199 | ||
200 | /** | |
201 | * Price set ID | |
202 | * @var integer | |
203 | */ | |
204 | public $_priceSetId; | |
205 | ||
206 | ||
207 | /** | |
208 | * Price set as an array | |
209 | * @var array | |
210 | */ | |
211 | public $_priceSet; | |
212 | ||
d77a0a58 | 213 | /** |
100fef9d | 214 | * Form defaults |
d77a0a58 EM |
215 | * @todo can we define this a as protected? can we define higher up the chain |
216 | * @var array | |
217 | */ | |
218 | public $_defaults; | |
219 | ||
220 | /** | |
221 | * User display name | |
222 | * | |
223 | * @var string | |
224 | */ | |
225 | public $userDisplayName; | |
353ffa53 | 226 | |
6a488035 | 227 | /** |
100fef9d | 228 | * Set variables up before form is built |
6a488035 TO |
229 | * |
230 | * @return void | |
6a488035 TO |
231 | */ |
232 | public function preProcess() { | |
81bc499e | 233 | |
6a488035 TO |
234 | //check permission for action. |
235 | if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { | |
0499b0ad | 236 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
237 | } |
238 | ||
44b6505d | 239 | //@todo - if anyone ever figures out what this cdtype subroutine is about (or even if it still applies) please add comments |
6a488035 | 240 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); |
6a488035 TO |
241 | $this->assign('cdType', FALSE); |
242 | if ($this->_cdType) { | |
243 | $this->assign('cdType', TRUE); | |
244 | CRM_Custom_Form_CustomData::preProcess($this); | |
245 | return; | |
246 | } | |
247 | ||
248 | $this->_formType = CRM_Utils_Array::value('formType', $_GET); | |
249 | ||
250 | // get price set id. | |
251 | $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET); | |
252 | $this->set('priceSetId', $this->_priceSetId); | |
253 | $this->assign('priceSetId', $this->_priceSetId); | |
254 | ||
255 | //get the pledge payment id | |
256 | $this->_ppID = CRM_Utils_Request::retrieve('ppid', 'Positive', $this); | |
257 | ||
258 | //get the contact id | |
259 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
260 | ||
261 | //get the action. | |
262 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
263 | $this->assign('action', $this->_action); | |
264 | ||
265 | //get the contribution id if update | |
266 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
267 | if (!empty($this->_id)) { | |
268 | $this->assign('contribID', $this->_id); | |
269 | } | |
270 | ||
271 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
272 | $this->assign('context', $this->_context); | |
273 | ||
274 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Positive', $this); | |
275 | ||
276 | $this->_compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this); | |
277 | ||
278 | //set the contribution mode. | |
279 | $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); | |
280 | ||
281 | $this->assign('contributionMode', $this->_mode); | |
459091e1 EM |
282 | if ($this->_action & CRM_Core_Action::DELETE) { |
283 | return; | |
284 | } | |
6a488035 TO |
285 | |
286 | $this->assign('showCheckNumber', TRUE); | |
287 | ||
288 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); | |
cc984198 | 289 | $this->assignPaymentRelatedVariables(); |
6a488035 | 290 | |
d77a0a58 | 291 | if (in_array('CiviPledge', CRM_Core_Config::singleton()->enableComponents) && !$this->_formType) { |
6a488035 TO |
292 | $this->preProcessPledge(); |
293 | } | |
294 | ||
d5397f2f PJ |
295 | if ($this->_id) { |
296 | $this->showRecordLinkMesssage($this->_id); | |
297 | } | |
6a488035 TO |
298 | $this->_values = array(); |
299 | ||
300 | // current contribution id | |
301 | if ($this->_id) { | |
302 | $this->assignPremiumProduct($this->_id); | |
303 | $this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values); | |
304 | } | |
305 | ||
306 | // when custom data is included in this page | |
a7488080 | 307 | if (!empty($_POST['hidden_custom'])) { |
6a488035 TO |
308 | $this->applyCustomData('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id); |
309 | } | |
310 | ||
311 | $this->_lineItems = array(); | |
312 | if ($this->_id) { | |
313 | if (!empty($this->_compId) && $this->_compContext == 'participant') { | |
314 | $this->assign('compId', $this->_compId); | |
315 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_compId); | |
316 | } | |
317 | else { | |
0b71718c | 318 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution', 1, TRUE, TRUE); |
6a488035 TO |
319 | } |
320 | empty($lineItem) ? NULL : $this->_lineItems[] = $lineItem; | |
321 | } | |
322 | ||
323 | $this->assign('lineItem', empty($this->_lineItems) ? FALSE : $this->_lineItems); | |
324 | ||
325 | // Set title | |
e2046b33 CW |
326 | if ($this->_mode) { |
327 | $this->setPageTitle($this->_ppID ? ts('Credit Card Pledge Payment') : ts('Credit Card Contribution')); | |
328 | } | |
329 | else { | |
330 | $this->setPageTitle($this->_ppID ? ts('Pledge Payment') : ts('Contribution')); | |
6a488035 | 331 | } |
1421174e | 332 | |
333 | if ($this->_id) { | |
334 | CRM_Contribute_Form_SoftCredit::preprocess($this); | |
335 | } | |
6a488035 TO |
336 | } |
337 | ||
00be9182 | 338 | public function setDefaultValues() { |
6a488035 TO |
339 | if ($this->_cdType) { |
340 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
341 | } | |
342 | ||
343 | $defaults = $this->_values; | |
344 | ||
345 | //set defaults for pledge payment. | |
346 | if ($this->_ppID) { | |
347 | $defaults['total_amount'] = CRM_Utils_Array::value('scheduled_amount', $this->_pledgeValues['pledgePayment']); | |
6a488035 TO |
348 | $defaults['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $this->_pledgeValues); |
349 | $defaults['currency'] = CRM_Utils_Array::value('currency', $this->_pledgeValues); | |
350 | $defaults['option_type'] = 1; | |
351 | } | |
352 | ||
6a488035 TO |
353 | if ($this->_action & CRM_Core_Action::DELETE) { |
354 | return $defaults; | |
355 | } | |
356 | ||
dfbad3f7 | 357 | // set soft credit defaults |
17db9f82 | 358 | CRM_Contribute_Form_SoftCredit::setDefaultValues($defaults, $this); |
dfbad3f7 | 359 | |
6a488035 TO |
360 | if ($this->_mode) { |
361 | $config = CRM_Core_Config::singleton(); | |
5a230af7 | 362 | // set default country from config if no country set |
a7488080 | 363 | if (empty($defaults["billing_country_id-{$this->_bltID}"])) { |
6a488035 TO |
364 | $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
365 | } | |
5a230af7 | 366 | |
a7488080 | 367 | if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { |
6a488035 TO |
368 | $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; |
369 | } | |
370 | ||
cc8ff73d DG |
371 | $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); |
372 | $defaults = array_merge($defaults, $billingDefaults); | |
6a488035 TO |
373 | } |
374 | ||
375 | if ($this->_id) { | |
376 | $this->_contactID = $defaults['contact_id']; | |
377 | } | |
5a230af7 | 378 | |
6a488035 | 379 | // Set $newCredit variable in template to control whether link to credit card mode is included |
9be1374d | 380 | $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()); |
5a230af7 | 381 | |
6a488035 TO |
382 | // fix the display of the monetary value, CRM-4038 |
383 | if (isset($defaults['total_amount'])) { | |
049db839 | 384 | if (!empty($defaults['tax_amount'])) { |
b6f65362 | 385 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); |
386 | if (!(CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails))) { | |
387 | $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'] - $defaults['tax_amount'], NULL, '%a'); | |
388 | } | |
049db839 | 389 | } |
390 | else { | |
391 | $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'], NULL, '%a'); | |
392 | } | |
6a488035 TO |
393 | } |
394 | ||
395 | if (isset($defaults['non_deductible_amount'])) { | |
396 | $defaults['non_deductible_amount'] = CRM_Utils_Money::format($defaults['non_deductible_amount'], NULL, '%a'); | |
397 | } | |
398 | ||
399 | if (isset($defaults['fee_amount'])) { | |
400 | $defaults['fee_amount'] = CRM_Utils_Money::format($defaults['fee_amount'], NULL, '%a'); | |
401 | } | |
402 | ||
403 | if (isset($defaults['net_amount'])) { | |
404 | $defaults['net_amount'] = CRM_Utils_Money::format($defaults['net_amount'], NULL, '%a'); | |
405 | } | |
406 | ||
407 | if ($this->_contributionType) { | |
408 | $defaults['financial_type_id'] = $this->_contributionType; | |
409 | } | |
133e2c99 | 410 | |
a7488080 | 411 | if (empty($defaults['payment_instrument_id'])) { |
d96cf288 DG |
412 | $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')); |
413 | } | |
6a488035 | 414 | |
a7488080 | 415 | if (!empty($defaults['is_test'])) { |
6a488035 TO |
416 | $this->assign('is_test', TRUE); |
417 | } | |
418 | ||
6a488035 TO |
419 | $this->assign('showOption', TRUE); |
420 | // for Premium section | |
421 | if ($this->_premiumID) { | |
422 | $this->assign('showOption', FALSE); | |
423 | $options = isset($this->_options[$this->_productDAO->product_id]) ? $this->_options[$this->_productDAO->product_id] : ""; | |
424 | if (!$options) { | |
425 | $this->assign('showOption', TRUE); | |
426 | } | |
427 | $options_key = CRM_Utils_Array::key($this->_productDAO->product_option, $options); | |
428 | if ($options_key) { | |
429 | $defaults['product_name'] = array($this->_productDAO->product_id, trim($options_key)); | |
430 | } | |
431 | else { | |
432 | $defaults['product_name'] = array($this->_productDAO->product_id); | |
433 | } | |
434 | if ($this->_productDAO->fulfilled_date) { | |
435 | list($defaults['fulfilled_date']) = CRM_Utils_Date::setDateDefaults($this->_productDAO->fulfilled_date); | |
436 | } | |
437 | } | |
438 | ||
439 | if (isset($this->userEmail)) { | |
440 | $this->assign('email', $this->userEmail); | |
441 | } | |
442 | ||
a7488080 | 443 | if (!empty($defaults['is_pay_later'])) { |
6a488035 TO |
444 | $this->assign('is_pay_later', TRUE); |
445 | } | |
446 | $this->assign('contribution_status_id', CRM_Utils_Array::value('contribution_status_id', $defaults)); | |
447 | ||
448 | $dates = array('receive_date', 'receipt_date', 'cancel_date', 'thankyou_date'); | |
449 | foreach ($dates as $key) { | |
a7488080 | 450 | if (!empty($defaults[$key])) { |
006389de TO |
451 | list($defaults[$key], $defaults[$key . '_time']) |
452 | = CRM_Utils_Date::setDateDefaults(CRM_Utils_Array::value($key, $defaults), 'activityDateTime'); | |
6a488035 TO |
453 | } |
454 | } | |
455 | ||
8cc574cf | 456 | if (!$this->_id && empty($defaults['receive_date'])) { |
6a488035 TO |
457 | list($defaults['receive_date'], |
458 | $defaults['receive_date_time'] | |
459 | ) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); | |
460 | } | |
461 | ||
462 | $this->assign('receive_date', CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $defaults), | |
463 | CRM_Utils_Array::value('receive_date_time', $defaults) | |
464 | )); | |
465 | $currency = CRM_Utils_Array::value('currency', $defaults); | |
466 | $this->assign('currency', $currency); | |
467 | // Hack to get currency info to the js layer. CRM-11440 | |
468 | CRM_Utils_Money::format(1); | |
469 | $this->assign('currencySymbol', CRM_Utils_Array::value($currency, CRM_Utils_Money::$_currencySymbols)); | |
470 | $this->assign('totalAmount', CRM_Utils_Array::value('total_amount', $defaults)); | |
471 | ||
472 | //inherit campaign from pledge. | |
8cc574cf | 473 | if ($this->_ppID && !empty($this->_pledgeValues['campaign_id'])) { |
6a488035 TO |
474 | $defaults['campaign_id'] = $this->_pledgeValues['campaign_id']; |
475 | } | |
476 | ||
477 | $this->_defaults = $defaults; | |
478 | return $defaults; | |
479 | } | |
480 | ||
481 | /** | |
c490a46a | 482 | * Build the form object |
6a488035 TO |
483 | * |
484 | * @return void | |
6a488035 TO |
485 | */ |
486 | public function buildQuickForm() { | |
dc913073 | 487 | //@todo document the purpose of cdType (if still in use) |
6a488035 TO |
488 | if ($this->_cdType) { |
489 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
490 | return; | |
491 | } | |
5032bf07 | 492 | $allPanes = array(); |
6daab7fa | 493 | //tax rate from financialType |
a1e457ff | 494 | $this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates())); |
049db839 | 495 | $this->assign('currencies', json_encode(CRM_Core_OptionGroup::values('currencies_enabled'))); |
6a488035 TO |
496 | |
497 | // build price set form. | |
498 | $buildPriceSet = FALSE; | |
353ffa53 | 499 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
03b412ae | 500 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); |
95f2107c | 501 | $this->assign('invoicing', $invoicing); |
a32709be | 502 | |
03b412ae PB |
503 | // display tax amount on edit contribution page |
504 | if ($invoicing && $this->_action & CRM_Core_Action::UPDATE && isset($this->_values['tax_amount'])) { | |
a32709be PB |
505 | $this->assign('totalTaxAmount', $this->_values['tax_amount']); |
506 | } | |
507 | ||
6a488035 | 508 | if (empty($this->_lineItems) && |
8cc574cf | 509 | ($this->_priceSetId || !empty($_POST['price_set_id'])) |
6a488035 TO |
510 | ) { |
511 | $buildPriceSet = TRUE; | |
512 | $getOnlyPriceSetElements = TRUE; | |
513 | if (!$this->_priceSetId) { | |
514 | $this->_priceSetId = $_POST['price_set_id']; | |
515 | $getOnlyPriceSetElements = FALSE; | |
516 | } | |
517 | ||
518 | $this->set('priceSetId', $this->_priceSetId); | |
9da8dc8c | 519 | CRM_Price_BAO_PriceSet::buildPriceSet($this); |
6a488035 TO |
520 | |
521 | // get only price set form elements. | |
522 | if ($getOnlyPriceSetElements) { | |
523 | return; | |
524 | } | |
525 | } | |
526 | // use to build form during form rule. | |
527 | $this->assign('buildPriceSet', $buildPriceSet); | |
528 | ||
529 | $showAdditionalInfo = FALSE; | |
530 | ||
6a488035 TO |
531 | $defaults = $this->_values; |
532 | $additionalDetailFields = array( | |
533 | 'note', | |
534 | 'thankyou_date', | |
535 | 'invoice_id', | |
536 | 'non_deductible_amount', | |
537 | 'fee_amount', | |
dfbad3f7 | 538 | 'net_amount', |
6a488035 TO |
539 | ); |
540 | foreach ($additionalDetailFields as $key) { | |
541 | if (!empty($defaults[$key])) { | |
542 | $defaults['hidden_AdditionalDetail'] = 1; | |
543 | break; | |
544 | } | |
545 | } | |
546 | ||
6a488035 TO |
547 | if ($this->_productDAO) { |
548 | if ($this->_productDAO->product_id) { | |
549 | $defaults['hidden_Premium'] = 1; | |
550 | } | |
551 | } | |
552 | ||
553 | if ($this->_noteID && | |
554 | isset($this->_values['note']) | |
555 | ) { | |
556 | $defaults['hidden_AdditionalDetail'] = 1; | |
557 | } | |
558 | ||
559 | $paneNames = array( | |
560 | ts('Additional Details') => 'AdditionalDetail', | |
6a488035 TO |
561 | ); |
562 | ||
563 | //Add Premium pane only if Premium is exists. | |
564 | $dao = new CRM_Contribute_DAO_Product(); | |
565 | $dao->is_active = 1; | |
566 | ||
567 | if ($dao->find(TRUE)) { | |
568 | $paneNames[ts('Premium Information')] = 'Premium'; | |
569 | } | |
570 | ||
6a488035 | 571 | if ($this->_mode) { |
dc913073 EM |
572 | if (CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE) == TRUE) { |
573 | $buildRecurBlock = TRUE; | |
574 | foreach ($this->billingPane as $name => $label) { | |
459091e1 EM |
575 | if (!empty($this->billingFieldSets[$name]['fields'])) { |
576 | //@todo reduce variation so we don't have to convert 'credit_card' to 'CreditCard' | |
577 | $paneNames[$label] = CRM_Utils_String::convertStringToCamel($name); | |
578 | } | |
dc913073 | 579 | } |
6a488035 TO |
580 | } |
581 | } | |
6a488035 | 582 | |
6a488035 TO |
583 | foreach ($paneNames as $name => $type) { |
584 | $urlParams = "snippet=4&formType={$type}"; | |
585 | if ($this->_mode) { | |
586 | $urlParams .= "&mode={$this->_mode}"; | |
587 | } | |
588 | ||
589 | $open = 'false'; | |
590 | if ($type == 'CreditCard' || | |
591 | $type == 'DirectDebit' | |
592 | ) { | |
593 | $open = 'true'; | |
594 | } | |
595 | ||
596 | $allPanes[$name] = array( | |
597 | 'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams), | |
598 | 'open' => $open, | |
21dfd5f5 | 599 | 'id' => $type, |
6a488035 TO |
600 | ); |
601 | ||
602 | // see if we need to include this paneName in the current form | |
8cc574cf | 603 | if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) || |
6a488035 TO |
604 | CRM_Utils_Array::value("hidden_{$type}", $defaults) |
605 | ) { | |
606 | $showAdditionalInfo = TRUE; | |
607 | $allPanes[$name]['open'] = 'true'; | |
608 | } | |
609 | ||
dc913073 EM |
610 | if ($type == 'CreditCard' || $type == 'DirectDebit') { |
611 | //@todo would be good to align tpl name with form name... | |
612 | $this->add('hidden', 'hidden_' . $type, 1); | |
6a488035 TO |
613 | } |
614 | else { | |
d37ade2e | 615 | $additionalInfoFormFunction = 'build' . $type; |
616 | CRM_Contribute_Form_AdditionalInfo::$additionalInfoFormFunction($this); | |
6a488035 TO |
617 | } |
618 | } | |
619 | if (empty($this->_recurPaymentProcessors)) { | |
620 | $buildRecurBlock = FALSE; | |
621 | } | |
622 | if ($buildRecurBlock) { | |
623 | CRM_Contribute_Form_Contribution_Main::buildRecur($this); | |
624 | $this->setDefaults(array('is_recur' => 0)); | |
625 | } | |
626 | $this->assign('buildRecurBlock', $buildRecurBlock); | |
627 | $qfKey = $this->controller->_key; | |
628 | $this->assign('qfKey', $qfKey); | |
629 | $this->assign('allPanes', $allPanes); | |
630 | $this->assign('showAdditionalInfo', $showAdditionalInfo); | |
631 | ||
632 | if ($this->_formType) { | |
633 | $this->assign('formType', $this->_formType); | |
634 | return; | |
635 | } | |
636 | ||
637 | $this->applyFilter('__ALL__', 'trim'); | |
5a230af7 | 638 | |
6a488035 TO |
639 | if ($this->_action & CRM_Core_Action::DELETE) { |
640 | $this->addButtons(array( | |
641 | array( | |
642 | 'type' => 'next', | |
643 | 'name' => ts('Delete'), | |
644 | 'spacing' => ' ', | |
645 | 'isDefault' => TRUE, | |
646 | ), | |
647 | array( | |
648 | 'type' => 'cancel', | |
21dfd5f5 TO |
649 | 'name' => ts('Cancel'), |
650 | ), | |
6a488035 TO |
651 | ) |
652 | ); | |
653 | return; | |
654 | } | |
655 | ||
656 | //need to assign custom data type and subtype to the template | |
657 | $this->assign('customDataType', 'Contribution'); | |
658 | $this->assign('customDataSubType', $this->_contributionType); | |
659 | $this->assign('entityID', $this->_id); | |
660 | ||
661 | if ($this->_context == 'standalone') { | |
353ffa53 TO |
662 | $this->addEntityRef('contact_id', ts('Contact'), array( |
663 | 'create' => TRUE, | |
acb1052e | 664 | 'api' => array('extra' => array('email')), |
353ffa53 | 665 | ), TRUE); |
6a488035 TO |
666 | } |
667 | ||
668 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); | |
669 | ||
670 | $financialType = $this->add('select', 'financial_type_id', | |
671 | ts('Financial Type'), | |
672 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType(), | |
79ca865b CW |
673 | TRUE, |
674 | array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );") | |
6a488035 | 675 | ); |
d77a0a58 | 676 | $paymentInstrument = FALSE; |
6a488035 TO |
677 | if (!$this->_mode) { |
678 | $paymentInstrument = $this->add('select', 'payment_instrument_id', | |
679 | ts('Paid By'), | |
680 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), | |
d96cf288 | 681 | TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);") |
6a488035 TO |
682 | ); |
683 | } | |
684 | ||
ce22fda6 | 685 | $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') + $attributes['trxn_id']); |
6a488035 TO |
686 | |
687 | //add receipt for offline contribution | |
688 | $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?')); | |
689 | ||
690 | $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails); | |
691 | ||
692 | $status = CRM_Contribute_PseudoConstant::contributionStatus(); | |
693 | ||
694 | // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169) | |
695 | $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
696 | if ($this->_ppID) { | |
697 | foreach (array( | |
698 | 'Cancelled', | |
699 | 'Failed', | |
21dfd5f5 | 700 | 'In Progress', |
6a488035 TO |
701 | ) as $suppress) { |
702 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); | |
703 | } | |
704 | } | |
705 | elseif ((!$this->_ppID && $this->_id) || !$this->_id) { | |
f73acc78 PN |
706 | $suppressFlag = FALSE; |
707 | if ($this->_id) { | |
708 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
709 | if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) { | |
710 | $suppressFlag = TRUE; | |
711 | } | |
712 | } | |
713 | if (!$suppressFlag) { | |
714 | foreach (array( | |
715 | 'Overdue', | |
21dfd5f5 | 716 | 'In Progress', |
f73acc78 PN |
717 | ) as $suppress) { |
718 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); | |
719 | } | |
720 | } | |
721 | else { | |
722 | unset($status[CRM_Utils_Array::key('Overdue', $statusName)]); | |
6a488035 TO |
723 | } |
724 | } | |
b6545333 | 725 | |
6a488035 TO |
726 | if ($this->_id) { |
727 | $contributionStatus = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id, 'contribution_status_id'); | |
728 | $name = CRM_Utils_Array::value($contributionStatus, $statusName); | |
d37ade2e | 729 | switch ($name) { |
6a488035 TO |
730 | case 'Completed': |
731 | case 'Cancelled': | |
732 | case 'Refunded': | |
f73acc78 | 733 | unset($status[CRM_Utils_Array::key('In Progress', $statusName)]); |
6a488035 TO |
734 | unset($status[CRM_Utils_Array::key('Pending', $statusName)]); |
735 | unset($status[CRM_Utils_Array::key('Failed', $statusName)]); | |
736 | break; | |
ea100cb5 | 737 | |
6a488035 | 738 | case 'Pending': |
f73acc78 | 739 | case 'In Progress': |
6a488035 TO |
740 | unset($status[CRM_Utils_Array::key('Refunded', $statusName)]); |
741 | break; | |
ea100cb5 | 742 | |
6a488035 | 743 | case 'Failed': |
d37ade2e | 744 | foreach (array( |
745 | 'Pending', | |
746 | 'Refunded', | |
747 | 'Completed', | |
f73acc78 | 748 | 'In Progress', |
21dfd5f5 | 749 | 'Cancelled', |
d37ade2e | 750 | ) as $suppress) { |
6a488035 TO |
751 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); |
752 | } | |
753 | break; | |
754 | } | |
d37ade2e | 755 | } |
756 | else { | |
6a488035 TO |
757 | unset($status[CRM_Utils_Array::key('Refunded', $statusName)]); |
758 | } | |
759 | ||
760 | $this->add('select', 'contribution_status_id', | |
761 | ts('Contribution Status'), | |
762 | $status, | |
763 | FALSE | |
764 | ); | |
765 | ||
766 | // add various dates | |
767 | $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime')); | |
768 | ||
769 | if ($this->_online) { | |
770 | $this->assign('hideCalender', TRUE); | |
771 | } | |
772 | $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']); | |
773 | ||
774 | $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime')); | |
775 | $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime')); | |
776 | ||
777 | $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']); | |
778 | ||
779 | $recurJs = NULL; | |
780 | if ($buildRecurBlock) { | |
781 | $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;"); | |
782 | } | |
783 | $element = $this->add('select', | |
784 | 'payment_processor_id', | |
785 | ts('Payment Processor'), | |
786 | $this->_processors, | |
787 | NULL, | |
788 | $recurJs | |
789 | ); | |
790 | ||
791 | if ($this->_online) { | |
792 | $element->freeze(); | |
793 | } | |
794 | $totalAmount = NULL; | |
795 | if (empty($this->_lineItems)) { | |
796 | $buildPriceSet = FALSE; | |
9da8dc8c | 797 | $priceSets = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute'); |
6a488035 TO |
798 | if (!empty($priceSets) && !$this->_ppID) { |
799 | $buildPriceSet = TRUE; | |
800 | } | |
801 | ||
802 | // don't allow price set for contribution if it is related to participant, or if it is a pledge payment | |
803 | // and if we already have line items for that participant. CRM-5095 | |
804 | if ($buildPriceSet && $this->_id) { | |
805 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
806 | $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', | |
807 | $this->_id, | |
808 | 'id', | |
809 | 'contribution_id' | |
810 | ); | |
811 | if ($pledgePaymentId) { | |
812 | $buildPriceSet = FALSE; | |
813 | } | |
814 | if ($participantID = CRM_Utils_Array::value('participant', $componentDetails)) { | |
815 | $participantLI = CRM_Price_BAO_LineItem::getLineItems($participantID); | |
816 | if (!CRM_Utils_System::isNull($participantLI)) { | |
817 | $buildPriceSet = FALSE; | |
818 | } | |
819 | } | |
820 | } | |
821 | ||
822 | $hasPriceSets = FALSE; | |
823 | if ($buildPriceSet) { | |
824 | $hasPriceSets = TRUE; | |
825 | $element = $this->add('select', 'price_set_id', ts('Choose price set'), | |
826 | array( | |
21dfd5f5 | 827 | '' => ts('Choose price set'), |
6a488035 TO |
828 | ) + $priceSets, |
829 | NULL, array('onchange' => "buildAmount( this.value );") | |
5a230af7 | 830 | ); |
6a488035 TO |
831 | if ($this->_online && !($this->_action & CRM_Core_Action::UPDATE)) { |
832 | $element->freeze(); | |
833 | } | |
834 | } | |
835 | $this->assign('hasPriceSets', $hasPriceSets); | |
836 | $currencyFreeze = FALSE; | |
837 | if (!($this->_action & CRM_Core_Action::UPDATE)) { | |
d37ade2e | 838 | if ($this->_online || $this->_ppID) { |
839 | $attributes['total_amount'] = array_merge($attributes['total_amount'], array( | |
840 | 'READONLY' => TRUE, | |
21dfd5f5 | 841 | 'style' => "background-color:#EBECE4", |
d37ade2e | 842 | )); |
843 | $optionTypes = array( | |
844 | '1' => ts('Adjust Pledge Payment Schedule?'), | |
845 | '2' => ts('Adjust Total Pledge Amount?'), | |
846 | ); | |
847 | $this->addRadio('option_type', | |
848 | NULL, | |
849 | $optionTypes, | |
850 | array(), '<br/>' | |
851 | ); | |
852 | ||
853 | $currencyFreeze = TRUE; | |
854 | } | |
6a488035 TO |
855 | } |
856 | ||
857 | $totalAmount = $this->addMoney('total_amount', | |
858 | ts('Total Amount'), | |
859 | ($hasPriceSets) ? FALSE : TRUE, | |
860 | $attributes['total_amount'], | |
861 | TRUE, 'currency', NULL, $currencyFreeze | |
862 | ); | |
863 | } | |
864 | ||
865 | $this->add('text', 'source', ts('Source'), CRM_Utils_Array::value('source', $attributes)); | |
866 | ||
867 | //CRM-7362 --add campaigns. | |
868 | CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values)); | |
869 | ||
0baed70b | 870 | CRM_Contribute_Form_SoftCredit::buildQuickForm($this); |
0baed70b | 871 | |
6a488035 TO |
872 | $js = NULL; |
873 | if (!$this->_mode) { | |
874 | $js = array('onclick' => "return verify( );"); | |
875 | } | |
876 | ||
877 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
878 | 'mailing_backend' | |
879 | ); | |
880 | $this->assign('outBound_option', $mailingInfo['outBound_option']); | |
881 | ||
882 | $this->addButtons(array( | |
883 | array( | |
884 | 'type' => 'upload', | |
885 | 'name' => ts('Save'), | |
886 | 'js' => $js, | |
21dfd5f5 | 887 | 'isDefault' => TRUE, |
6a488035 TO |
888 | ), |
889 | array( | |
890 | 'type' => 'upload', | |
891 | 'name' => ts('Save and New'), | |
892 | 'js' => $js, | |
21dfd5f5 | 893 | 'subName' => 'new', |
6a488035 TO |
894 | ), |
895 | array( | |
896 | 'type' => 'cancel', | |
21dfd5f5 | 897 | 'name' => ts('Cancel'), |
6a488035 TO |
898 | ), |
899 | ) | |
900 | ); | |
901 | ||
902 | // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type, | |
903 | // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail | |
904 | if ($this->_id && $this->_values['contribution_status_id'] == array_search('Cancelled', $statusName)) { | |
5a230af7 | 905 | if ($totalAmount) { |
6a488035 TO |
906 | $totalAmount->freeze(); |
907 | } | |
908 | $checkNumber->freeze(); | |
909 | $paymentInstrument->freeze(); | |
910 | $trxnId->freeze(); | |
911 | $financialType->freeze(); | |
912 | } | |
913 | ||
914 | $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this); | |
915 | ||
b6f65362 | 916 | // if contribution is related to membership or participant freeze Financial Type, Amount |
917 | if ($this->_id && isset($this->_values['tax_amount'])) { | |
918 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
919 | if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) { | |
b5527a9a | 920 | if ($totalAmount) { |
a13f3d8c | 921 | $totalAmount->freeze(); |
b5527a9a | 922 | } |
b6f65362 | 923 | $financialType->freeze(); |
b6f65362 | 924 | $this->assign('freezeFinancialType', TRUE); |
925 | } | |
926 | } | |
927 | ||
6a488035 TO |
928 | if ($this->_action & CRM_Core_Action::VIEW) { |
929 | $this->freeze(); | |
930 | } | |
931 | } | |
932 | ||
933 | /** | |
100fef9d | 934 | * Global form rule |
6a488035 | 935 | * |
014c4014 TO |
936 | * @param array $fields |
937 | * The input form values. | |
938 | * @param array $files | |
939 | * The uploaded files if any. | |
2a6da8d7 EM |
940 | * @param $self |
941 | * | |
6a488035 | 942 | * |
72b3a70c CW |
943 | * @return bool|array |
944 | * true if no errors, else array of errors | |
6a488035 | 945 | */ |
00be9182 | 946 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
947 | $errors = array(); |
948 | ||
6a488035 TO |
949 | //check for Credit Card Contribution. |
950 | if ($self->_mode) { | |
951 | if (empty($fields['payment_processor_id'])) { | |
952 | $errors['payment_processor_id'] = ts('Payment Processor is a required field.'); | |
953 | } | |
954 | } | |
955 | ||
956 | // do the amount validations. | |
a7488080 | 957 | if (empty($fields['total_amount']) && empty($self->_lineItems)) { |
6a488035 | 958 | if ($priceSetId = CRM_Utils_Array::value('price_set_id', $fields)) { |
9da8dc8c | 959 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $fields, $errors); |
6a488035 TO |
960 | } |
961 | } | |
962 | ||
b326919b | 963 | $softErrors = CRM_Contribute_Form_SoftCredit::formRule($fields, $errors, $self); |
d37ade2e | 964 | |
3256b416 JM |
965 | if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) || !empty($fields['fee_amount']))) { |
966 | $sum = CRM_Utils_Rule::cleanMoney($fields['net_amount']) + CRM_Utils_Rule::cleanMoney($fields['fee_amount']); | |
967 | if (CRM_Utils_Rule::cleanMoney($fields['total_amount']) != $sum) { | |
6a488035 TO |
968 | $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount'); |
969 | } | |
970 | } | |
971 | //form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow | |
972 | if ($self->_id && $self->_values['contribution_status_id'] != $fields['contribution_status_id']) { | |
973 | CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors); | |
974 | } | |
975 | ||
976 | //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow | |
353ffa53 | 977 | if (!empty($fields['fee_amount']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($fields['financial_type_id'])) { |
51fa20cb | 978 | $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType; |
6a488035 | 979 | } |
133e2c99 | 980 | |
7272e4fa DG |
981 | // $trxn_id must be unique CRM-13919 |
982 | if (!empty($fields['trxn_id'])) { | |
983 | $queryParams = array(1 => array($fields['trxn_id'], 'String')); | |
984 | $query = 'select count(*) from civicrm_contribution where trxn_id = %1'; | |
985 | if ($self->_id) { | |
353ffa53 | 986 | $queryParams[2] = array((int) $self->_id, 'Integer'); |
7272e4fa DG |
987 | $query .= ' and id !=%2'; |
988 | } | |
989 | $tCnt = CRM_Core_DAO::singleValueQuery($query, $queryParams); | |
990 | if ($tCnt) { | |
991 | $errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id'])); | |
133e2c99 | 992 | } |
7272e4fa | 993 | } |
d37ade2e | 994 | |
ac0c89ed | 995 | $errors = array_merge($errors, $softErrors); |
6a488035 TO |
996 | return $errors; |
997 | } | |
998 | ||
999 | /** | |
c490a46a | 1000 | * Process the form submission |
6a488035 | 1001 | * |
6a488035 TO |
1002 | * |
1003 | * @return void | |
1004 | */ | |
1005 | public function postProcess() { | |
cb579c66 | 1006 | $sendReceipt = $pId = $contribution = $isRelatedId = FALSE; |
d3e86119 | 1007 | $softParams = $softIDs = array(); |
d77a0a58 | 1008 | |
6a488035 TO |
1009 | if ($this->_action & CRM_Core_Action::DELETE) { |
1010 | CRM_Contribute_BAO_Contribution::deleteContribution($this->_id); | |
c1cc3e0c | 1011 | CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', |
6a488035 TO |
1012 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" |
1013 | )); | |
1014 | return; | |
1015 | } | |
1016 | ||
1017 | // get the submitted form values. | |
1018 | $submittedValues = $this->controller->exportValues($this->_name); | |
a7488080 | 1019 | if (!empty($submittedValues['price_set_id']) && $this->_action & CRM_Core_Action::UPDATE) { |
d37ade2e | 1020 | $line = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution'); |
6a488035 | 1021 | $lineID = key($line); |
9da8dc8c | 1022 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('price_field_id', $line[$lineID]), 'price_set_id'); |
1023 | $quickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); | |
6a488035 TO |
1024 | if ($quickConfig) { |
1025 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_contribution'); | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | // process price set and get total amount and line items. | |
1030 | $lineItem = array(); | |
6a488035 TO |
1031 | $priceSetId = CRM_Utils_Array::value('price_set_id', $submittedValues); |
1032 | if (empty($priceSetId) && !$this->_id) { | |
9da8dc8c | 1033 | $this->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name'); |
1034 | $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); | |
6a488035 TO |
1035 | $fieldID = key($this->_priceSet['fields']); |
1036 | $fieldValueId = key($this->_priceSet['fields'][$fieldID]['options']); | |
1037 | $this->_priceSet['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount']; | |
1038 | $submittedValues['price_' . $fieldID] = 1; | |
1039 | } | |
1040 | ||
1041 | if ($priceSetId) { | |
9da8dc8c | 1042 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], |
5a230af7 | 1043 | $submittedValues, $lineItem[$priceSetId]); |
1044 | ||
3f7bb5e2 | 1045 | // unset tax amount for offline 'is_quick_config' contribution |
d75f2f47 | 1046 | if ($this->_priceSet['is_quick_config'] && |
3f7bb5e2 PB |
1047 | !array_key_exists($submittedValues['financial_type_id'], CRM_Core_PseudoConstant::getTaxRates()) |
1048 | ) { | |
1049 | unset($submittedValues['tax_amount']); | |
1050 | } | |
6a488035 TO |
1051 | $submittedValues['total_amount'] = CRM_Utils_Array::value('amount', $submittedValues); |
1052 | } | |
1053 | if ($this->_id) { | |
cb579c66 PN |
1054 | if ($this->_compId) { |
1055 | if ($this->_context == 'participant') { | |
1056 | $pId = $this->_compId; | |
1057 | } | |
1058 | elseif ($this->_context == 'membership') { | |
1059 | $isRelatedId = TRUE; | |
1060 | } | |
34434cd4 | 1061 | else { |
1062 | $pId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'participant_id', 'contribution_id'); | |
1063 | } | |
cb579c66 PN |
1064 | } |
1065 | else { | |
1066 | $contributionDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
1067 | if (array_key_exists('membership', $contributionDetails)) { | |
1068 | $isRelatedId = TRUE; | |
1069 | } | |
1070 | elseif (array_key_exists('participant', $contributionDetails)) { | |
1071 | $pId = current($contributionDetails['participant']); | |
77e18ec7 | 1072 | } |
cb579c66 | 1073 | } |
6a488035 | 1074 | } |
77e18ec7 | 1075 | |
1076 | $isQuickConfig = 0; | |
1077 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { | |
1078 | $isQuickConfig = 1; | |
1079 | } | |
1080 | ||
8cc574cf | 1081 | if (!$priceSetId && !empty($submittedValues['total_amount']) && $this->_id) { |
6a488035 TO |
1082 | // 10117 update th line items for participants |
1083 | if ($pId) { | |
1084 | $entityTable = 'participant'; | |
1085 | $entityID = $pId; | |
77e18ec7 | 1086 | $isRelatedId = FALSE; |
6a488035 TO |
1087 | $participantParams = array( |
1088 | 'fee_amount' => $submittedValues['total_amount'], | |
21dfd5f5 | 1089 | 'id' => $entityID, |
6a488035 TO |
1090 | ); |
1091 | CRM_Event_BAO_Participant::add($participantParams); | |
1092 | if (empty($this->_lineItems)) { | |
01e72fbc | 1093 | $this->_lineItems[] = CRM_Price_BAO_LineItem::getLineItems($entityID, 'participant', 1); |
6a488035 | 1094 | } |
d37ade2e | 1095 | } |
1096 | else { | |
6a488035 TO |
1097 | $entityTable = 'contribution'; |
1098 | $entityID = $this->_id; | |
1099 | } | |
1100 | ||
77e18ec7 | 1101 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, $entityTable, $isQuickConfig, $isRelatedId); |
5af5956c CB |
1102 | foreach (array_keys($lineItems) as $id) { |
1103 | $lineItems[$id]['id'] = $id; | |
1104 | } | |
6a488035 | 1105 | $itemId = key($lineItems); |
8cc574cf | 1106 | if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) { |
870f2eec PJ |
1107 | $this->_priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id'); |
1108 | } | |
1109 | ||
1110 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { | |
1111 | $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('total_amount', $submittedValues)); | |
04930780 | 1112 | |
049db839 | 1113 | // Update line total and total amount with tax on edit |
04930780 | 1114 | $financialItemsId = CRM_Core_PseudoConstant::getTaxRates(); |
1115 | if (array_key_exists($submittedValues['financial_type_id'], $financialItemsId)) { | |
1116 | $lineItems[$itemId]['tax_rate'] = $financialItemsId[$submittedValues['financial_type_id']]; | |
1117 | } | |
1118 | else { | |
33421d01 | 1119 | $lineItems[$itemId]['tax_rate'] = $lineItems[$itemId]['tax_amount'] = ""; |
04930780 | 1120 | $submittedValues['tax_amount'] = 'null'; |
1121 | } | |
1122 | if ($lineItems[$itemId]['tax_rate']) { | |
d3e86119 | 1123 | $lineItems[$itemId]['tax_amount'] = ($lineItems[$itemId]['tax_rate'] / 100) * $lineItems[$itemId]['line_total']; |
04930780 | 1124 | $submittedValues['total_amount'] = $lineItems[$itemId]['line_total'] + $lineItems[$itemId]['tax_amount']; |
1125 | $submittedValues['tax_amount'] = $lineItems[$itemId]['tax_amount']; | |
049db839 | 1126 | } |
6a488035 | 1127 | } |
6a488035 | 1128 | // 10117 update th line items for participants |
32cb2feb | 1129 | if (!empty($lineItems[$itemId]['price_field_id'])) { |
32cb2feb CW |
1130 | $lineItem[$this->_priceSetId] = $lineItems; |
1131 | } | |
6a488035 | 1132 | } |
77e18ec7 | 1133 | |
d77a0a58 | 1134 | //CRM-11529 for quick config back office transactions |
5a230af7 | 1135 | //when financial_type_id is passed in form, update the |
d77a0a58 | 1136 | //line items with the financial type selected in form |
8cc574cf | 1137 | if ($isQuickConfig && !empty($submittedValues['financial_type_id']) && CRM_Utils_Array::value($this->_priceSetId, $lineItem) |
d37ade2e | 1138 | ) { |
6a488035 TO |
1139 | foreach ($lineItem[$this->_priceSetId] as &$values) { |
1140 | $values['financial_type_id'] = $submittedValues['financial_type_id']; | |
1141 | } | |
1142 | } | |
5a230af7 | 1143 | |
3b557cc8 | 1144 | if (!isset($submittedValues['total_amount'])) { |
6a488035 TO |
1145 | $submittedValues['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_values); |
1146 | } | |
1147 | $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE); | |
1148 | ||
a7488080 | 1149 | if (!empty($submittedValues['pcp_made_through_id'])) { |
912e7aaa | 1150 | $pcp = array(); |
d37ade2e | 1151 | $fields = array( |
1152 | 'pcp_made_through_id', | |
1153 | 'pcp_display_in_roll', | |
1154 | 'pcp_roll_nickname', | |
1155 | 'pcp_personal_note', | |
1156 | ); | |
ce8be7f2 | 1157 | foreach ($fields as $f) { |
1158 | $pcp[$f] = CRM_Utils_Array::value($f, $submittedValues); | |
1159 | } | |
cd0a30ac | 1160 | } |
b6545333 | 1161 | |
1162 | $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id'])); | |
1163 | if ($this->_id && count($isEmpty) == 1 && key($isEmpty) == NULL) { | |
1164 | //Delete existing soft credit records if soft credit list is empty on update | |
1165 | CRM_Contribute_BAO_ContributionSoft::del(array('contribution_id' => $this->_id)); | |
1166 | } | |
cd0a30ac | 1167 | else { |
b6545333 | 1168 | //build soft credit params |
b6545333 | 1169 | foreach ($submittedValues['soft_credit_contact_id'] as $key => $val) { |
1170 | if ($val && $submittedValues['soft_credit_amount'][$key]) { | |
1171 | $softParams[$key]['contact_id'] = $val; | |
1172 | $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($submittedValues['soft_credit_amount'][$key]); | |
1173 | $softParams[$key]['soft_credit_type_id'] = $submittedValues['soft_credit_type'][$key]; | |
1174 | if (!empty($submittedValues['soft_credit_id'][$key])) { | |
1175 | $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key]; | |
cd0a30ac | 1176 | } |
1177 | } | |
d37ade2e | 1178 | } |
cd0a30ac | 1179 | } |
1180 | ||
6a488035 | 1181 | // set the contact, when contact is selected |
ccec9d6b CW |
1182 | if (!empty($submittedValues['contact_id'])) { |
1183 | $this->_contactID = $submittedValues['contact_id']; | |
6a488035 TO |
1184 | } |
1185 | ||
1186 | $config = CRM_Core_Config::singleton(); | |
1187 | ||
1188 | //Credit Card Contribution. | |
1189 | if ($this->_mode) { | |
c1cc3e0c | 1190 | $session = CRM_Core_Session::singleton(); |
d77a0a58 | 1191 | $this->processCreditCard($submittedValues, $config, $session, $lineItem); |
6a488035 TO |
1192 | } |
1193 | else { | |
1194 | //Offline Contribution. | |
1195 | $submittedValues = $this->unsetCreditCardFields($submittedValues); | |
1196 | ||
1197 | // get the required field value only. | |
1198 | $formValues = $submittedValues; | |
1199 | $params = $ids = array(); | |
1200 | ||
1201 | $params['contact_id'] = $this->_contactID; | |
1202 | ||
1203 | $params['currency'] = $this->getCurrency($submittedValues); | |
1204 | ||
1205 | $fields = array( | |
1206 | 'financial_type_id', | |
1207 | 'contribution_status_id', | |
1208 | 'payment_instrument_id', | |
1209 | 'cancel_reason', | |
1210 | 'source', | |
1211 | 'check_number', | |
6a488035 TO |
1212 | ); |
1213 | foreach ($fields as $f) { | |
1214 | $params[$f] = CRM_Utils_Array::value($f, $formValues); | |
1215 | } | |
ce8be7f2 | 1216 | |
1217 | if (!empty($pcp)) { | |
3f662d9d | 1218 | $params['pcp'] = $pcp; |
ce8be7f2 | 1219 | } |
cd0a30ac | 1220 | if (!empty($softParams)) { |
3f662d9d | 1221 | $params['soft_credit'] = $softParams; |
2cfc0f58 | 1222 | $params['soft_credit_ids'] = $softIDs; |
6a488035 | 1223 | } |
3f662d9d | 1224 | |
6a488035 TO |
1225 | //if priceset is used, no need to cleanup money |
1226 | //CRM-5740 | |
1227 | if ($priceSetId) { | |
1228 | $params['skipCleanMoney'] = 1; | |
1229 | } | |
1230 | ||
1231 | $dates = array( | |
1232 | 'receive_date', | |
1233 | 'receipt_date', | |
1234 | 'cancel_date', | |
1235 | ); | |
1236 | ||
1237 | foreach ($dates as $d) { | |
1238 | $params[$d] = CRM_Utils_Date::processDate($formValues[$d], $formValues[$d . '_time'], TRUE); | |
1239 | } | |
1240 | ||
a7488080 | 1241 | if (!empty($formValues['is_email_receipt'])) { |
6a488035 TO |
1242 | $params['receipt_date'] = date("Y-m-d"); |
1243 | } | |
1244 | ||
1245 | if ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Cancelled', 'name') | |
d37ade2e | 1246 | || $params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name') |
1247 | ) { | |
6a488035 | 1248 | if (CRM_Utils_System::isNull(CRM_Utils_Array::value('cancel_date', $params))) { |
d37ade2e | 1249 | $params['cancel_date'] = date('Y-m-d'); |
6a488035 TO |
1250 | } |
1251 | } | |
1252 | else { | |
d37ade2e | 1253 | $params['cancel_date'] = $params['cancel_reason'] = 'null'; |
6a488035 TO |
1254 | } |
1255 | ||
1256 | // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996 | |
5f6d46e1 | 1257 | // else if contribution_status is changed to Completed is_pay_later flag is changed to 0, CRM-15041 |
6a488035 TO |
1258 | if ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name')) { |
1259 | $params['is_pay_later'] = 1; | |
1260 | } | |
5f6d46e1 | 1261 | elseif ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) { |
1262 | $params['is_pay_later'] = 0; | |
1263 | } | |
6a488035 TO |
1264 | |
1265 | $ids['contribution'] = $params['id'] = $this->_id; | |
1266 | ||
1267 | //Add Additional common information to formatted params | |
1268 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($formValues, $params, $this); | |
1269 | if ($pId) { | |
1270 | $params['contribution_mode'] = 'participant'; | |
1271 | $params['participant_id'] = $pId; | |
464bb009 | 1272 | $params['skipLineItem'] = 1; |
6a488035 | 1273 | } |
cb579c66 PN |
1274 | elseif ($isRelatedId) { |
1275 | $params['contribution_mode'] = 'membership'; | |
1276 | } | |
6a488035 TO |
1277 | $params['line_item'] = $lineItem; |
1278 | $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array::value('id', $this->_paymentProcessor); | |
3f7bb5e2 | 1279 | if (isset($submittedValues['tax_amount'])) { |
d91b8b33 | 1280 | $params['tax_amount'] = $submittedValues['tax_amount']; |
1281 | } | |
6a488035 TO |
1282 | //create contribution. |
1283 | if ($isQuickConfig) { | |
1284 | $params['is_quick_config'] = 1; | |
1285 | } | |
1286 | ||
1287 | // CRM-11956 | |
d77a0a58 | 1288 | // if non_deductible_amount exists i.e. Additional Details field set was opened [and staff typed something] - |
6a488035 TO |
1289 | // if non_deductible_amount does NOT exist - then calculate it depending on: |
1290 | // $ContributionType->is_deductible and whether there is a product (premium). | |
0f1a91cd | 1291 | if (empty($params['non_deductible_amount'])) { |
6a488035 TO |
1292 | $contributionType = new CRM_Financial_DAO_FinancialType(); |
1293 | $contributionType->id = $params['financial_type_id']; | |
1294 | if (!$contributionType->find(TRUE)) { | |
1295 | CRM_Core_Error::fatal('Could not find a system table'); | |
1296 | } | |
1297 | if ($contributionType->is_deductible) { | |
1298 | ||
1299 | if (isset($formValues['product_name'][0])) { | |
1300 | $selectProduct = $formValues['product_name'][0]; | |
1301 | } | |
1302 | // if there is a product - compare the value to the contribution amount | |
d37ade2e | 1303 | if (isset($selectProduct)) { |
6a488035 TO |
1304 | $productDAO = new CRM_Contribute_DAO_Product(); |
1305 | $productDAO->id = $selectProduct; | |
1306 | $productDAO->find(TRUE); | |
1307 | // product value exceeds contribution amount | |
1308 | if ($params['total_amount'] < $productDAO->price) { | |
1309 | $params['non_deductible_amount'] = $params['total_amount']; | |
1310 | } | |
1311 | // product value does NOT exceed contribution amount | |
1312 | else { | |
1313 | $params['non_deductible_amount'] = $productDAO->price; | |
1314 | } | |
1315 | } | |
1316 | // contribution is deductible - but there is no product | |
1317 | else { | |
1318 | $params['non_deductible_amount'] = '0.00'; | |
1319 | } | |
1320 | } | |
1321 | // contribution is NOT deductible | |
1322 | else { | |
1323 | $params['non_deductible_amount'] = $params['total_amount']; | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); | |
1328 | ||
1329 | // process associated membership / participant, CRM-4395 | |
1330 | $relatedComponentStatusMsg = NULL; | |
1331 | if ($contribution->id && $this->_action & CRM_Core_Action::UPDATE) { | |
1332 | $relatedComponentStatusMsg = $this->updateRelatedComponent($contribution->id, | |
1333 | $contribution->contribution_status_id, | |
1334 | CRM_Utils_Array::value('contribution_status_id', | |
1335 | $this->_values | |
1336 | ) | |
1337 | ); | |
1338 | } | |
1339 | ||
1340 | //process note | |
1341 | if ($contribution->id && isset($formValues['note'])) { | |
1342 | CRM_Contribute_Form_AdditionalInfo::processNote($formValues, $this->_contactID, $contribution->id, $this->_noteID); | |
1343 | } | |
1344 | ||
1345 | //process premium | |
5a230af7 | 1346 | if ($contribution->id && isset($formValues['product_name'][0])) { |
6a488035 TO |
1347 | CRM_Contribute_Form_AdditionalInfo::processPremium($formValues, $contribution->id, |
1348 | $this->_premiumID, $this->_options | |
1349 | ); | |
1350 | } | |
1351 | ||
9b1fa6eb | 1352 | // assign tax calculation for contribution receipts |
1353 | $taxRate = array(); | |
3f7bb5e2 | 1354 | $getTaxDetails = FALSE; |
353ffa53 | 1355 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
3f7bb5e2 | 1356 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); |
3f7bb5e2 PB |
1357 | if ($invoicing) { |
1358 | if ($this->_action & CRM_Core_Action::ADD) { | |
1359 | $line = $lineItem; | |
1360 | } | |
1361 | elseif ($this->_action & CRM_Core_Action::UPDATE) { | |
1362 | $line = $this->_lineItems; | |
1363 | } | |
1364 | foreach ($line as $key => $value) { | |
9b1fa6eb | 1365 | foreach ($value as $v) { |
288ec043 | 1366 | if (isset($taxRate[(string) CRM_Utils_Array::value('tax_rate', $v)])) { |
9b1fa6eb | 1367 | $taxRate[(string) $v['tax_rate']] = $taxRate[(string) $v['tax_rate']] + CRM_Utils_Array::value('tax_amount', $v); |
1368 | } | |
1369 | else { | |
3f7bb5e2 PB |
1370 | if (isset($v['tax_rate'])) { |
1371 | $taxRate[(string) $v['tax_rate']] = CRM_Utils_Array::value('tax_amount', $v); | |
1372 | $getTaxDetails = TRUE; | |
1373 | } | |
9b1fa6eb | 1374 | } |
1375 | } | |
1376 | } | |
1377 | } | |
d75f2f47 | 1378 | |
3f7bb5e2 PB |
1379 | if ($invoicing) { |
1380 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
1381 | if (isset($submittedValues['tax_amount'])) { | |
1382 | $totalTaxAmount = $submittedValues['tax_amount']; | |
1383 | } | |
1384 | else { | |
1385 | $totalTaxAmount = $this->_values['tax_amount']; | |
1386 | } | |
1387 | $this->assign('totalTaxAmount', $totalTaxAmount); | |
1388 | $this->assign('dataArray', $taxRate); | |
1389 | } | |
1390 | else { | |
1391 | if (CRM_Utils_Array::value('price_set_id', $submittedValues)) { | |
1392 | $this->assign('totalTaxAmount', $submittedValues['tax_amount']); | |
1393 | $this->assign('getTaxDetails', $getTaxDetails); | |
1394 | $this->assign('dataArray', $taxRate); | |
01604562 | 1395 | $this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings)); |
3f7bb5e2 PB |
1396 | } |
1397 | else { | |
1398 | $this->assign('totalTaxAmount', CRM_Utils_Array::value('tax_amount', $submittedValues)); | |
1399 | } | |
1400 | } | |
1401 | } | |
9b1fa6eb | 1402 | |
6a488035 | 1403 | //send receipt mail. |
8cc574cf | 1404 | if ($contribution->id && !empty($formValues['is_email_receipt'])) { |
6a488035 TO |
1405 | $formValues['contact_id'] = $this->_contactID; |
1406 | $formValues['contribution_id'] = $contribution->id; | |
1407 | ||
1421174e | 1408 | $formValues += CRM_Contribute_BAO_ContributionSoft::getSoftContribution($contribution->id); |
1409 | ||
6a488035 TO |
1410 | // to get 'from email id' for send receipt |
1411 | $this->fromEmailId = $formValues['from_email_address']; | |
1412 | $sendReceipt = CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $formValues); | |
1413 | } | |
1414 | ||
1415 | $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', | |
1416 | $contribution->id, | |
1417 | 'id', | |
1418 | 'contribution_id' | |
1419 | ); | |
1420 | ||
1421 | //update pledge payment status. | |
1422 | if ((($this->_ppID && $contribution->id) && $this->_action & CRM_Core_Action::ADD) || | |
1423 | (($pledgePaymentId) && $this->_action & CRM_Core_Action::UPDATE) | |
1424 | ) { | |
1425 | ||
1426 | if ($this->_ppID) { | |
1427 | //store contribution id in payment record. | |
1428 | CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id); | |
1429 | } | |
1430 | else { | |
1431 | $this->_ppID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', | |
1432 | $contribution->id, | |
1433 | 'id', | |
1434 | 'contribution_id' | |
1435 | ); | |
1436 | $this->_pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', | |
1437 | $contribution->id, | |
1438 | 'pledge_id', | |
1439 | 'contribution_id' | |
1440 | ); | |
1441 | } | |
1442 | ||
1443 | $adjustTotalAmount = FALSE; | |
1444 | if (CRM_Utils_Array::value('option_type', $formValues) == 2) { | |
1445 | $adjustTotalAmount = TRUE; | |
1446 | } | |
1447 | ||
1448 | $updatePledgePaymentStatus = FALSE; | |
1449 | //do only if either the status or the amount has changed | |
1450 | if ($this->_action & CRM_Core_Action::ADD) { | |
1451 | $updatePledgePaymentStatus = TRUE; | |
1452 | } | |
1453 | elseif ($this->_action & CRM_Core_Action::UPDATE && (($this->_defaults['contribution_status_id'] != $formValues['contribution_status_id']) || | |
353ffa53 | 1454 | ($this->_defaults['total_amount'] != $formValues['total_amount'])) |
6a488035 TO |
1455 | ) { |
1456 | $updatePledgePaymentStatus = TRUE; | |
1457 | } | |
1458 | ||
1459 | if ($updatePledgePaymentStatus) { | |
1460 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID, | |
1461 | array($this->_ppID), | |
1462 | $contribution->contribution_status_id, | |
1463 | NULL, | |
1464 | $contribution->total_amount, | |
1465 | $adjustTotalAmount | |
1466 | ); | |
1467 | } | |
1468 | } | |
5a230af7 | 1469 | |
6a488035 | 1470 | $statusMsg = ts('The contribution record has been saved.'); |
a7488080 | 1471 | if (!empty($formValues['is_email_receipt']) && $sendReceipt) { |
6a488035 TO |
1472 | $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.'); |
1473 | } | |
5a230af7 | 1474 | |
6a488035 TO |
1475 | if ($relatedComponentStatusMsg) { |
1476 | $statusMsg .= ' ' . $relatedComponentStatusMsg; | |
1477 | } | |
5a230af7 | 1478 | |
6a488035 TO |
1479 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); |
1480 | //Offline Contribution ends. | |
5a230af7 | 1481 | } |
a15a9bb8 | 1482 | $session = CRM_Core_Session::singleton(); |
6a488035 TO |
1483 | $buttonName = $this->controller->getButtonName(); |
1484 | if ($this->_context == 'standalone') { | |
1485 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
1486 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contribute/add', | |
1487 | 'reset=1&action=add&context=standalone' | |
1488 | )); | |
1489 | } | |
1490 | else { | |
1491 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
1492 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" | |
1493 | )); | |
1494 | } | |
1495 | } | |
1496 | elseif ($this->_context == 'contribution' && $this->_mode && $buttonName == $this->getButtonName('upload', 'new')) { | |
1497 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', | |
1498 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}" | |
1499 | )); | |
1500 | } | |
1501 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
1502 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', | |
1503 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}" | |
1504 | )); | |
1505 | } | |
28d44c9f BS |
1506 | |
1507 | //store contribution ID if not yet set (on create) | |
481a74f4 | 1508 | if (empty($this->_id) && !empty($contribution->id)) { |
28d44c9f BS |
1509 | $this->_id = $contribution->id; |
1510 | } | |
6a488035 | 1511 | } |
5a230af7 | 1512 | |
d77a0a58 EM |
1513 | /** |
1514 | * @param $submittedValues | |
1515 | * @param $config | |
5a4f6742 | 1516 | * @param CRM_Core_Session $session |
d77a0a58 EM |
1517 | * @param $lineItem |
1518 | * | |
1519 | * @throws CRM_Core_Exception | |
1520 | */ | |
1521 | public function processCreditCard($submittedValues, $config, $session, $lineItem) { | |
1522 | $sendReceipt = $contribution = FALSE; | |
c1cc3e0c | 1523 | |
6a488035 TO |
1524 | $unsetParams = array( |
1525 | 'trxn_id', | |
1526 | 'payment_instrument_id', | |
1527 | 'contribution_status_id', | |
1528 | 'cancel_date', | |
1529 | 'cancel_reason', | |
1530 | ); | |
1531 | foreach ($unsetParams as $key) { | |
1532 | if (isset($submittedValues[$key])) { | |
1533 | unset($submittedValues[$key]); | |
1534 | } | |
1535 | } | |
4bd318e0 | 1536 | $isTest = ($this->_mode == 'test') ? 1 : 0; |
25e92485 PN |
1537 | // CRM-12680 set $_lineItem if its not set |
1538 | if (empty($this->_lineItem) && !empty($lineItem)) { | |
1539 | $this->_lineItem = $lineItem; | |
1540 | } | |
eea16664 | 1541 | |
5032bf07 | 1542 | //Get the require fields value only. |
6a488035 TO |
1543 | $params = $this->_params = $submittedValues; |
1544 | ||
1545 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], | |
1546 | $this->_mode | |
1547 | ); | |
1548 | ||
1549 | //get the payment processor id as per mode. | |
acb1052e WA |
1550 | $this->_params['payment_processor'] = $params['payment_processor_id'] |
1551 | = $this->_params['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor['id']; | |
6a488035 TO |
1552 | |
1553 | $now = date('YmdHis'); | |
1554 | $fields = array(); | |
1555 | ||
1556 | // we need to retrieve email address | |
8cc574cf | 1557 | if ($this->_context == 'standalone' && !empty($submittedValues['is_email_receipt'])) { |
6a488035 TO |
1558 | list($this->userDisplayName, |
1559 | $this->userEmail | |
1560 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
1561 | $this->assign('displayName', $this->userDisplayName); | |
1562 | } | |
1563 | ||
1564 | //set email for primary location. | |
1565 | $fields['email-Primary'] = 1; | |
1566 | $params['email-Primary'] = $this->userEmail; | |
1567 | ||
1568 | // now set the values for the billing location. | |
d77a0a58 | 1569 | foreach (array_keys($this->_fields) as $name) { |
6a488035 TO |
1570 | $fields[$name] = 1; |
1571 | } | |
1572 | ||
1573 | // also add location name to the array | |
1574 | $params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params); | |
c1cc3e0c | 1575 | |
6a488035 TO |
1576 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); |
1577 | $fields["address_name-{$this->_bltID}"] = 1; | |
6a488035 TO |
1578 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
1579 | $this->_contactID, | |
1580 | 'contact_type' | |
1581 | ); | |
1582 | ||
1583 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
1584 | foreach ($nameFields as $name) { | |
1585 | $fields[$name] = 1; | |
1586 | if (array_key_exists("billing_$name", $params)) { | |
1587 | $params[$name] = $params["billing_{$name}"]; | |
1588 | $params['preserveDBName'] = TRUE; | |
1589 | } | |
1590 | } | |
1591 | ||
a7488080 | 1592 | if (!empty($params['source'])) { |
6a488035 TO |
1593 | unset($params['source']); |
1594 | } | |
1595 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, | |
1596 | $this->_contactID, | |
1597 | NULL, NULL, | |
1598 | $ctype | |
1599 | ); | |
1600 | ||
5032bf07 | 1601 | // add all the additional payment params we need |
ef523266 EM |
1602 | if (!empty($this->_params["billing_state_province_id-{$this->_bltID}"])) { |
1603 | $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}"]); | |
1604 | } | |
1605 | if (!empty($this->_params["billing_country_id-{$this->_bltID}"])) { | |
1606 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
1607 | } | |
c1cc3e0c EM |
1608 | $legacyCreditCardExpiryCheck = FALSE; |
1609 | if ($this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_CREDIT_CARD && !isset($this->_paymentFields)) { | |
1610 | $legacyCreditCardExpiryCheck = TRUE; | |
1611 | } | |
1612 | if ($legacyCreditCardExpiryCheck || in_array('credit_card_exp_date', array_keys($this->_paymentFields))) { | |
6a488035 TO |
1613 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); |
1614 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
1615 | } | |
1616 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); | |
1617 | $this->_params['amount'] = $this->_params['total_amount']; | |
1618 | $this->_params['amount_level'] = 0; | |
e0a8df2b | 1619 | $this->_params['description'] = ts('Office Credit Card contribution'); |
6a488035 TO |
1620 | $this->_params['currencyID'] = CRM_Utils_Array::value('currency', |
1621 | $this->_params, | |
1622 | $config->defaultCurrency | |
1623 | ); | |
1624 | $this->_params['payment_action'] = 'Sale'; | |
a7488080 | 1625 | if (!empty($this->_params['receive_date'])) { |
6a488035 TO |
1626 | $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['receive_date'], $this->_params['receive_date_time']); |
1627 | } | |
1628 | ||
a7488080 | 1629 | if (!empty($params['soft_credit_to'])) { |
6a488035 TO |
1630 | $this->_params['soft_credit_to'] = $params['soft_credit_to']; |
1631 | $this->_params['pcp_made_through_id'] = $params['pcp_made_through_id']; | |
1632 | } | |
1633 | ||
1634 | $this->_params['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $params); | |
1635 | $this->_params['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $params); | |
1636 | $this->_params['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $params); | |
1637 | ||
1638 | //Add common data to formatted params | |
1639 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($params, $this->_params, $this); | |
1640 | ||
1641 | if (empty($this->_params['invoice_id'])) { | |
1642 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1643 | } | |
1644 | else { | |
1645 | $this->_params['invoiceID'] = $this->_params['invoice_id']; | |
1646 | } | |
1647 | ||
1648 | // at this point we've created a contact and stored its address etc | |
1649 | // all the payment processors expect the name and address to be in the | |
1650 | // so we copy stuff over to first_name etc. | |
1651 | $paymentParams = $this->_params; | |
1652 | $paymentParams['contactID'] = $this->_contactID; | |
1653 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); | |
1654 | ||
1655 | $contributionType = new CRM_Financial_DAO_FinancialType(); | |
1656 | $contributionType->id = $params['financial_type_id']; | |
6a488035 TO |
1657 | |
1658 | // add some financial type details to the params list | |
1659 | // if folks need to use it | |
1660 | $paymentParams['contributionType_name'] = $this->_params['contributionType_name'] = $contributionType->name; | |
1661 | $paymentParams['contributionPageID'] = NULL; | |
c1cc3e0c | 1662 | |
a7488080 | 1663 | if (!empty($this->_params['is_email_receipt'])) { |
6a488035 TO |
1664 | $paymentParams['email'] = $this->userEmail; |
1665 | $paymentParams['is_email_receipt'] = 1; | |
1666 | } | |
1667 | else { | |
1668 | $paymentParams['is_email_receipt'] = 0; | |
1669 | $this->_params['is_email_receipt'] = 0; | |
1670 | } | |
a7488080 | 1671 | if (!empty($this->_params['receive_date'])) { |
6a488035 TO |
1672 | $paymentParams['receive_date'] = $this->_params['receive_date']; |
1673 | } | |
6a488035 | 1674 | |
6a488035 TO |
1675 | // For recurring contribution, create Contribution Record first. |
1676 | // Contribution ID, Recurring ID and Contact ID needed | |
1677 | // When we get a callback from the payment processor, CRM-7115 | |
c1cc3e0c | 1678 | |
a7488080 | 1679 | if (!empty($paymentParams['is_recur'])) { |
6a488035 TO |
1680 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($this, |
1681 | $this->_params, | |
5901ddf9 | 1682 | NULL, |
6a488035 TO |
1683 | $this->_contactID, |
1684 | $contributionType, | |
6a488035 | 1685 | TRUE, |
4bd318e0 EM |
1686 | FALSE, |
1687 | $isTest, | |
1688 | $this->_lineItem | |
6a488035 TO |
1689 | ); |
1690 | $paymentParams['contributionID'] = $contribution->id; | |
1691 | $paymentParams['contributionTypeID'] = $contribution->financial_type_id; | |
1692 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; | |
1693 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; | |
1694 | } | |
c1cc3e0c | 1695 | $result = array(); |
6a488035 | 1696 | if ($paymentParams['amount'] > 0.0) { |
d77a0a58 | 1697 | // force a re-get of the payment processor in case the form changed it, CRM-7179 |
6a488035 | 1698 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this, TRUE); |
c1cc3e0c EM |
1699 | try { |
1700 | $result = $payment->doPayment($paymentParams, 'contribute'); | |
1701 | } | |
1702 | catch (CRM_Core_Exception $e) { | |
1703 | $message = ts("Payment Processor Error message") . $e->getMessage(); | |
1704 | $this->cleanupDBAfterPaymentFailure($paymentParams, $message); | |
1705 | //set the contribution mode. | |
1706 | $urlParams = "action=add&cid={$this->_contactID}"; | |
1707 | if ($this->_mode) { | |
1708 | $urlParams .= "&mode={$this->_mode}"; | |
1709 | } | |
1710 | if (!empty($this->_ppID)) { | |
1711 | $urlParams .= "&context=pledge&ppid={$this->_ppID}"; | |
1712 | } | |
1713 | CRM_Core_Error::statusBounce($message, $urlParams, ts('Payment Processor Error')); | |
6a488035 | 1714 | } |
6a488035 TO |
1715 | } |
1716 | ||
c1cc3e0c | 1717 | $this->_params = array_merge($this->_params, $result); |
6a488035 TO |
1718 | |
1719 | $this->_params['receive_date'] = $now; | |
1720 | ||
a7488080 | 1721 | if (!empty($this->_params['is_email_receipt'])) { |
6a488035 TO |
1722 | $this->_params['receipt_date'] = $now; |
1723 | } | |
1724 | else { | |
1725 | $this->_params['receipt_date'] = CRM_Utils_Date::processDate($this->_params['receipt_date'], | |
1726 | $params['receipt_date_time'], TRUE | |
1727 | ); | |
1728 | } | |
1729 | ||
1730 | $this->set('params', $this->_params); | |
1731 | $this->assign('trxn_id', $result['trxn_id']); | |
1732 | $this->assign('receive_date', $this->_params['receive_date']); | |
1733 | ||
1734 | // result has all the stuff we need | |
1735 | // lets archive it to a financial transaction | |
1736 | if ($contributionType->is_deductible) { | |
1737 | $this->assign('is_deductible', TRUE); | |
1738 | $this->set('is_deductible', TRUE); | |
1739 | } | |
1740 | ||
1741 | // set source if not set | |
1742 | if (empty($this->_params['source'])) { | |
1743 | $userID = $session->get('userID'); | |
1744 | $userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID, | |
1745 | 'sort_name' | |
1746 | ); | |
1747 | $this->_params['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName)); | |
1748 | } | |
1749 | ||
1750 | // build custom data getFields array | |
1751 | $customFieldsContributionType = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, | |
1752 | CRM_Utils_Array::value('financial_type_id', $params) | |
1753 | ); | |
1754 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsContributionType, | |
1755 | CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, TRUE) | |
1756 | ); | |
1757 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
1758 | $customFields, | |
1759 | $this->_id, | |
1760 | 'Contribution' | |
1761 | ); | |
a7488080 | 1762 | if (empty($paymentParams['is_recur'])) { |
6a488035 TO |
1763 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($this, |
1764 | $this->_params, | |
1765 | $result, | |
1766 | $this->_contactID, | |
1767 | $contributionType, | |
4bd318e0 EM |
1768 | FALSE, FALSE, |
1769 | $isTest, | |
1770 | $this->_lineItem | |
6a488035 TO |
1771 | ); |
1772 | } | |
1773 | ||
1774 | //send receipt mail. | |
8cc574cf | 1775 | if ($contribution->id && !empty($this->_params['is_email_receipt'])) { |
6a488035 TO |
1776 | $this->_params['trxn_id'] = CRM_Utils_Array::value('trxn_id', $result); |
1777 | $this->_params['contact_id'] = $this->_contactID; | |
1778 | $this->_params['contribution_id'] = $contribution->id; | |
1779 | $sendReceipt = CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $this->_params, TRUE); | |
1780 | } | |
1781 | ||
1782 | //process the note | |
1783 | if ($contribution->id && isset($params['note'])) { | |
1784 | CRM_Contribute_Form_AdditionalInfo::processNote($params, $contactID, $contribution->id, NULL); | |
1785 | } | |
1786 | //process premium | |
1787 | if ($contribution->id && isset($params['product_name'][0])) { | |
1788 | CRM_Contribute_Form_AdditionalInfo::processPremium($params, $contribution->id, NULL, $this->_options); | |
1789 | } | |
1790 | ||
1791 | //update pledge payment status. | |
1792 | if ($this->_ppID && $contribution->id) { | |
1793 | //store contribution id in payment record. | |
1794 | CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id); | |
1795 | ||
1796 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID, | |
1797 | array($this->_ppID), | |
1798 | $contribution->contribution_status_id, | |
1799 | NULL, | |
1800 | $contribution->total_amount | |
1801 | ); | |
1802 | } | |
1803 | ||
1804 | if ($contribution->id) { | |
1805 | $statusMsg = ts('The contribution record has been processed.'); | |
a7488080 | 1806 | if (!empty($this->_params['is_email_receipt']) && $sendReceipt) { |
6a488035 TO |
1807 | $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.'); |
1808 | } | |
1809 | CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success'); | |
1810 | } | |
1811 | } | |
c1cc3e0c EM |
1812 | |
1813 | /** | |
100fef9d | 1814 | * @param array $paymentParams |
7a9ab499 | 1815 | * @param $message |
c1cc3e0c EM |
1816 | */ |
1817 | public function cleanupDBAfterPaymentFailure($paymentParams, $message) { | |
1818 | //make sure to cleanup db for recurring case. | |
1819 | if (!empty($paymentParams['contributionID'])) { | |
1820 | CRM_Core_Error::debug_log_message($message . | |
1821 | "contact id={$this->_contactID} (deleting contribution {$paymentParams['contributionID']}"); | |
1822 | CRM_Contribute_BAO_Contribution::deleteContribution($paymentParams['contributionID']); | |
1823 | } | |
1824 | if (!empty($paymentParams['contributionRecurID'])) { | |
1825 | CRM_Core_Error::debug_log_message($message . | |
1826 | "contact id={$this->_contactID} (deleting recurring contribution {$paymentParams['contributionRecurID']}"); | |
1827 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']); | |
1828 | } | |
1829 | } | |
96025800 | 1830 | |
6a488035 | 1831 | } |