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