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 | /** |
100fef9d | 165 | * Array of the payment fields to be displayed in the payment fieldset (pane) in billingBlock.tpl |
459091e1 EM |
166 | * this contains all the information to describe these fields from quickform. See CRM_Core_Form_Payment getPaymentFormFieldsMetadata |
167 | * | |
168 | * @var array | |
169 | */ | |
353ffa53 | 170 | public $_paymentFields = array(); |
4691b077 | 171 | /** |
28a4070b | 172 | * Logged in user's email. |
4691b077 EM |
173 | * @var string |
174 | */ | |
175 | public $userEmail; | |
176 | ||
177 | /** | |
178 | * Price set ID | |
179 | * @var integer | |
180 | */ | |
181 | public $_priceSetId; | |
182 | ||
4691b077 EM |
183 | /** |
184 | * Price set as an array | |
185 | * @var array | |
186 | */ | |
187 | public $_priceSet; | |
188 | ||
d77a0a58 | 189 | /** |
100fef9d | 190 | * Form defaults |
d77a0a58 EM |
191 | * @todo can we define this a as protected? can we define higher up the chain |
192 | * @var array | |
193 | */ | |
194 | public $_defaults; | |
195 | ||
196 | /** | |
197 | * User display name | |
198 | * | |
199 | * @var string | |
200 | */ | |
201 | public $userDisplayName; | |
353ffa53 | 202 | |
7e5961cf EM |
203 | /** |
204 | * Status message to be shown to the user. | |
205 | * | |
3e6a1f4a | 206 | * @var array |
7e5961cf | 207 | */ |
3e6a1f4a | 208 | protected $statusMessage = array(); |
7e5961cf EM |
209 | |
210 | /** | |
211 | * Status message title to be shown to the user. | |
212 | * | |
213 | * Generally the payment processor message title is 'Complete' and offline is 'Saved' | |
214 | * although this might not be a good fit with the broad range of processors. | |
215 | * | |
216 | * @var string | |
217 | */ | |
218 | protected $statusMessageTitle; | |
219 | ||
6a488035 | 220 | /** |
22ab45ae | 221 | * Set variables up before form is built. |
6a488035 TO |
222 | */ |
223 | public function preProcess() { | |
28a4070b | 224 | // Check permission for action. |
6a488035 | 225 | if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { |
0499b0ad | 226 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 | 227 | } |
bf56a083 EM |
228 | // @todo - if anyone ever figures out what this _cdType subroutine is about |
229 | // (or even if it still applies) please add comments!!!!!!!!!! | |
6a488035 | 230 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); |
6a488035 TO |
231 | $this->assign('cdType', FALSE); |
232 | if ($this->_cdType) { | |
233 | $this->assign('cdType', TRUE); | |
234 | CRM_Custom_Form_CustomData::preProcess($this); | |
235 | return; | |
236 | } | |
237 | ||
42e8b05c EM |
238 | parent::preProcess(); |
239 | ||
6a488035 TO |
240 | $this->_formType = CRM_Utils_Array::value('formType', $_GET); |
241 | ||
28a4070b | 242 | // Get price set id. |
6a488035 TO |
243 | $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET); |
244 | $this->set('priceSetId', $this->_priceSetId); | |
245 | $this->assign('priceSetId', $this->_priceSetId); | |
246 | ||
28a4070b | 247 | // Get the pledge payment id |
6a488035 TO |
248 | $this->_ppID = CRM_Utils_Request::retrieve('ppid', 'Positive', $this); |
249 | ||
6a488035 TO |
250 | $this->assign('action', $this->_action); |
251 | ||
28a4070b | 252 | // Get the contribution id if update |
6a488035 TO |
253 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
254 | if (!empty($this->_id)) { | |
255 | $this->assign('contribID', $this->_id); | |
256 | } | |
257 | ||
258 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
259 | $this->assign('context', $this->_context); | |
260 | ||
261 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Positive', $this); | |
262 | ||
263 | $this->_compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this); | |
264 | ||
265 | //set the contribution mode. | |
266 | $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); | |
267 | ||
268 | $this->assign('contributionMode', $this->_mode); | |
459091e1 EM |
269 | if ($this->_action & CRM_Core_Action::DELETE) { |
270 | return; | |
271 | } | |
6a488035 TO |
272 | |
273 | $this->assign('showCheckNumber', TRUE); | |
274 | ||
275 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); | |
cc984198 | 276 | $this->assignPaymentRelatedVariables(); |
6a488035 | 277 | |
d77a0a58 | 278 | if (in_array('CiviPledge', CRM_Core_Config::singleton()->enableComponents) && !$this->_formType) { |
6a488035 TO |
279 | $this->preProcessPledge(); |
280 | } | |
281 | ||
d5397f2f PJ |
282 | if ($this->_id) { |
283 | $this->showRecordLinkMesssage($this->_id); | |
284 | } | |
6a488035 TO |
285 | $this->_values = array(); |
286 | ||
bf56a083 | 287 | // Current contribution id. |
6a488035 TO |
288 | if ($this->_id) { |
289 | $this->assignPremiumProduct($this->_id); | |
290 | $this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values); | |
291 | } | |
292 | ||
293 | // when custom data is included in this page | |
a7488080 | 294 | if (!empty($_POST['hidden_custom'])) { |
6a488035 TO |
295 | $this->applyCustomData('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id); |
296 | } | |
297 | ||
298 | $this->_lineItems = array(); | |
299 | if ($this->_id) { | |
300 | if (!empty($this->_compId) && $this->_compContext == 'participant') { | |
301 | $this->assign('compId', $this->_compId); | |
302 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_compId); | |
303 | } | |
304 | else { | |
0b71718c | 305 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution', 1, TRUE, TRUE); |
6a488035 TO |
306 | } |
307 | empty($lineItem) ? NULL : $this->_lineItems[] = $lineItem; | |
308 | } | |
309 | ||
310 | $this->assign('lineItem', empty($this->_lineItems) ? FALSE : $this->_lineItems); | |
311 | ||
312 | // Set title | |
e2046b33 | 313 | if ($this->_mode) { |
bc44463a | 314 | CRM_Financial_Form_Payment::addCreditCardJs(); |
e2046b33 CW |
315 | $this->setPageTitle($this->_ppID ? ts('Credit Card Pledge Payment') : ts('Credit Card Contribution')); |
316 | } | |
317 | else { | |
318 | $this->setPageTitle($this->_ppID ? ts('Pledge Payment') : ts('Contribution')); | |
6a488035 | 319 | } |
1421174e | 320 | |
321 | if ($this->_id) { | |
322 | CRM_Contribute_Form_SoftCredit::preprocess($this); | |
323 | } | |
6a488035 TO |
324 | } |
325 | ||
28a4070b EM |
326 | /** |
327 | * Set default values. | |
328 | * | |
329 | * @return array | |
330 | */ | |
00be9182 | 331 | public function setDefaultValues() { |
6a488035 | 332 | if ($this->_cdType) { |
bf56a083 EM |
333 | // @todo document when this function would be called in this way |
334 | // (and whether it is valid or an overloading of this form). | |
6a488035 TO |
335 | return CRM_Custom_Form_CustomData::setDefaultValues($this); |
336 | } | |
337 | ||
338 | $defaults = $this->_values; | |
339 | ||
bf56a083 | 340 | // Set defaults for pledge payment. |
6a488035 TO |
341 | if ($this->_ppID) { |
342 | $defaults['total_amount'] = CRM_Utils_Array::value('scheduled_amount', $this->_pledgeValues['pledgePayment']); | |
6a488035 TO |
343 | $defaults['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $this->_pledgeValues); |
344 | $defaults['currency'] = CRM_Utils_Array::value('currency', $this->_pledgeValues); | |
345 | $defaults['option_type'] = 1; | |
346 | } | |
347 | ||
6a488035 TO |
348 | if ($this->_action & CRM_Core_Action::DELETE) { |
349 | return $defaults; | |
350 | } | |
351 | ||
7fe22c48 EM |
352 | $defaults['frequency_interval'] = 1; |
353 | $defaults['frequency_unit'] = 'month'; | |
354 | ||
bf56a083 | 355 | // Set soft credit defaults. |
17db9f82 | 356 | CRM_Contribute_Form_SoftCredit::setDefaultValues($defaults, $this); |
dfbad3f7 | 357 | |
6a488035 TO |
358 | if ($this->_mode) { |
359 | $config = CRM_Core_Config::singleton(); | |
bf56a083 | 360 | // Set default country from config if no country set. |
a7488080 | 361 | if (empty($defaults["billing_country_id-{$this->_bltID}"])) { |
6a488035 TO |
362 | $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
363 | } | |
5a230af7 | 364 | |
a7488080 | 365 | if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { |
6a488035 TO |
366 | $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; |
367 | } | |
368 | ||
cc8ff73d DG |
369 | $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); |
370 | $defaults = array_merge($defaults, $billingDefaults); | |
6a488035 TO |
371 | } |
372 | ||
373 | if ($this->_id) { | |
374 | $this->_contactID = $defaults['contact_id']; | |
375 | } | |
5a230af7 | 376 | |
bf56a083 | 377 | // Set $newCredit variable in template to control whether link to credit card mode is included. |
9be1374d | 378 | $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()); |
5a230af7 | 379 | |
bf56a083 | 380 | // Fix the display of the monetary value, CRM-4038. |
6a488035 | 381 | if (isset($defaults['total_amount'])) { |
049db839 | 382 | if (!empty($defaults['tax_amount'])) { |
b6f65362 | 383 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); |
384 | if (!(CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails))) { | |
385 | $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'] - $defaults['tax_amount'], NULL, '%a'); | |
386 | } | |
049db839 | 387 | } |
388 | else { | |
389 | $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'], NULL, '%a'); | |
390 | } | |
6a488035 TO |
391 | } |
392 | ||
393 | if (isset($defaults['non_deductible_amount'])) { | |
394 | $defaults['non_deductible_amount'] = CRM_Utils_Money::format($defaults['non_deductible_amount'], NULL, '%a'); | |
395 | } | |
396 | ||
397 | if (isset($defaults['fee_amount'])) { | |
398 | $defaults['fee_amount'] = CRM_Utils_Money::format($defaults['fee_amount'], NULL, '%a'); | |
399 | } | |
400 | ||
401 | if (isset($defaults['net_amount'])) { | |
402 | $defaults['net_amount'] = CRM_Utils_Money::format($defaults['net_amount'], NULL, '%a'); | |
403 | } | |
404 | ||
405 | if ($this->_contributionType) { | |
406 | $defaults['financial_type_id'] = $this->_contributionType; | |
407 | } | |
133e2c99 | 408 | |
a7488080 | 409 | if (empty($defaults['payment_instrument_id'])) { |
d96cf288 DG |
410 | $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')); |
411 | } | |
6a488035 | 412 | |
a7488080 | 413 | if (!empty($defaults['is_test'])) { |
6a488035 TO |
414 | $this->assign('is_test', TRUE); |
415 | } | |
416 | ||
6a488035 | 417 | $this->assign('showOption', TRUE); |
bf56a083 | 418 | // For Premium section. |
6a488035 TO |
419 | if ($this->_premiumID) { |
420 | $this->assign('showOption', FALSE); | |
421 | $options = isset($this->_options[$this->_productDAO->product_id]) ? $this->_options[$this->_productDAO->product_id] : ""; | |
422 | if (!$options) { | |
423 | $this->assign('showOption', TRUE); | |
424 | } | |
425 | $options_key = CRM_Utils_Array::key($this->_productDAO->product_option, $options); | |
426 | if ($options_key) { | |
427 | $defaults['product_name'] = array($this->_productDAO->product_id, trim($options_key)); | |
428 | } | |
429 | else { | |
430 | $defaults['product_name'] = array($this->_productDAO->product_id); | |
431 | } | |
432 | if ($this->_productDAO->fulfilled_date) { | |
433 | list($defaults['fulfilled_date']) = CRM_Utils_Date::setDateDefaults($this->_productDAO->fulfilled_date); | |
434 | } | |
435 | } | |
436 | ||
437 | if (isset($this->userEmail)) { | |
438 | $this->assign('email', $this->userEmail); | |
439 | } | |
440 | ||
a7488080 | 441 | if (!empty($defaults['is_pay_later'])) { |
6a488035 TO |
442 | $this->assign('is_pay_later', TRUE); |
443 | } | |
444 | $this->assign('contribution_status_id', CRM_Utils_Array::value('contribution_status_id', $defaults)); | |
445 | ||
22ab45ae EM |
446 | $dates = array( |
447 | 'receive_date', | |
448 | 'receipt_date', | |
449 | 'cancel_date', | |
450 | 'thankyou_date', | |
28a4070b | 451 | ); |
6a488035 | 452 | foreach ($dates as $key) { |
a7488080 | 453 | if (!empty($defaults[$key])) { |
006389de TO |
454 | list($defaults[$key], $defaults[$key . '_time']) |
455 | = CRM_Utils_Date::setDateDefaults(CRM_Utils_Array::value($key, $defaults), 'activityDateTime'); | |
6a488035 TO |
456 | } |
457 | } | |
458 | ||
8cc574cf | 459 | if (!$this->_id && empty($defaults['receive_date'])) { |
6a488035 TO |
460 | list($defaults['receive_date'], |
461 | $defaults['receive_date_time'] | |
462 | ) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); | |
463 | } | |
464 | ||
465 | $this->assign('receive_date', CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $defaults), | |
466 | CRM_Utils_Array::value('receive_date_time', $defaults) | |
467 | )); | |
468 | $currency = CRM_Utils_Array::value('currency', $defaults); | |
469 | $this->assign('currency', $currency); | |
bf56a083 | 470 | // Hack to get currency info to the js layer. CRM-11440. |
6a488035 TO |
471 | CRM_Utils_Money::format(1); |
472 | $this->assign('currencySymbol', CRM_Utils_Array::value($currency, CRM_Utils_Money::$_currencySymbols)); | |
473 | $this->assign('totalAmount', CRM_Utils_Array::value('total_amount', $defaults)); | |
474 | ||
bf56a083 | 475 | // Inherit campaign from pledge. |
8cc574cf | 476 | if ($this->_ppID && !empty($this->_pledgeValues['campaign_id'])) { |
6a488035 TO |
477 | $defaults['campaign_id'] = $this->_pledgeValues['campaign_id']; |
478 | } | |
479 | ||
480 | $this->_defaults = $defaults; | |
481 | return $defaults; | |
482 | } | |
483 | ||
484 | /** | |
fe482240 | 485 | * Build the form object. |
6a488035 TO |
486 | */ |
487 | public function buildQuickForm() { | |
3e6a1f4a | 488 | |
dc913073 | 489 | //@todo document the purpose of cdType (if still in use) |
6a488035 TO |
490 | if ($this->_cdType) { |
491 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
492 | return; | |
493 | } | |
5032bf07 | 494 | $allPanes = array(); |
5e27919e | 495 | $recurJs = NULL; |
6daab7fa | 496 | //tax rate from financialType |
a1e457ff | 497 | $this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates())); |
049db839 | 498 | $this->assign('currencies', json_encode(CRM_Core_OptionGroup::values('currencies_enabled'))); |
6a488035 TO |
499 | |
500 | // build price set form. | |
501 | $buildPriceSet = FALSE; | |
353ffa53 | 502 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
03b412ae | 503 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); |
95f2107c | 504 | $this->assign('invoicing', $invoicing); |
a32709be | 505 | |
03b412ae PB |
506 | // display tax amount on edit contribution page |
507 | if ($invoicing && $this->_action & CRM_Core_Action::UPDATE && isset($this->_values['tax_amount'])) { | |
a32709be PB |
508 | $this->assign('totalTaxAmount', $this->_values['tax_amount']); |
509 | } | |
510 | ||
6a488035 | 511 | if (empty($this->_lineItems) && |
8cc574cf | 512 | ($this->_priceSetId || !empty($_POST['price_set_id'])) |
6a488035 TO |
513 | ) { |
514 | $buildPriceSet = TRUE; | |
515 | $getOnlyPriceSetElements = TRUE; | |
516 | if (!$this->_priceSetId) { | |
517 | $this->_priceSetId = $_POST['price_set_id']; | |
518 | $getOnlyPriceSetElements = FALSE; | |
519 | } | |
520 | ||
521 | $this->set('priceSetId', $this->_priceSetId); | |
9da8dc8c | 522 | CRM_Price_BAO_PriceSet::buildPriceSet($this); |
6a488035 TO |
523 | |
524 | // get only price set form elements. | |
525 | if ($getOnlyPriceSetElements) { | |
526 | return; | |
527 | } | |
528 | } | |
529 | // use to build form during form rule. | |
530 | $this->assign('buildPriceSet', $buildPriceSet); | |
531 | ||
6a488035 TO |
532 | $defaults = $this->_values; |
533 | $additionalDetailFields = array( | |
534 | 'note', | |
535 | 'thankyou_date', | |
536 | 'invoice_id', | |
537 | 'non_deductible_amount', | |
538 | 'fee_amount', | |
dfbad3f7 | 539 | 'net_amount', |
6a488035 TO |
540 | ); |
541 | foreach ($additionalDetailFields as $key) { | |
542 | if (!empty($defaults[$key])) { | |
543 | $defaults['hidden_AdditionalDetail'] = 1; | |
544 | break; | |
545 | } | |
546 | } | |
547 | ||
6a488035 TO |
548 | if ($this->_productDAO) { |
549 | if ($this->_productDAO->product_id) { | |
550 | $defaults['hidden_Premium'] = 1; | |
551 | } | |
552 | } | |
553 | ||
554 | if ($this->_noteID && | |
555 | isset($this->_values['note']) | |
556 | ) { | |
557 | $defaults['hidden_AdditionalDetail'] = 1; | |
558 | } | |
559 | ||
560 | $paneNames = array( | |
561 | ts('Additional Details') => 'AdditionalDetail', | |
6a488035 TO |
562 | ); |
563 | ||
564 | //Add Premium pane only if Premium is exists. | |
565 | $dao = new CRM_Contribute_DAO_Product(); | |
566 | $dao->is_active = 1; | |
567 | ||
568 | if ($dao->find(TRUE)) { | |
569 | $paneNames[ts('Premium Information')] = 'Premium'; | |
570 | } | |
571 | ||
6a488035 | 572 | if ($this->_mode) { |
225584c9 | 573 | if (CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE) == TRUE) { |
5e27919e EM |
574 | if (!empty($this->_recurPaymentProcessors)) { |
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 | } | |
6a488035 TO |
580 | } |
581 | } | |
6a488035 | 582 | |
6a488035 | 583 | foreach ($paneNames as $name => $type) { |
a5399a39 | 584 | $allPanes[$name] = $this->generatePane($type, $defaults); |
6a488035 | 585 | } |
5e27919e | 586 | |
6a488035 TO |
587 | $qfKey = $this->controller->_key; |
588 | $this->assign('qfKey', $qfKey); | |
589 | $this->assign('allPanes', $allPanes); | |
6a488035 | 590 | |
6c75c831 | 591 | $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this); |
592 | ||
6a488035 TO |
593 | if ($this->_formType) { |
594 | $this->assign('formType', $this->_formType); | |
595 | return; | |
596 | } | |
597 | ||
598 | $this->applyFilter('__ALL__', 'trim'); | |
5a230af7 | 599 | |
6a488035 TO |
600 | if ($this->_action & CRM_Core_Action::DELETE) { |
601 | $this->addButtons(array( | |
602 | array( | |
603 | 'type' => 'next', | |
604 | 'name' => ts('Delete'), | |
605 | 'spacing' => ' ', | |
606 | 'isDefault' => TRUE, | |
607 | ), | |
608 | array( | |
609 | 'type' => 'cancel', | |
21dfd5f5 TO |
610 | 'name' => ts('Cancel'), |
611 | ), | |
6a488035 TO |
612 | ) |
613 | ); | |
614 | return; | |
615 | } | |
616 | ||
617 | //need to assign custom data type and subtype to the template | |
618 | $this->assign('customDataType', 'Contribution'); | |
619 | $this->assign('customDataSubType', $this->_contributionType); | |
620 | $this->assign('entityID', $this->_id); | |
621 | ||
622 | if ($this->_context == 'standalone') { | |
353ffa53 TO |
623 | $this->addEntityRef('contact_id', ts('Contact'), array( |
624 | 'create' => TRUE, | |
acb1052e | 625 | 'api' => array('extra' => array('email')), |
353ffa53 | 626 | ), TRUE); |
6a488035 TO |
627 | } |
628 | ||
629 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); | |
630 | ||
631 | $financialType = $this->add('select', 'financial_type_id', | |
632 | ts('Financial Type'), | |
633 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType(), | |
79ca865b CW |
634 | TRUE, |
635 | array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );") | |
6a488035 | 636 | ); |
d77a0a58 | 637 | $paymentInstrument = FALSE; |
6a488035 TO |
638 | if (!$this->_mode) { |
639 | $paymentInstrument = $this->add('select', 'payment_instrument_id', | |
640 | ts('Paid By'), | |
641 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), | |
d96cf288 | 642 | TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);") |
6a488035 TO |
643 | ); |
644 | } | |
645 | ||
ce22fda6 | 646 | $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') + $attributes['trxn_id']); |
6a488035 TO |
647 | |
648 | //add receipt for offline contribution | |
649 | $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?')); | |
650 | ||
651 | $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails); | |
652 | ||
653 | $status = CRM_Contribute_PseudoConstant::contributionStatus(); | |
654 | ||
655 | // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169) | |
656 | $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
657 | if ($this->_ppID) { | |
658 | foreach (array( | |
659 | 'Cancelled', | |
660 | 'Failed', | |
21dfd5f5 | 661 | 'In Progress', |
6a488035 TO |
662 | ) as $suppress) { |
663 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); | |
664 | } | |
665 | } | |
666 | elseif ((!$this->_ppID && $this->_id) || !$this->_id) { | |
f73acc78 PN |
667 | $suppressFlag = FALSE; |
668 | if ($this->_id) { | |
669 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
670 | if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) { | |
671 | $suppressFlag = TRUE; | |
672 | } | |
673 | } | |
674 | if (!$suppressFlag) { | |
675 | foreach (array( | |
676 | 'Overdue', | |
21dfd5f5 | 677 | 'In Progress', |
f73acc78 PN |
678 | ) as $suppress) { |
679 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); | |
680 | } | |
681 | } | |
682 | else { | |
683 | unset($status[CRM_Utils_Array::key('Overdue', $statusName)]); | |
6a488035 TO |
684 | } |
685 | } | |
b6545333 | 686 | |
6a488035 TO |
687 | if ($this->_id) { |
688 | $contributionStatus = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id, 'contribution_status_id'); | |
689 | $name = CRM_Utils_Array::value($contributionStatus, $statusName); | |
d37ade2e | 690 | switch ($name) { |
6a488035 TO |
691 | case 'Completed': |
692 | case 'Cancelled': | |
693 | case 'Refunded': | |
f73acc78 | 694 | unset($status[CRM_Utils_Array::key('In Progress', $statusName)]); |
6a488035 TO |
695 | unset($status[CRM_Utils_Array::key('Pending', $statusName)]); |
696 | unset($status[CRM_Utils_Array::key('Failed', $statusName)]); | |
697 | break; | |
ea100cb5 | 698 | |
6a488035 | 699 | case 'Pending': |
f73acc78 | 700 | case 'In Progress': |
6a488035 TO |
701 | unset($status[CRM_Utils_Array::key('Refunded', $statusName)]); |
702 | break; | |
ea100cb5 | 703 | |
6a488035 | 704 | case 'Failed': |
d37ade2e | 705 | foreach (array( |
706 | 'Pending', | |
707 | 'Refunded', | |
708 | 'Completed', | |
f73acc78 | 709 | 'In Progress', |
21dfd5f5 | 710 | 'Cancelled', |
d37ade2e | 711 | ) as $suppress) { |
6a488035 TO |
712 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); |
713 | } | |
714 | break; | |
715 | } | |
d37ade2e | 716 | } |
717 | else { | |
6a488035 TO |
718 | unset($status[CRM_Utils_Array::key('Refunded', $statusName)]); |
719 | } | |
720 | ||
721 | $this->add('select', 'contribution_status_id', | |
722 | ts('Contribution Status'), | |
723 | $status, | |
724 | FALSE | |
725 | ); | |
726 | ||
727 | // add various dates | |
728 | $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime')); | |
729 | ||
730 | if ($this->_online) { | |
731 | $this->assign('hideCalender', TRUE); | |
732 | } | |
733 | $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']); | |
734 | ||
735 | $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime')); | |
736 | $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime')); | |
737 | ||
738 | $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']); | |
739 | ||
6a488035 TO |
740 | $element = $this->add('select', |
741 | 'payment_processor_id', | |
742 | ts('Payment Processor'), | |
743 | $this->_processors, | |
744 | NULL, | |
745 | $recurJs | |
746 | ); | |
747 | ||
748 | if ($this->_online) { | |
749 | $element->freeze(); | |
750 | } | |
e02d7e96 | 751 | |
6a488035 TO |
752 | $totalAmount = NULL; |
753 | if (empty($this->_lineItems)) { | |
754 | $buildPriceSet = FALSE; | |
9da8dc8c | 755 | $priceSets = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute'); |
6a488035 TO |
756 | if (!empty($priceSets) && !$this->_ppID) { |
757 | $buildPriceSet = TRUE; | |
758 | } | |
759 | ||
760 | // don't allow price set for contribution if it is related to participant, or if it is a pledge payment | |
761 | // and if we already have line items for that participant. CRM-5095 | |
762 | if ($buildPriceSet && $this->_id) { | |
763 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
764 | $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', | |
765 | $this->_id, | |
766 | 'id', | |
767 | 'contribution_id' | |
768 | ); | |
769 | if ($pledgePaymentId) { | |
770 | $buildPriceSet = FALSE; | |
771 | } | |
772 | if ($participantID = CRM_Utils_Array::value('participant', $componentDetails)) { | |
773 | $participantLI = CRM_Price_BAO_LineItem::getLineItems($participantID); | |
774 | if (!CRM_Utils_System::isNull($participantLI)) { | |
775 | $buildPriceSet = FALSE; | |
776 | } | |
777 | } | |
778 | } | |
779 | ||
780 | $hasPriceSets = FALSE; | |
781 | if ($buildPriceSet) { | |
782 | $hasPriceSets = TRUE; | |
6a3d8d2d WA |
783 | // CRM-16451: set financial type of 'Price Set' in back office contribution |
784 | // instead of selecting manually | |
785 | $financialTypeIds = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute', 'financial_type_id'); | |
6a488035 TO |
786 | $element = $this->add('select', 'price_set_id', ts('Choose price set'), |
787 | array( | |
21dfd5f5 | 788 | '' => ts('Choose price set'), |
6a488035 | 789 | ) + $priceSets, |
d6d7af07 | 790 | NULL, array('onchange' => "buildAmount( this.value, " . json_encode($financialTypeIds) . ");") |
5a230af7 | 791 | ); |
6a488035 TO |
792 | if ($this->_online && !($this->_action & CRM_Core_Action::UPDATE)) { |
793 | $element->freeze(); | |
794 | } | |
795 | } | |
796 | $this->assign('hasPriceSets', $hasPriceSets); | |
797 | $currencyFreeze = FALSE; | |
798 | if (!($this->_action & CRM_Core_Action::UPDATE)) { | |
d37ade2e | 799 | if ($this->_online || $this->_ppID) { |
800 | $attributes['total_amount'] = array_merge($attributes['total_amount'], array( | |
801 | 'READONLY' => TRUE, | |
21dfd5f5 | 802 | 'style' => "background-color:#EBECE4", |
d37ade2e | 803 | )); |
804 | $optionTypes = array( | |
805 | '1' => ts('Adjust Pledge Payment Schedule?'), | |
806 | '2' => ts('Adjust Total Pledge Amount?'), | |
807 | ); | |
808 | $this->addRadio('option_type', | |
809 | NULL, | |
810 | $optionTypes, | |
811 | array(), '<br/>' | |
812 | ); | |
813 | ||
814 | $currencyFreeze = TRUE; | |
815 | } | |
6a488035 TO |
816 | } |
817 | ||
818 | $totalAmount = $this->addMoney('total_amount', | |
819 | ts('Total Amount'), | |
820 | ($hasPriceSets) ? FALSE : TRUE, | |
821 | $attributes['total_amount'], | |
822 | TRUE, 'currency', NULL, $currencyFreeze | |
823 | ); | |
824 | } | |
825 | ||
826 | $this->add('text', 'source', ts('Source'), CRM_Utils_Array::value('source', $attributes)); | |
827 | ||
161a2fe7 | 828 | // CRM-7362 --add campaigns. |
6a488035 TO |
829 | CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values)); |
830 | ||
0baed70b | 831 | CRM_Contribute_Form_SoftCredit::buildQuickForm($this); |
0baed70b | 832 | |
6a488035 TO |
833 | $js = NULL; |
834 | if (!$this->_mode) { | |
835 | $js = array('onclick' => "return verify( );"); | |
836 | } | |
837 | ||
838 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
839 | 'mailing_backend' | |
840 | ); | |
841 | $this->assign('outBound_option', $mailingInfo['outBound_option']); | |
842 | ||
843 | $this->addButtons(array( | |
844 | array( | |
845 | 'type' => 'upload', | |
846 | 'name' => ts('Save'), | |
847 | 'js' => $js, | |
21dfd5f5 | 848 | 'isDefault' => TRUE, |
6a488035 TO |
849 | ), |
850 | array( | |
851 | 'type' => 'upload', | |
852 | 'name' => ts('Save and New'), | |
853 | 'js' => $js, | |
21dfd5f5 | 854 | 'subName' => 'new', |
6a488035 TO |
855 | ), |
856 | array( | |
857 | 'type' => 'cancel', | |
21dfd5f5 | 858 | 'name' => ts('Cancel'), |
6a488035 TO |
859 | ), |
860 | ) | |
861 | ); | |
862 | ||
863 | // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type, | |
864 | // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail | |
865 | if ($this->_id && $this->_values['contribution_status_id'] == array_search('Cancelled', $statusName)) { | |
5a230af7 | 866 | if ($totalAmount) { |
6a488035 TO |
867 | $totalAmount->freeze(); |
868 | } | |
869 | $checkNumber->freeze(); | |
870 | $paymentInstrument->freeze(); | |
871 | $trxnId->freeze(); | |
872 | $financialType->freeze(); | |
873 | } | |
874 | ||
b6f65362 | 875 | // if contribution is related to membership or participant freeze Financial Type, Amount |
876 | if ($this->_id && isset($this->_values['tax_amount'])) { | |
877 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); | |
878 | if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) { | |
b5527a9a | 879 | if ($totalAmount) { |
a13f3d8c | 880 | $totalAmount->freeze(); |
b5527a9a | 881 | } |
b6f65362 | 882 | $financialType->freeze(); |
b6f65362 | 883 | $this->assign('freezeFinancialType', TRUE); |
884 | } | |
885 | } | |
886 | ||
6a488035 TO |
887 | if ($this->_action & CRM_Core_Action::VIEW) { |
888 | $this->freeze(); | |
889 | } | |
890 | } | |
891 | ||
892 | /** | |
fe482240 | 893 | * Global form rule. |
6a488035 | 894 | * |
014c4014 TO |
895 | * @param array $fields |
896 | * The input form values. | |
897 | * @param array $files | |
898 | * The uploaded files if any. | |
2a6da8d7 EM |
899 | * @param $self |
900 | * | |
72b3a70c CW |
901 | * @return bool|array |
902 | * true if no errors, else array of errors | |
6a488035 | 903 | */ |
1cf3816f | 904 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
905 | $errors = array(); |
906 | ||
161a2fe7 | 907 | // Check for Credit Card Contribution. |
6a488035 TO |
908 | if ($self->_mode) { |
909 | if (empty($fields['payment_processor_id'])) { | |
910 | $errors['payment_processor_id'] = ts('Payment Processor is a required field.'); | |
911 | } | |
34089500 | 912 | else { |
913 | // validate payment instrument (e.g. credit card number) | |
914 | CRM_Core_Payment_Form::validatePaymentInstrument($fields['payment_processor_id'], $fields, $errors, $self); | |
915 | } | |
6a488035 TO |
916 | } |
917 | ||
161a2fe7 | 918 | // Do the amount validations. |
a7488080 | 919 | if (empty($fields['total_amount']) && empty($self->_lineItems)) { |
6a488035 | 920 | if ($priceSetId = CRM_Utils_Array::value('price_set_id', $fields)) { |
9da8dc8c | 921 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $fields, $errors); |
6a488035 TO |
922 | } |
923 | } | |
924 | ||
b326919b | 925 | $softErrors = CRM_Contribute_Form_SoftCredit::formRule($fields, $errors, $self); |
d37ade2e | 926 | |
3256b416 JM |
927 | if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) || !empty($fields['fee_amount']))) { |
928 | $sum = CRM_Utils_Rule::cleanMoney($fields['net_amount']) + CRM_Utils_Rule::cleanMoney($fields['fee_amount']); | |
f21310d8 | 929 | // For taxable contribution we need to deduct taxable amount from |
930 | // (net amount + fee amount) before comparing it with total amount | |
931 | if (!empty($self->_values['tax_amount'])) { | |
932 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($self->_id); | |
933 | if (!(CRM_Utils_Array::value('membership', $componentDetails) || | |
934 | CRM_Utils_Array::value('participant', $componentDetails)) | |
935 | ) { | |
936 | $sum = CRM_Utils_Money::format($sum - $self->_values['tax_amount'], NULL, '%a'); | |
937 | } | |
938 | } | |
3256b416 | 939 | if (CRM_Utils_Rule::cleanMoney($fields['total_amount']) != $sum) { |
6a488035 TO |
940 | $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount'); |
941 | } | |
942 | } | |
577d1236 WA |
943 | |
944 | //CRM-16285 - Function to handle validation errors on form, for recurring contribution field. | |
945 | CRM_Contribute_BAO_ContributionRecur::validateRecurContribution($fields, $files, $self, $errors); | |
946 | ||
161a2fe7 | 947 | // Form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow |
6a488035 TO |
948 | if ($self->_id && $self->_values['contribution_status_id'] != $fields['contribution_status_id']) { |
949 | CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors); | |
950 | } | |
4d47ad17 PN |
951 | // CRM-16015, add form-rule to restrict change of financial type if using price field of different financial type |
952 | if ($self->_id && $self->_values['financial_type_id'] != $fields['financial_type_id']) { | |
953 | CRM_Contribute_BAO_Contribution::checkFinancialTypeChange(NULL, $self->_id, $errors); | |
954 | } | |
6a488035 | 955 | //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow |
34089500 | 956 | if (!empty($fields['fee_amount']) && !empty($fields['financial_type_id']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($fields['financial_type_id'])) { |
51fa20cb | 957 | $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType; |
6a488035 | 958 | } |
133e2c99 | 959 | |
7272e4fa DG |
960 | // $trxn_id must be unique CRM-13919 |
961 | if (!empty($fields['trxn_id'])) { | |
962 | $queryParams = array(1 => array($fields['trxn_id'], 'String')); | |
963 | $query = 'select count(*) from civicrm_contribution where trxn_id = %1'; | |
964 | if ($self->_id) { | |
353ffa53 | 965 | $queryParams[2] = array((int) $self->_id, 'Integer'); |
7272e4fa DG |
966 | $query .= ' and id !=%2'; |
967 | } | |
968 | $tCnt = CRM_Core_DAO::singleValueQuery($query, $queryParams); | |
969 | if ($tCnt) { | |
970 | $errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id'])); | |
133e2c99 | 971 | } |
7272e4fa | 972 | } |
d37ade2e | 973 | |
ac0c89ed | 974 | $errors = array_merge($errors, $softErrors); |
6a488035 TO |
975 | return $errors; |
976 | } | |
977 | ||
978 | /** | |
fe482240 | 979 | * Process the form submission. |
6a488035 TO |
980 | */ |
981 | public function postProcess() { | |
6a488035 TO |
982 | if ($this->_action & CRM_Core_Action::DELETE) { |
983 | CRM_Contribute_BAO_Contribution::deleteContribution($this->_id); | |
c1cc3e0c | 984 | CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', |
6a488035 TO |
985 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" |
986 | )); | |
987 | return; | |
988 | } | |
6a488035 | 989 | $submittedValues = $this->controller->exportValues($this->_name); |
7758bd2b EM |
990 | |
991 | try { | |
992 | // Get the submitted form values. | |
993 | $contribution = $this->submit($submittedValues, $this->_action, $this->_ppID); | |
994 | } | |
995 | catch (PaymentProcessorException $e) { | |
996 | // Set the contribution mode. | |
997 | $urlParams = "action=add&cid={$this->_contactID}"; | |
998 | if ($this->_mode) { | |
999 | $urlParams .= "&mode={$this->_mode}"; | |
1000 | } | |
1001 | if (!empty($this->_ppID)) { | |
1002 | $urlParams .= "&context=pledge&ppid={$this->_ppID}"; | |
1003 | } | |
1004 | ||
1005 | CRM_Core_Error::statusBounce($e->getMessage(), $urlParams, ts('Payment Processor Error')); | |
1006 | } | |
f75f0921 EM |
1007 | $session = CRM_Core_Session::singleton(); |
1008 | $buttonName = $this->controller->getButtonName(); | |
1009 | if ($this->_context == 'standalone') { | |
1010 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
1011 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contribute/add', | |
1012 | 'reset=1&action=add&context=standalone' | |
1013 | )); | |
6a488035 | 1014 | } |
f75f0921 EM |
1015 | else { |
1016 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
1017 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" | |
1018 | )); | |
1019 | } | |
1020 | } | |
1021 | elseif ($this->_context == 'contribution' && $this->_mode && $buttonName == $this->getButtonName('upload', 'new')) { | |
1022 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', | |
1023 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}" | |
1024 | )); | |
1025 | } | |
1026 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
1027 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', | |
1028 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}" | |
1029 | )); | |
6a488035 TO |
1030 | } |
1031 | ||
f75f0921 EM |
1032 | //store contribution ID if not yet set (on create) |
1033 | if (empty($this->_id) && !empty($contribution->id)) { | |
1034 | $this->_id = $contribution->id; | |
6a488035 | 1035 | } |
f75f0921 | 1036 | } |
6a488035 | 1037 | |
f75f0921 EM |
1038 | /** |
1039 | * Process credit card payment. | |
1040 | * | |
1041 | * @param array $submittedValues | |
1042 | * @param array $lineItem | |
456b0145 | 1043 | * |
7126b55c EM |
1044 | * @param int $contactID |
1045 | * Contact ID | |
1046 | * | |
456b0145 | 1047 | * @return bool|\CRM_Contribute_DAO_Contribution |
7758bd2b EM |
1048 | * @throws \CiviCRM_API3_Exception |
1049 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
456b0145 | 1050 | */ |
7126b55c | 1051 | protected function processCreditCard($submittedValues, $lineItem, $contactID) { |
c52884b2 | 1052 | $contribution = FALSE; |
5a230af7 | 1053 | |
549cf0b9 | 1054 | $isTest = ($this->_mode == 'test') ? 1 : 0; |
f75f0921 | 1055 | // CRM-12680 set $_lineItem if its not set |
bf4253e7 EM |
1056 | // @todo - I don't believe this would ever BE set. I can't find anywhere in the code. |
1057 | // It would be better to pass line item out to functions than $this->_lineItem as | |
1058 | // we don't know what is being changed where. | |
f75f0921 EM |
1059 | if (empty($this->_lineItem) && !empty($lineItem)) { |
1060 | $this->_lineItem = $lineItem; | |
6a488035 | 1061 | } |
6a488035 | 1062 | |
bf4253e7 EM |
1063 | $this->_paymentObject = Civi\Payment\System::singleton()->getById($submittedValues['payment_processor_id']); |
1064 | $this->_paymentProcessor = $this->_paymentObject->getPaymentProcessor(); | |
c52884b2 | 1065 | |
7758bd2b EM |
1066 | // Set source if not set |
1067 | if (empty($submittedValues['source'])) { | |
1068 | $userID = CRM_Core_Session::singleton()->get('userID'); | |
1069 | $userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID, | |
1070 | 'sort_name' | |
1071 | ); | |
1072 | $submittedValues['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName)); | |
1073 | } | |
1074 | ||
c52884b2 | 1075 | $params = $this->_params = $submittedValues; |
77e18ec7 | 1076 | |
bf4253e7 EM |
1077 | // Mapping requiring documentation. |
1078 | $this->_params['payment_processor'] = $submittedValues['payment_processor_id']; | |
1079 | ||
f75f0921 EM |
1080 | $now = date('YmdHis'); |
1081 | $fields = array(); | |
5a230af7 | 1082 | |
f75f0921 EM |
1083 | // we need to retrieve email address |
1084 | if ($this->_context == 'standalone' && !empty($submittedValues['is_email_receipt'])) { | |
1085 | list($this->userDisplayName, | |
1086 | $this->userEmail | |
7126b55c | 1087 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID); |
f75f0921 | 1088 | $this->assign('displayName', $this->userDisplayName); |
6a488035 | 1089 | } |
6a488035 | 1090 | |
f75f0921 EM |
1091 | // Set email for primary location. |
1092 | $fields['email-Primary'] = 1; | |
1093 | $params['email-Primary'] = $this->userEmail; | |
b6545333 | 1094 | |
f75f0921 EM |
1095 | // now set the values for the billing location. |
1096 | foreach (array_keys($this->_fields) as $name) { | |
1097 | $fields[$name] = 1; | |
b6545333 | 1098 | } |
f75f0921 EM |
1099 | |
1100 | // also add location name to the array | |
1101 | $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); | |
1102 | ||
1103 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); | |
1104 | $fields["address_name-{$this->_bltID}"] = 1; | |
f75f0921 EM |
1105 | |
1106 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
1107 | foreach ($nameFields as $name) { | |
1108 | $fields[$name] = 1; | |
1109 | if (array_key_exists("billing_$name", $params)) { | |
1110 | $params[$name] = $params["billing_{$name}"]; | |
1111 | $params['preserveDBName'] = TRUE; | |
d37ade2e | 1112 | } |
cd0a30ac | 1113 | } |
1114 | ||
f75f0921 EM |
1115 | if (!empty($params['source'])) { |
1116 | unset($params['source']); | |
6a488035 | 1117 | } |
c52884b2 | 1118 | CRM_Contact_BAO_Contact::createProfileContact($params, $fields, |
7126b55c | 1119 | $contactID, |
f75f0921 | 1120 | NULL, NULL, |
c52884b2 | 1121 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
7126b55c | 1122 | $contactID, |
c52884b2 EM |
1123 | 'contact_type' |
1124 | ) | |
f75f0921 | 1125 | ); |
6a488035 | 1126 | |
f75f0921 EM |
1127 | // add all the additional payment params we need |
1128 | if (!empty($this->_params["billing_state_province_id-{$this->_bltID}"])) { | |
1129 | $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}"]); | |
1130 | } | |
1131 | if (!empty($this->_params["billing_country_id-{$this->_bltID}"])) { | |
1132 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
1133 | } | |
c52884b2 | 1134 | |
9f5430b8 | 1135 | if (in_array('credit_card_exp_date', array_keys($this->_paymentFields))) { |
549cf0b9 EM |
1136 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); |
1137 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
f75f0921 | 1138 | } |
c52884b2 | 1139 | |
f75f0921 EM |
1140 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); |
1141 | $this->_params['amount'] = $this->_params['total_amount']; | |
1142 | $this->_params['amount_level'] = 0; | |
1143 | $this->_params['description'] = ts('Office Credit Card contribution'); | |
1144 | $this->_params['currencyID'] = CRM_Utils_Array::value('currency', | |
1145 | $this->_params, | |
1146 | CRM_Core_Config::singleton()->defaultCurrency | |
1147 | ); | |
aefd7f6b | 1148 | |
f75f0921 EM |
1149 | if (!empty($this->_params['receive_date'])) { |
1150 | $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['receive_date'], $this->_params['receive_date_time']); | |
6a488035 | 1151 | } |
6a488035 | 1152 | |
f75f0921 EM |
1153 | if (!empty($params['soft_credit_to'])) { |
1154 | $this->_params['soft_credit_to'] = $params['soft_credit_to']; | |
1155 | $this->_params['pcp_made_through_id'] = $params['pcp_made_through_id']; | |
1156 | } | |
ce8be7f2 | 1157 | |
f75f0921 EM |
1158 | $this->_params['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $params); |
1159 | $this->_params['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $params); | |
1160 | $this->_params['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $params); | |
3f662d9d | 1161 | |
f75f0921 EM |
1162 | //Add common data to formatted params |
1163 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($params, $this->_params, $this); | |
6a488035 | 1164 | |
f75f0921 EM |
1165 | if (empty($this->_params['invoice_id'])) { |
1166 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1167 | } | |
1168 | else { | |
1169 | $this->_params['invoiceID'] = $this->_params['invoice_id']; | |
1170 | } | |
6a488035 | 1171 | |
f75f0921 EM |
1172 | // At this point we've created a contact and stored its address etc |
1173 | // all the payment processors expect the name and address to be in the | |
1174 | // so we copy stuff over to first_name etc. | |
1175 | $paymentParams = $this->_params; | |
7126b55c | 1176 | $paymentParams['contactID'] = $contactID; |
f75f0921 | 1177 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); |
6a488035 | 1178 | |
67256e0c EM |
1179 | $financialType = new CRM_Financial_DAO_FinancialType(); |
1180 | $financialType->id = $params['financial_type_id']; | |
6a488035 | 1181 | |
f75f0921 EM |
1182 | // Add some financial type details to the params list |
1183 | // if folks need to use it. | |
67256e0c | 1184 | $paymentParams['contributionType_name'] = $this->_params['contributionType_name'] = $financialType->name; |
f75f0921 | 1185 | $paymentParams['contributionPageID'] = NULL; |
6a488035 | 1186 | |
f75f0921 EM |
1187 | if (!empty($this->_params['is_email_receipt'])) { |
1188 | $paymentParams['email'] = $this->userEmail; | |
1189 | $paymentParams['is_email_receipt'] = 1; | |
1190 | } | |
1191 | else { | |
1192 | $paymentParams['is_email_receipt'] = 0; | |
1193 | $this->_params['is_email_receipt'] = 0; | |
1194 | } | |
1195 | if (!empty($this->_params['receive_date'])) { | |
1196 | $paymentParams['receive_date'] = $this->_params['receive_date']; | |
1197 | } | |
6a488035 | 1198 | |
f75f0921 | 1199 | $this->_params['receive_date'] = $now; |
6a488035 | 1200 | |
f75f0921 EM |
1201 | if (!empty($this->_params['is_email_receipt'])) { |
1202 | $this->_params['receipt_date'] = $now; | |
1203 | } | |
1204 | else { | |
1205 | $this->_params['receipt_date'] = CRM_Utils_Date::processDate($this->_params['receipt_date'], | |
1206 | $params['receipt_date_time'], TRUE | |
1207 | ); | |
1208 | } | |
6a488035 | 1209 | |
f75f0921 | 1210 | $this->set('params', $this->_params); |
7758bd2b | 1211 | |
f75f0921 | 1212 | $this->assign('receive_date', $this->_params['receive_date']); |
6a488035 | 1213 | |
f75f0921 EM |
1214 | // Result has all the stuff we need |
1215 | // lets archive it to a financial transaction | |
67256e0c | 1216 | if ($financialType->is_deductible) { |
f75f0921 EM |
1217 | $this->assign('is_deductible', TRUE); |
1218 | $this->set('is_deductible', TRUE); | |
1219 | } | |
6a488035 | 1220 | |
ba013eea | 1221 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($this, |
7758bd2b EM |
1222 | $this->_params, |
1223 | NULL, | |
1224 | $contactID, | |
1225 | $financialType, | |
1226 | TRUE, | |
1227 | FALSE, | |
1228 | $isTest, | |
1229 | $lineItem, | |
1230 | $this->_bltID | |
1231 | ); | |
6a488035 | 1232 | |
7758bd2b EM |
1233 | $paymentParams['contributionID'] = $contribution->id; |
1234 | $paymentParams['contributionTypeID'] = $contribution->financial_type_id; | |
1235 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; | |
1236 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; | |
6a488035 | 1237 | |
7758bd2b EM |
1238 | if ($paymentParams['amount'] > 0.0) { |
1239 | // force a re-get of the payment processor in case the form changed it, CRM-7179 | |
1240 | // NOTE - I expect this is not obsolete. | |
1241 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this, TRUE); | |
1242 | try { | |
1243 | $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id'); | |
1244 | $result = $payment->doPayment($paymentParams, 'contribute'); | |
1245 | $this->assign('trxn_id', $result['trxn_id']); | |
1246 | $contribution->trxn_id = $result['trxn_id']; | |
1247 | /* Our scenarios here are | |
1248 | * 1) the payment failed & an Exception should have been thrown | |
1249 | * 2) the payment succeeded but the payment is not immediate (for example a recurring payment | |
1250 | * with a delayed start) | |
1251 | * 3) the payment succeeded with an immediate payment. | |
1252 | * | |
1253 | * The doPayment function ensures that contribution_status_id is always set | |
1254 | * as historically we have had to guess from the context - ie doDirectPayment | |
1255 | * = error or success, unless it is a recurring contribution in which case it is pending. | |
1256 | */ | |
1257 | if (!isset($result['contribution_status_id']) || $result['contribution_status_id'] == | |
1258 | array_search('Completed', $statuses)) { | |
1259 | civicrm_api3('contribution', 'completetransaction', array('id' => $contribution->id, 'trxn_id' => $result['trxn_id'])); | |
1260 | } | |
1261 | else { | |
1262 | // Save the trxn_id. | |
1263 | $contribution->save(); | |
1264 | } | |
1265 | } | |
1266 | catch (PaymentProcessorException $e) { | |
06d062ce | 1267 | CRM_Contribute_BAO_Contribution::failPayment($contribution->id, $paymentParams['contactID'], $e->getMessage()); |
7758bd2b EM |
1268 | throw new PaymentProcessorException($e->getMessage()); |
1269 | } | |
1270 | } | |
f75f0921 EM |
1271 | // Send receipt mail. |
1272 | if ($contribution->id && !empty($this->_params['is_email_receipt'])) { | |
1273 | $this->_params['trxn_id'] = CRM_Utils_Array::value('trxn_id', $result); | |
7126b55c | 1274 | $this->_params['contact_id'] = $contactID; |
f75f0921 | 1275 | $this->_params['contribution_id'] = $contribution->id; |
c52884b2 | 1276 | if (CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $this->_params, TRUE)) { |
3e6a1f4a | 1277 | $this->statusMessage[] = ts('A receipt has been emailed to the contributor.'); |
6a488035 | 1278 | } |
28d44c9f | 1279 | } |
c52884b2 | 1280 | |
5e27919e | 1281 | return $contribution; |
6a488035 | 1282 | } |
5a230af7 | 1283 | |
f75f0921 EM |
1284 | /** |
1285 | * Generate the data to construct a snippet based pane. | |
1286 | * | |
1287 | * This form also assigns the showAdditionalInfo var based on historical code. | |
1288 | * This appears to mean 'there is a pane to show'. | |
1289 | * | |
1290 | * @param string $type | |
1291 | * Type of Pane - this is generally used to determine the function name used to build it | |
1292 | * - e.g CreditCard, AdditionalDetail | |
1293 | * @param array $defaults | |
1294 | * | |
1295 | * @return array | |
1296 | * We aim to further refactor & simplify this but currently | |
1297 | * - the panes array | |
1298 | * - should additional info be shown? | |
1299 | */ | |
1300 | protected function generatePane($type, $defaults) { | |
1301 | $urlParams = "snippet=4&formType={$type}"; | |
1302 | if ($this->_mode) { | |
1303 | $urlParams .= "&mode={$this->_mode}"; | |
6a488035 TO |
1304 | } |
1305 | ||
f75f0921 EM |
1306 | $open = 'false'; |
1307 | if ($type == 'CreditCard' || | |
1308 | $type == 'DirectDebit' | |
1309 | ) { | |
1310 | $open = 'true'; | |
6a488035 | 1311 | } |
6a488035 | 1312 | |
f75f0921 EM |
1313 | $pane = array( |
1314 | 'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams), | |
1315 | 'open' => $open, | |
1316 | 'id' => $type, | |
6a488035 | 1317 | ); |
6a488035 | 1318 | |
f75f0921 EM |
1319 | // See if we need to include this paneName in the current form. |
1320 | if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) || | |
1321 | CRM_Utils_Array::value("hidden_{$type}", $defaults) | |
1322 | ) { | |
1323 | $this->assign('showAdditionalInfo', TRUE); | |
1324 | $pane['open'] = 'true'; | |
6a488035 TO |
1325 | } |
1326 | ||
f75f0921 EM |
1327 | if ($type == 'CreditCard' || $type == 'DirectDebit') { |
1328 | // @todo would be good to align tpl name with form name... | |
1329 | // @todo document why this hidden variable is required. | |
1330 | $this->add('hidden', 'hidden_' . $type, 1); | |
1331 | return $pane; | |
6a488035 TO |
1332 | } |
1333 | else { | |
f75f0921 EM |
1334 | $additionalInfoFormFunction = 'build' . $type; |
1335 | CRM_Contribute_Form_AdditionalInfo::$additionalInfoFormFunction($this); | |
1336 | return $pane; | |
6a488035 | 1337 | } |
f75f0921 | 1338 | } |
6a488035 | 1339 | |
f75f0921 EM |
1340 | /** |
1341 | * Wrapper for unit testing the post process submit function. | |
1342 | * | |
1343 | * (If we expose through api we can get default additions 'for free'). | |
1344 | * | |
1345 | * @param array $params | |
5e27919e | 1346 | * @param int $action |
39f47c0d EM |
1347 | * @param string|null $creditCardMode |
1348 | * | |
1349 | * @throws \CiviCRM_API3_Exception | |
f75f0921 | 1350 | */ |
39f47c0d | 1351 | public function testSubmit($params, $action, $creditCardMode = NULL) { |
f75f0921 EM |
1352 | $defaults = array( |
1353 | 'soft_credit_contact_id' => array(), | |
1354 | 'receipt_date' => '', | |
1355 | 'receipt_date_time' => '', | |
1356 | 'cancel_date' => '', | |
1357 | 'cancel_date_time' => '', | |
43bf07d6 | 1358 | 'hidden_Premium' => 1, |
f75f0921 | 1359 | ); |
5e27919e EM |
1360 | if (!empty($params['id'])) { |
1361 | $existingContribution = civicrm_api3('contribution', 'getsingle', array( | |
1362 | 'id' => $params['id'], | |
1363 | )); | |
55e55e84 | 1364 | $this->_id = $params['id']; |
5e27919e EM |
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 | } |