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