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