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