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