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