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