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