Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | | | | |
74ab7ba8 | 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
6a488035 TO |
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 | 27 | |
7758bd2b EM |
28 | use Civi\Payment\Exception\PaymentProcessorException; |
29 | ||
6a488035 | 30 | /** |
22ab45ae | 31 | * This class generates form components for processing a contribution. |
6a488035 TO |
32 | */ |
33 | class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditPayment { | |
6a488035 | 34 | /** |
fe482240 | 35 | * The id of the contribution that we are processing. |
6a488035 TO |
36 | * |
37 | * @var int | |
6a488035 TO |
38 | */ |
39 | public $_id; | |
40 | ||
41 | /** | |
fe482240 | 42 | * The id of the premium that we are processing. |
6a488035 TO |
43 | * |
44 | * @var int | |
6a488035 TO |
45 | */ |
46 | public $_premiumID = NULL; | |
4691b077 EM |
47 | |
48 | /** | |
49 | * @var CRM_Contribute_DAO_ContributionProduct | |
50 | */ | |
6a488035 TO |
51 | public $_productDAO = NULL; |
52 | ||
53 | /** | |
fe482240 | 54 | * The id of the note. |
6a488035 TO |
55 | * |
56 | * @var int | |
6a488035 TO |
57 | */ |
58 | public $_noteID; | |
59 | ||
60 | /** | |
fe482240 | 61 | * The id of the contact associated with this contribution. |
6a488035 TO |
62 | * |
63 | * @var int | |
6a488035 TO |
64 | */ |
65 | public $_contactID; | |
66 | ||
67 | /** | |
fe482240 | 68 | * The id of the pledge payment that we are processing. |
6a488035 TO |
69 | * |
70 | * @var int | |
6a488035 TO |
71 | */ |
72 | public $_ppID; | |
73 | ||
6a488035 | 74 | /** |
fe482240 | 75 | * Is this contribution associated with an online. |
6a488035 TO |
76 | * financial transaction |
77 | * | |
78 | * @var boolean | |
6a488035 TO |
79 | */ |
80 | public $_online = FALSE; | |
81 | ||
82 | /** | |
bf56a083 | 83 | * Stores all product options. |
6a488035 TO |
84 | * |
85 | * @var array | |
6a488035 TO |
86 | */ |
87 | public $_options; | |
88 | ||
4691b077 EM |
89 | /** |
90 | * Storage of parameters from form | |
91 | * | |
92 | * @var array | |
4691b077 EM |
93 | */ |
94 | public $_params; | |
95 | ||
6a488035 TO |
96 | /** |
97 | * Store the contribution Type ID | |
98 | * | |
99 | * @var array | |
100 | */ | |
101 | public $_contributionType; | |
102 | ||
103 | /** | |
104 | * The contribution values if an existing contribution | |
105 | */ | |
106 | public $_values; | |
107 | ||
108 | /** | |
109 | * The pledge values if this contribution is associated with pledge | |
110 | */ | |
111 | public $_pledgeValues; | |
112 | ||
113 | public $_contributeMode = 'direct'; | |
114 | ||
115 | public $_context; | |
116 | ||
5032bf07 | 117 | /** |
fe482240 | 118 | * Parameter with confusing name. |
5032bf07 EM |
119 | * @todo what is it? |
120 | * @var string | |
121 | */ | |
122 | public $_compContext; | |
123 | ||
6a488035 TO |
124 | public $_compId; |
125 | ||
5032bf07 EM |
126 | /** |
127 | * Possible From email addresses | |
128 | * @var array | |
129 | */ | |
130 | public $_fromEmails; | |
d77a0a58 EM |
131 | |
132 | /** | |
133 | * ID of from email | |
134 | * @var integer | |
135 | */ | |
136 | public $fromEmailId; | |
d424ffde CW |
137 | |
138 | /** | |
6a488035 TO |
139 | * Store the line items if price set used. |
140 | */ | |
141 | public $_lineItems; | |
142 | ||
d77a0a58 | 143 | /** |
100fef9d | 144 | * Line item |
d77a0a58 EM |
145 | * @todo explain why we use lineItem & lineItems |
146 | * @var array | |
147 | */ | |
148 | public $_lineItem; | |
149 | ||
17db9f82 | 150 | /** |
efe42cdc | 151 | * @var array soft credit info |
17db9f82 KJ |
152 | */ |
153 | public $_softCreditInfo; | |
154 | ||
6a488035 | 155 | protected $_formType; |
efe42cdc | 156 | |
459091e1 | 157 | /** |
100fef9d | 158 | * Array of the payment fields to be displayed in the payment fieldset (pane) in billingBlock.tpl |
459091e1 EM |
159 | * this contains all the information to describe these fields from quickform. See CRM_Core_Form_Payment getPaymentFormFieldsMetadata |
160 | * | |
161 | * @var array | |
162 | */ | |
353ffa53 | 163 | public $_paymentFields = array(); |
4691b077 | 164 | /** |
28a4070b | 165 | * Logged in user's email. |
4691b077 EM |
166 | * @var string |
167 | */ | |
168 | public $userEmail; | |
169 | ||
170 | /** | |
171 | * Price set ID | |
172 | * @var integer | |
173 | */ | |
174 | public $_priceSetId; | |
175 | ||
4691b077 EM |
176 | /** |
177 | * Price set as an array | |
178 | * @var array | |
179 | */ | |
180 | public $_priceSet; | |
181 | ||
d77a0a58 EM |
182 | /** |
183 | * User display name | |
184 | * | |
185 | * @var string | |
186 | */ | |
187 | public $userDisplayName; | |
353ffa53 | 188 | |
7e5961cf EM |
189 | /** |
190 | * Status message to be shown to the user. | |
191 | * | |
3e6a1f4a | 192 | * @var array |
7e5961cf | 193 | */ |
3e6a1f4a | 194 | protected $statusMessage = array(); |
7e5961cf EM |
195 | |
196 | /** | |
197 | * Status message title to be shown to the user. | |
198 | * | |
199 | * Generally the payment processor message title is 'Complete' and offline is 'Saved' | |
200 | * although this might not be a good fit with the broad range of processors. | |
201 | * | |
202 | * @var string | |
203 | */ | |
204 | protected $statusMessageTitle; | |
205 | ||
b25212e8 PN |
206 | /** |
207 | * Explicitly declare the form context. | |
208 | */ | |
209 | public function getDefaultContext() { | |
210 | return 'create'; | |
211 | } | |
212 | ||
6a488035 | 213 | /** |
22ab45ae | 214 | * Set variables up before form is built. |
6a488035 TO |
215 | */ |
216 | public function preProcess() { | |
28a4070b | 217 | // Check permission for action. |
6a488035 | 218 | if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { |
8f262a0b | 219 | // @todo replace with throw new CRM_Core_Exception(). |
0499b0ad | 220 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 | 221 | } |
6a488035 | 222 | |
42e8b05c EM |
223 | parent::preProcess(); |
224 | ||
6a488035 TO |
225 | $this->_formType = CRM_Utils_Array::value('formType', $_GET); |
226 | ||
28a4070b | 227 | // Get price set id. |
6a488035 TO |
228 | $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET); |
229 | $this->set('priceSetId', $this->_priceSetId); | |
230 | $this->assign('priceSetId', $this->_priceSetId); | |
231 | ||
28a4070b | 232 | // Get the pledge payment id |
6a488035 TO |
233 | $this->_ppID = CRM_Utils_Request::retrieve('ppid', 'Positive', $this); |
234 | ||
6a488035 TO |
235 | $this->assign('action', $this->_action); |
236 | ||
28a4070b | 237 | // Get the contribution id if update |
6a488035 TO |
238 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
239 | if (!empty($this->_id)) { | |
240 | $this->assign('contribID', $this->_id); | |
241 | } | |
242 | ||
243 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
244 | $this->assign('context', $this->_context); | |
245 | ||
246 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Positive', $this); | |
247 | ||
248 | $this->_compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this); | |
249 | ||
250 | //set the contribution mode. | |
251 | $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); | |
252 | ||
253 | $this->assign('contributionMode', $this->_mode); | |
459091e1 EM |
254 | if ($this->_action & CRM_Core_Action::DELETE) { |
255 | return; | |
256 | } | |
6a488035 | 257 | |
6a488035 | 258 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); |
6a488035 | 259 | |
d77a0a58 | 260 | if (in_array('CiviPledge', CRM_Core_Config::singleton()->enableComponents) && !$this->_formType) { |
6a488035 TO |
261 | $this->preProcessPledge(); |
262 | } | |
263 | ||
d5397f2f PJ |
264 | if ($this->_id) { |
265 | $this->showRecordLinkMesssage($this->_id); | |
266 | } | |
6a488035 TO |
267 | $this->_values = array(); |
268 | ||
bf56a083 | 269 | // Current contribution id. |
6a488035 TO |
270 | if ($this->_id) { |
271 | $this->assignPremiumProduct($this->_id); | |
272 | $this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values); | |
273 | } | |
274 | ||
275 | // when custom data is included in this page | |
a7488080 | 276 | if (!empty($_POST['hidden_custom'])) { |
6a488035 TO |
277 | $this->applyCustomData('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id); |
278 | } | |
279 | ||
280 | $this->_lineItems = array(); | |
281 | if ($this->_id) { | |
282 | if (!empty($this->_compId) && $this->_compContext == 'participant') { | |
283 | $this->assign('compId', $this->_compId); | |
284 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_compId); | |
285 | } | |
286 | else { | |
0b71718c | 287 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution', 1, TRUE, TRUE); |
6a488035 | 288 | } |
f1662cbd | 289 | // wtf? |
6a488035 TO |
290 | empty($lineItem) ? NULL : $this->_lineItems[] = $lineItem; |
291 | } | |
292 | ||
9308f1c2 | 293 | $this->assign('lineItem', empty($lineItem) ? FALSE : array($lineItem)); |
6a488035 TO |
294 | |
295 | // Set title | |
29cb4724 | 296 | if ($this->_mode && $this->_id) { |
17d83f50 | 297 | $this->_payNow = TRUE; |
298 | $this->assign('payNow', $this->_payNow); | |
b9a90a9c | 299 | CRM_Utils_System::setTitle(ts('Pay with Credit Card')); |
29cb4724 | 300 | } |
301 | elseif ($this->_mode) { | |
e2046b33 CW |
302 | $this->setPageTitle($this->_ppID ? ts('Credit Card Pledge Payment') : ts('Credit Card Contribution')); |
303 | } | |
304 | else { | |
305 | $this->setPageTitle($this->_ppID ? ts('Pledge Payment') : ts('Contribution')); | |
6a488035 | 306 | } |
6a488035 TO |
307 | } |
308 | ||
28a4070b EM |
309 | /** |
310 | * Set default values. | |
311 | * | |
312 | * @return array | |
313 | */ | |
00be9182 | 314 | public function setDefaultValues() { |
6a488035 TO |
315 | $defaults = $this->_values; |
316 | ||
bf56a083 | 317 | // Set defaults for pledge payment. |
6a488035 TO |
318 | if ($this->_ppID) { |
319 | $defaults['total_amount'] = CRM_Utils_Array::value('scheduled_amount', $this->_pledgeValues['pledgePayment']); | |
6a488035 TO |
320 | $defaults['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $this->_pledgeValues); |
321 | $defaults['currency'] = CRM_Utils_Array::value('currency', $this->_pledgeValues); | |
322 | $defaults['option_type'] = 1; | |
323 | } | |
324 | ||
6a488035 TO |
325 | if ($this->_action & CRM_Core_Action::DELETE) { |
326 | return $defaults; | |
327 | } | |
328 | ||
7fe22c48 EM |
329 | $defaults['frequency_interval'] = 1; |
330 | $defaults['frequency_unit'] = 'month'; | |
331 | ||
bf56a083 | 332 | // Set soft credit defaults. |
17db9f82 | 333 | CRM_Contribute_Form_SoftCredit::setDefaultValues($defaults, $this); |
dfbad3f7 | 334 | |
6a488035 | 335 | if ($this->_mode) { |
99cdd94d | 336 | // @todo - remove this function as the parent does it too. |
6a488035 | 337 | $config = CRM_Core_Config::singleton(); |
bf56a083 | 338 | // Set default country from config if no country set. |
a7488080 | 339 | if (empty($defaults["billing_country_id-{$this->_bltID}"])) { |
6a488035 TO |
340 | $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
341 | } | |
5a230af7 | 342 | |
a7488080 | 343 | if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { |
6a488035 TO |
344 | $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; |
345 | } | |
346 | ||
cc8ff73d DG |
347 | $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); |
348 | $defaults = array_merge($defaults, $billingDefaults); | |
6a488035 TO |
349 | } |
350 | ||
351 | if ($this->_id) { | |
352 | $this->_contactID = $defaults['contact_id']; | |
353 | } | |
5a230af7 | 354 | |
bf56a083 | 355 | // Set $newCredit variable in template to control whether link to credit card mode is included. |
9be1374d | 356 | $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()); |
5a230af7 | 357 | |
bf56a083 | 358 | // Fix the display of the monetary value, CRM-4038. |
6a488035 | 359 | if (isset($defaults['total_amount'])) { |
049db839 | 360 | if (!empty($defaults['tax_amount'])) { |
b6f65362 | 361 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); |
362 | if (!(CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails))) { | |
363 | $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'] - $defaults['tax_amount'], NULL, '%a'); | |
364 | } | |
049db839 | 365 | } |
366 | else { | |
367 | $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'], NULL, '%a'); | |
368 | } | |
6a488035 TO |
369 | } |
370 | ||
3ac2ffb6 | 371 | $amountFields = array('non_deductible_amount', 'fee_amount', 'net_amount'); |
372 | foreach ($amountFields as $amt) { | |
373 | if (isset($defaults[$amt])) { | |
374 | $defaults[$amt] = CRM_Utils_Money::format($defaults[$amt], NULL, '%a'); | |
375 | } | |
6a488035 TO |
376 | } |
377 | ||
6a488035 TO |
378 | if ($this->_contributionType) { |
379 | $defaults['financial_type_id'] = $this->_contributionType; | |
380 | } | |
133e2c99 | 381 | |
a7488080 | 382 | if (empty($defaults['payment_instrument_id'])) { |
794d4fc0 | 383 | $defaults['payment_instrument_id'] = $this->getDefaultPaymentInstrumentId(); |
d96cf288 | 384 | } |
6a488035 | 385 | |
a7488080 | 386 | if (!empty($defaults['is_test'])) { |
6a488035 TO |
387 | $this->assign('is_test', TRUE); |
388 | } | |
389 | ||
6a488035 | 390 | $this->assign('showOption', TRUE); |
bf56a083 | 391 | // For Premium section. |
6a488035 TO |
392 | if ($this->_premiumID) { |
393 | $this->assign('showOption', FALSE); | |
394 | $options = isset($this->_options[$this->_productDAO->product_id]) ? $this->_options[$this->_productDAO->product_id] : ""; | |
395 | if (!$options) { | |
396 | $this->assign('showOption', TRUE); | |
397 | } | |
398 | $options_key = CRM_Utils_Array::key($this->_productDAO->product_option, $options); | |
399 | if ($options_key) { | |
400 | $defaults['product_name'] = array($this->_productDAO->product_id, trim($options_key)); | |
401 | } | |
402 | else { | |
403 | $defaults['product_name'] = array($this->_productDAO->product_id); | |
404 | } | |
405 | if ($this->_productDAO->fulfilled_date) { | |
406 | list($defaults['fulfilled_date']) = CRM_Utils_Date::setDateDefaults($this->_productDAO->fulfilled_date); | |
407 | } | |
408 | } | |
409 | ||
410 | if (isset($this->userEmail)) { | |
411 | $this->assign('email', $this->userEmail); | |
412 | } | |
413 | ||
a7488080 | 414 | if (!empty($defaults['is_pay_later'])) { |
6a488035 TO |
415 | $this->assign('is_pay_later', TRUE); |
416 | } | |
417 | $this->assign('contribution_status_id', CRM_Utils_Array::value('contribution_status_id', $defaults)); | |
d682fc35 | 418 | if (!empty($defaults['contribution_status_id']) && in_array( |
419 | CRM_Contribute_PseudoConstant::contributionStatus($defaults['contribution_status_id'], 'name'), | |
420 | // Historically not 'Cancelled' hence not using CRM_Contribute_BAO_Contribution::isContributionStatusNegative. | |
421 | array('Refunded', 'Chargeback') | |
422 | )) { | |
99cdd94d | 423 | $defaults['refund_trxn_id'] = CRM_Core_BAO_FinancialTrxn::getRefundTransactionTrxnID($this->_id); |
424 | } | |
425 | else { | |
426 | $defaults['refund_trxn_id'] = isset($defaults['trxn_id']) ? $defaults['trxn_id'] : NULL; | |
427 | } | |
6a488035 | 428 | |
8cc574cf | 429 | if (!$this->_id && empty($defaults['receive_date'])) { |
b25212e8 | 430 | $defaults['receive_date'] = date('Y-m-d H:i:s'); |
6a488035 TO |
431 | } |
432 | ||
6a488035 TO |
433 | $currency = CRM_Utils_Array::value('currency', $defaults); |
434 | $this->assign('currency', $currency); | |
bf56a083 | 435 | // Hack to get currency info to the js layer. CRM-11440. |
6a488035 TO |
436 | CRM_Utils_Money::format(1); |
437 | $this->assign('currencySymbol', CRM_Utils_Array::value($currency, CRM_Utils_Money::$_currencySymbols)); | |
438 | $this->assign('totalAmount', CRM_Utils_Array::value('total_amount', $defaults)); | |
439 | ||
bf56a083 | 440 | // Inherit campaign from pledge. |
8cc574cf | 441 | if ($this->_ppID && !empty($this->_pledgeValues['campaign_id'])) { |
6a488035 TO |
442 | $defaults['campaign_id'] = $this->_pledgeValues['campaign_id']; |
443 | } | |
444 | ||
445 | $this->_defaults = $defaults; | |
446 | return $defaults; | |
447 | } | |
448 | ||
449 | /** | |
fe482240 | 450 | * Build the form object. |
6a488035 TO |
451 | */ |
452 | public function buildQuickForm() { | |
3db2d5ef JP |
453 | if ($this->_action & CRM_Core_Action::DELETE) { |
454 | $this->addButtons(array( | |
455 | array( | |
456 | 'type' => 'next', | |
457 | 'name' => ts('Delete'), | |
458 | 'spacing' => ' ', | |
459 | 'isDefault' => TRUE, | |
460 | ), | |
461 | array( | |
462 | 'type' => 'cancel', | |
463 | 'name' => ts('Cancel'), | |
464 | ), | |
465 | ) | |
466 | ); | |
467 | return; | |
468 | } | |
469 | ||
e3fa3fea | 470 | // FIXME: This probably needs to be done in preprocess |
40c655aa E |
471 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
472 | && $this->_action & CRM_Core_Action::UPDATE | |
66af7c48 PN |
473 | && CRM_Utils_Array::value('financial_type_id', $this->_values) |
474 | ) { | |
e3fa3fea | 475 | $financialTypeID = CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id']); |
59ccc8cd | 476 | CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($this->_id, 'edit'); |
e3fa3fea E |
477 | if (!CRM_Core_Permission::check('edit contributions of type ' . $financialTypeID)) { |
478 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); | |
479 | } | |
480 | } | |
5032bf07 | 481 | $allPanes = array(); |
b15c60e1 | 482 | |
6daab7fa | 483 | //tax rate from financialType |
a1e457ff | 484 | $this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates())); |
049db839 | 485 | $this->assign('currencies', json_encode(CRM_Core_OptionGroup::values('currencies_enabled'))); |
6a488035 TO |
486 | |
487 | // build price set form. | |
488 | $buildPriceSet = FALSE; | |
aaffa79f | 489 | $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); |
03b412ae | 490 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); |
95f2107c | 491 | $this->assign('invoicing', $invoicing); |
a32709be | 492 | |
b15c60e1 | 493 | $buildRecurBlock = FALSE; |
494 | ||
03b412ae PB |
495 | // display tax amount on edit contribution page |
496 | if ($invoicing && $this->_action & CRM_Core_Action::UPDATE && isset($this->_values['tax_amount'])) { | |
a32709be PB |
497 | $this->assign('totalTaxAmount', $this->_values['tax_amount']); |
498 | } | |
499 | ||
6a488035 | 500 | if (empty($this->_lineItems) && |
8cc574cf | 501 | ($this->_priceSetId || !empty($_POST['price_set_id'])) |
6a488035 TO |
502 | ) { |
503 | $buildPriceSet = TRUE; | |
504 | $getOnlyPriceSetElements = TRUE; | |
505 | if (!$this->_priceSetId) { | |
506 | $this->_priceSetId = $_POST['price_set_id']; | |
507 | $getOnlyPriceSetElements = FALSE; | |
508 | } | |
509 | ||
510 | $this->set('priceSetId', $this->_priceSetId); | |
9da8dc8c | 511 | CRM_Price_BAO_PriceSet::buildPriceSet($this); |
6a488035 TO |
512 | |
513 | // get only price set form elements. | |
514 | if ($getOnlyPriceSetElements) { | |
515 | return; | |
516 | } | |
517 | } | |
518 | // use to build form during form rule. | |
519 | $this->assign('buildPriceSet', $buildPriceSet); | |
520 | ||
6a488035 TO |
521 | $defaults = $this->_values; |
522 | $additionalDetailFields = array( | |
523 | 'note', | |
524 | 'thankyou_date', | |
525 | 'invoice_id', | |
526 | 'non_deductible_amount', | |
527 | 'fee_amount', | |
dfbad3f7 | 528 | 'net_amount', |
6a488035 TO |
529 | ); |
530 | foreach ($additionalDetailFields as $key) { | |
531 | if (!empty($defaults[$key])) { | |
532 | $defaults['hidden_AdditionalDetail'] = 1; | |
533 | break; | |
534 | } | |
535 | } | |
536 | ||
6a488035 TO |
537 | if ($this->_productDAO) { |
538 | if ($this->_productDAO->product_id) { | |
539 | $defaults['hidden_Premium'] = 1; | |
540 | } | |
541 | } | |
542 | ||
543 | if ($this->_noteID && | |
5888a52d | 544 | !CRM_Utils_System::isNull($this->_values['note']) |
6a488035 TO |
545 | ) { |
546 | $defaults['hidden_AdditionalDetail'] = 1; | |
547 | } | |
548 | ||
b9a90a9c | 549 | $paneNames = array(); |
550 | if (empty($this->_payNow)) { | |
551 | $paneNames[ts('Additional Details')] = 'AdditionalDetail'; | |
552 | } | |
6a488035 TO |
553 | |
554 | //Add Premium pane only if Premium is exists. | |
555 | $dao = new CRM_Contribute_DAO_Product(); | |
556 | $dao->is_active = 1; | |
557 | ||
b9a90a9c | 558 | if ($dao->find(TRUE) && empty($this->_payNow)) { |
6a488035 TO |
559 | $paneNames[ts('Premium Information')] = 'Premium'; |
560 | } | |
561 | ||
a576ecfa | 562 | $this->payment_instrument_id = CRM_Utils_Array::value('payment_instrument_id', $defaults, $this->getDefaultPaymentInstrumentId()); |
563 | if (CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE, $this->payment_instrument_id) == TRUE) { | |
794d4fc0 | 564 | if (!empty($this->_recurPaymentProcessors)) { |
565 | $buildRecurBlock = TRUE; | |
566 | if ($this->_ppID) { | |
567 | // ppID denotes a pledge payment. | |
568 | foreach ($this->_paymentProcessors as $processor) { | |
569 | if (!empty($processor['is_recur']) && !empty($processor['object']) && $processor['object']->supports('recurContributionsForPledges')) { | |
570 | $buildRecurBlock = TRUE; | |
571 | break; | |
4f3c361d | 572 | } |
794d4fc0 | 573 | $buildRecurBlock = FALSE; |
4f3c361d | 574 | } |
794d4fc0 | 575 | } |
576 | if ($buildRecurBlock) { | |
577 | CRM_Contribute_Form_Contribution_Main::buildRecur($this); | |
578 | $this->setDefaults(array('is_recur' => 0)); | |
579 | $this->assign('buildRecurBlock', TRUE); | |
5e27919e | 580 | } |
6a488035 TO |
581 | } |
582 | } | |
b15c60e1 | 583 | $this->addPaymentProcessorSelect(FALSE, $buildRecurBlock); |
6a488035 | 584 | |
6a488035 | 585 | foreach ($paneNames as $name => $type) { |
a5399a39 | 586 | $allPanes[$name] = $this->generatePane($type, $defaults); |
6a488035 | 587 | } |
5e27919e | 588 | |
6a488035 TO |
589 | $qfKey = $this->controller->_key; |
590 | $this->assign('qfKey', $qfKey); | |
591 | $this->assign('allPanes', $allPanes); | |
6a488035 | 592 | |
6c75c831 | 593 | $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this); |
594 | ||
6a488035 TO |
595 | if ($this->_formType) { |
596 | $this->assign('formType', $this->_formType); | |
597 | return; | |
598 | } | |
599 | ||
600 | $this->applyFilter('__ALL__', 'trim'); | |
5a230af7 | 601 | |
6a488035 TO |
602 | //need to assign custom data type and subtype to the template |
603 | $this->assign('customDataType', 'Contribution'); | |
604 | $this->assign('customDataSubType', $this->_contributionType); | |
605 | $this->assign('entityID', $this->_id); | |
606 | ||
607 | if ($this->_context == 'standalone') { | |
353ffa53 TO |
608 | $this->addEntityRef('contact_id', ts('Contact'), array( |
609 | 'create' => TRUE, | |
acb1052e | 610 | 'api' => array('extra' => array('email')), |
353ffa53 | 611 | ), TRUE); |
6a488035 TO |
612 | } |
613 | ||
614 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); | |
615 | ||
e3fa3fea | 616 | // Check permissions for financial type first |
573fd305 | 617 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $this->_action); |
b19a01c7 | 618 | if (empty($financialTypes)) { |
b55aa3de | 619 | CRM_Core_Error::statusBounce(ts('You do not have all the permissions needed for this page.')); |
b19a01c7 | 620 | } |
6a488035 TO |
621 | $financialType = $this->add('select', 'financial_type_id', |
622 | ts('Financial Type'), | |
e3fa3fea | 623 | array('' => ts('- select -')) + $financialTypes, |
79ca865b CW |
624 | TRUE, |
625 | array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );") | |
6a488035 | 626 | ); |
e3fa3fea | 627 | |
d77a0a58 | 628 | $paymentInstrument = FALSE; |
6a488035 | 629 | if (!$this->_mode) { |
6f26f78f | 630 | $checkPaymentID = array_search('Check', CRM_Contribute_PseudoConstant::paymentInstrument('name')); |
6a488035 | 631 | $paymentInstrument = $this->add('select', 'payment_instrument_id', |
4db803dd | 632 | ts('Payment Method'), |
6a488035 | 633 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), |
6f26f78f | 634 | TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','{$checkPaymentID}','checkNumber','table-row','select',false);") |
6a488035 TO |
635 | ); |
636 | } | |
637 | ||
ce22fda6 | 638 | $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') + $attributes['trxn_id']); |
6a488035 TO |
639 | |
640 | //add receipt for offline contribution | |
641 | $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?')); | |
642 | ||
643 | $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails); | |
644 | ||
7fe7b63d | 645 | $component = 'contribution'; |
646 | if ($this->_id) { | |
7fe7b63d | 647 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); |
648 | if (CRM_Utils_Array::value('membership', $componentDetails)) { | |
649 | $component = 'membership'; | |
f73acc78 | 650 | } |
7fe7b63d | 651 | elseif (CRM_Utils_Array::value('participant', $componentDetails)) { |
652 | $component = 'participant'; | |
6a488035 TO |
653 | } |
654 | } | |
2077cf67 | 655 | if ($this->_ppID) { |
7fe7b63d | 656 | $component = 'pledge'; |
657 | } | |
658 | $status = CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses($component, $this->_id); | |
b6545333 | 659 | |
d4849da4 | 660 | // define the status IDs that show the cancellation info, see CRM-17589 |
661 | $cancelInfo_show_ids = array(); | |
7fe7b63d | 662 | foreach (array_keys($status) as $status_id) { |
d682fc35 | 663 | if (CRM_Contribute_BAO_Contribution::isContributionStatusNegative($status_id)) { |
d4849da4 | 664 | $cancelInfo_show_ids[] = "'$status_id'"; |
665 | } | |
666 | } | |
667 | $this->assign('cancelInfo_show_ids', implode(',', $cancelInfo_show_ids)); | |
668 | ||
29cb4724 | 669 | $statusElement = $this->add('select', 'contribution_status_id', |
6a488035 TO |
670 | ts('Contribution Status'), |
671 | $status, | |
672 | FALSE | |
673 | ); | |
674 | ||
29cb4724 | 675 | $currencyFreeze = FALSE; |
b9a90a9c | 676 | if (!empty($this->_payNow) && ($this->_action & CRM_Core_Action::UPDATE)) { |
29cb4724 | 677 | $statusElement->freeze(); |
678 | $currencyFreeze = TRUE; | |
679 | $attributes['total_amount']['readonly'] = TRUE; | |
680 | } | |
681 | ||
46d173ef PN |
682 | // CRM-16189, add Revenue Recognition Date |
683 | if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) { | |
cb8ce66b PN |
684 | $revenueDate = $this->add('date', 'revenue_recognition_date', ts('Revenue Recognition Date'), CRM_Core_SelectValues::date(NULL, 'M Y', NULL, 5)); |
685 | if ($this->_id && !CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($this->_id)) { | |
686 | $revenueDate->freeze(); | |
687 | } | |
46d173ef PN |
688 | } |
689 | ||
6a488035 | 690 | // add various dates |
b25212e8 PN |
691 | $this->addField('receive_date', array('entity' => 'contribution'), FALSE, FALSE); |
692 | $this->addField('receipt_date', array('entity' => 'contribution'), FALSE, FALSE); | |
693 | $this->addField('cancel_date', array('entity' => 'contribution', 'label' => ts('Cancelled / Refunded Date')), FALSE, FALSE); | |
6a488035 TO |
694 | |
695 | if ($this->_online) { | |
696 | $this->assign('hideCalender', TRUE); | |
697 | } | |
6a488035 | 698 | |
6a488035 | 699 | $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']); |
e02d7e96 | 700 | |
6a488035 TO |
701 | $totalAmount = NULL; |
702 | if (empty($this->_lineItems)) { | |
703 | $buildPriceSet = FALSE; | |
9da8dc8c | 704 | $priceSets = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute'); |
6a488035 TO |
705 | if (!empty($priceSets) && !$this->_ppID) { |
706 | $buildPriceSet = TRUE; | |
707 | } | |
708 | ||
709 | // don't allow price set for contribution if it is related to participant, or if it is a pledge payment | |
710 | // and if we already have line items for that participant. CRM-5095 | |
711 | if ($buildPriceSet && $this->_id) { | |
712 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
713 | $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', | |
714 | $this->_id, | |
715 | 'id', | |
716 | 'contribution_id' | |
717 | ); | |
718 | if ($pledgePaymentId) { | |
719 | $buildPriceSet = FALSE; | |
720 | } | |
721 | if ($participantID = CRM_Utils_Array::value('participant', $componentDetails)) { | |
722 | $participantLI = CRM_Price_BAO_LineItem::getLineItems($participantID); | |
723 | if (!CRM_Utils_System::isNull($participantLI)) { | |
724 | $buildPriceSet = FALSE; | |
725 | } | |
726 | } | |
727 | } | |
728 | ||
729 | $hasPriceSets = FALSE; | |
730 | if ($buildPriceSet) { | |
731 | $hasPriceSets = TRUE; | |
6a3d8d2d WA |
732 | // CRM-16451: set financial type of 'Price Set' in back office contribution |
733 | // instead of selecting manually | |
734 | $financialTypeIds = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute', 'financial_type_id'); | |
6a488035 TO |
735 | $element = $this->add('select', 'price_set_id', ts('Choose price set'), |
736 | array( | |
21dfd5f5 | 737 | '' => ts('Choose price set'), |
6a488035 | 738 | ) + $priceSets, |
d6d7af07 | 739 | NULL, array('onchange' => "buildAmount( this.value, " . json_encode($financialTypeIds) . ");") |
5a230af7 | 740 | ); |
6a488035 TO |
741 | if ($this->_online && !($this->_action & CRM_Core_Action::UPDATE)) { |
742 | $element->freeze(); | |
743 | } | |
744 | } | |
745 | $this->assign('hasPriceSets', $hasPriceSets); | |
6a488035 | 746 | if (!($this->_action & CRM_Core_Action::UPDATE)) { |
d37ade2e | 747 | if ($this->_online || $this->_ppID) { |
748 | $attributes['total_amount'] = array_merge($attributes['total_amount'], array( | |
749 | 'READONLY' => TRUE, | |
21dfd5f5 | 750 | 'style' => "background-color:#EBECE4", |
d37ade2e | 751 | )); |
752 | $optionTypes = array( | |
753 | '1' => ts('Adjust Pledge Payment Schedule?'), | |
754 | '2' => ts('Adjust Total Pledge Amount?'), | |
755 | ); | |
756 | $this->addRadio('option_type', | |
757 | NULL, | |
758 | $optionTypes, | |
759 | array(), '<br/>' | |
760 | ); | |
761 | ||
762 | $currencyFreeze = TRUE; | |
763 | } | |
6a488035 TO |
764 | } |
765 | ||
766 | $totalAmount = $this->addMoney('total_amount', | |
767 | ts('Total Amount'), | |
768 | ($hasPriceSets) ? FALSE : TRUE, | |
769 | $attributes['total_amount'], | |
770 | TRUE, 'currency', NULL, $currencyFreeze | |
771 | ); | |
772 | } | |
773 | ||
774 | $this->add('text', 'source', ts('Source'), CRM_Utils_Array::value('source', $attributes)); | |
775 | ||
161a2fe7 | 776 | // CRM-7362 --add campaigns. |
6a488035 TO |
777 | CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values)); |
778 | ||
b9a90a9c | 779 | if (empty($this->_payNow)) { |
780 | CRM_Contribute_Form_SoftCredit::buildQuickForm($this); | |
781 | } | |
0baed70b | 782 | |
6a488035 TO |
783 | $js = NULL; |
784 | if (!$this->_mode) { | |
785 | $js = array('onclick' => "return verify( );"); | |
786 | } | |
787 | ||
aaffa79f | 788 | $mailingInfo = Civi::settings()->get('mailing_backend'); |
6a488035 TO |
789 | $this->assign('outBound_option', $mailingInfo['outBound_option']); |
790 | ||
791 | $this->addButtons(array( | |
792 | array( | |
793 | 'type' => 'upload', | |
794 | 'name' => ts('Save'), | |
795 | 'js' => $js, | |
21dfd5f5 | 796 | 'isDefault' => TRUE, |
6a488035 TO |
797 | ), |
798 | array( | |
799 | 'type' => 'upload', | |
800 | 'name' => ts('Save and New'), | |
801 | 'js' => $js, | |
21dfd5f5 | 802 | 'subName' => 'new', |
6a488035 TO |
803 | ), |
804 | array( | |
805 | 'type' => 'cancel', | |
fc9f8155 | 806 | 'name' => ts('Cancel'), |
6a488035 TO |
807 | ), |
808 | ) | |
809 | ); | |
810 | ||
811 | // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type, | |
812 | // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail | |
2077cf67 | 813 | if ($this->_id && ($this->_values['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Cancelled'))) { |
5a230af7 | 814 | if ($totalAmount) { |
6a488035 TO |
815 | $totalAmount->freeze(); |
816 | } | |
6a488035 TO |
817 | $paymentInstrument->freeze(); |
818 | $trxnId->freeze(); | |
819 | $financialType->freeze(); | |
820 | } | |
821 | ||
b6f65362 | 822 | // if contribution is related to membership or participant freeze Financial Type, Amount |
823 | if ($this->_id && isset($this->_values['tax_amount'])) { | |
824 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
825 | if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) { | |
b5527a9a | 826 | if ($totalAmount) { |
a13f3d8c | 827 | $totalAmount->freeze(); |
b5527a9a | 828 | } |
b6f65362 | 829 | $financialType->freeze(); |
b6f65362 | 830 | $this->assign('freezeFinancialType', TRUE); |
831 | } | |
832 | } | |
833 | ||
6a488035 TO |
834 | if ($this->_action & CRM_Core_Action::VIEW) { |
835 | $this->freeze(); | |
836 | } | |
837 | } | |
838 | ||
839 | /** | |
fe482240 | 840 | * Global form rule. |
6a488035 | 841 | * |
014c4014 TO |
842 | * @param array $fields |
843 | * The input form values. | |
844 | * @param array $files | |
845 | * The uploaded files if any. | |
2a6da8d7 EM |
846 | * @param $self |
847 | * | |
72b3a70c CW |
848 | * @return bool|array |
849 | * true if no errors, else array of errors | |
6a488035 | 850 | */ |
1cf3816f | 851 | public static function formRule($fields, $files, $self) { |
6a488035 | 852 | $errors = array(); |
161a2fe7 | 853 | // Check for Credit Card Contribution. |
6a488035 TO |
854 | if ($self->_mode) { |
855 | if (empty($fields['payment_processor_id'])) { | |
856 | $errors['payment_processor_id'] = ts('Payment Processor is a required field.'); | |
857 | } | |
34089500 | 858 | else { |
859 | // validate payment instrument (e.g. credit card number) | |
1d1fee72 | 860 | CRM_Core_Payment_Form::validatePaymentInstrument($fields['payment_processor_id'], $fields, $errors, NULL); |
34089500 | 861 | } |
6a488035 TO |
862 | } |
863 | ||
161a2fe7 | 864 | // Do the amount validations. |
a7488080 | 865 | if (empty($fields['total_amount']) && empty($self->_lineItems)) { |
6a488035 | 866 | if ($priceSetId = CRM_Utils_Array::value('price_set_id', $fields)) { |
9da8dc8c | 867 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $fields, $errors); |
6a488035 TO |
868 | } |
869 | } | |
870 | ||
b326919b | 871 | $softErrors = CRM_Contribute_Form_SoftCredit::formRule($fields, $errors, $self); |
d37ade2e | 872 | |
3256b416 JM |
873 | if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) || !empty($fields['fee_amount']))) { |
874 | $sum = CRM_Utils_Rule::cleanMoney($fields['net_amount']) + CRM_Utils_Rule::cleanMoney($fields['fee_amount']); | |
875 | if (CRM_Utils_Rule::cleanMoney($fields['total_amount']) != $sum) { | |
6a488035 TO |
876 | $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount'); |
877 | } | |
878 | } | |
577d1236 WA |
879 | |
880 | //CRM-16285 - Function to handle validation errors on form, for recurring contribution field. | |
881 | CRM_Contribute_BAO_ContributionRecur::validateRecurContribution($fields, $files, $self, $errors); | |
882 | ||
161a2fe7 | 883 | // Form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow |
58d33659 PN |
884 | if (($self->_action & CRM_Core_Action::UPDATE) |
885 | && $self->_id | |
886 | && $self->_values['contribution_status_id'] != $fields['contribution_status_id'] | |
887 | ) { | |
6a488035 TO |
888 | CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors); |
889 | } | |
4d47ad17 | 890 | // CRM-16015, add form-rule to restrict change of financial type if using price field of different financial type |
58d33659 PN |
891 | if (($self->_action & CRM_Core_Action::UPDATE) |
892 | && $self->_id | |
893 | && $self->_values['financial_type_id'] != $fields['financial_type_id'] | |
894 | ) { | |
4d47ad17 PN |
895 | CRM_Contribute_BAO_Contribution::checkFinancialTypeChange(NULL, $self->_id, $errors); |
896 | } | |
6a488035 | 897 | //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow |
34089500 | 898 | if (!empty($fields['fee_amount']) && !empty($fields['financial_type_id']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($fields['financial_type_id'])) { |
51fa20cb | 899 | $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType; |
6a488035 | 900 | } |
133e2c99 | 901 | |
7272e4fa DG |
902 | // $trxn_id must be unique CRM-13919 |
903 | if (!empty($fields['trxn_id'])) { | |
904 | $queryParams = array(1 => array($fields['trxn_id'], 'String')); | |
905 | $query = 'select count(*) from civicrm_contribution where trxn_id = %1'; | |
906 | if ($self->_id) { | |
353ffa53 | 907 | $queryParams[2] = array((int) $self->_id, 'Integer'); |
7272e4fa DG |
908 | $query .= ' and id !=%2'; |
909 | } | |
910 | $tCnt = CRM_Core_DAO::singleValueQuery($query, $queryParams); | |
911 | if ($tCnt) { | |
912 | $errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id'])); | |
133e2c99 | 913 | } |
7272e4fa | 914 | } |
46d173ef PN |
915 | if (!empty($fields['revenue_recognition_date']) |
916 | && count(array_filter($fields['revenue_recognition_date'])) == 1 | |
917 | ) { | |
918 | $errors['revenue_recognition_date'] = ts('Month and Year are required field for Revenue Recognition.'); | |
919 | } | |
3adac159 | 920 | // CRM-16189 |
96039749 PN |
921 | try { |
922 | CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($fields, $self->_id, $self->_priceSet['fields']); | |
923 | } | |
924 | catch (CRM_Core_Exception $e) { | |
c9b68130 | 925 | $errors['financial_type_id'] = ' '; |
96039749 | 926 | $errors['_qf_default'] = $e->getMessage(); |
3adac159 | 927 | } |
ac0c89ed | 928 | $errors = array_merge($errors, $softErrors); |
6a488035 TO |
929 | return $errors; |
930 | } | |
931 | ||
932 | /** | |
fe482240 | 933 | * Process the form submission. |
6a488035 TO |
934 | */ |
935 | public function postProcess() { | |
6a488035 TO |
936 | if ($this->_action & CRM_Core_Action::DELETE) { |
937 | CRM_Contribute_BAO_Contribution::deleteContribution($this->_id); | |
c1cc3e0c | 938 | CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', |
6a488035 TO |
939 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" |
940 | )); | |
941 | return; | |
942 | } | |
77c081c2 | 943 | // Get the submitted form values. |
6a488035 | 944 | $submittedValues = $this->controller->exportValues($this->_name); |
7758bd2b EM |
945 | |
946 | try { | |
7758bd2b EM |
947 | $contribution = $this->submit($submittedValues, $this->_action, $this->_ppID); |
948 | } | |
949 | catch (PaymentProcessorException $e) { | |
950 | // Set the contribution mode. | |
951 | $urlParams = "action=add&cid={$this->_contactID}"; | |
952 | if ($this->_mode) { | |
953 | $urlParams .= "&mode={$this->_mode}"; | |
954 | } | |
955 | if (!empty($this->_ppID)) { | |
956 | $urlParams .= "&context=pledge&ppid={$this->_ppID}"; | |
957 | } | |
958 | ||
959 | CRM_Core_Error::statusBounce($e->getMessage(), $urlParams, ts('Payment Processor Error')); | |
960 | } | |
f75f0921 EM |
961 | $session = CRM_Core_Session::singleton(); |
962 | $buttonName = $this->controller->getButtonName(); | |
963 | if ($this->_context == 'standalone') { | |
964 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
965 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contribute/add', | |
966 | 'reset=1&action=add&context=standalone' | |
967 | )); | |
6a488035 | 968 | } |
f75f0921 EM |
969 | else { |
970 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
971 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" | |
972 | )); | |
973 | } | |
974 | } | |
975 | elseif ($this->_context == 'contribution' && $this->_mode && $buttonName == $this->getButtonName('upload', 'new')) { | |
976 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', | |
977 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}" | |
978 | )); | |
979 | } | |
980 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
981 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', | |
982 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}" | |
983 | )); | |
6a488035 TO |
984 | } |
985 | ||
f75f0921 EM |
986 | //store contribution ID if not yet set (on create) |
987 | if (empty($this->_id) && !empty($contribution->id)) { | |
988 | $this->_id = $contribution->id; | |
6a488035 | 989 | } |
94c8aed1 | 990 | if (!empty($this->_id) && CRM_Core_Permission::access('CiviMember')) { |
5a53dd1f E |
991 | $membershipPaymentCount = civicrm_api3('MembershipPayment', 'getCount', array('contribution_id' => $this->_id)); |
992 | if ($membershipPaymentCount) { | |
94c8aed1 E |
993 | $this->ajaxResponse['updateTabs']['#tab_member'] = CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactID); |
994 | } | |
995 | } | |
5a53dd1f E |
996 | if (!empty($this->_id) && CRM_Core_Permission::access('CiviEvent')) { |
997 | $participantPaymentCount = civicrm_api3('ParticipantPayment', 'getCount', array('contribution_id' => $this->_id)); | |
998 | if ($participantPaymentCount) { | |
999 | $this->ajaxResponse['updateTabs']['#tab_participant'] = CRM_Contact_BAO_Contact::getCountComponent('participant', $this->_contactID); | |
1000 | } | |
1001 | } | |
f75f0921 | 1002 | } |
6a488035 | 1003 | |
f75f0921 EM |
1004 | /** |
1005 | * Process credit card payment. | |
1006 | * | |
1007 | * @param array $submittedValues | |
1008 | * @param array $lineItem | |
456b0145 | 1009 | * |
7126b55c EM |
1010 | * @param int $contactID |
1011 | * Contact ID | |
1012 | * | |
456b0145 | 1013 | * @return bool|\CRM_Contribute_DAO_Contribution |
f8453bef | 1014 | * @throws \CRM_Core_Exception |
7758bd2b | 1015 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
456b0145 | 1016 | */ |
7126b55c | 1017 | protected function processCreditCard($submittedValues, $lineItem, $contactID) { |
549cf0b9 | 1018 | $isTest = ($this->_mode == 'test') ? 1 : 0; |
f75f0921 | 1019 | // CRM-12680 set $_lineItem if its not set |
bf4253e7 EM |
1020 | // @todo - I don't believe this would ever BE set. I can't find anywhere in the code. |
1021 | // It would be better to pass line item out to functions than $this->_lineItem as | |
1022 | // we don't know what is being changed where. | |
f75f0921 EM |
1023 | if (empty($this->_lineItem) && !empty($lineItem)) { |
1024 | $this->_lineItem = $lineItem; | |
6a488035 | 1025 | } |
6a488035 | 1026 | |
bf4253e7 EM |
1027 | $this->_paymentObject = Civi\Payment\System::singleton()->getById($submittedValues['payment_processor_id']); |
1028 | $this->_paymentProcessor = $this->_paymentObject->getPaymentProcessor(); | |
c52884b2 | 1029 | |
7758bd2b EM |
1030 | // Set source if not set |
1031 | if (empty($submittedValues['source'])) { | |
1032 | $userID = CRM_Core_Session::singleton()->get('userID'); | |
1033 | $userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID, | |
1034 | 'sort_name' | |
1035 | ); | |
1036 | $submittedValues['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName)); | |
1037 | } | |
1038 | ||
3b8e6c3f | 1039 | $params = $submittedValues; |
1040 | $this->_params = array_merge($this->_params, $submittedValues); | |
77e18ec7 | 1041 | |
bf4253e7 EM |
1042 | // Mapping requiring documentation. |
1043 | $this->_params['payment_processor'] = $submittedValues['payment_processor_id']; | |
1044 | ||
f75f0921 | 1045 | $now = date('YmdHis'); |
5a230af7 | 1046 | |
f75f0921 EM |
1047 | // we need to retrieve email address |
1048 | if ($this->_context == 'standalone' && !empty($submittedValues['is_email_receipt'])) { | |
1049 | list($this->userDisplayName, | |
1050 | $this->userEmail | |
7126b55c | 1051 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID); |
f75f0921 | 1052 | $this->assign('displayName', $this->userDisplayName); |
6a488035 | 1053 | } |
6a488035 | 1054 | |
3b8e6c3f | 1055 | $this->_contributorEmail = $this->userEmail; |
1056 | $this->_contributorContactID = $contactID; | |
1057 | $this->processBillingAddress(); | |
f75f0921 EM |
1058 | if (!empty($params['source'])) { |
1059 | unset($params['source']); | |
6a488035 | 1060 | } |
c52884b2 | 1061 | |
f75f0921 | 1062 | $this->_params['amount'] = $this->_params['total_amount']; |
c039f658 | 1063 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
1064 | // function to get correct amount level consistently. Remove setting of the amount level in | |
1065 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest | |
1066 | // to cover all variants. | |
f75f0921 | 1067 | $this->_params['amount_level'] = 0; |
e47238e5 | 1068 | $this->_params['description'] = ts("Contribution submitted by a staff person using contributor's credit card"); |
f75f0921 EM |
1069 | $this->_params['currencyID'] = CRM_Utils_Array::value('currency', |
1070 | $this->_params, | |
1071 | CRM_Core_Config::singleton()->defaultCurrency | |
1072 | ); | |
aefd7f6b | 1073 | |
f75f0921 EM |
1074 | $this->_params['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $params); |
1075 | $this->_params['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $params); | |
1076 | $this->_params['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $params); | |
3f662d9d | 1077 | |
f75f0921 EM |
1078 | //Add common data to formatted params |
1079 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($params, $this->_params, $this); | |
6a488035 | 1080 | |
f75f0921 EM |
1081 | if (empty($this->_params['invoice_id'])) { |
1082 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1083 | } | |
1084 | else { | |
1085 | $this->_params['invoiceID'] = $this->_params['invoice_id']; | |
1086 | } | |
6a488035 | 1087 | |
f75f0921 EM |
1088 | // At this point we've created a contact and stored its address etc |
1089 | // all the payment processors expect the name and address to be in the | |
1090 | // so we copy stuff over to first_name etc. | |
1091 | $paymentParams = $this->_params; | |
7126b55c | 1092 | $paymentParams['contactID'] = $contactID; |
f75f0921 | 1093 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); |
6a488035 | 1094 | |
67256e0c EM |
1095 | $financialType = new CRM_Financial_DAO_FinancialType(); |
1096 | $financialType->id = $params['financial_type_id']; | |
11829025 | 1097 | $financialType->find(TRUE); |
6a488035 | 1098 | |
f75f0921 EM |
1099 | // Add some financial type details to the params list |
1100 | // if folks need to use it. | |
67256e0c | 1101 | $paymentParams['contributionType_name'] = $this->_params['contributionType_name'] = $financialType->name; |
f75f0921 | 1102 | $paymentParams['contributionPageID'] = NULL; |
6a488035 | 1103 | |
f75f0921 EM |
1104 | if (!empty($this->_params['is_email_receipt'])) { |
1105 | $paymentParams['email'] = $this->userEmail; | |
1106 | $paymentParams['is_email_receipt'] = 1; | |
1107 | } | |
1108 | else { | |
1109 | $paymentParams['is_email_receipt'] = 0; | |
1110 | $this->_params['is_email_receipt'] = 0; | |
1111 | } | |
1112 | if (!empty($this->_params['receive_date'])) { | |
1113 | $paymentParams['receive_date'] = $this->_params['receive_date']; | |
1114 | } | |
6a488035 | 1115 | |
f75f0921 EM |
1116 | if (!empty($this->_params['is_email_receipt'])) { |
1117 | $this->_params['receipt_date'] = $now; | |
1118 | } | |
6a488035 | 1119 | |
f75f0921 | 1120 | $this->set('params', $this->_params); |
7758bd2b | 1121 | |
f75f0921 | 1122 | $this->assign('receive_date', $this->_params['receive_date']); |
6a488035 | 1123 | |
f75f0921 EM |
1124 | // Result has all the stuff we need |
1125 | // lets archive it to a financial transaction | |
67256e0c | 1126 | if ($financialType->is_deductible) { |
f75f0921 EM |
1127 | $this->assign('is_deductible', TRUE); |
1128 | $this->set('is_deductible', TRUE); | |
1129 | } | |
3febe800 | 1130 | $contributionParams = array( |
29cb4724 | 1131 | 'id' => CRM_Utils_Array::value('contribution_id', $this->_params), |
3febe800 | 1132 | 'contact_id' => $contactID, |
1133 | 'line_item' => $lineItem, | |
1134 | 'is_test' => $isTest, | |
1135 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $this->_params), | |
1136 | 'contribution_page_id' => CRM_Utils_Array::value('contribution_page_id', $this->_params), | |
1137 | 'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)), | |
1138 | 'thankyou_date' => CRM_Utils_Array::value('thankyou_date', $this->_params), | |
1139 | ); | |
16f3bd02 | 1140 | $contributionParams['payment_instrument_id'] = $this->_paymentProcessor['payment_instrument_id']; |
6a488035 | 1141 | |
ba013eea | 1142 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($this, |
7758bd2b EM |
1143 | $this->_params, |
1144 | NULL, | |
3febe800 | 1145 | $contributionParams, |
7758bd2b | 1146 | $financialType, |
7758bd2b | 1147 | FALSE, |
449f4c90 | 1148 | $this->_bltID, |
1149 | CRM_Utils_Array::value('is_recur', $this->_params) | |
7758bd2b | 1150 | ); |
6a488035 | 1151 | |
7758bd2b EM |
1152 | $paymentParams['contributionID'] = $contribution->id; |
1153 | $paymentParams['contributionTypeID'] = $contribution->financial_type_id; | |
1154 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; | |
1155 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; | |
6a488035 | 1156 | |
7758bd2b EM |
1157 | if ($paymentParams['amount'] > 0.0) { |
1158 | // force a re-get of the payment processor in case the form changed it, CRM-7179 | |
9d8f43b1 EM |
1159 | // NOTE - I expect this is obsolete. |
1160 | $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); | |
7758bd2b EM |
1161 | try { |
1162 | $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id'); | |
1163 | $result = $payment->doPayment($paymentParams, 'contribute'); | |
1164 | $this->assign('trxn_id', $result['trxn_id']); | |
1165 | $contribution->trxn_id = $result['trxn_id']; | |
1166 | /* Our scenarios here are | |
1167 | * 1) the payment failed & an Exception should have been thrown | |
1168 | * 2) the payment succeeded but the payment is not immediate (for example a recurring payment | |
1169 | * with a delayed start) | |
1170 | * 3) the payment succeeded with an immediate payment. | |
1171 | * | |
7c85dc65 | 1172 | * The doPayment function ensures that payment_status_id is always set |
7758bd2b EM |
1173 | * as historically we have had to guess from the context - ie doDirectPayment |
1174 | * = error or success, unless it is a recurring contribution in which case it is pending. | |
1175 | */ | |
7c85dc65 | 1176 | if ($result['payment_status_id'] == array_search('Completed', $statuses)) { |
760278d3 EM |
1177 | try { |
1178 | civicrm_api3('contribution', 'completetransaction', array( | |
1179 | 'id' => $contribution->id, | |
1180 | 'trxn_id' => $result['trxn_id'], | |
1181 | 'payment_processor_id' => $this->_paymentProcessor['id'], | |
1182 | 'is_transactional' => FALSE, | |
22e39333 | 1183 | 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), |
a55e39e9 | 1184 | 'card_type_id' => CRM_Utils_Array::value('card_type_id', $paymentParams), |
1185 | 'pan_truncation' => CRM_Utils_Array::value('pan_truncation', $paymentParams), | |
0af52f61 | 1186 | 'is_email_receipt' => FALSE, |
760278d3 EM |
1187 | )); |
1188 | // This has now been set to 1 in the DB - declare it here also | |
1189 | $contribution->contribution_status_id = 1; | |
1190 | } | |
1191 | catch (CiviCRM_API3_Exception $e) { | |
1192 | if ($e->getErrorCode() != 'contribution_completed') { | |
1193 | throw new CRM_Core_Exception('Failed to update contribution in database'); | |
1194 | } | |
1195 | } | |
7758bd2b EM |
1196 | } |
1197 | else { | |
1198 | // Save the trxn_id. | |
1199 | $contribution->save(); | |
1200 | } | |
1201 | } | |
1202 | catch (PaymentProcessorException $e) { | |
06d062ce | 1203 | CRM_Contribute_BAO_Contribution::failPayment($contribution->id, $paymentParams['contactID'], $e->getMessage()); |
7758bd2b EM |
1204 | throw new PaymentProcessorException($e->getMessage()); |
1205 | } | |
1206 | } | |
f75f0921 | 1207 | // Send receipt mail. |
e390a377 | 1208 | array_unshift($this->statusMessage, ts('The contribution record has been saved.')); |
f75f0921 EM |
1209 | if ($contribution->id && !empty($this->_params['is_email_receipt'])) { |
1210 | $this->_params['trxn_id'] = CRM_Utils_Array::value('trxn_id', $result); | |
7126b55c | 1211 | $this->_params['contact_id'] = $contactID; |
f75f0921 | 1212 | $this->_params['contribution_id'] = $contribution->id; |
c52884b2 | 1213 | if (CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $this->_params, TRUE)) { |
3e6a1f4a | 1214 | $this->statusMessage[] = ts('A receipt has been emailed to the contributor.'); |
6a488035 | 1215 | } |
28d44c9f | 1216 | } |
c52884b2 | 1217 | |
5e27919e | 1218 | return $contribution; |
6a488035 | 1219 | } |
5a230af7 | 1220 | |
f75f0921 EM |
1221 | /** |
1222 | * Generate the data to construct a snippet based pane. | |
1223 | * | |
1224 | * This form also assigns the showAdditionalInfo var based on historical code. | |
1225 | * This appears to mean 'there is a pane to show'. | |
1226 | * | |
1227 | * @param string $type | |
1228 | * Type of Pane - this is generally used to determine the function name used to build it | |
1229 | * - e.g CreditCard, AdditionalDetail | |
1230 | * @param array $defaults | |
1231 | * | |
1232 | * @return array | |
1233 | * We aim to further refactor & simplify this but currently | |
1234 | * - the panes array | |
1235 | * - should additional info be shown? | |
1236 | */ | |
1237 | protected function generatePane($type, $defaults) { | |
1238 | $urlParams = "snippet=4&formType={$type}"; | |
1239 | if ($this->_mode) { | |
1240 | $urlParams .= "&mode={$this->_mode}"; | |
6a488035 TO |
1241 | } |
1242 | ||
f75f0921 EM |
1243 | $open = 'false'; |
1244 | if ($type == 'CreditCard' || | |
1245 | $type == 'DirectDebit' | |
1246 | ) { | |
1247 | $open = 'true'; | |
6a488035 | 1248 | } |
6a488035 | 1249 | |
f75f0921 EM |
1250 | $pane = array( |
1251 | 'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams), | |
1252 | 'open' => $open, | |
1253 | 'id' => $type, | |
6a488035 | 1254 | ); |
6a488035 | 1255 | |
f75f0921 EM |
1256 | // See if we need to include this paneName in the current form. |
1257 | if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) || | |
1258 | CRM_Utils_Array::value("hidden_{$type}", $defaults) | |
1259 | ) { | |
1260 | $this->assign('showAdditionalInfo', TRUE); | |
1261 | $pane['open'] = 'true'; | |
6a488035 TO |
1262 | } |
1263 | ||
f75f0921 EM |
1264 | if ($type == 'CreditCard' || $type == 'DirectDebit') { |
1265 | // @todo would be good to align tpl name with form name... | |
1266 | // @todo document why this hidden variable is required. | |
1267 | $this->add('hidden', 'hidden_' . $type, 1); | |
1268 | return $pane; | |
6a488035 TO |
1269 | } |
1270 | else { | |
f75f0921 EM |
1271 | $additionalInfoFormFunction = 'build' . $type; |
1272 | CRM_Contribute_Form_AdditionalInfo::$additionalInfoFormFunction($this); | |
1273 | return $pane; | |
6a488035 | 1274 | } |
f75f0921 | 1275 | } |
6a488035 | 1276 | |
f75f0921 EM |
1277 | /** |
1278 | * Wrapper for unit testing the post process submit function. | |
1279 | * | |
1280 | * (If we expose through api we can get default additions 'for free'). | |
1281 | * | |
1282 | * @param array $params | |
5e27919e | 1283 | * @param int $action |
39f47c0d EM |
1284 | * @param string|null $creditCardMode |
1285 | * | |
1286 | * @throws \CiviCRM_API3_Exception | |
f75f0921 | 1287 | */ |
39f47c0d | 1288 | public function testSubmit($params, $action, $creditCardMode = NULL) { |
f75f0921 EM |
1289 | $defaults = array( |
1290 | 'soft_credit_contact_id' => array(), | |
6298b166 | 1291 | 'receive_date' => date('Y-m-d H:i:s'), |
f75f0921 | 1292 | 'receipt_date' => '', |
f75f0921 | 1293 | 'cancel_date' => '', |
43bf07d6 | 1294 | 'hidden_Premium' => 1, |
f75f0921 | 1295 | ); |
739a8336 | 1296 | $this->_bltID = 5; |
5e27919e EM |
1297 | if (!empty($params['id'])) { |
1298 | $existingContribution = civicrm_api3('contribution', 'getsingle', array( | |
1299 | 'id' => $params['id'], | |
1300 | )); | |
55e55e84 | 1301 | $this->_id = $params['id']; |
0f07bb06 | 1302 | $this->_values = $existingContribution; |
7c002a4d | 1303 | if (CRM_Contribute_BAO_Contribution::checkContributeSettings('invoicing')) { |
1304 | $this->_values['tax_amount'] = civicrm_api3('contribution', 'getvalue', array( | |
1305 | 'id' => $params['id'], | |
1306 | 'return' => 'tax_amount', | |
1307 | )); | |
1308 | } | |
5e27919e EM |
1309 | } |
1310 | else { | |
1311 | $existingContribution = array(); | |
1312 | } | |
1313 | ||
1314 | $this->_defaults['contribution_status_id'] = CRM_Utils_Array::value('contribution_status_id', | |
1315 | $existingContribution | |
1316 | ); | |
1317 | ||
1318 | $this->_defaults['total_amount'] = CRM_Utils_Array::value('total_amount', | |
1319 | $existingContribution | |
1320 | ); | |
6a488035 | 1321 | |
39f47c0d EM |
1322 | if ($creditCardMode) { |
1323 | $this->_mode = $creditCardMode; | |
1324 | } | |
1325 | ||
1326 | // Required because processCreditCard calls set method on this. | |
1327 | $_SERVER['REQUEST_METHOD'] = 'GET'; | |
1328 | $this->controller = new CRM_Core_Controller(); | |
1329 | ||
3e6a1f4a EM |
1330 | CRM_Contribute_Form_AdditionalInfo::buildPremium($this); |
1331 | ||
39f47c0d | 1332 | $this->_fields = array(); |
0495f30c | 1333 | return $this->submit(array_merge($defaults, $params), $action, CRM_Utils_Array::value('pledge_payment_id', $params)); |
43bf07d6 | 1334 | |
f75f0921 | 1335 | } |
c1cc3e0c | 1336 | |
f75f0921 EM |
1337 | /** |
1338 | * @param array $submittedValues | |
1339 | * | |
bdbc1044 EM |
1340 | * @param int $action |
1341 | * Action constant | |
1342 | * - CRM_Core_Action::UPDATE | |
1343 | * | |
5e27919e EM |
1344 | * @param $pledgePaymentID |
1345 | * | |
f75f0921 EM |
1346 | * @return array |
1347 | * @throws \Exception | |
1348 | */ | |
31760f81 | 1349 | protected function submit($submittedValues, $action, $pledgePaymentID) { |
f75f0921 | 1350 | $pId = $contribution = $isRelatedId = FALSE; |
3b8e6c3f | 1351 | $this->_params = $submittedValues; |
1352 | $this->beginPostProcess(); | |
a55e39e9 | 1353 | // reassign submitted form values if the any information is formatted via beginPostProcess |
1354 | $submittedValues = $this->_params; | |
3e6a1f4a | 1355 | |
bdbc1044 | 1356 | if (!empty($submittedValues['price_set_id']) && $action & CRM_Core_Action::UPDATE) { |
f75f0921 EM |
1357 | $line = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution'); |
1358 | $lineID = key($line); | |
1359 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('price_field_id', $line[$lineID]), 'price_set_id'); | |
1360 | $quickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); | |
2b308818 DG |
1361 | // Why do we do this? Seems like a like a wrapper for old functionality - but single line price sets & quick |
1362 | // config should be treated the same. | |
f75f0921 EM |
1363 | if ($quickConfig) { |
1364 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_contribution'); | |
1365 | } | |
6a488035 | 1366 | } |
f75f0921 EM |
1367 | |
1368 | // Process price set and get total amount and line items. | |
1369 | $lineItem = array(); | |
1370 | $priceSetId = CRM_Utils_Array::value('price_set_id', $submittedValues); | |
1371 | if (empty($priceSetId) && !$this->_id) { | |
1372 | $this->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name'); | |
1373 | $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); | |
1374 | $fieldID = key($this->_priceSet['fields']); | |
1375 | $fieldValueId = key($this->_priceSet['fields'][$fieldID]['options']); | |
1376 | $this->_priceSet['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount']; | |
1377 | $submittedValues['price_' . $fieldID] = 1; | |
6a488035 | 1378 | } |
6a488035 | 1379 | |
2b308818 DG |
1380 | // Every contribution has a price-set - the only reason it shouldn't be set is if we are dealing with |
1381 | // quick config (very very arguably) & yet we see that this could still be quick config so this should be understood | |
1382 | // as a point of fragility rather than a logical 'if' clause. | |
f75f0921 EM |
1383 | if ($priceSetId) { |
1384 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], | |
f436577a | 1385 | $submittedValues, $lineItem[$priceSetId], NULL, $priceSetId); |
f75f0921 | 1386 | // Unset tax amount for offline 'is_quick_config' contribution. |
2b308818 DG |
1387 | // @todo WHY - quick config was conceived as a quick way to configure contribution forms. |
1388 | // this is an example of 'other' functionality being hung off it. | |
f75f0921 EM |
1389 | if ($this->_priceSet['is_quick_config'] && |
1390 | !array_key_exists($submittedValues['financial_type_id'], CRM_Core_PseudoConstant::getTaxRates()) | |
1391 | ) { | |
1392 | unset($submittedValues['tax_amount']); | |
1393 | } | |
71a3f5f9 | 1394 | // @todo - look to remove this line. I believe it relates to CRM-16460 |
1395 | // and possibly contributes to fixing the issue described there but | |
1396 | // would cause breakage for negative values in some cases. | |
f75f0921 | 1397 | $submittedValues['total_amount'] = CRM_Utils_Array::value('amount', $submittedValues); |
6a488035 | 1398 | } |
f75f0921 EM |
1399 | if ($this->_id) { |
1400 | if ($this->_compId) { | |
1401 | if ($this->_context == 'participant') { | |
1402 | $pId = $this->_compId; | |
1403 | } | |
1404 | elseif ($this->_context == 'membership') { | |
1405 | $isRelatedId = TRUE; | |
1406 | } | |
1407 | else { | |
1408 | $pId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'participant_id', 'contribution_id'); | |
1409 | } | |
c1cc3e0c | 1410 | } |
f75f0921 EM |
1411 | else { |
1412 | $contributionDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
1413 | if (array_key_exists('membership', $contributionDetails)) { | |
1414 | $isRelatedId = TRUE; | |
c1cc3e0c | 1415 | } |
f75f0921 EM |
1416 | elseif (array_key_exists('participant', $contributionDetails)) { |
1417 | $pId = $contributionDetails['participant']; | |
c1cc3e0c | 1418 | } |
6a488035 | 1419 | } |
17d83f50 | 1420 | if (!empty($this->_payNow)) { |
1421 | $this->_params['contribution_id'] = $this->_id; | |
1422 | } | |
6a488035 | 1423 | } |
2b308818 | 1424 | |
f75f0921 EM |
1425 | if (!$priceSetId && !empty($submittedValues['total_amount']) && $this->_id) { |
1426 | // CRM-10117 update the line items for participants. | |
2b308818 DG |
1427 | // @todo - if we are completing a contribution then the api call |
1428 | // civicrm_api3('Contribution', 'completetransaction') should take care of | |
1429 | // all associated updates rather than replicating them on the form layer. | |
f75f0921 EM |
1430 | if ($pId) { |
1431 | $entityTable = 'participant'; | |
1432 | $entityID = $pId; | |
1433 | $isRelatedId = FALSE; | |
1434 | $participantParams = array( | |
1435 | 'fee_amount' => $submittedValues['total_amount'], | |
1436 | 'id' => $entityID, | |
1437 | ); | |
1438 | CRM_Event_BAO_Participant::add($participantParams); | |
1439 | if (empty($this->_lineItems)) { | |
77dbdcbc | 1440 | $this->_lineItems[] = CRM_Price_BAO_LineItem::getLineItems($entityID, 'participant', TRUE); |
f75f0921 EM |
1441 | } |
1442 | } | |
1443 | else { | |
1444 | $entityTable = 'contribution'; | |
1445 | $entityID = $this->_id; | |
1446 | } | |
6a488035 | 1447 | |
77dbdcbc | 1448 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, $entityTable, FALSE, TRUE, $isRelatedId); |
f75f0921 EM |
1449 | foreach (array_keys($lineItems) as $id) { |
1450 | $lineItems[$id]['id'] = $id; | |
1451 | } | |
1452 | $itemId = key($lineItems); | |
1453 | if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) { | |
1454 | $this->_priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id'); | |
1455 | } | |
6a488035 | 1456 | |
2b308818 DG |
1457 | // @todo see above - new functionality has been inappropriately added to the quick config concept |
1458 | // and new functionality has been added onto the form layer rather than the BAO :-( | |
f75f0921 | 1459 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
f85b8063 KJ |
1460 | //CRM-16833: Ensure tax is applied only once for membership conribution, when status changed.(e.g Pending to Completed). |
1461 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
a5b08a92 | 1462 | if (!(CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails))) { |
f85b8063 KJ |
1463 | if (!($this->_action & CRM_Core_Action::UPDATE && (($this->_defaults['contribution_status_id'] != $submittedValues['contribution_status_id'])))) { |
1464 | $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('total_amount', $submittedValues)); | |
1465 | } | |
1466 | } | |
6a488035 | 1467 | |
f75f0921 EM |
1468 | // Update line total and total amount with tax on edit. |
1469 | $financialItemsId = CRM_Core_PseudoConstant::getTaxRates(); | |
1470 | if (array_key_exists($submittedValues['financial_type_id'], $financialItemsId)) { | |
1471 | $lineItems[$itemId]['tax_rate'] = $financialItemsId[$submittedValues['financial_type_id']]; | |
1472 | } | |
1473 | else { | |
1474 | $lineItems[$itemId]['tax_rate'] = $lineItems[$itemId]['tax_amount'] = ""; | |
1475 | $submittedValues['tax_amount'] = 'null'; | |
1476 | } | |
1477 | if ($lineItems[$itemId]['tax_rate']) { | |
1478 | $lineItems[$itemId]['tax_amount'] = ($lineItems[$itemId]['tax_rate'] / 100) * $lineItems[$itemId]['line_total']; | |
1479 | $submittedValues['total_amount'] = $lineItems[$itemId]['line_total'] + $lineItems[$itemId]['tax_amount']; | |
1480 | $submittedValues['tax_amount'] = $lineItems[$itemId]['tax_amount']; | |
1481 | } | |
1482 | } | |
1483 | // CRM-10117 update the line items for participants. | |
1484 | if (!empty($lineItems[$itemId]['price_field_id'])) { | |
1485 | $lineItem[$this->_priceSetId] = $lineItems; | |
1486 | } | |
6a488035 TO |
1487 | } |
1488 | ||
f75f0921 EM |
1489 | $isQuickConfig = 0; |
1490 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { | |
1491 | $isQuickConfig = 1; | |
6a488035 | 1492 | } |
f75f0921 EM |
1493 | //CRM-11529 for quick config back office transactions |
1494 | //when financial_type_id is passed in form, update the | |
1495 | //line items with the financial type selected in form | |
2b308818 DG |
1496 | // NOTE that this IS still a legitimate use of 'quick-config' for contributions under the current DB but |
1497 | // we should look at having a price field per contribution type & then there would be little reason | |
1498 | // for the back-office contribution form postProcess to know if it is a quick-config form. | |
f75f0921 EM |
1499 | if ($isQuickConfig && !empty($submittedValues['financial_type_id']) && CRM_Utils_Array::value($this->_priceSetId, $lineItem) |
1500 | ) { | |
1501 | foreach ($lineItem[$this->_priceSetId] as &$values) { | |
1502 | $values['financial_type_id'] = $submittedValues['financial_type_id']; | |
1503 | } | |
6a488035 TO |
1504 | } |
1505 | ||
f75f0921 | 1506 | if (!isset($submittedValues['total_amount'])) { |
7c002a4d | 1507 | $submittedValues['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_values); |
1508 | // Avoid tax amount deduction on edit form and keep it original, because this will lead to error described in CRM-20676 | |
1509 | if (!$this->_id) { | |
1510 | $submittedValues['total_amount'] -= CRM_Utils_Array::value('tax_amount', $this->_values, 0); | |
1511 | } | |
6a488035 | 1512 | } |
f75f0921 | 1513 | $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE); |
6a488035 | 1514 | |
f75f0921 EM |
1515 | $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id'])); |
1516 | if ($this->_id && count($isEmpty) == 1 && key($isEmpty) == NULL) { | |
23274e68 | 1517 | civicrm_api3('ContributionSoft', 'get', array('contribution_id' => $this->_id, 'pcp_id' => NULL, 'api.ContributionSoft.delete' => 1)); |
f75f0921 | 1518 | } |
c1cc3e0c | 1519 | |
f75f0921 EM |
1520 | // set the contact, when contact is selected |
1521 | if (!empty($submittedValues['contact_id'])) { | |
1522 | $this->_contactID = $submittedValues['contact_id']; | |
c1cc3e0c | 1523 | } |
7a13735b | 1524 | |
3e6a1f4a | 1525 | $formValues = $submittedValues; |
96025800 | 1526 | |
f75f0921 | 1527 | // Credit Card Contribution. |
a5399a39 | 1528 | if ($this->_mode) { |
bf4253e7 EM |
1529 | $paramsSetByPaymentProcessingSubsystem = array( |
1530 | 'trxn_id', | |
1531 | 'payment_instrument_id', | |
1532 | 'contribution_status_id', | |
1533 | 'cancel_date', | |
1534 | 'cancel_reason', | |
1535 | ); | |
1536 | foreach ($paramsSetByPaymentProcessingSubsystem as $key) { | |
1537 | if (isset($formValues[$key])) { | |
1538 | unset($formValues[$key]); | |
1539 | } | |
1540 | } | |
7126b55c | 1541 | $contribution = $this->processCreditCard($formValues, $lineItem, $this->_contactID); |
bf4253e7 EM |
1542 | foreach ($paramsSetByPaymentProcessingSubsystem as $key) { |
1543 | $formValues[$key] = $contribution->$key; | |
1544 | } | |
a5399a39 | 1545 | } |
f75f0921 EM |
1546 | else { |
1547 | // Offline Contribution. | |
1548 | $submittedValues = $this->unsetCreditCardFields($submittedValues); | |
a5399a39 | 1549 | |
f75f0921 | 1550 | // get the required field value only. |
3e6a1f4a | 1551 | |
f75f0921 | 1552 | $params = $ids = array(); |
a5399a39 | 1553 | |
f75f0921 | 1554 | $params['contact_id'] = $this->_contactID; |
f75f0921 | 1555 | $params['currency'] = $this->getCurrency($submittedValues); |
a5399a39 | 1556 | |
7a13735b | 1557 | //format soft-credit/pcp param first |
1558 | CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($submittedValues, $this); | |
1559 | $params = array_merge($params, $submittedValues); | |
1560 | ||
f75f0921 EM |
1561 | $fields = array( |
1562 | 'financial_type_id', | |
1563 | 'contribution_status_id', | |
1564 | 'payment_instrument_id', | |
1565 | 'cancel_reason', | |
1566 | 'source', | |
1567 | 'check_number', | |
a55e39e9 | 1568 | 'card_type_id', |
1569 | 'pan_truncation', | |
f75f0921 EM |
1570 | ); |
1571 | foreach ($fields as $f) { | |
1572 | $params[$f] = CRM_Utils_Array::value($f, $formValues); | |
1573 | } | |
a5399a39 | 1574 | |
f75f0921 EM |
1575 | // CRM-5740 if priceset is used, no need to cleanup money. |
1576 | if ($priceSetId) { | |
1577 | $params['skipCleanMoney'] = 1; | |
1578 | } | |
46d173ef PN |
1579 | $params['revenue_recognition_date'] = NULL; |
1580 | if (!empty($formValues['revenue_recognition_date']) | |
1581 | && count(array_filter($formValues['revenue_recognition_date'])) == 2 | |
1582 | ) { | |
1583 | $params['revenue_recognition_date'] = CRM_Utils_Date::processDate( | |
1584 | '01-' . implode('-', $formValues['revenue_recognition_date']) | |
1585 | ); | |
1586 | } | |
f75f0921 EM |
1587 | |
1588 | if (!empty($formValues['is_email_receipt'])) { | |
1589 | $params['receipt_date'] = date("Y-m-d"); | |
1590 | } | |
1591 | ||
d682fc35 | 1592 | if (CRM_Contribute_BAO_Contribution::isContributionStatusNegative($params['contribution_status_id']) |
f75f0921 EM |
1593 | ) { |
1594 | if (CRM_Utils_System::isNull(CRM_Utils_Array::value('cancel_date', $params))) { | |
b53052cb | 1595 | $params['cancel_date'] = date('YmdHis'); |
f75f0921 EM |
1596 | } |
1597 | } | |
1598 | else { | |
1599 | $params['cancel_date'] = $params['cancel_reason'] = 'null'; | |
1600 | } | |
1601 | ||
1602 | // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996 | |
1603 | // else if contribution_status is changed to Completed is_pay_later flag is changed to 0, CRM-15041 | |
f527e012 | 1604 | if ($params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending')) { |
f75f0921 EM |
1605 | $params['is_pay_later'] = 1; |
1606 | } | |
f527e012 | 1607 | elseif ($params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')) { |
f75f0921 EM |
1608 | $params['is_pay_later'] = 0; |
1609 | } | |
1610 | ||
1611 | $ids['contribution'] = $params['id'] = $this->_id; | |
1612 | ||
1613 | // Add Additional common information to formatted params. | |
1614 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($formValues, $params, $this); | |
1615 | if ($pId) { | |
1616 | $params['contribution_mode'] = 'participant'; | |
1617 | $params['participant_id'] = $pId; | |
1618 | $params['skipLineItem'] = 1; | |
1619 | } | |
1620 | elseif ($isRelatedId) { | |
1621 | $params['contribution_mode'] = 'membership'; | |
1622 | } | |
1623 | $params['line_item'] = $lineItem; | |
1624 | $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array::value('id', $this->_paymentProcessor); | |
1625 | if (isset($submittedValues['tax_amount'])) { | |
1626 | $params['tax_amount'] = $submittedValues['tax_amount']; | |
1627 | } | |
1628 | //create contribution. | |
1629 | if ($isQuickConfig) { | |
1630 | $params['is_quick_config'] = 1; | |
1631 | } | |
07dd658b | 1632 | $params['non_deductible_amount'] = $this->calculateNonDeductibleAmount($params, $formValues); |
f75f0921 | 1633 | |
7fd21004 | 1634 | // we are already handling note below, so to avoid duplicate notes against $contribution |
1635 | if (!empty($params['note']) && !empty($submittedValues['note'])) { | |
1636 | unset($params['note']); | |
1637 | } | |
f75f0921 EM |
1638 | $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); |
1639 | ||
1640 | // process associated membership / participant, CRM-4395 | |
bdbc1044 | 1641 | if ($contribution->id && $action & CRM_Core_Action::UPDATE) { |
643413a0 | 1642 | $this->statusMessage[] = CRM_Contribute_BAO_Contribution::transitionComponentWithReturnMessage($contribution->id, |
f75f0921 EM |
1643 | $contribution->contribution_status_id, |
1644 | CRM_Utils_Array::value('contribution_status_id', | |
1645 | $this->_values | |
1646 | ), | |
1647 | $contribution->receive_date | |
1648 | ); | |
1649 | } | |
1650 | ||
3e6a1f4a | 1651 | array_unshift($this->statusMessage, ts('The contribution record has been saved.')); |
f75f0921 | 1652 | |
6d4f5b21 | 1653 | $this->invoicingPostProcessHook($submittedValues, $action, $lineItem); |
f75f0921 EM |
1654 | |
1655 | //send receipt mail. | |
1656 | if ($contribution->id && !empty($formValues['is_email_receipt'])) { | |
1657 | $formValues['contact_id'] = $this->_contactID; | |
1658 | $formValues['contribution_id'] = $contribution->id; | |
1659 | ||
1660 | $formValues += CRM_Contribute_BAO_ContributionSoft::getSoftContribution($contribution->id); | |
1661 | ||
1662 | // to get 'from email id' for send receipt | |
beac1417 | 1663 | $this->fromEmailId = CRM_Utils_Array::value('from_email_address', $formValues); |
5e27919e | 1664 | if (CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $formValues)) { |
3e6a1f4a | 1665 | $this->statusMessage[] = ts('A receipt has been emailed to the contributor.'); |
5e27919e | 1666 | } |
f75f0921 EM |
1667 | } |
1668 | ||
7e5961cf EM |
1669 | $this->statusMessageTitle = ts('Saved'); |
1670 | ||
f75f0921 | 1671 | } |
5e27919e | 1672 | |
99af166d | 1673 | if ($contribution->id && isset($formValues['product_name'][0])) { |
2dcbf765 | 1674 | CRM_Contribute_Form_AdditionalInfo::processPremium($submittedValues, $contribution->id, |
945f423d EM |
1675 | $this->_premiumID, $this->_options |
1676 | ); | |
1677 | } | |
1678 | ||
dce847cf | 1679 | if ($contribution->id && array_key_exists('note', $submittedValues)) { |
945f423d EM |
1680 | CRM_Contribute_Form_AdditionalInfo::processNote($submittedValues, $this->_contactID, $contribution->id, $this->_noteID); |
1681 | } | |
1682 | ||
3e6a1f4a | 1683 | CRM_Core_Session::setStatus(implode(' ', $this->statusMessage), $this->statusMessageTitle, 'success'); |
7e5961cf | 1684 | |
5e27919e EM |
1685 | CRM_Contribute_BAO_Contribution::updateRelatedPledge( |
1686 | $action, | |
1687 | $pledgePaymentID, | |
1688 | $contribution->id, | |
1689 | (CRM_Utils_Array::value('option_type', $formValues) == 2) ? TRUE : FALSE, | |
1690 | $formValues['total_amount'], | |
3e6a1f4a | 1691 | CRM_Utils_Array::value('total_amount', $this->_defaults), |
5e27919e | 1692 | $formValues['contribution_status_id'], |
3e6a1f4a | 1693 | CRM_Utils_Array::value('contribution_status_id', $this->_defaults) |
5e27919e EM |
1694 | ); |
1695 | return $contribution; | |
2e03ce51 EM |
1696 | } |
1697 | ||
6d4f5b21 EM |
1698 | /** |
1699 | * Assign tax calculations to contribution receipts. | |
1700 | * | |
1701 | * @param array $submittedValues | |
1702 | * @param int $action | |
1703 | * @param array $lineItem | |
1704 | */ | |
1705 | protected function invoicingPostProcessHook($submittedValues, $action, $lineItem) { | |
1706 | ||
aaffa79f | 1707 | $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); |
6d4f5b21 EM |
1708 | if (!CRM_Utils_Array::value('invoicing', $invoiceSettings)) { |
1709 | return; | |
1710 | } | |
1711 | $taxRate = array(); | |
1712 | $getTaxDetails = FALSE; | |
f1662cbd | 1713 | |
1714 | foreach ($lineItem as $key => $value) { | |
6d4f5b21 EM |
1715 | foreach ($value as $v) { |
1716 | if (isset($taxRate[(string) CRM_Utils_Array::value('tax_rate', $v)])) { | |
1717 | $taxRate[(string) $v['tax_rate']] = $taxRate[(string) $v['tax_rate']] + CRM_Utils_Array::value('tax_amount', $v); | |
1718 | } | |
1719 | else { | |
1720 | if (isset($v['tax_rate'])) { | |
1721 | $taxRate[(string) $v['tax_rate']] = CRM_Utils_Array::value('tax_amount', $v); | |
1722 | $getTaxDetails = TRUE; | |
1723 | } | |
1724 | } | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | if ($action & CRM_Core_Action::UPDATE) { | |
1729 | if (isset($submittedValues['tax_amount'])) { | |
1730 | $totalTaxAmount = $submittedValues['tax_amount']; | |
1731 | } | |
1732 | else { | |
1733 | $totalTaxAmount = $this->_values['tax_amount']; | |
1734 | } | |
1735 | $this->assign('totalTaxAmount', $totalTaxAmount); | |
1736 | $this->assign('dataArray', $taxRate); | |
1737 | } | |
1738 | else { | |
1739 | if (!empty($submittedValues['price_set_id'])) { | |
1740 | $this->assign('totalTaxAmount', $submittedValues['tax_amount']); | |
1741 | $this->assign('getTaxDetails', $getTaxDetails); | |
1742 | $this->assign('dataArray', $taxRate); | |
1743 | $this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings)); | |
1744 | } | |
1745 | else { | |
1746 | $this->assign('totalTaxAmount', CRM_Utils_Array::value('tax_amount', $submittedValues)); | |
1747 | } | |
1748 | } | |
1749 | } | |
1750 | ||
07dd658b EM |
1751 | /** |
1752 | * Calculate non deductible amount. | |
1753 | * | |
1754 | * CRM-11956 | |
1755 | * if non_deductible_amount exists i.e. Additional Details field set was opened [and staff typed something] - | |
1756 | * if non_deductible_amount does NOT exist - then calculate it depending on: | |
1757 | * $financialType->is_deductible and whether there is a product (premium). | |
1758 | * | |
1759 | * @param $params | |
1760 | * @param $formValues | |
1761 | * | |
1762 | * @return array | |
1763 | */ | |
1764 | protected function calculateNonDeductibleAmount($params, $formValues) { | |
1765 | if (!empty($params['non_deductible_amount'])) { | |
1766 | return $params['non_deductible_amount']; | |
1767 | } | |
5afce5ad | 1768 | |
05465712 | 1769 | $priceSetId = CRM_Utils_Array::value('price_set_id', $params); |
5afce5ad | 1770 | // return non-deductible amount if it is set at the price field option level |
05465712 | 1771 | if ($priceSetId && !empty($params['line_item'])) { |
1772 | $nonDeductibleAmount = CRM_Price_BAO_PriceSet::getNonDeductibleAmountFromPriceSet($priceSetId, $params['line_item']); | |
1773 | if (!empty($nonDeductibleAmount)) { | |
5afce5ad | 1774 | return $nonDeductibleAmount; |
1775 | } | |
1776 | } | |
07dd658b | 1777 | |
f70eb51f EM |
1778 | $financialType = new CRM_Financial_DAO_FinancialType(); |
1779 | $financialType->id = $params['financial_type_id']; | |
11829025 | 1780 | $financialType->find(TRUE); |
07dd658b | 1781 | |
f70eb51f EM |
1782 | if ($financialType->is_deductible) { |
1783 | ||
1784 | if (isset($formValues['product_name'][0])) { | |
1785 | $selectProduct = $formValues['product_name'][0]; | |
1786 | } | |
1787 | // if there is a product - compare the value to the contribution amount | |
1788 | if (isset($selectProduct)) { | |
1789 | $productDAO = new CRM_Contribute_DAO_Product(); | |
1790 | $productDAO->id = $selectProduct; | |
1791 | $productDAO->find(TRUE); | |
1792 | // product value exceeds contribution amount | |
1793 | if ($params['total_amount'] < $productDAO->price) { | |
1794 | return $params['total_amount']; | |
07dd658b | 1795 | } |
f70eb51f | 1796 | // product value does NOT exceed contribution amount |
07dd658b | 1797 | else { |
f70eb51f | 1798 | return $productDAO->price; |
07dd658b EM |
1799 | } |
1800 | } | |
f70eb51f | 1801 | // contribution is deductible - but there is no product |
07dd658b | 1802 | else { |
f70eb51f | 1803 | return '0.00'; |
07dd658b EM |
1804 | } |
1805 | } | |
f70eb51f EM |
1806 | // contribution is NOT deductible |
1807 | else { | |
1808 | return $params['total_amount']; | |
1809 | } | |
1810 | ||
07dd658b EM |
1811 | return 0; |
1812 | } | |
1813 | ||
6a488035 | 1814 | } |