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 | |
e47077ce | 12 | use Civi\Api4\Contribution; |
7758bd2b EM |
13 | use Civi\Payment\Exception\PaymentProcessorException; |
14 | ||
6a488035 | 15 | /** |
22ab45ae | 16 | * This class generates form components for processing a contribution. |
6a488035 TO |
17 | */ |
18 | class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditPayment { | |
6a488035 | 19 | /** |
fe482240 | 20 | * The id of the contribution that we are processing. |
6a488035 TO |
21 | * |
22 | * @var int | |
6a488035 TO |
23 | */ |
24 | public $_id; | |
25 | ||
26 | /** | |
fe482240 | 27 | * The id of the premium that we are processing. |
6a488035 TO |
28 | * |
29 | * @var int | |
6a488035 TO |
30 | */ |
31 | public $_premiumID = NULL; | |
4691b077 EM |
32 | |
33 | /** | |
34 | * @var CRM_Contribute_DAO_ContributionProduct | |
35 | */ | |
6a488035 TO |
36 | public $_productDAO = NULL; |
37 | ||
38 | /** | |
fe482240 | 39 | * The id of the note. |
6a488035 TO |
40 | * |
41 | * @var int | |
6a488035 TO |
42 | */ |
43 | public $_noteID; | |
44 | ||
45 | /** | |
fe482240 | 46 | * The id of the contact associated with this contribution. |
6a488035 TO |
47 | * |
48 | * @var int | |
6a488035 TO |
49 | */ |
50 | public $_contactID; | |
51 | ||
52 | /** | |
fe482240 | 53 | * The id of the pledge payment that we are processing. |
6a488035 TO |
54 | * |
55 | * @var int | |
6a488035 TO |
56 | */ |
57 | public $_ppID; | |
58 | ||
6a488035 | 59 | /** |
fe482240 | 60 | * Is this contribution associated with an online. |
6a488035 TO |
61 | * financial transaction |
62 | * | |
b67daa72 | 63 | * @var bool |
6a488035 TO |
64 | */ |
65 | public $_online = FALSE; | |
66 | ||
67 | /** | |
bf56a083 | 68 | * Stores all product options. |
6a488035 TO |
69 | * |
70 | * @var array | |
6a488035 TO |
71 | */ |
72 | public $_options; | |
73 | ||
4691b077 EM |
74 | /** |
75 | * Storage of parameters from form | |
76 | * | |
77 | * @var array | |
4691b077 EM |
78 | */ |
79 | public $_params; | |
80 | ||
6a488035 TO |
81 | /** |
82 | * The contribution values if an existing contribution | |
1330f57a | 83 | * @var array |
6a488035 TO |
84 | */ |
85 | public $_values; | |
86 | ||
87 | /** | |
88 | * The pledge values if this contribution is associated with pledge | |
1330f57a | 89 | * @var array |
6a488035 TO |
90 | */ |
91 | public $_pledgeValues; | |
92 | ||
93 | public $_contributeMode = 'direct'; | |
94 | ||
95 | public $_context; | |
96 | ||
5032bf07 | 97 | /** |
fe482240 | 98 | * Parameter with confusing name. |
5032bf07 | 99 | * @var string |
1330f57a | 100 | * @todo what is it? |
5032bf07 EM |
101 | */ |
102 | public $_compContext; | |
103 | ||
6a488035 TO |
104 | public $_compId; |
105 | ||
5032bf07 EM |
106 | /** |
107 | * Possible From email addresses | |
108 | * @var array | |
109 | */ | |
110 | public $_fromEmails; | |
d77a0a58 EM |
111 | |
112 | /** | |
9f266042 | 113 | * ID of from email. |
114 | * | |
115 | * @var int | |
d77a0a58 EM |
116 | */ |
117 | public $fromEmailId; | |
d424ffde CW |
118 | |
119 | /** | |
6a488035 | 120 | * Store the line items if price set used. |
1330f57a | 121 | * @var array |
6a488035 TO |
122 | */ |
123 | public $_lineItems; | |
124 | ||
d77a0a58 | 125 | /** |
100fef9d | 126 | * Line item |
d77a0a58 | 127 | * @var array |
1330f57a | 128 | * @todo explain why we use lineItem & lineItems |
d77a0a58 EM |
129 | */ |
130 | public $_lineItem; | |
131 | ||
17db9f82 | 132 | /** |
5ba7d840 | 133 | * Soft credit info. |
134 | * | |
135 | * @var array | |
17db9f82 KJ |
136 | */ |
137 | public $_softCreditInfo; | |
138 | ||
6a488035 | 139 | protected $_formType; |
efe42cdc | 140 | |
459091e1 | 141 | /** |
100fef9d | 142 | * Array of the payment fields to be displayed in the payment fieldset (pane) in billingBlock.tpl |
459091e1 EM |
143 | * this contains all the information to describe these fields from quickform. See CRM_Core_Form_Payment getPaymentFormFieldsMetadata |
144 | * | |
145 | * @var array | |
146 | */ | |
be2fb01f | 147 | public $_paymentFields = []; |
4691b077 | 148 | /** |
28a4070b | 149 | * Logged in user's email. |
4691b077 EM |
150 | * @var string |
151 | */ | |
152 | public $userEmail; | |
153 | ||
154 | /** | |
9f266042 | 155 | * Price set ID. |
156 | * | |
157 | * @var int | |
4691b077 EM |
158 | */ |
159 | public $_priceSetId; | |
160 | ||
4691b077 EM |
161 | /** |
162 | * Price set as an array | |
163 | * @var array | |
164 | */ | |
165 | public $_priceSet; | |
166 | ||
d77a0a58 EM |
167 | /** |
168 | * User display name | |
169 | * | |
170 | * @var string | |
171 | */ | |
172 | public $userDisplayName; | |
353ffa53 | 173 | |
7e5961cf EM |
174 | /** |
175 | * Status message to be shown to the user. | |
176 | * | |
3e6a1f4a | 177 | * @var array |
7e5961cf | 178 | */ |
be2fb01f | 179 | protected $statusMessage = []; |
7e5961cf EM |
180 | |
181 | /** | |
182 | * Status message title to be shown to the user. | |
183 | * | |
184 | * Generally the payment processor message title is 'Complete' and offline is 'Saved' | |
185 | * although this might not be a good fit with the broad range of processors. | |
186 | * | |
187 | * @var string | |
188 | */ | |
189 | protected $statusMessageTitle; | |
190 | ||
e56e813e BS |
191 | /** |
192 | * @var int | |
193 | * | |
194 | * Max row count for soft credits. The value here is +1 the actual number of | |
195 | * rows displayed. | |
196 | */ | |
197 | public $_softCreditItemCount = 11; | |
198 | ||
88aae6d4 A |
199 | /** |
200 | * @var bool | |
201 | */ | |
202 | public $submitOnce = TRUE; | |
203 | ||
e47077ce EM |
204 | /** |
205 | * Status of contribution prior to edit. | |
206 | * | |
207 | * @var string | |
208 | */ | |
209 | protected $previousContributionStatus; | |
210 | ||
b25212e8 PN |
211 | /** |
212 | * Explicitly declare the form context. | |
213 | */ | |
214 | public function getDefaultContext() { | |
215 | return 'create'; | |
216 | } | |
217 | ||
6a488035 | 218 | /** |
22ab45ae | 219 | * Set variables up before form is built. |
f174ed0b | 220 | * |
221 | * @throws \CRM_Core_Exception | |
222 | * @throws \CiviCRM_API3_Exception | |
6a488035 TO |
223 | */ |
224 | public function preProcess() { | |
28a4070b | 225 | // Check permission for action. |
6a488035 | 226 | if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { |
beb414cc | 227 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
6a488035 | 228 | } |
6a488035 | 229 | |
42e8b05c EM |
230 | parent::preProcess(); |
231 | ||
9c1bc317 | 232 | $this->_formType = $_GET['formType'] ?? NULL; |
6a488035 | 233 | |
28a4070b | 234 | // Get price set id. |
9c1bc317 | 235 | $this->_priceSetId = $_GET['priceSetId'] ?? NULL; |
6a488035 TO |
236 | $this->set('priceSetId', $this->_priceSetId); |
237 | $this->assign('priceSetId', $this->_priceSetId); | |
238 | ||
28a4070b | 239 | // Get the pledge payment id |
6a488035 TO |
240 | $this->_ppID = CRM_Utils_Request::retrieve('ppid', 'Positive', $this); |
241 | ||
6a488035 TO |
242 | $this->assign('action', $this->_action); |
243 | ||
28a4070b | 244 | // Get the contribution id if update |
b4e8c2b4 | 245 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive'); |
59691709 | 246 | $this->assign('isUsePaymentBlock', !empty($this->_id)); |
6a488035 | 247 | if (!empty($this->_id)) { |
c485e792 | 248 | $this->assignPaymentInfoBlock(); |
6a488035 TO |
249 | $this->assign('contribID', $this->_id); |
250 | } | |
251 | ||
edc80cda | 252 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
253 | $this->assign('context', $this->_context); |
254 | ||
255 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Positive', $this); | |
256 | ||
257 | $this->_compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this); | |
258 | ||
259 | //set the contribution mode. | |
19046166 | 260 | $this->_mode = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this); |
6a488035 TO |
261 | |
262 | $this->assign('contributionMode', $this->_mode); | |
459091e1 EM |
263 | if ($this->_action & CRM_Core_Action::DELETE) { |
264 | return; | |
265 | } | |
6a488035 | 266 | |
6a488035 | 267 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); |
6a488035 | 268 | |
d77a0a58 | 269 | if (in_array('CiviPledge', CRM_Core_Config::singleton()->enableComponents) && !$this->_formType) { |
6a488035 TO |
270 | $this->preProcessPledge(); |
271 | } | |
272 | ||
d5397f2f PJ |
273 | if ($this->_id) { |
274 | $this->showRecordLinkMesssage($this->_id); | |
275 | } | |
be2fb01f | 276 | $this->_values = []; |
6a488035 | 277 | |
bf56a083 | 278 | // Current contribution id. |
6a488035 TO |
279 | if ($this->_id) { |
280 | $this->assignPremiumProduct($this->_id); | |
281 | $this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values); | |
282 | } | |
283 | ||
284 | // when custom data is included in this page | |
a7488080 | 285 | if (!empty($_POST['hidden_custom'])) { |
c6a8691e | 286 | $this->applyCustomData('Contribution', $this->getFinancialTypeID(), $this->_id); |
6a488035 TO |
287 | } |
288 | ||
7d238e35 JJ |
289 | if (!empty($this->_values['is_template'])) { |
290 | $this->assign('is_template', TRUE); | |
291 | } | |
292 | ||
be2fb01f | 293 | $this->_lineItems = []; |
6a488035 | 294 | if ($this->_id) { |
f174ed0b | 295 | if (!empty($this->_compId) && $this->_compContext === 'participant') { |
6a488035 TO |
296 | $this->assign('compId', $this->_compId); |
297 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_compId); | |
298 | } | |
299 | else { | |
0b71718c | 300 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution', 1, TRUE, TRUE); |
6a488035 | 301 | } |
f1662cbd | 302 | // wtf? |
6a488035 TO |
303 | empty($lineItem) ? NULL : $this->_lineItems[] = $lineItem; |
304 | } | |
305 | ||
be2fb01f | 306 | $this->assign('lineItem', empty($lineItem) ? FALSE : [$lineItem]); |
6a488035 TO |
307 | |
308 | // Set title | |
29cb4724 | 309 | if ($this->_mode && $this->_id) { |
17d83f50 | 310 | $this->_payNow = TRUE; |
311 | $this->assign('payNow', $this->_payNow); | |
7e2e2551 | 312 | $this->setTitle(ts('Pay with Credit Card')); |
29cb4724 | 313 | } |
7d238e35 JJ |
314 | elseif (!empty($this->_values['is_template'])) { |
315 | $this->setPageTitle(ts('Template Contribution')); | |
316 | } | |
29cb4724 | 317 | elseif ($this->_mode) { |
e2046b33 CW |
318 | $this->setPageTitle($this->_ppID ? ts('Credit Card Pledge Payment') : ts('Credit Card Contribution')); |
319 | } | |
320 | else { | |
321 | $this->setPageTitle($this->_ppID ? ts('Pledge Payment') : ts('Contribution')); | |
6a488035 | 322 | } |
6a488035 TO |
323 | } |
324 | ||
28a4070b EM |
325 | /** |
326 | * Set default values. | |
327 | * | |
328 | * @return array | |
f174ed0b | 329 | * |
330 | * @throws \CRM_Core_Exception | |
28a4070b | 331 | */ |
00be9182 | 332 | public function setDefaultValues() { |
6a488035 TO |
333 | $defaults = $this->_values; |
334 | ||
bf56a083 | 335 | // Set defaults for pledge payment. |
6a488035 | 336 | if ($this->_ppID) { |
9c1bc317 CW |
337 | $defaults['total_amount'] = $this->_pledgeValues['pledgePayment']['scheduled_amount'] ?? NULL; |
338 | $defaults['financial_type_id'] = $this->_pledgeValues['financial_type_id'] ?? NULL; | |
339 | $defaults['currency'] = $this->_pledgeValues['currency'] ?? NULL; | |
6a488035 TO |
340 | $defaults['option_type'] = 1; |
341 | } | |
342 | ||
6a488035 TO |
343 | if ($this->_action & CRM_Core_Action::DELETE) { |
344 | return $defaults; | |
345 | } | |
346 | ||
7fe22c48 EM |
347 | $defaults['frequency_interval'] = 1; |
348 | $defaults['frequency_unit'] = 'month'; | |
349 | ||
bf56a083 | 350 | // Set soft credit defaults. |
17db9f82 | 351 | CRM_Contribute_Form_SoftCredit::setDefaultValues($defaults, $this); |
dfbad3f7 | 352 | |
6a488035 | 353 | if ($this->_mode) { |
99cdd94d | 354 | // @todo - remove this function as the parent does it too. |
6a488035 | 355 | $config = CRM_Core_Config::singleton(); |
bf56a083 | 356 | // Set default country from config if no country set. |
a7488080 | 357 | if (empty($defaults["billing_country_id-{$this->_bltID}"])) { |
6a488035 TO |
358 | $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
359 | } | |
5a230af7 | 360 | |
a7488080 | 361 | if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { |
6a488035 TO |
362 | $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; |
363 | } | |
364 | ||
cc8ff73d DG |
365 | $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); |
366 | $defaults = array_merge($defaults, $billingDefaults); | |
6a488035 TO |
367 | } |
368 | ||
369 | if ($this->_id) { | |
370 | $this->_contactID = $defaults['contact_id']; | |
371 | } | |
c1af62ca | 372 | elseif ($this->_contactID) { |
373 | $defaults['contact_id'] = $this->_contactID; | |
374 | } | |
5a230af7 | 375 | |
bf56a083 | 376 | // Set $newCredit variable in template to control whether link to credit card mode is included. |
9be1374d | 377 | $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()); |
5a230af7 | 378 | |
bf56a083 | 379 | // Fix the display of the monetary value, CRM-4038. |
6a488035 | 380 | if (isset($defaults['total_amount'])) { |
0bf170de | 381 | $total_value = $defaults['total_amount']; |
48845185 | 382 | $defaults['total_amount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($total_value); |
049db839 | 383 | if (!empty($defaults['tax_amount'])) { |
b6f65362 | 384 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); |
b99f3e96 | 385 | if (empty($componentDetails['membership']) && empty($componentDetails['participant'])) { |
48845185 | 386 | $defaults['total_amount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($total_value - $defaults['tax_amount']); |
b6f65362 | 387 | } |
049db839 | 388 | } |
6a488035 TO |
389 | } |
390 | ||
be2fb01f | 391 | $amountFields = ['non_deductible_amount', 'fee_amount']; |
3ac2ffb6 | 392 | foreach ($amountFields as $amt) { |
393 | if (isset($defaults[$amt])) { | |
d90d1031 | 394 | $defaults[$amt] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($defaults[$amt]); |
3ac2ffb6 | 395 | } |
6a488035 TO |
396 | } |
397 | ||
a7488080 | 398 | if (empty($defaults['payment_instrument_id'])) { |
794d4fc0 | 399 | $defaults['payment_instrument_id'] = $this->getDefaultPaymentInstrumentId(); |
d96cf288 | 400 | } |
6a488035 | 401 | |
a7488080 | 402 | if (!empty($defaults['is_test'])) { |
6a488035 TO |
403 | $this->assign('is_test', TRUE); |
404 | } | |
405 | ||
6a488035 | 406 | $this->assign('showOption', TRUE); |
bf56a083 | 407 | // For Premium section. |
6a488035 TO |
408 | if ($this->_premiumID) { |
409 | $this->assign('showOption', FALSE); | |
77c21b32 | 410 | $options = $this->_options[$this->_productDAO->product_id] ?? ""; |
6a488035 TO |
411 | if (!$options) { |
412 | $this->assign('showOption', TRUE); | |
413 | } | |
414 | $options_key = CRM_Utils_Array::key($this->_productDAO->product_option, $options); | |
415 | if ($options_key) { | |
be2fb01f | 416 | $defaults['product_name'] = [$this->_productDAO->product_id, trim($options_key)]; |
6a488035 TO |
417 | } |
418 | else { | |
be2fb01f | 419 | $defaults['product_name'] = [$this->_productDAO->product_id]; |
6a488035 TO |
420 | } |
421 | if ($this->_productDAO->fulfilled_date) { | |
a69cf1d1 | 422 | $defaults['fulfilled_date'] = $this->_productDAO->fulfilled_date; |
6a488035 TO |
423 | } |
424 | } | |
425 | ||
426 | if (isset($this->userEmail)) { | |
427 | $this->assign('email', $this->userEmail); | |
428 | } | |
429 | ||
a7488080 | 430 | if (!empty($defaults['is_pay_later'])) { |
6a488035 TO |
431 | $this->assign('is_pay_later', TRUE); |
432 | } | |
433 | $this->assign('contribution_status_id', CRM_Utils_Array::value('contribution_status_id', $defaults)); | |
d682fc35 | 434 | if (!empty($defaults['contribution_status_id']) && in_array( |
435 | CRM_Contribute_PseudoConstant::contributionStatus($defaults['contribution_status_id'], 'name'), | |
436 | // Historically not 'Cancelled' hence not using CRM_Contribute_BAO_Contribution::isContributionStatusNegative. | |
be2fb01f | 437 | ['Refunded', 'Chargeback'] |
d682fc35 | 438 | )) { |
99cdd94d | 439 | $defaults['refund_trxn_id'] = CRM_Core_BAO_FinancialTrxn::getRefundTransactionTrxnID($this->_id); |
440 | } | |
441 | else { | |
77c21b32 | 442 | $defaults['refund_trxn_id'] = $defaults['trxn_id'] ?? NULL; |
99cdd94d | 443 | } |
6a488035 | 444 | |
8cc574cf | 445 | if (!$this->_id && empty($defaults['receive_date'])) { |
b25212e8 | 446 | $defaults['receive_date'] = date('Y-m-d H:i:s'); |
6a488035 TO |
447 | } |
448 | ||
9c1bc317 | 449 | $currency = $defaults['currency'] ?? NULL; |
6a488035 | 450 | $this->assign('currency', $currency); |
bf56a083 | 451 | // Hack to get currency info to the js layer. CRM-11440. |
6a488035 TO |
452 | CRM_Utils_Money::format(1); |
453 | $this->assign('currencySymbol', CRM_Utils_Array::value($currency, CRM_Utils_Money::$_currencySymbols)); | |
454 | $this->assign('totalAmount', CRM_Utils_Array::value('total_amount', $defaults)); | |
455 | ||
bf56a083 | 456 | // Inherit campaign from pledge. |
8cc574cf | 457 | if ($this->_ppID && !empty($this->_pledgeValues['campaign_id'])) { |
6a488035 TO |
458 | $defaults['campaign_id'] = $this->_pledgeValues['campaign_id']; |
459 | } | |
460 | ||
461 | $this->_defaults = $defaults; | |
462 | return $defaults; | |
463 | } | |
464 | ||
465 | /** | |
fe482240 | 466 | * Build the form object. |
f174ed0b | 467 | * |
468 | * @throws \CiviCRM_API3_Exception | |
469 | * @throws \CRM_Core_Exception | |
e47077ce | 470 | * @throws \API_Exception |
6a488035 TO |
471 | */ |
472 | public function buildQuickForm() { | |
5774b54f PF |
473 | if ($this->_id) { |
474 | $this->add('hidden', 'id', $this->_id); | |
475 | } | |
476 | ||
3db2d5ef | 477 | if ($this->_action & CRM_Core_Action::DELETE) { |
be2fb01f | 478 | $this->addButtons([ |
1330f57a SL |
479 | [ |
480 | 'type' => 'next', | |
481 | 'name' => ts('Delete'), | |
482 | 'spacing' => ' ', | |
483 | 'isDefault' => TRUE, | |
484 | ], | |
485 | [ | |
486 | 'type' => 'cancel', | |
487 | 'name' => ts('Cancel'), | |
488 | ], | |
489 | ]); | |
3db2d5ef JP |
490 | return; |
491 | } | |
492 | ||
e3fa3fea | 493 | // FIXME: This probably needs to be done in preprocess |
40c655aa E |
494 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
495 | && $this->_action & CRM_Core_Action::UPDATE | |
b99f3e96 | 496 | && !empty($this->_values['financial_type_id']) |
66af7c48 | 497 | ) { |
e3fa3fea | 498 | $financialTypeID = CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id']); |
59ccc8cd | 499 | CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($this->_id, 'edit'); |
e3fa3fea | 500 | if (!CRM_Core_Permission::check('edit contributions of type ' . $financialTypeID)) { |
beb414cc | 501 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
e3fa3fea E |
502 | } |
503 | } | |
be2fb01f | 504 | $allPanes = []; |
b15c60e1 | 505 | |
6daab7fa | 506 | //tax rate from financialType |
a1e457ff | 507 | $this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates())); |
049db839 | 508 | $this->assign('currencies', json_encode(CRM_Core_OptionGroup::values('currencies_enabled'))); |
6a488035 TO |
509 | |
510 | // build price set form. | |
511 | $buildPriceSet = FALSE; | |
d0df87f2 | 512 | $invoicing = CRM_Invoicing_Utils::isInvoicingEnabled(); |
59691709 EM |
513 | $this->assign('invoicing', CRM_Invoicing_Utils::isInvoicingEnabled()); |
514 | // This is a probably-deprecated approach to partial payments - assign here | |
515 | // & if true it will be overwritten. | |
516 | $this->assign('payNow', FALSE); | |
b15c60e1 | 517 | $buildRecurBlock = FALSE; |
518 | ||
03b412ae PB |
519 | // display tax amount on edit contribution page |
520 | if ($invoicing && $this->_action & CRM_Core_Action::UPDATE && isset($this->_values['tax_amount'])) { | |
a32709be PB |
521 | $this->assign('totalTaxAmount', $this->_values['tax_amount']); |
522 | } | |
523 | ||
6a488035 | 524 | if (empty($this->_lineItems) && |
8cc574cf | 525 | ($this->_priceSetId || !empty($_POST['price_set_id'])) |
6a488035 TO |
526 | ) { |
527 | $buildPriceSet = TRUE; | |
528 | $getOnlyPriceSetElements = TRUE; | |
529 | if (!$this->_priceSetId) { | |
530 | $this->_priceSetId = $_POST['price_set_id']; | |
531 | $getOnlyPriceSetElements = FALSE; | |
532 | } | |
533 | ||
534 | $this->set('priceSetId', $this->_priceSetId); | |
9da8dc8c | 535 | CRM_Price_BAO_PriceSet::buildPriceSet($this); |
6a488035 TO |
536 | |
537 | // get only price set form elements. | |
538 | if ($getOnlyPriceSetElements) { | |
539 | return; | |
540 | } | |
541 | } | |
542 | // use to build form during form rule. | |
543 | $this->assign('buildPriceSet', $buildPriceSet); | |
544 | ||
6a488035 | 545 | $defaults = $this->_values; |
be2fb01f | 546 | $additionalDetailFields = [ |
6a488035 TO |
547 | 'note', |
548 | 'thankyou_date', | |
549 | 'invoice_id', | |
550 | 'non_deductible_amount', | |
551 | 'fee_amount', | |
be2fb01f | 552 | ]; |
6a488035 TO |
553 | foreach ($additionalDetailFields as $key) { |
554 | if (!empty($defaults[$key])) { | |
555 | $defaults['hidden_AdditionalDetail'] = 1; | |
556 | break; | |
557 | } | |
558 | } | |
559 | ||
6a488035 TO |
560 | if ($this->_productDAO) { |
561 | if ($this->_productDAO->product_id) { | |
562 | $defaults['hidden_Premium'] = 1; | |
563 | } | |
564 | } | |
565 | ||
566 | if ($this->_noteID && | |
5888a52d | 567 | !CRM_Utils_System::isNull($this->_values['note']) |
6a488035 TO |
568 | ) { |
569 | $defaults['hidden_AdditionalDetail'] = 1; | |
570 | } | |
571 | ||
be2fb01f | 572 | $paneNames = []; |
b9a90a9c | 573 | if (empty($this->_payNow)) { |
574 | $paneNames[ts('Additional Details')] = 'AdditionalDetail'; | |
575 | } | |
6a488035 TO |
576 | |
577 | //Add Premium pane only if Premium is exists. | |
578 | $dao = new CRM_Contribute_DAO_Product(); | |
579 | $dao->is_active = 1; | |
580 | ||
b9a90a9c | 581 | if ($dao->find(TRUE) && empty($this->_payNow)) { |
6a488035 TO |
582 | $paneNames[ts('Premium Information')] = 'Premium'; |
583 | } | |
584 | ||
a576ecfa | 585 | $this->payment_instrument_id = CRM_Utils_Array::value('payment_instrument_id', $defaults, $this->getDefaultPaymentInstrumentId()); |
3148d4cd JM |
586 | CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE, $this->payment_instrument_id); |
587 | if (!empty($this->_recurPaymentProcessors)) { | |
588 | $buildRecurBlock = TRUE; | |
589 | if ($this->_ppID) { | |
590 | // ppID denotes a pledge payment. | |
591 | foreach ($this->_paymentProcessors as $processor) { | |
592 | if (!empty($processor['is_recur']) && !empty($processor['object']) && $processor['object']->supports('recurContributionsForPledges')) { | |
593 | $buildRecurBlock = TRUE; | |
594 | break; | |
4f3c361d | 595 | } |
3148d4cd | 596 | $buildRecurBlock = FALSE; |
794d4fc0 | 597 | } |
3148d4cd JM |
598 | } |
599 | if ($buildRecurBlock) { | |
600 | CRM_Contribute_Form_Contribution_Main::buildRecur($this); | |
601 | $this->setDefaults(['is_recur' => 0]); | |
602 | $this->assign('buildRecurBlock', TRUE); | |
6a488035 TO |
603 | } |
604 | } | |
b15c60e1 | 605 | $this->addPaymentProcessorSelect(FALSE, $buildRecurBlock); |
6a488035 | 606 | |
6a488035 | 607 | foreach ($paneNames as $name => $type) { |
a5399a39 | 608 | $allPanes[$name] = $this->generatePane($type, $defaults); |
6a488035 | 609 | } |
5e27919e | 610 | |
6a488035 TO |
611 | $qfKey = $this->controller->_key; |
612 | $this->assign('qfKey', $qfKey); | |
613 | $this->assign('allPanes', $allPanes); | |
6a488035 | 614 | |
be2fb01f | 615 | $this->addFormRule(['CRM_Contribute_Form_Contribution', 'formRule'], $this); |
6c75c831 | 616 | |
6a488035 TO |
617 | if ($this->_formType) { |
618 | $this->assign('formType', $this->_formType); | |
619 | return; | |
620 | } | |
621 | ||
622 | $this->applyFilter('__ALL__', 'trim'); | |
5a230af7 | 623 | |
6a488035 TO |
624 | //need to assign custom data type and subtype to the template |
625 | $this->assign('customDataType', 'Contribution'); | |
c6a8691e | 626 | $this->assign('customDataSubType', $this->getFinancialTypeID()); |
6a488035 TO |
627 | $this->assign('entityID', $this->_id); |
628 | ||
d0c83583 | 629 | $contactField = $this->addEntityRef('contact_id', ts('Contributor'), ['create' => TRUE, 'api' => ['extra' => ['email']]], TRUE); |
f174ed0b | 630 | if ($this->_context !== 'standalone') { |
c1af62ca | 631 | $contactField->freeze(); |
6a488035 TO |
632 | } |
633 | ||
634 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); | |
635 | ||
e3fa3fea | 636 | // Check permissions for financial type first |
573fd305 | 637 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $this->_action); |
b19a01c7 | 638 | if (empty($financialTypes)) { |
b55aa3de | 639 | CRM_Core_Error::statusBounce(ts('You do not have all the permissions needed for this page.')); |
b19a01c7 | 640 | } |
6a488035 TO |
641 | $financialType = $this->add('select', 'financial_type_id', |
642 | ts('Financial Type'), | |
be2fb01f | 643 | ['' => ts('- select -')] + $financialTypes, |
79ca865b | 644 | TRUE, |
be2fb01f | 645 | ['onChange' => "CRM.buildCustomData( 'Contribution', this.value );"] |
6a488035 | 646 | ); |
e3fa3fea | 647 | |
d77a0a58 | 648 | $paymentInstrument = FALSE; |
6a488035 | 649 | if (!$this->_mode) { |
c485e792 | 650 | // payment_instrument isn't required in edit and will not be present when payment block is enabled. |
91768280 | 651 | $required = !$this->_id; |
81936ec3 | 652 | $checkPaymentID = array_search('Check', CRM_Contribute_BAO_Contribution::buildOptions('payment_instrument_id', 'validate')); |
6a488035 | 653 | $paymentInstrument = $this->add('select', 'payment_instrument_id', |
4db803dd | 654 | ts('Payment Method'), |
81936ec3 | 655 | ['' => ts('- select -')] + CRM_Contribute_BAO_Contribution::buildOptions('payment_instrument_id', 'create'), |
be2fb01f | 656 | $required, ['onChange' => "return showHideByValue('payment_instrument_id','{$checkPaymentID}','checkNumber','table-row','select',false);"] |
6a488035 TO |
657 | ); |
658 | } | |
659 | ||
be2fb01f | 660 | $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), ['class' => 'twelve'] + $attributes['trxn_id']); |
6a488035 TO |
661 | |
662 | //add receipt for offline contribution | |
663 | $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?')); | |
664 | ||
665 | $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails); | |
666 | ||
6bc9ca96 | 667 | $componentDetails = []; |
7fe7b63d | 668 | if ($this->_id) { |
7fe7b63d | 669 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); |
6a488035 | 670 | } |
e47077ce | 671 | $status = CRM_Contribute_BAO_Contribution_Utils::getContributionStatuses('contribution', $this->getPreviousContributionStatus()); |
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'])) { | |
68cbdd09 | 924 | $order->setOverrideTotalAmount((float) CRM_Utils_Rule::cleanMoney($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); |
8f3a9b39 EM |
1098 | // It actually makes no sense that we would set receive_date in params |
1099 | // for credit card payments.... | |
1100 | $this->assign('receive_date', $this->_params['receive_date'] ?? date('Y-m-d H:i:s')); | |
6a488035 | 1101 | |
f75f0921 EM |
1102 | // Result has all the stuff we need |
1103 | // lets archive it to a financial transaction | |
67256e0c | 1104 | if ($financialType->is_deductible) { |
f75f0921 EM |
1105 | $this->assign('is_deductible', TRUE); |
1106 | $this->set('is_deductible', TRUE); | |
1107 | } | |
be2fb01f | 1108 | $contributionParams = [ |
6b409353 | 1109 | 'id' => $this->_params['contribution_id'] ?? NULL, |
3febe800 | 1110 | 'contact_id' => $contactID, |
1111 | 'line_item' => $lineItem, | |
1112 | 'is_test' => $isTest, | |
6b409353 CW |
1113 | 'campaign_id' => $this->_params['campaign_id'] ?? NULL, |
1114 | 'contribution_page_id' => $this->_params['contribution_page_id'] ?? NULL, | |
3febe800 | 1115 | 'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)), |
6b409353 | 1116 | 'thankyou_date' => $this->_params['thankyou_date'] ?? NULL, |
be2fb01f | 1117 | ]; |
16f3bd02 | 1118 | $contributionParams['payment_instrument_id'] = $this->_paymentProcessor['payment_instrument_id']; |
6a488035 | 1119 | |
2414241f | 1120 | $contribution = $this->processFormContribution( |
7758bd2b EM |
1121 | $this->_params, |
1122 | NULL, | |
3febe800 | 1123 | $contributionParams, |
7758bd2b | 1124 | $financialType, |
7758bd2b | 1125 | FALSE, |
449f4c90 | 1126 | $this->_bltID, |
1127 | CRM_Utils_Array::value('is_recur', $this->_params) | |
7758bd2b | 1128 | ); |
6a488035 | 1129 | |
7758bd2b | 1130 | $paymentParams['contributionID'] = $contribution->id; |
7758bd2b EM |
1131 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; |
1132 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; | |
6a488035 | 1133 | |
7758bd2b EM |
1134 | if ($paymentParams['amount'] > 0.0) { |
1135 | // force a re-get of the payment processor in case the form changed it, CRM-7179 | |
9d8f43b1 EM |
1136 | // NOTE - I expect this is obsolete. |
1137 | $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); | |
7758bd2b | 1138 | try { |
003e9340 | 1139 | $completeStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
7758bd2b EM |
1140 | $result = $payment->doPayment($paymentParams, 'contribute'); |
1141 | $this->assign('trxn_id', $result['trxn_id']); | |
1142 | $contribution->trxn_id = $result['trxn_id']; | |
1143 | /* Our scenarios here are | |
1144 | * 1) the payment failed & an Exception should have been thrown | |
1145 | * 2) the payment succeeded but the payment is not immediate (for example a recurring payment | |
1146 | * with a delayed start) | |
1147 | * 3) the payment succeeded with an immediate payment. | |
1148 | * | |
7c85dc65 | 1149 | * The doPayment function ensures that payment_status_id is always set |
7758bd2b EM |
1150 | * as historically we have had to guess from the context - ie doDirectPayment |
1151 | * = error or success, unless it is a recurring contribution in which case it is pending. | |
1152 | */ | |
003e9340 | 1153 | if ($result['payment_status_id'] == $completeStatusId) { |
760278d3 | 1154 | try { |
be2fb01f | 1155 | civicrm_api3('contribution', 'completetransaction', [ |
760278d3 EM |
1156 | 'id' => $contribution->id, |
1157 | 'trxn_id' => $result['trxn_id'], | |
1158 | 'payment_processor_id' => $this->_paymentProcessor['id'], | |
1159 | 'is_transactional' => FALSE, | |
6b409353 CW |
1160 | 'fee_amount' => $result['fee_amount'] ?? NULL, |
1161 | 'card_type_id' => $paymentParams['card_type_id'] ?? NULL, | |
1162 | 'pan_truncation' => $paymentParams['pan_truncation'] ?? NULL, | |
0af52f61 | 1163 | 'is_email_receipt' => FALSE, |
be2fb01f | 1164 | ]); |
760278d3 EM |
1165 | // This has now been set to 1 in the DB - declare it here also |
1166 | $contribution->contribution_status_id = 1; | |
1167 | } | |
1168 | catch (CiviCRM_API3_Exception $e) { | |
1169 | if ($e->getErrorCode() != 'contribution_completed') { | |
f30b037a | 1170 | \Civi::log()->error('CRM_Contribute_Form_Contribution::processCreditCard CiviCRM_API3_Exception: ' . $e->getMessage()); |
760278d3 EM |
1171 | throw new CRM_Core_Exception('Failed to update contribution in database'); |
1172 | } | |
1173 | } | |
7758bd2b EM |
1174 | } |
1175 | else { | |
1176 | // Save the trxn_id. | |
1177 | $contribution->save(); | |
1178 | } | |
1179 | } | |
1180 | catch (PaymentProcessorException $e) { | |
06d062ce | 1181 | CRM_Contribute_BAO_Contribution::failPayment($contribution->id, $paymentParams['contactID'], $e->getMessage()); |
7758bd2b EM |
1182 | throw new PaymentProcessorException($e->getMessage()); |
1183 | } | |
1184 | } | |
f75f0921 | 1185 | // Send receipt mail. |
e390a377 | 1186 | array_unshift($this->statusMessage, ts('The contribution record has been saved.')); |
f75f0921 | 1187 | if ($contribution->id && !empty($this->_params['is_email_receipt'])) { |
9c1bc317 | 1188 | $this->_params['trxn_id'] = $result['trxn_id'] ?? NULL; |
7126b55c | 1189 | $this->_params['contact_id'] = $contactID; |
f75f0921 | 1190 | $this->_params['contribution_id'] = $contribution->id; |
c52884b2 | 1191 | if (CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $this->_params, TRUE)) { |
3e6a1f4a | 1192 | $this->statusMessage[] = ts('A receipt has been emailed to the contributor.'); |
6a488035 | 1193 | } |
28d44c9f | 1194 | } |
c52884b2 | 1195 | |
5e27919e | 1196 | return $contribution; |
6a488035 | 1197 | } |
5a230af7 | 1198 | |
2414241f EM |
1199 | /** |
1200 | * Process the contribution. | |
1201 | * | |
1202 | * @todo - this form is a copy of the previously shared code on the front | |
1203 | * end form - not all aspects of the code will be relevant to this form. | |
1204 | * | |
1205 | * @param array $params | |
1206 | * @param array $result | |
1207 | * @param array $contributionParams | |
1208 | * Parameters to be passed to contribution create action. | |
1209 | * This differs from params in that we are currently adding params to it and 1) ensuring they are being | |
1210 | * passed consistently & 2) documenting them here. | |
1211 | * - contact_id | |
1212 | * - line_item | |
1213 | * - is_test | |
1214 | * - campaign_id | |
1215 | * - contribution_page_id | |
1216 | * - source | |
1217 | * - payment_type_id | |
1218 | * - thankyou_date (not all forms will set this) | |
1219 | * | |
1220 | * @param CRM_Financial_DAO_FinancialType $financialType | |
1221 | * @param bool $online | |
1222 | * Is the form a front end form? If so set a bunch of unpredictable things that should be passed in from the form. | |
1223 | * | |
1224 | * @param int $billingLocationID | |
1225 | * ID of billing location type. | |
1226 | * @param bool $isRecur | |
1227 | * Is this recurring? | |
1228 | * | |
1229 | * @return \CRM_Contribute_DAO_Contribution | |
1230 | * | |
1231 | * @throws \CRM_Core_Exception | |
1232 | * @throws \CiviCRM_API3_Exception | |
1233 | */ | |
1234 | protected function processFormContribution( | |
1235 | $params, | |
1236 | $result, | |
1237 | $contributionParams, | |
1238 | $financialType, | |
1239 | $online, | |
1240 | $billingLocationID, | |
1241 | $isRecur | |
1242 | ) { | |
1243 | $form = $this; | |
1244 | $transaction = new CRM_Core_Transaction(); | |
1245 | $contactID = $contributionParams['contact_id']; | |
1246 | ||
1247 | $isEmailReceipt = !empty($form->_values['is_email_receipt']); | |
1248 | $isSeparateMembershipPayment = !empty($params['separate_membership_payment']); | |
1249 | $pledgeID = !empty($params['pledge_id']) ? $params['pledge_id'] : $form->_values['pledge_id'] ?? NULL; | |
1250 | if (!$isSeparateMembershipPayment && !empty($form->_values['pledge_block_id']) && | |
1251 | (!empty($params['is_pledge']) || $pledgeID)) { | |
1252 | $isPledge = TRUE; | |
1253 | } | |
1254 | else { | |
1255 | $isPledge = FALSE; | |
1256 | } | |
1257 | ||
1258 | // add these values for the recurringContrib function ,CRM-10188 | |
1259 | $params['financial_type_id'] = $financialType->id; | |
1260 | ||
1261 | $contributionParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $billingLocationID); | |
1262 | ||
1263 | //@todo - this is being set from the form to resolve CRM-10188 - an | |
1264 | // eNotice caused by it not being set @ the front end | |
1265 | // however, we then get it being over-written with null for backend contributions | |
1266 | // a better fix would be to set the values in the respective forms rather than require | |
1267 | // a function being shared by two forms to deal with their respective values | |
1268 | // moving it to the BAO & not taking the $form as a param would make sense here. | |
1269 | if (!isset($params['is_email_receipt']) && $isEmailReceipt) { | |
1270 | $params['is_email_receipt'] = $isEmailReceipt; | |
1271 | } | |
1272 | $params['is_recur'] = $isRecur; | |
1273 | $params['payment_instrument_id'] = $contributionParams['payment_instrument_id'] ?? NULL; | |
1274 | $recurringContributionID = CRM_Contribute_Form_Contribution_Confirm::processRecurringContribution($form, $params, $contactID, $financialType); | |
1275 | ||
1276 | $now = date('YmdHis'); | |
1277 | $receiptDate = $params['receipt_date'] ?? NULL; | |
1278 | if ($isEmailReceipt) { | |
1279 | $receiptDate = $now; | |
1280 | } | |
1281 | ||
1282 | if (isset($params['amount'])) { | |
1283 | $contributionParams = array_merge(CRM_Contribute_Form_Contribution_Confirm::getContributionParams( | |
1284 | $params, $financialType->id, | |
1285 | $result, $receiptDate, | |
1286 | $recurringContributionID), $contributionParams | |
1287 | ); | |
a281a8ea | 1288 | $contributionParams['non_deductible_amount'] = CRM_Contribute_Form_Contribution_Confirm::getNonDeductibleAmount($params, $financialType, FALSE, $form); |
2414241f EM |
1289 | $contributionParams['skipCleanMoney'] = TRUE; |
1290 | // @todo this is the wrong place for this - it should be done as close to form submission | |
1291 | // as possible | |
1292 | $contributionParams['total_amount'] = $params['amount']; | |
1293 | ||
1294 | $contribution = CRM_Contribute_BAO_Contribution::add($contributionParams); | |
1295 | ||
1296 | if (Civi::settings()->get('invoicing')) { | |
1297 | $dataArray = []; | |
1298 | // @todo - interrogate the line items passed in on the params array. | |
1299 | // No reason to assume line items will be set on the form. | |
1300 | foreach ($form->_lineItem as $lineItemKey => $lineItemValue) { | |
1301 | foreach ($lineItemValue as $key => $value) { | |
1302 | if (isset($value['tax_amount']) && isset($value['tax_rate'])) { | |
1303 | if (isset($dataArray[$value['tax_rate']])) { | |
1304 | $dataArray[$value['tax_rate']] = $dataArray[$value['tax_rate']] + $value['tax_amount']; | |
1305 | } | |
1306 | else { | |
1307 | $dataArray[$value['tax_rate']] = $value['tax_amount']; | |
1308 | } | |
1309 | } | |
1310 | } | |
1311 | } | |
1312 | $smarty = CRM_Core_Smarty::singleton(); | |
1313 | $smarty->assign('dataArray', $dataArray); | |
1314 | $smarty->assign('totalTaxAmount', $params['tax_amount'] ?? NULL); | |
1315 | } | |
1316 | ||
1317 | // lets store it in the form variable so postProcess hook can get to this and use it | |
1318 | $form->_contributionID = $contribution->id; | |
1319 | } | |
1320 | ||
1321 | // process soft credit / pcp params first | |
1322 | CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($params, $form); | |
1323 | ||
1324 | //CRM-13981, processing honor contact into soft-credit contribution | |
1325 | CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); | |
1326 | ||
1327 | if ($isPledge) { | |
1328 | $form = CRM_Contribute_Form_Contribution_Confirm::handlePledge($form, $params, $contributionParams, $pledgeID, $contribution, $isEmailReceipt); | |
1329 | } | |
1330 | ||
1331 | if ($online && $contribution) { | |
1332 | CRM_Core_BAO_CustomValueTable::postProcess($params, | |
1333 | 'civicrm_contribution', | |
1334 | $contribution->id, | |
1335 | 'Contribution' | |
1336 | ); | |
1337 | } | |
1338 | elseif ($contribution) { | |
1339 | //handle custom data. | |
1340 | $params['contribution_id'] = $contribution->id; | |
1341 | if (!empty($params['custom']) && | |
1342 | is_array($params['custom']) | |
1343 | ) { | |
1344 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contribution', $contribution->id); | |
1345 | } | |
1346 | } | |
1347 | // Save note | |
1348 | if ($contribution && !empty($params['contribution_note'])) { | |
1349 | $noteParams = [ | |
1350 | 'entity_table' => 'civicrm_contribution', | |
1351 | 'note' => $params['contribution_note'], | |
1352 | 'entity_id' => $contribution->id, | |
1353 | 'contact_id' => $contribution->contact_id, | |
1354 | ]; | |
1355 | ||
1356 | CRM_Core_BAO_Note::add($noteParams, []); | |
1357 | } | |
1358 | ||
1359 | //create contribution activity w/ individual and target | |
1360 | //activity w/ organisation contact id when onbelf, CRM-4027 | |
1361 | $actParams = []; | |
1362 | $targetContactID = NULL; | |
1363 | if (!empty($params['onbehalf_contact_id'])) { | |
1364 | $actParams = [ | |
1365 | 'source_contact_id' => $params['onbehalf_contact_id'], | |
1366 | 'on_behalf' => TRUE, | |
1367 | ]; | |
1368 | $targetContactID = $contribution->contact_id; | |
1369 | } | |
1370 | ||
1371 | // create an activity record | |
1372 | if ($contribution) { | |
1373 | CRM_Activity_BAO_Activity::addActivity($contribution, 'Contribution', $targetContactID, $actParams); | |
1374 | } | |
1375 | ||
1376 | $transaction->commit(); | |
1377 | return $contribution; | |
1378 | } | |
1379 | ||
f75f0921 EM |
1380 | /** |
1381 | * Generate the data to construct a snippet based pane. | |
1382 | * | |
1383 | * This form also assigns the showAdditionalInfo var based on historical code. | |
1384 | * This appears to mean 'there is a pane to show'. | |
1385 | * | |
1386 | * @param string $type | |
1387 | * Type of Pane - this is generally used to determine the function name used to build it | |
1388 | * - e.g CreditCard, AdditionalDetail | |
1389 | * @param array $defaults | |
1390 | * | |
1391 | * @return array | |
1392 | * We aim to further refactor & simplify this but currently | |
1393 | * - the panes array | |
1394 | * - should additional info be shown? | |
1395 | */ | |
1396 | protected function generatePane($type, $defaults) { | |
1397 | $urlParams = "snippet=4&formType={$type}"; | |
1398 | if ($this->_mode) { | |
1399 | $urlParams .= "&mode={$this->_mode}"; | |
6a488035 TO |
1400 | } |
1401 | ||
f75f0921 EM |
1402 | $open = 'false'; |
1403 | if ($type == 'CreditCard' || | |
1404 | $type == 'DirectDebit' | |
1405 | ) { | |
1406 | $open = 'true'; | |
6a488035 | 1407 | } |
6a488035 | 1408 | |
be2fb01f | 1409 | $pane = [ |
f75f0921 EM |
1410 | 'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams), |
1411 | 'open' => $open, | |
1412 | 'id' => $type, | |
be2fb01f | 1413 | ]; |
6a488035 | 1414 | |
f75f0921 EM |
1415 | // See if we need to include this paneName in the current form. |
1416 | if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) || | |
b99f3e96 | 1417 | !empty($defaults["hidden_{$type}"]) |
f75f0921 EM |
1418 | ) { |
1419 | $this->assign('showAdditionalInfo', TRUE); | |
1420 | $pane['open'] = 'true'; | |
6a488035 TO |
1421 | } |
1422 | ||
f75f0921 EM |
1423 | if ($type == 'CreditCard' || $type == 'DirectDebit') { |
1424 | // @todo would be good to align tpl name with form name... | |
1425 | // @todo document why this hidden variable is required. | |
1426 | $this->add('hidden', 'hidden_' . $type, 1); | |
1427 | return $pane; | |
6a488035 TO |
1428 | } |
1429 | else { | |
f75f0921 EM |
1430 | $additionalInfoFormFunction = 'build' . $type; |
1431 | CRM_Contribute_Form_AdditionalInfo::$additionalInfoFormFunction($this); | |
1432 | return $pane; | |
6a488035 | 1433 | } |
f75f0921 | 1434 | } |
6a488035 | 1435 | |
f75f0921 EM |
1436 | /** |
1437 | * Wrapper for unit testing the post process submit function. | |
1438 | * | |
1439 | * (If we expose through api we can get default additions 'for free'). | |
1440 | * | |
1441 | * @param array $params | |
5e27919e | 1442 | * @param int $action |
39f47c0d EM |
1443 | * @param string|null $creditCardMode |
1444 | * | |
12fde6ae | 1445 | * @return CRM_Contribute_BAO_Contribution |
1446 | * | |
1447 | * @throws \CRM_Core_Exception | |
39f47c0d | 1448 | * @throws \CiviCRM_API3_Exception |
12fde6ae | 1449 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
f75f0921 | 1450 | */ |
39f47c0d | 1451 | public function testSubmit($params, $action, $creditCardMode = NULL) { |
be2fb01f CW |
1452 | $defaults = [ |
1453 | 'soft_credit_contact_id' => [], | |
6298b166 | 1454 | 'receive_date' => date('Y-m-d H:i:s'), |
f75f0921 | 1455 | 'receipt_date' => '', |
f75f0921 | 1456 | 'cancel_date' => '', |
43bf07d6 | 1457 | 'hidden_Premium' => 1, |
be2fb01f | 1458 | ]; |
739a8336 | 1459 | $this->_bltID = 5; |
5e27919e | 1460 | if (!empty($params['id'])) { |
be2fb01f | 1461 | $existingContribution = civicrm_api3('contribution', 'getsingle', [ |
5e27919e | 1462 | 'id' => $params['id'], |
be2fb01f | 1463 | ]); |
55e55e84 | 1464 | $this->_id = $params['id']; |
0f07bb06 | 1465 | $this->_values = $existingContribution; |
5d288dc4 | 1466 | if (CRM_Invoicing_Utils::isInvoicingEnabled()) { |
be2fb01f | 1467 | $this->_values['tax_amount'] = civicrm_api3('contribution', 'getvalue', [ |
7c002a4d | 1468 | 'id' => $params['id'], |
1469 | 'return' => 'tax_amount', | |
be2fb01f | 1470 | ]); |
7c002a4d | 1471 | } |
5e27919e EM |
1472 | } |
1473 | else { | |
be2fb01f | 1474 | $existingContribution = []; |
5e27919e EM |
1475 | } |
1476 | ||
1477 | $this->_defaults['contribution_status_id'] = CRM_Utils_Array::value('contribution_status_id', | |
1478 | $existingContribution | |
1479 | ); | |
1480 | ||
1481 | $this->_defaults['total_amount'] = CRM_Utils_Array::value('total_amount', | |
1482 | $existingContribution | |
1483 | ); | |
6a488035 | 1484 | |
39f47c0d EM |
1485 | if ($creditCardMode) { |
1486 | $this->_mode = $creditCardMode; | |
1487 | } | |
1488 | ||
1489 | // Required because processCreditCard calls set method on this. | |
1490 | $_SERVER['REQUEST_METHOD'] = 'GET'; | |
1491 | $this->controller = new CRM_Core_Controller(); | |
1492 | ||
3e6a1f4a EM |
1493 | CRM_Contribute_Form_AdditionalInfo::buildPremium($this); |
1494 | ||
be2fb01f | 1495 | $this->_fields = []; |
0495f30c | 1496 | return $this->submit(array_merge($defaults, $params), $action, CRM_Utils_Array::value('pledge_payment_id', $params)); |
43bf07d6 | 1497 | |
f75f0921 | 1498 | } |
c1cc3e0c | 1499 | |
f75f0921 EM |
1500 | /** |
1501 | * @param array $submittedValues | |
1502 | * | |
bdbc1044 EM |
1503 | * @param int $action |
1504 | * Action constant | |
1505 | * - CRM_Core_Action::UPDATE | |
1506 | * | |
5e27919e EM |
1507 | * @param $pledgePaymentID |
1508 | * | |
12fde6ae | 1509 | * @return \CRM_Contribute_BAO_Contribution |
1510 | * | |
1511 | * @throws \CRM_Core_Exception | |
1512 | * @throws \CiviCRM_API3_Exception | |
1513 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
f75f0921 | 1514 | */ |
31760f81 | 1515 | protected function submit($submittedValues, $action, $pledgePaymentID) { |
f75f0921 | 1516 | $pId = $contribution = $isRelatedId = FALSE; |
3b8e6c3f | 1517 | $this->_params = $submittedValues; |
1518 | $this->beginPostProcess(); | |
a55e39e9 | 1519 | // reassign submitted form values if the any information is formatted via beginPostProcess |
1520 | $submittedValues = $this->_params; | |
3e6a1f4a | 1521 | |
bdbc1044 | 1522 | if (!empty($submittedValues['price_set_id']) && $action & CRM_Core_Action::UPDATE) { |
f75f0921 EM |
1523 | $line = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution'); |
1524 | $lineID = key($line); | |
1525 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('price_field_id', $line[$lineID]), 'price_set_id'); | |
1526 | $quickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); | |
2b308818 DG |
1527 | // Why do we do this? Seems like a like a wrapper for old functionality - but single line price sets & quick |
1528 | // config should be treated the same. | |
f75f0921 EM |
1529 | if ($quickConfig) { |
1530 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_contribution'); | |
1531 | } | |
6a488035 | 1532 | } |
f75f0921 EM |
1533 | |
1534 | // Process price set and get total amount and line items. | |
be2fb01f | 1535 | $lineItem = []; |
9c1bc317 | 1536 | $priceSetId = $submittedValues['price_set_id'] ?? NULL; |
f75f0921 EM |
1537 | if (empty($priceSetId) && !$this->_id) { |
1538 | $this->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name'); | |
1539 | $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); | |
1540 | $fieldID = key($this->_priceSet['fields']); | |
1541 | $fieldValueId = key($this->_priceSet['fields'][$fieldID]['options']); | |
1542 | $this->_priceSet['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount']; | |
1543 | $submittedValues['price_' . $fieldID] = 1; | |
6a488035 | 1544 | } |
6a488035 | 1545 | |
2b308818 DG |
1546 | // Every contribution has a price-set - the only reason it shouldn't be set is if we are dealing with |
1547 | // quick config (very very arguably) & yet we see that this could still be quick config so this should be understood | |
1548 | // as a point of fragility rather than a logical 'if' clause. | |
f75f0921 EM |
1549 | if ($priceSetId) { |
1550 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], | |
bdcbbfea | 1551 | $submittedValues, $lineItem[$priceSetId], $priceSetId); |
f75f0921 | 1552 | // Unset tax amount for offline 'is_quick_config' contribution. |
2b308818 DG |
1553 | // @todo WHY - quick config was conceived as a quick way to configure contribution forms. |
1554 | // this is an example of 'other' functionality being hung off it. | |
f75f0921 EM |
1555 | if ($this->_priceSet['is_quick_config'] && |
1556 | !array_key_exists($submittedValues['financial_type_id'], CRM_Core_PseudoConstant::getTaxRates()) | |
1557 | ) { | |
1558 | unset($submittedValues['tax_amount']); | |
1559 | } | |
9c1bc317 | 1560 | $submittedValues['total_amount'] = $submittedValues['amount'] ?? NULL; |
6a488035 | 1561 | } |
189f0360 | 1562 | |
f75f0921 EM |
1563 | if ($this->_id) { |
1564 | if ($this->_compId) { | |
1565 | if ($this->_context == 'participant') { | |
1566 | $pId = $this->_compId; | |
1567 | } | |
1568 | elseif ($this->_context == 'membership') { | |
1569 | $isRelatedId = TRUE; | |
1570 | } | |
1571 | else { | |
1572 | $pId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'participant_id', 'contribution_id'); | |
1573 | } | |
c1cc3e0c | 1574 | } |
f75f0921 EM |
1575 | else { |
1576 | $contributionDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
1577 | if (array_key_exists('membership', $contributionDetails)) { | |
1578 | $isRelatedId = TRUE; | |
c1cc3e0c | 1579 | } |
f75f0921 EM |
1580 | elseif (array_key_exists('participant', $contributionDetails)) { |
1581 | $pId = $contributionDetails['participant']; | |
c1cc3e0c | 1582 | } |
6a488035 | 1583 | } |
17d83f50 | 1584 | if (!empty($this->_payNow)) { |
1585 | $this->_params['contribution_id'] = $this->_id; | |
1586 | } | |
6a488035 | 1587 | } |
2b308818 | 1588 | |
f75f0921 EM |
1589 | if (!$priceSetId && !empty($submittedValues['total_amount']) && $this->_id) { |
1590 | // CRM-10117 update the line items for participants. | |
2b308818 DG |
1591 | // @todo - if we are completing a contribution then the api call |
1592 | // civicrm_api3('Contribution', 'completetransaction') should take care of | |
1593 | // all associated updates rather than replicating them on the form layer. | |
f75f0921 EM |
1594 | if ($pId) { |
1595 | $entityTable = 'participant'; | |
1596 | $entityID = $pId; | |
1597 | $isRelatedId = FALSE; | |
be2fb01f | 1598 | $participantParams = [ |
f75f0921 EM |
1599 | 'fee_amount' => $submittedValues['total_amount'], |
1600 | 'id' => $entityID, | |
be2fb01f | 1601 | ]; |
f75f0921 EM |
1602 | CRM_Event_BAO_Participant::add($participantParams); |
1603 | if (empty($this->_lineItems)) { | |
77dbdcbc | 1604 | $this->_lineItems[] = CRM_Price_BAO_LineItem::getLineItems($entityID, 'participant', TRUE); |
f75f0921 EM |
1605 | } |
1606 | } | |
1607 | else { | |
1608 | $entityTable = 'contribution'; | |
1609 | $entityID = $this->_id; | |
1610 | } | |
6a488035 | 1611 | |
77dbdcbc | 1612 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, $entityTable, FALSE, TRUE, $isRelatedId); |
f75f0921 EM |
1613 | foreach (array_keys($lineItems) as $id) { |
1614 | $lineItems[$id]['id'] = $id; | |
1615 | } | |
1616 | $itemId = key($lineItems); | |
1617 | if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) { | |
1618 | $this->_priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id'); | |
1619 | } | |
6a488035 | 1620 | |
2b308818 DG |
1621 | // @todo see above - new functionality has been inappropriately added to the quick config concept |
1622 | // and new functionality has been added onto the form layer rather than the BAO :-( | |
f75f0921 | 1623 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
f85b8063 KJ |
1624 | //CRM-16833: Ensure tax is applied only once for membership conribution, when status changed.(e.g Pending to Completed). |
1625 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
b99f3e96 | 1626 | if (empty($componentDetails['membership']) && empty($componentDetails['participant'])) { |
f85b8063 KJ |
1627 | if (!($this->_action & CRM_Core_Action::UPDATE && (($this->_defaults['contribution_status_id'] != $submittedValues['contribution_status_id'])))) { |
1628 | $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('total_amount', $submittedValues)); | |
1629 | } | |
1630 | } | |
6a488035 | 1631 | |
f75f0921 EM |
1632 | // Update line total and total amount with tax on edit. |
1633 | $financialItemsId = CRM_Core_PseudoConstant::getTaxRates(); | |
1634 | if (array_key_exists($submittedValues['financial_type_id'], $financialItemsId)) { | |
1635 | $lineItems[$itemId]['tax_rate'] = $financialItemsId[$submittedValues['financial_type_id']]; | |
1636 | } | |
1637 | else { | |
1638 | $lineItems[$itemId]['tax_rate'] = $lineItems[$itemId]['tax_amount'] = ""; | |
9f907d9f | 1639 | $submittedValues['tax_amount'] = 0; |
f75f0921 EM |
1640 | } |
1641 | if ($lineItems[$itemId]['tax_rate']) { | |
1642 | $lineItems[$itemId]['tax_amount'] = ($lineItems[$itemId]['tax_rate'] / 100) * $lineItems[$itemId]['line_total']; | |
1643 | $submittedValues['total_amount'] = $lineItems[$itemId]['line_total'] + $lineItems[$itemId]['tax_amount']; | |
1644 | $submittedValues['tax_amount'] = $lineItems[$itemId]['tax_amount']; | |
1645 | } | |
1646 | } | |
1647 | // CRM-10117 update the line items for participants. | |
1648 | if (!empty($lineItems[$itemId]['price_field_id'])) { | |
1649 | $lineItem[$this->_priceSetId] = $lineItems; | |
1650 | } | |
6a488035 TO |
1651 | } |
1652 | ||
f75f0921 EM |
1653 | $isQuickConfig = 0; |
1654 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { | |
1655 | $isQuickConfig = 1; | |
6a488035 | 1656 | } |
f75f0921 EM |
1657 | //CRM-11529 for quick config back office transactions |
1658 | //when financial_type_id is passed in form, update the | |
1659 | //line items with the financial type selected in form | |
2b308818 DG |
1660 | // NOTE that this IS still a legitimate use of 'quick-config' for contributions under the current DB but |
1661 | // we should look at having a price field per contribution type & then there would be little reason | |
1662 | // for the back-office contribution form postProcess to know if it is a quick-config form. | |
b99f3e96 | 1663 | if ($isQuickConfig && !empty($submittedValues['financial_type_id']) && !empty($lineItem[$this->_priceSetId]) |
f75f0921 EM |
1664 | ) { |
1665 | foreach ($lineItem[$this->_priceSetId] as &$values) { | |
1666 | $values['financial_type_id'] = $submittedValues['financial_type_id']; | |
1667 | } | |
6a488035 TO |
1668 | } |
1669 | ||
f75f0921 | 1670 | if (!isset($submittedValues['total_amount'])) { |
9c1bc317 | 1671 | $submittedValues['total_amount'] = $this->_values['total_amount'] ?? NULL; |
7c002a4d | 1672 | // Avoid tax amount deduction on edit form and keep it original, because this will lead to error described in CRM-20676 |
1673 | if (!$this->_id) { | |
1674 | $submittedValues['total_amount'] -= CRM_Utils_Array::value('tax_amount', $this->_values, 0); | |
1675 | } | |
6a488035 | 1676 | } |
f75f0921 | 1677 | $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE); |
6a488035 | 1678 | |
0eb9f5f0 | 1679 | $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id'] ?? [])); |
f75f0921 | 1680 | if ($this->_id && count($isEmpty) == 1 && key($isEmpty) == NULL) { |
e11eeed7 | 1681 | civicrm_api3('ContributionSoft', 'get', ['contribution_id' => $this->_id, 'pcp_id' => ['IS NULL' => 1], 'api.ContributionSoft.delete' => 1]); |
f75f0921 | 1682 | } |
c1cc3e0c | 1683 | |
f75f0921 EM |
1684 | // set the contact, when contact is selected |
1685 | if (!empty($submittedValues['contact_id'])) { | |
1686 | $this->_contactID = $submittedValues['contact_id']; | |
c1cc3e0c | 1687 | } |
7a13735b | 1688 | |
3e6a1f4a | 1689 | $formValues = $submittedValues; |
96025800 | 1690 | |
f75f0921 | 1691 | // Credit Card Contribution. |
a5399a39 | 1692 | if ($this->_mode) { |
be2fb01f | 1693 | $paramsSetByPaymentProcessingSubsystem = [ |
bf4253e7 EM |
1694 | 'trxn_id', |
1695 | 'payment_instrument_id', | |
1696 | 'contribution_status_id', | |
1697 | 'cancel_date', | |
1698 | 'cancel_reason', | |
be2fb01f | 1699 | ]; |
bf4253e7 EM |
1700 | foreach ($paramsSetByPaymentProcessingSubsystem as $key) { |
1701 | if (isset($formValues[$key])) { | |
1702 | unset($formValues[$key]); | |
1703 | } | |
1704 | } | |
7126b55c | 1705 | $contribution = $this->processCreditCard($formValues, $lineItem, $this->_contactID); |
bf4253e7 EM |
1706 | foreach ($paramsSetByPaymentProcessingSubsystem as $key) { |
1707 | $formValues[$key] = $contribution->$key; | |
1708 | } | |
a5399a39 | 1709 | } |
f75f0921 EM |
1710 | else { |
1711 | // Offline Contribution. | |
1712 | $submittedValues = $this->unsetCreditCardFields($submittedValues); | |
a5399a39 | 1713 | |
f75f0921 | 1714 | // get the required field value only. |
3e6a1f4a | 1715 | |
66ea9833 | 1716 | $params = [ |
1717 | 'contact_id' => $this->_contactID, | |
1718 | 'currency' => $this->getCurrency($submittedValues), | |
1719 | 'skipCleanMoney' => TRUE, | |
1720 | 'id' => $this->_id, | |
1721 | ]; | |
a5399a39 | 1722 | |
7a13735b | 1723 | //format soft-credit/pcp param first |
1724 | CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($submittedValues, $this); | |
1725 | $params = array_merge($params, $submittedValues); | |
1726 | ||
be2fb01f | 1727 | $fields = [ |
f75f0921 EM |
1728 | 'financial_type_id', |
1729 | 'contribution_status_id', | |
1730 | 'payment_instrument_id', | |
1731 | 'cancel_reason', | |
1732 | 'source', | |
1733 | 'check_number', | |
a55e39e9 | 1734 | 'card_type_id', |
1735 | 'pan_truncation', | |
be2fb01f | 1736 | ]; |
f75f0921 | 1737 | foreach ($fields as $f) { |
9c1bc317 | 1738 | $params[$f] = $formValues[$f] ?? NULL; |
f75f0921 | 1739 | } |
a5399a39 | 1740 | |
46d173ef | 1741 | $params['revenue_recognition_date'] = NULL; |
b3bf6844 AF |
1742 | if (!empty($formValues['revenue_recognition_date'])) { |
1743 | $params['revenue_recognition_date'] = $formValues['revenue_recognition_date']; | |
46d173ef | 1744 | } |
f75f0921 EM |
1745 | |
1746 | if (!empty($formValues['is_email_receipt'])) { | |
1747 | $params['receipt_date'] = date("Y-m-d"); | |
1748 | } | |
1749 | ||
d682fc35 | 1750 | if (CRM_Contribute_BAO_Contribution::isContributionStatusNegative($params['contribution_status_id']) |
f75f0921 EM |
1751 | ) { |
1752 | if (CRM_Utils_System::isNull(CRM_Utils_Array::value('cancel_date', $params))) { | |
b53052cb | 1753 | $params['cancel_date'] = date('YmdHis'); |
f75f0921 EM |
1754 | } |
1755 | } | |
1756 | else { | |
1757 | $params['cancel_date'] = $params['cancel_reason'] = 'null'; | |
1758 | } | |
1759 | ||
1760 | // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996 | |
1761 | // else if contribution_status is changed to Completed is_pay_later flag is changed to 0, CRM-15041 | |
f527e012 | 1762 | if ($params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending')) { |
f75f0921 EM |
1763 | $params['is_pay_later'] = 1; |
1764 | } | |
f527e012 | 1765 | elseif ($params['contribution_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed')) { |
a9f512d8 | 1766 | // @todo - if the contribution is new then it should be Pending status & then we use |
1767 | // Payment.create to update to Completed. | |
f75f0921 EM |
1768 | $params['is_pay_later'] = 0; |
1769 | } | |
1770 | ||
f75f0921 EM |
1771 | // Add Additional common information to formatted params. |
1772 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($formValues, $params, $this); | |
1773 | if ($pId) { | |
1774 | $params['contribution_mode'] = 'participant'; | |
1775 | $params['participant_id'] = $pId; | |
1776 | $params['skipLineItem'] = 1; | |
1777 | } | |
f75f0921 | 1778 | $params['line_item'] = $lineItem; |
9c1bc317 | 1779 | $params['payment_processor_id'] = $params['payment_processor'] = $this->_paymentProcessor['id'] ?? NULL; |
2e813c49 | 1780 | $params['tax_amount'] = CRM_Utils_Array::value('tax_amount', $submittedValues, CRM_Utils_Array::value('tax_amount', $this->_values)); |
f75f0921 EM |
1781 | //create contribution. |
1782 | if ($isQuickConfig) { | |
1783 | $params['is_quick_config'] = 1; | |
1784 | } | |
07dd658b | 1785 | $params['non_deductible_amount'] = $this->calculateNonDeductibleAmount($params, $formValues); |
f75f0921 | 1786 | |
7fd21004 | 1787 | // we are already handling note below, so to avoid duplicate notes against $contribution |
1788 | if (!empty($params['note']) && !empty($submittedValues['note'])) { | |
1789 | unset($params['note']); | |
1790 | } | |
66ea9833 | 1791 | $contribution = CRM_Contribute_BAO_Contribution::create($params); |
f75f0921 EM |
1792 | |
1793 | // process associated membership / participant, CRM-4395 | |
bdbc1044 | 1794 | if ($contribution->id && $action & CRM_Core_Action::UPDATE) { |
a9f512d8 | 1795 | // @todo use Payment.create to do this, remove transitioncomponents function |
1796 | // if contribution is being created with a completed status it should be | |
1797 | // created pending & then Payment.create adds the payment | |
436f3267 | 1798 | CRM_Contribute_BAO_Contribution::transitionComponents([ |
1799 | 'contribution_id' => $contribution->id, | |
1800 | 'contribution_status_id' => $contribution->contribution_status_id, | |
1801 | 'previous_contribution_status_id' => $this->_values['contribution_status_id'] ?? NULL, | |
1802 | 'receive_date' => $contribution->receive_date, | |
1803 | ]); | |
f75f0921 EM |
1804 | } |
1805 | ||
3e6a1f4a | 1806 | array_unshift($this->statusMessage, ts('The contribution record has been saved.')); |
f75f0921 | 1807 | |
6d4f5b21 | 1808 | $this->invoicingPostProcessHook($submittedValues, $action, $lineItem); |
f75f0921 EM |
1809 | |
1810 | //send receipt mail. | |
1811 | if ($contribution->id && !empty($formValues['is_email_receipt'])) { | |
1812 | $formValues['contact_id'] = $this->_contactID; | |
1813 | $formValues['contribution_id'] = $contribution->id; | |
1814 | ||
1815 | $formValues += CRM_Contribute_BAO_ContributionSoft::getSoftContribution($contribution->id); | |
1816 | ||
1817 | // to get 'from email id' for send receipt | |
9c1bc317 | 1818 | $this->fromEmailId = $formValues['from_email_address'] ?? NULL; |
5e27919e | 1819 | if (CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $formValues)) { |
3e6a1f4a | 1820 | $this->statusMessage[] = ts('A receipt has been emailed to the contributor.'); |
5e27919e | 1821 | } |
f75f0921 EM |
1822 | } |
1823 | ||
7e5961cf EM |
1824 | $this->statusMessageTitle = ts('Saved'); |
1825 | ||
f75f0921 | 1826 | } |
5e27919e | 1827 | |
99af166d | 1828 | if ($contribution->id && isset($formValues['product_name'][0])) { |
2dcbf765 | 1829 | CRM_Contribute_Form_AdditionalInfo::processPremium($submittedValues, $contribution->id, |
945f423d EM |
1830 | $this->_premiumID, $this->_options |
1831 | ); | |
1832 | } | |
1833 | ||
dce847cf | 1834 | if ($contribution->id && array_key_exists('note', $submittedValues)) { |
945f423d EM |
1835 | CRM_Contribute_Form_AdditionalInfo::processNote($submittedValues, $this->_contactID, $contribution->id, $this->_noteID); |
1836 | } | |
1837 | ||
3e6a1f4a | 1838 | CRM_Core_Session::setStatus(implode(' ', $this->statusMessage), $this->statusMessageTitle, 'success'); |
7e5961cf | 1839 | |
5e27919e EM |
1840 | CRM_Contribute_BAO_Contribution::updateRelatedPledge( |
1841 | $action, | |
1842 | $pledgePaymentID, | |
1843 | $contribution->id, | |
3ec3dcbe | 1844 | ($formValues['option_type'] ?? 0) == 2, |
5e27919e | 1845 | $formValues['total_amount'], |
3e6a1f4a | 1846 | CRM_Utils_Array::value('total_amount', $this->_defaults), |
5e27919e | 1847 | $formValues['contribution_status_id'], |
3e6a1f4a | 1848 | CRM_Utils_Array::value('contribution_status_id', $this->_defaults) |
5e27919e EM |
1849 | ); |
1850 | return $contribution; | |
2e03ce51 EM |
1851 | } |
1852 | ||
6d4f5b21 EM |
1853 | /** |
1854 | * Assign tax calculations to contribution receipts. | |
1855 | * | |
1856 | * @param array $submittedValues | |
1857 | * @param int $action | |
1858 | * @param array $lineItem | |
1859 | */ | |
1860 | protected function invoicingPostProcessHook($submittedValues, $action, $lineItem) { | |
fe4d1285 | 1861 | if (!Civi::settings()->get('invoicing')) { |
6d4f5b21 EM |
1862 | return; |
1863 | } | |
be2fb01f | 1864 | $taxRate = []; |
6d4f5b21 | 1865 | $getTaxDetails = FALSE; |
f1662cbd | 1866 | |
1867 | foreach ($lineItem as $key => $value) { | |
6d4f5b21 EM |
1868 | foreach ($value as $v) { |
1869 | if (isset($taxRate[(string) CRM_Utils_Array::value('tax_rate', $v)])) { | |
1870 | $taxRate[(string) $v['tax_rate']] = $taxRate[(string) $v['tax_rate']] + CRM_Utils_Array::value('tax_amount', $v); | |
1871 | } | |
1872 | else { | |
1873 | if (isset($v['tax_rate'])) { | |
9c1bc317 | 1874 | $taxRate[(string) $v['tax_rate']] = $v['tax_amount'] ?? NULL; |
6d4f5b21 EM |
1875 | $getTaxDetails = TRUE; |
1876 | } | |
1877 | } | |
1878 | } | |
1879 | } | |
1880 | ||
1881 | if ($action & CRM_Core_Action::UPDATE) { | |
1882 | if (isset($submittedValues['tax_amount'])) { | |
1883 | $totalTaxAmount = $submittedValues['tax_amount']; | |
1884 | } | |
1885 | else { | |
1886 | $totalTaxAmount = $this->_values['tax_amount']; | |
1887 | } | |
1888 | $this->assign('totalTaxAmount', $totalTaxAmount); | |
1889 | $this->assign('dataArray', $taxRate); | |
1890 | } | |
1891 | else { | |
1892 | if (!empty($submittedValues['price_set_id'])) { | |
1893 | $this->assign('totalTaxAmount', $submittedValues['tax_amount']); | |
1894 | $this->assign('getTaxDetails', $getTaxDetails); | |
1895 | $this->assign('dataArray', $taxRate); | |
530f1e12 | 1896 | $this->assign('taxTerm', Civi::settings()->get('tax_term')); |
6d4f5b21 EM |
1897 | } |
1898 | else { | |
1899 | $this->assign('totalTaxAmount', CRM_Utils_Array::value('tax_amount', $submittedValues)); | |
1900 | } | |
1901 | } | |
1902 | } | |
1903 | ||
07dd658b EM |
1904 | /** |
1905 | * Calculate non deductible amount. | |
1906 | * | |
0e480632 | 1907 | * @see https://issues.civicrm.org/jira/browse/CRM-11956 |
07dd658b EM |
1908 | * if non_deductible_amount exists i.e. Additional Details field set was opened [and staff typed something] - |
1909 | * if non_deductible_amount does NOT exist - then calculate it depending on: | |
1910 | * $financialType->is_deductible and whether there is a product (premium). | |
1911 | * | |
1912 | * @param $params | |
1913 | * @param $formValues | |
1914 | * | |
1915 | * @return array | |
1916 | */ | |
1917 | protected function calculateNonDeductibleAmount($params, $formValues) { | |
1918 | if (!empty($params['non_deductible_amount'])) { | |
1919 | return $params['non_deductible_amount']; | |
1920 | } | |
5afce5ad | 1921 | |
9c1bc317 | 1922 | $priceSetId = $params['price_set_id'] ?? NULL; |
5afce5ad | 1923 | // return non-deductible amount if it is set at the price field option level |
05465712 | 1924 | if ($priceSetId && !empty($params['line_item'])) { |
1925 | $nonDeductibleAmount = CRM_Price_BAO_PriceSet::getNonDeductibleAmountFromPriceSet($priceSetId, $params['line_item']); | |
1926 | if (!empty($nonDeductibleAmount)) { | |
5afce5ad | 1927 | return $nonDeductibleAmount; |
1928 | } | |
1929 | } | |
07dd658b | 1930 | |
f70eb51f EM |
1931 | $financialType = new CRM_Financial_DAO_FinancialType(); |
1932 | $financialType->id = $params['financial_type_id']; | |
11829025 | 1933 | $financialType->find(TRUE); |
07dd658b | 1934 | |
f70eb51f EM |
1935 | if ($financialType->is_deductible) { |
1936 | ||
1937 | if (isset($formValues['product_name'][0])) { | |
1938 | $selectProduct = $formValues['product_name'][0]; | |
1939 | } | |
1940 | // if there is a product - compare the value to the contribution amount | |
1941 | if (isset($selectProduct)) { | |
1942 | $productDAO = new CRM_Contribute_DAO_Product(); | |
1943 | $productDAO->id = $selectProduct; | |
1944 | $productDAO->find(TRUE); | |
1945 | // product value exceeds contribution amount | |
1946 | if ($params['total_amount'] < $productDAO->price) { | |
1947 | return $params['total_amount']; | |
07dd658b | 1948 | } |
f70eb51f | 1949 | // product value does NOT exceed contribution amount |
07dd658b | 1950 | else { |
f70eb51f | 1951 | return $productDAO->price; |
07dd658b EM |
1952 | } |
1953 | } | |
f70eb51f | 1954 | // contribution is deductible - but there is no product |
07dd658b | 1955 | else { |
f70eb51f | 1956 | return '0.00'; |
07dd658b EM |
1957 | } |
1958 | } | |
f70eb51f EM |
1959 | // contribution is NOT deductible |
1960 | else { | |
1961 | return $params['total_amount']; | |
1962 | } | |
1963 | ||
07dd658b EM |
1964 | return 0; |
1965 | } | |
1966 | ||
c6a8691e | 1967 | /** |
1968 | * Get the financial Type ID for the contribution either from the submitted values or from the contribution values if possible. | |
1969 | * | |
1970 | * This is important for dev/core#1728 - ie ensure that if we are returned to the form for a form | |
1971 | * error that any custom fields based on the selected financial type are loaded. | |
1972 | * | |
1973 | * @return int | |
1974 | */ | |
1975 | protected function getFinancialTypeID() { | |
1976 | if (!empty($this->_submitValues['financial_type_id'])) { | |
1977 | return $this->_submitValues['financial_type_id']; | |
1978 | } | |
1979 | if (!empty($this->_values['financial_type_id'])) { | |
1980 | return $this->_values['financial_type_id']; | |
1981 | } | |
1982 | } | |
1983 | ||
e5777079 MW |
1984 | /** |
1985 | * Set context in session | |
1986 | */ | |
1987 | public function setUserContext(): void { | |
1988 | $session = CRM_Core_Session::singleton(); | |
1989 | $buttonName = $this->controller->getButtonName(); | |
1990 | if ($this->_context == 'standalone') { | |
1991 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
1992 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contribute/add', | |
1993 | 'reset=1&action=add&context=standalone' | |
1994 | )); | |
1995 | } | |
1996 | else { | |
1997 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
1998 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" | |
1999 | )); | |
2000 | } | |
2001 | } | |
2002 | elseif ($this->_context == 'contribution' && $this->_mode && $buttonName == $this->getButtonName('upload', 'new')) { | |
2003 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', | |
2004 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}" | |
2005 | )); | |
2006 | } | |
2007 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
2008 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', | |
2009 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}" | |
2010 | )); | |
2011 | } | |
2012 | } | |
2013 | ||
e47077ce EM |
2014 | /** |
2015 | * Get the contribution ID. | |
2016 | * | |
2017 | * @return int|null | |
2018 | */ | |
2019 | protected function getContributionID(): ?int { | |
2020 | return $this->_id; | |
2021 | } | |
2022 | ||
2023 | /** | |
2024 | * Get the selected contribution status. | |
2025 | * | |
21adcaec | 2026 | * @return string|null |
e47077ce EM |
2027 | * |
2028 | * @throws \API_Exception | |
2029 | */ | |
21adcaec EM |
2030 | protected function getPreviousContributionStatus(): ?string { |
2031 | if (!$this->getContributionID()) { | |
2032 | return NULL; | |
2033 | } | |
e47077ce EM |
2034 | if (!$this->previousContributionStatus) { |
2035 | $this->previousContributionStatus = Contribution::get(FALSE) | |
2036 | ->addWhere('id', '=', $this->getContributionID()) | |
2037 | ->addSelect('contribution_status_id:name') | |
2038 | ->execute() | |
2039 | ->first()['contribution_status_id:name']; | |
2040 | } | |
2041 | return $this->previousContributionStatus; | |
2042 | } | |
2043 | ||
6a488035 | 2044 | } |