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