popup fixes for download invoice
[civicrm-core.git] / CRM / Contribute / Form / Contribution.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 | |
14 | CiviCRM is distributed in the hope that it will be usefusul, but |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for processing a contribution
38 *
39 */
40class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditPayment {
6a488035 41 /**
d77a0a58 42 * the id of the contribution that we are processing
6a488035
TO
43 *
44 * @var int
45 * @public
46 */
47 public $_id;
48
49 /**
d77a0a58 50 * the id of the premium that we are processing
6a488035
TO
51 *
52 * @var int
53 * @public
54 */
55 public $_premiumID = NULL;
4691b077
EM
56
57 /**
58 * @var CRM_Contribute_DAO_ContributionProduct
59 */
6a488035
TO
60 public $_productDAO = NULL;
61
62 /**
63 * the id of the note
64 *
65 * @var int
66 * @public
67 */
68 public $_noteID;
69
70 /**
71 * the id of the contact associated with this contribution
72 *
73 * @var int
74 * @public
75 */
76 public $_contactID;
77
78 /**
79 * the id of the pledge payment that we are processing
80 *
81 * @var int
82 * @public
83 */
84 public $_ppID;
85
86 /**
87 * the id of the pledge that we are processing
88 *
89 * @var int
90 * @public
91 */
92 public $_pledgeID;
93
94 /**
95 * is this contribution associated with an online
96 * financial transaction
97 *
98 * @var boolean
99 * @public
100 */
101 public $_online = FALSE;
102
103 /**
104 * Stores all product option
105 *
106 * @var array
107 * @public
108 */
109 public $_options;
110
4691b077
EM
111 /**
112 * Storage of parameters from form
113 *
114 * @var array
115 * @public
116 */
117 public $_params;
118
6a488035
TO
119 /**
120 * Store the contribution Type ID
121 *
122 * @var array
123 */
124 public $_contributionType;
125
126 /**
127 * The contribution values if an existing contribution
128 */
129 public $_values;
130
131 /**
132 * The pledge values if this contribution is associated with pledge
133 */
134 public $_pledgeValues;
135
136 public $_contributeMode = 'direct';
137
138 public $_context;
139
5032bf07
EM
140 /**
141 * Parameter with confusing name
142 * @todo what is it?
143 * @var string
144 */
145 public $_compContext;
146
6a488035
TO
147 public $_compId;
148
5032bf07
EM
149 /**
150 * Possible From email addresses
151 * @var array
152 */
153 public $_fromEmails;
d77a0a58
EM
154
155 /**
156 * ID of from email
157 * @var integer
158 */
159 public $fromEmailId;
6a488035
TO
160 /*
161 * Store the line items if price set used.
162 */
163 public $_lineItems;
164
d77a0a58
EM
165 /**
166 * line item
167 * @todo explain why we use lineItem & lineItems
168 * @var array
169 */
170 public $_lineItem;
171
17db9f82 172 /**
efe42cdc 173 * @var array soft credit info
17db9f82
KJ
174 */
175 public $_softCreditInfo;
176
6a488035 177 protected $_formType;
efe42cdc
EM
178
179 /**
180 * @todo what on earth does cdType stand for????
181 * @var
182 */
6a488035 183 protected $_cdType;
1421174e 184 public $_honoreeProfileType;
6a488035 185
4691b077
EM
186 /**
187 * logged in user's email
188 * @var string
189 */
190 public $userEmail;
191
192 /**
193 * Price set ID
194 * @var integer
195 */
196 public $_priceSetId;
197
198
199 /**
200 * Price set as an array
201 * @var array
202 */
203 public $_priceSet;
204
d77a0a58
EM
205 /**
206 * form defaults
207 * @todo can we define this a as protected? can we define higher up the chain
208 * @var array
209 */
210 public $_defaults;
211
212 /**
213 * User display name
214 *
215 * @var string
216 */
217 public $userDisplayName;
6a488035
TO
218 /**
219 * Function to set variables up before form is built
220 *
221 * @return void
222 * @access public
223 */
224 public function preProcess() {
81bc499e 225
6a488035
TO
226 //check permission for action.
227 if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) {
228 CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
229 }
230
231 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
232
233 $this->assign('cdType', FALSE);
234 if ($this->_cdType) {
235 $this->assign('cdType', TRUE);
236 CRM_Custom_Form_CustomData::preProcess($this);
237 return;
238 }
239
240 $this->_formType = CRM_Utils_Array::value('formType', $_GET);
241
242 // get price set id.
243 $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET);
244 $this->set('priceSetId', $this->_priceSetId);
245 $this->assign('priceSetId', $this->_priceSetId);
246
247 //get the pledge payment id
248 $this->_ppID = CRM_Utils_Request::retrieve('ppid', 'Positive', $this);
249
250 //get the contact id
251 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
252
253 //get the action.
254 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
255 $this->assign('action', $this->_action);
256
257 //get the contribution id if update
258 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
259 if (!empty($this->_id)) {
260 $this->assign('contribID', $this->_id);
261 }
262
263 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
264 $this->assign('context', $this->_context);
265
266 $this->_compId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
267
268 $this->_compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
269
270 //set the contribution mode.
271 $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
272
273 $this->assign('contributionMode', $this->_mode);
274
275 $this->_paymentProcessor = array('billing_mode' => 1);
276
277 $this->assign('showCheckNumber', TRUE);
278
279 $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
280 $this->assignProcessors();
281
282 if ($this->_contactID) {
283 list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
284 $this->assign('displayName', $this->userDisplayName);
285 }
286
287 // also check for billing information
288 // get the billing location type
289 $this->assignBillingType();
290
291 $this->_fields = array();
292
293 CRM_Core_Payment_Form::setPaymentFieldsByType(CRM_Utils_Array::value('payment_type', $this->_processors), $this);
294
295 if ($this->_action & CRM_Core_Action::DELETE) {
296 return;
297 }
298
d77a0a58 299 if (in_array('CiviPledge', CRM_Core_Config::singleton()->enableComponents) && !$this->_formType) {
6a488035
TO
300 $this->preProcessPledge();
301 }
302
d5397f2f
PJ
303 if ($this->_id) {
304 $this->showRecordLinkMesssage($this->_id);
305 }
6a488035
TO
306 $this->_values = array();
307
308 // current contribution id
309 if ($this->_id) {
310 $this->assignPremiumProduct($this->_id);
311 $this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values);
312 }
313
314 // when custom data is included in this page
a7488080 315 if (!empty($_POST['hidden_custom'])) {
6a488035
TO
316 $this->applyCustomData('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id);
317 }
318
319 $this->_lineItems = array();
320 if ($this->_id) {
321 if (!empty($this->_compId) && $this->_compContext == 'participant') {
322 $this->assign('compId', $this->_compId);
323 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_compId);
324 }
325 else {
d37ade2e 326 $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution', 1);
6a488035
TO
327 }
328 empty($lineItem) ? NULL : $this->_lineItems[] = $lineItem;
329 }
330
331 $this->assign('lineItem', empty($this->_lineItems) ? FALSE : $this->_lineItems);
332
333 // Set title
334 if ($this->_contactID) {
335 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactID);
5a230af7 336
6a488035
TO
337 // Check if this is default domain contact CRM-10482
338 if (CRM_Contact_BAO_Contact::checkDomainContact($this->_contactID)) {
339 $displayName .= ' (' . ts('default organization') . ')';
340 }
341
342 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
d37ade2e 343 CRM_Utils_System::setTitle(ts('Contribution from') . ' ' . $displayName);
6a488035 344 }
1421174e 345
346 if ($this->_id) {
347 CRM_Contribute_Form_SoftCredit::preprocess($this);
348 }
6a488035
TO
349 }
350
351 function setDefaultValues() {
352 if ($this->_cdType) {
353 return CRM_Custom_Form_CustomData::setDefaultValues($this);
354 }
355
356 $defaults = $this->_values;
357
358 //set defaults for pledge payment.
359 if ($this->_ppID) {
360 $defaults['total_amount'] = CRM_Utils_Array::value('scheduled_amount', $this->_pledgeValues['pledgePayment']);
6a488035
TO
361 $defaults['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $this->_pledgeValues);
362 $defaults['currency'] = CRM_Utils_Array::value('currency', $this->_pledgeValues);
363 $defaults['option_type'] = 1;
364 }
365
6a488035
TO
366 if ($this->_action & CRM_Core_Action::DELETE) {
367 return $defaults;
368 }
369
dfbad3f7 370 // set soft credit defaults
17db9f82 371 CRM_Contribute_Form_SoftCredit::setDefaultValues($defaults, $this);
dfbad3f7 372
6a488035
TO
373 if ($this->_mode) {
374 $config = CRM_Core_Config::singleton();
5a230af7 375 // set default country from config if no country set
a7488080 376 if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
6a488035
TO
377 $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
378 }
5a230af7 379
a7488080 380 if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) {
6a488035
TO
381 $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
382 }
383
cc8ff73d
DG
384 $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID);
385 $defaults = array_merge($defaults, $billingDefaults);
6a488035 386
cc44e307 387 // now fix all state country selectors, set correct state based on country
5a230af7 388 CRM_Core_BAO_Address::fixAllStateSelects($this, $defaults);
6a488035
TO
389 }
390
391 if ($this->_id) {
392 $this->_contactID = $defaults['contact_id'];
393 }
5a230af7 394
6a488035 395 // Set $newCredit variable in template to control whether link to credit card mode is included
9be1374d 396 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
5a230af7 397
6a488035
TO
398 // fix the display of the monetary value, CRM-4038
399 if (isset($defaults['total_amount'])) {
049db839 400 if (!empty($defaults['tax_amount'])) {
401 $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'] - $defaults['tax_amount'], NULL, '%a');
402 }
403 else {
404 $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'], NULL, '%a');
405 }
6a488035
TO
406 }
407
408 if (isset($defaults['non_deductible_amount'])) {
409 $defaults['non_deductible_amount'] = CRM_Utils_Money::format($defaults['non_deductible_amount'], NULL, '%a');
410 }
411
412 if (isset($defaults['fee_amount'])) {
413 $defaults['fee_amount'] = CRM_Utils_Money::format($defaults['fee_amount'], NULL, '%a');
414 }
415
416 if (isset($defaults['net_amount'])) {
417 $defaults['net_amount'] = CRM_Utils_Money::format($defaults['net_amount'], NULL, '%a');
418 }
419
420 if ($this->_contributionType) {
421 $defaults['financial_type_id'] = $this->_contributionType;
422 }
133e2c99 423
a7488080 424 if (empty($defaults['payment_instrument_id'])) {
d96cf288
DG
425 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
426 }
6a488035 427
a7488080 428 if (!empty($defaults['is_test'])) {
6a488035
TO
429 $this->assign('is_test', TRUE);
430 }
431
6a488035
TO
432 $this->assign('showOption', TRUE);
433 // for Premium section
434 if ($this->_premiumID) {
435 $this->assign('showOption', FALSE);
436 $options = isset($this->_options[$this->_productDAO->product_id]) ? $this->_options[$this->_productDAO->product_id] : "";
437 if (!$options) {
438 $this->assign('showOption', TRUE);
439 }
440 $options_key = CRM_Utils_Array::key($this->_productDAO->product_option, $options);
441 if ($options_key) {
442 $defaults['product_name'] = array($this->_productDAO->product_id, trim($options_key));
443 }
444 else {
445 $defaults['product_name'] = array($this->_productDAO->product_id);
446 }
447 if ($this->_productDAO->fulfilled_date) {
448 list($defaults['fulfilled_date']) = CRM_Utils_Date::setDateDefaults($this->_productDAO->fulfilled_date);
449 }
450 }
451
452 if (isset($this->userEmail)) {
453 $this->assign('email', $this->userEmail);
454 }
455
a7488080 456 if (!empty($defaults['is_pay_later'])) {
6a488035
TO
457 $this->assign('is_pay_later', TRUE);
458 }
459 $this->assign('contribution_status_id', CRM_Utils_Array::value('contribution_status_id', $defaults));
460
461 $dates = array('receive_date', 'receipt_date', 'cancel_date', 'thankyou_date');
462 foreach ($dates as $key) {
a7488080 463 if (!empty($defaults[$key])) {
6a488035
TO
464 list($defaults[$key],
465 $defaults[$key . '_time']
466 ) = CRM_Utils_Date::setDateDefaults(CRM_Utils_Array::value($key, $defaults),
467 'activityDateTime'
468 );
469 }
470 }
471
8cc574cf 472 if (!$this->_id && empty($defaults['receive_date'])) {
6a488035
TO
473 list($defaults['receive_date'],
474 $defaults['receive_date_time']
475 ) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
476 }
477
478 $this->assign('receive_date', CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $defaults),
479 CRM_Utils_Array::value('receive_date_time', $defaults)
480 ));
481 $currency = CRM_Utils_Array::value('currency', $defaults);
482 $this->assign('currency', $currency);
483 // Hack to get currency info to the js layer. CRM-11440
484 CRM_Utils_Money::format(1);
485 $this->assign('currencySymbol', CRM_Utils_Array::value($currency, CRM_Utils_Money::$_currencySymbols));
486 $this->assign('totalAmount', CRM_Utils_Array::value('total_amount', $defaults));
487
488 //inherit campaign from pledge.
8cc574cf 489 if ($this->_ppID && !empty($this->_pledgeValues['campaign_id'])) {
6a488035
TO
490 $defaults['campaign_id'] = $this->_pledgeValues['campaign_id'];
491 }
492
493 $this->_defaults = $defaults;
494 return $defaults;
495 }
496
497 /**
498 * Function to build the form
499 *
500 * @return void
501 * @access public
502 */
503 public function buildQuickForm() {
504 if ($this->_cdType) {
505 CRM_Custom_Form_CustomData::buildQuickForm($this);
506 return;
507 }
5032bf07 508 $allPanes = array();
6daab7fa 509 //tax rate from financialType
a1e457ff 510 $this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates()));
049db839 511 $this->assign('currencies', json_encode(CRM_Core_OptionGroup::values('currencies_enabled')));
6a488035
TO
512
513 // build price set form.
514 $buildPriceSet = FALSE;
03b412ae
PB
515 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings');
516 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
95f2107c 517 $this->assign('invoicing', $invoicing);
a32709be 518
03b412ae
PB
519 // display tax amount on edit contribution page
520 if ($invoicing && $this->_action & CRM_Core_Action::UPDATE && isset($this->_values['tax_amount'])) {
a32709be
PB
521 $this->assign('totalTaxAmount', $this->_values['tax_amount']);
522 }
523
6a488035 524 if (empty($this->_lineItems) &&
8cc574cf 525 ($this->_priceSetId || !empty($_POST['price_set_id']))
6a488035
TO
526 ) {
527 $buildPriceSet = TRUE;
528 $getOnlyPriceSetElements = TRUE;
529 if (!$this->_priceSetId) {
530 $this->_priceSetId = $_POST['price_set_id'];
531 $getOnlyPriceSetElements = FALSE;
532 }
533
534 $this->set('priceSetId', $this->_priceSetId);
9da8dc8c 535 CRM_Price_BAO_PriceSet::buildPriceSet($this);
6a488035
TO
536
537 // get only price set form elements.
538 if ($getOnlyPriceSetElements) {
539 return;
540 }
541 }
542 // use to build form during form rule.
543 $this->assign('buildPriceSet', $buildPriceSet);
544
545 $showAdditionalInfo = FALSE;
546
6a488035
TO
547 $defaults = $this->_values;
548 $additionalDetailFields = array(
549 'note',
550 'thankyou_date',
551 'invoice_id',
552 'non_deductible_amount',
553 'fee_amount',
dfbad3f7 554 'net_amount',
6a488035
TO
555 );
556 foreach ($additionalDetailFields as $key) {
557 if (!empty($defaults[$key])) {
558 $defaults['hidden_AdditionalDetail'] = 1;
559 break;
560 }
561 }
562
6a488035
TO
563 if ($this->_productDAO) {
564 if ($this->_productDAO->product_id) {
565 $defaults['hidden_Premium'] = 1;
566 }
567 }
568
569 if ($this->_noteID &&
570 isset($this->_values['note'])
571 ) {
572 $defaults['hidden_AdditionalDetail'] = 1;
573 }
574
575 $paneNames = array(
576 ts('Additional Details') => 'AdditionalDetail',
6a488035
TO
577 );
578
579 //Add Premium pane only if Premium is exists.
580 $dao = new CRM_Contribute_DAO_Product();
581 $dao->is_active = 1;
582
583 if ($dao->find(TRUE)) {
584 $paneNames[ts('Premium Information')] = 'Premium';
585 }
586
587 $ccPane = NULL;
588 if ($this->_mode) {
589 if (CRM_Utils_Array::value('payment_type', $this->_processors) & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
590 ) {
591 $ccPane = array(ts('Direct Debit Information') => 'DirectDebit');
592 }
593 else {
594 $ccPane = array(ts('Credit Card Information') => 'CreditCard');
595 }
596 }
597 if (is_array($ccPane)) {
598 $paneNames = array_merge($ccPane, $paneNames);
599 }
600
601 $buildRecurBlock = FALSE;
602 foreach ($paneNames as $name => $type) {
603 $urlParams = "snippet=4&formType={$type}";
604 if ($this->_mode) {
605 $urlParams .= "&mode={$this->_mode}";
606 }
607
608 $open = 'false';
609 if ($type == 'CreditCard' ||
610 $type == 'DirectDebit'
611 ) {
612 $open = 'true';
613 }
614
615 $allPanes[$name] = array(
616 'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams),
617 'open' => $open,
618 'id' => $type
619 );
620
621 // see if we need to include this paneName in the current form
8cc574cf 622 if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) ||
6a488035
TO
623 CRM_Utils_Array::value("hidden_{$type}", $defaults)
624 ) {
625 $showAdditionalInfo = TRUE;
626 $allPanes[$name]['open'] = 'true';
627 }
628
629 if ($type == 'CreditCard') {
630 $buildRecurBlock = TRUE;
631 $this->add('hidden', 'hidden_CreditCard', 1);
632 CRM_Core_Payment_Form::buildCreditCard($this, TRUE);
633 }
634 elseif ($type == 'DirectDebit') {
635 $buildRecurBlock = TRUE;
636 $this->add('hidden', 'hidden_DirectDebit', 1);
637 CRM_Core_Payment_Form::buildDirectDebit($this, TRUE);
638 }
639 else {
d37ade2e 640 $additionalInfoFormFunction = 'build' . $type;
641 CRM_Contribute_Form_AdditionalInfo::$additionalInfoFormFunction($this);
6a488035
TO
642 }
643 }
644 if (empty($this->_recurPaymentProcessors)) {
645 $buildRecurBlock = FALSE;
646 }
647 if ($buildRecurBlock) {
648 CRM_Contribute_Form_Contribution_Main::buildRecur($this);
649 $this->setDefaults(array('is_recur' => 0));
650 }
651 $this->assign('buildRecurBlock', $buildRecurBlock);
652 $qfKey = $this->controller->_key;
653 $this->assign('qfKey', $qfKey);
654 $this->assign('allPanes', $allPanes);
655 $this->assign('showAdditionalInfo', $showAdditionalInfo);
656
657 if ($this->_formType) {
658 $this->assign('formType', $this->_formType);
659 return;
660 }
661
662 $this->applyFilter('__ALL__', 'trim');
5a230af7 663
6a488035
TO
664 if ($this->_action & CRM_Core_Action::DELETE) {
665 $this->addButtons(array(
666 array(
667 'type' => 'next',
668 'name' => ts('Delete'),
669 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
670 'isDefault' => TRUE,
671 ),
672 array(
673 'type' => 'cancel',
674 'name' => ts('Cancel')
675 )
676 )
677 );
678 return;
679 }
680
681 //need to assign custom data type and subtype to the template
682 $this->assign('customDataType', 'Contribution');
683 $this->assign('customDataSubType', $this->_contributionType);
684 $this->assign('entityID', $this->_id);
685
686 if ($this->_context == 'standalone') {
ccec9d6b 687 $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE), TRUE);
6a488035
TO
688 }
689
690 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution');
691
692 $financialType = $this->add('select', 'financial_type_id',
693 ts('Financial Type'),
694 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType(),
79ca865b
CW
695 TRUE,
696 array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );")
6a488035 697 );
d77a0a58 698 $paymentInstrument = FALSE;
6a488035
TO
699 if (!$this->_mode) {
700 $paymentInstrument = $this->add('select', 'payment_instrument_id',
701 ts('Paid By'),
702 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(),
d96cf288 703 TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
6a488035
TO
704 );
705 }
706
707 $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), $attributes['trxn_id']);
708
709 //add receipt for offline contribution
710 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
711
712 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails);
713
714 $status = CRM_Contribute_PseudoConstant::contributionStatus();
715
716 // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169)
717 $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
718 if ($this->_ppID) {
719 foreach (array(
720 'Cancelled',
721 'Failed',
722 'In Progress'
723 ) as $suppress) {
724 unset($status[CRM_Utils_Array::key($suppress, $statusName)]);
725 }
726 }
727 elseif ((!$this->_ppID && $this->_id) || !$this->_id) {
f73acc78
PN
728 $suppressFlag = FALSE;
729 if ($this->_id) {
730 $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id);
731 if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) {
732 $suppressFlag = TRUE;
733 }
734 }
735 if (!$suppressFlag) {
736 foreach (array(
737 'Overdue',
738 'In Progress'
739 ) as $suppress) {
740 unset($status[CRM_Utils_Array::key($suppress, $statusName)]);
741 }
742 }
743 else {
744 unset($status[CRM_Utils_Array::key('Overdue', $statusName)]);
6a488035
TO
745 }
746 }
b6545333 747
6a488035
TO
748 if ($this->_id) {
749 $contributionStatus = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id, 'contribution_status_id');
750 $name = CRM_Utils_Array::value($contributionStatus, $statusName);
d37ade2e 751 switch ($name) {
6a488035
TO
752 case 'Completed':
753 case 'Cancelled':
754 case 'Refunded':
f73acc78 755 unset($status[CRM_Utils_Array::key('In Progress', $statusName)]);
6a488035
TO
756 unset($status[CRM_Utils_Array::key('Pending', $statusName)]);
757 unset($status[CRM_Utils_Array::key('Failed', $statusName)]);
758 break;
759 case 'Pending':
f73acc78 760 case 'In Progress':
6a488035
TO
761 unset($status[CRM_Utils_Array::key('Refunded', $statusName)]);
762 break;
763 case 'Failed':
d37ade2e 764 foreach (array(
765 'Pending',
766 'Refunded',
767 'Completed',
f73acc78 768 'In Progress',
d37ade2e 769 'Cancelled'
770 ) as $suppress) {
6a488035
TO
771 unset($status[CRM_Utils_Array::key($suppress, $statusName)]);
772 }
773 break;
774 }
d37ade2e 775 }
776 else {
6a488035
TO
777 unset($status[CRM_Utils_Array::key('Refunded', $statusName)]);
778 }
779
780 $this->add('select', 'contribution_status_id',
781 ts('Contribution Status'),
782 $status,
783 FALSE
784 );
785
786 // add various dates
787 $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
788
789 if ($this->_online) {
790 $this->assign('hideCalender', TRUE);
791 }
792 $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']);
793
794 $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime'));
795 $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime'));
796
797 $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']);
798
799 $recurJs = NULL;
800 if ($buildRecurBlock) {
801 $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
802 }
803 $element = $this->add('select',
804 'payment_processor_id',
805 ts('Payment Processor'),
806 $this->_processors,
807 NULL,
808 $recurJs
809 );
810
811 if ($this->_online) {
812 $element->freeze();
813 }
814 $totalAmount = NULL;
815 if (empty($this->_lineItems)) {
816 $buildPriceSet = FALSE;
9da8dc8c 817 $priceSets = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute');
6a488035
TO
818 if (!empty($priceSets) && !$this->_ppID) {
819 $buildPriceSet = TRUE;
820 }
821
822 // don't allow price set for contribution if it is related to participant, or if it is a pledge payment
823 // and if we already have line items for that participant. CRM-5095
824 if ($buildPriceSet && $this->_id) {
825 $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id);
826 $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
827 $this->_id,
828 'id',
829 'contribution_id'
830 );
831 if ($pledgePaymentId) {
832 $buildPriceSet = FALSE;
833 }
834 if ($participantID = CRM_Utils_Array::value('participant', $componentDetails)) {
835 $participantLI = CRM_Price_BAO_LineItem::getLineItems($participantID);
836 if (!CRM_Utils_System::isNull($participantLI)) {
837 $buildPriceSet = FALSE;
838 }
839 }
840 }
841
842 $hasPriceSets = FALSE;
843 if ($buildPriceSet) {
844 $hasPriceSets = TRUE;
845 $element = $this->add('select', 'price_set_id', ts('Choose price set'),
846 array(
847 '' => ts('Choose price set')
848 ) + $priceSets,
849 NULL, array('onchange' => "buildAmount( this.value );")
5a230af7 850 );
6a488035
TO
851 if ($this->_online && !($this->_action & CRM_Core_Action::UPDATE)) {
852 $element->freeze();
853 }
854 }
855 $this->assign('hasPriceSets', $hasPriceSets);
856 $currencyFreeze = FALSE;
857 if (!($this->_action & CRM_Core_Action::UPDATE)) {
d37ade2e 858 if ($this->_online || $this->_ppID) {
859 $attributes['total_amount'] = array_merge($attributes['total_amount'], array(
860 'READONLY' => TRUE,
861 'style' => "background-color:#EBECE4"
862 ));
863 $optionTypes = array(
864 '1' => ts('Adjust Pledge Payment Schedule?'),
865 '2' => ts('Adjust Total Pledge Amount?'),
866 );
867 $this->addRadio('option_type',
868 NULL,
869 $optionTypes,
870 array(), '<br/>'
871 );
872
873 $currencyFreeze = TRUE;
874 }
6a488035
TO
875 }
876
877 $totalAmount = $this->addMoney('total_amount',
878 ts('Total Amount'),
879 ($hasPriceSets) ? FALSE : TRUE,
880 $attributes['total_amount'],
881 TRUE, 'currency', NULL, $currencyFreeze
882 );
883 }
884
885 $this->add('text', 'source', ts('Source'), CRM_Utils_Array::value('source', $attributes));
886
887 //CRM-7362 --add campaigns.
888 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
889
0baed70b 890 CRM_Contribute_Form_SoftCredit::buildQuickForm($this);
0baed70b 891
6a488035
TO
892 $js = NULL;
893 if (!$this->_mode) {
894 $js = array('onclick' => "return verify( );");
895 }
896
897 $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
898 'mailing_backend'
899 );
900 $this->assign('outBound_option', $mailingInfo['outBound_option']);
901
902 $this->addButtons(array(
903 array(
904 'type' => 'upload',
905 'name' => ts('Save'),
906 'js' => $js,
907 'isDefault' => TRUE
908 ),
909 array(
910 'type' => 'upload',
911 'name' => ts('Save and New'),
912 'js' => $js,
913 'subName' => 'new'
914 ),
915 array(
916 'type' => 'cancel',
917 'name' => ts('Cancel')
918 ),
919 )
920 );
921
922 // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type,
923 // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail
924 if ($this->_id && $this->_values['contribution_status_id'] == array_search('Cancelled', $statusName)) {
5a230af7 925 if ($totalAmount) {
6a488035
TO
926 $totalAmount->freeze();
927 }
928 $checkNumber->freeze();
929 $paymentInstrument->freeze();
930 $trxnId->freeze();
931 $financialType->freeze();
932 }
933
934 $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this);
935
936 if ($this->_action & CRM_Core_Action::VIEW) {
937 $this->freeze();
938 }
939 }
940
941 /**
942 * global form rule
943 *
2a6da8d7
EM
944 * @param array $fields the input form values
945 * @param array $files the uploaded files if any
946 * @param $self
947 *
948 * @internal param array $options additional user data
6a488035
TO
949 *
950 * @return true if no errors, else array of errors
951 * @access public
952 * @static
953 */
954 static function formRule($fields, $files, $self) {
955 $errors = array();
956
6a488035
TO
957 //check for Credit Card Contribution.
958 if ($self->_mode) {
959 if (empty($fields['payment_processor_id'])) {
960 $errors['payment_processor_id'] = ts('Payment Processor is a required field.');
961 }
962 }
963
964 // do the amount validations.
a7488080 965 if (empty($fields['total_amount']) && empty($self->_lineItems)) {
6a488035 966 if ($priceSetId = CRM_Utils_Array::value('price_set_id', $fields)) {
9da8dc8c 967 CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $fields, $errors);
6a488035
TO
968 }
969 }
970
b326919b 971 $softErrors = CRM_Contribute_Form_SoftCredit::formRule($fields, $errors, $self);
d37ade2e 972
b29eb760
JM
973 // If we have a net amount or fee amount that is NOT set to 0.00, then ensure
974 // that the net_amount + the fee_amount is equal to the total amount.
975 $total_amount = NULL;
976 if(!empty($fields['total_amount']) && $fields['total_amount'] != '0.00') {
977 $total_amount = $fields['total_amount'];
978 }
979 $fee_amount = NULL;
980 if(!empty($fields['fee_amount']) && $fields['fee_amount'] != '0.00') {
981 $fee_amount = $fields['fee_amount'];
982 }
983 $net_amount = NULL;
984 if(!empty($fields['net_amount']) && $fields['net_amount'] != '0.00') {
985 $net_amount = $fields['net_amount'];
986 }
987 if ($total_amount && ($net_amount || $fee_amount)) {
988 $sum = CRM_Utils_Rule::cleanMoney($net_amount) + CRM_Utils_Rule::cleanMoney($fee_amount);
989 if (CRM_Utils_Rule::cleanMoney($total_amount) != $sum) {
6a488035
TO
990 $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
991 }
992 }
993 //form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
994 if ($self->_id && $self->_values['contribution_status_id'] != $fields['contribution_status_id']) {
995 CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors);
996 }
997
998 //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
a7488080 999 if (!empty($fields['fee_amount']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($fields['financial_type_id'])) {
51fa20cb 1000 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
6a488035 1001 }
133e2c99 1002
7272e4fa
DG
1003 // $trxn_id must be unique CRM-13919
1004 if (!empty($fields['trxn_id'])) {
1005 $queryParams = array(1 => array($fields['trxn_id'], 'String'));
1006 $query = 'select count(*) from civicrm_contribution where trxn_id = %1';
1007 if ($self->_id) {
1008 $queryParams[2] = array((int)$self->_id, 'Integer');
1009 $query .= ' and id !=%2';
1010 }
1011 $tCnt = CRM_Core_DAO::singleValueQuery($query, $queryParams);
1012 if ($tCnt) {
1013 $errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id']));
133e2c99 1014 }
7272e4fa 1015 }
d37ade2e 1016
ac0c89ed 1017 $errors = array_merge($errors, $softErrors);
6a488035
TO
1018 return $errors;
1019 }
1020
1021 /**
1022 * Function to process the form
1023 *
1024 * @access public
1025 *
1026 * @return void
1027 */
1028 public function postProcess() {
1029 $session = CRM_Core_Session::singleton();
d77a0a58
EM
1030 $sendReceipt = $pId = $contribution = FALSE;
1031 $softParams = $softIDs =array();
1032
6a488035
TO
1033 if ($this->_action & CRM_Core_Action::DELETE) {
1034 CRM_Contribute_BAO_Contribution::deleteContribution($this->_id);
1035 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
1036 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
1037 ));
1038 return;
1039 }
1040
1041 // get the submitted form values.
1042 $submittedValues = $this->controller->exportValues($this->_name);
a7488080 1043 if (!empty($submittedValues['price_set_id']) && $this->_action & CRM_Core_Action::UPDATE) {
d37ade2e 1044 $line = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution');
6a488035 1045 $lineID = key($line);
9da8dc8c 1046 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('price_field_id', $line[$lineID]), 'price_set_id');
1047 $quickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
6a488035
TO
1048 if ($quickConfig) {
1049 CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_contribution');
1050 }
1051 }
1052
a32709be
PB
1053 // assign dataArray for contribution receipts
1054 $dataArray = array();
1055 foreach ($this->_lineItems as $key => $value) {
1056 foreach ($value as $v) {
d9e4ebe7
PB
1057 if (isset($dataArray[(string)$v['tax_rate']])) {
1058 $dataArray[(string)$v['tax_rate']] = $dataArray[(string)$v['tax_rate']] + CRM_Utils_Array::value('tax_amount', $v);
a32709be
PB
1059 }
1060 else {
d9e4ebe7 1061 $dataArray[(string)$v['tax_rate']] = CRM_Utils_Array::value('tax_amount', $v);
a32709be
PB
1062 }
1063 }
1064 }
1065 $smarty = CRM_Core_Smarty::singleton();
1066 $smarty->assign('dataArray', $dataArray);
1067
6a488035
TO
1068 // process price set and get total amount and line items.
1069 $lineItem = array();
6a488035
TO
1070 $priceSetId = CRM_Utils_Array::value('price_set_id', $submittedValues);
1071 if (empty($priceSetId) && !$this->_id) {
9da8dc8c 1072 $this->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
1073 $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
6a488035
TO
1074 $fieldID = key($this->_priceSet['fields']);
1075 $fieldValueId = key($this->_priceSet['fields'][$fieldID]['options']);
1076 $this->_priceSet['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount'];
1077 $submittedValues['price_' . $fieldID] = 1;
1078 }
1079
1080 if ($priceSetId) {
9da8dc8c 1081 CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'],
5a230af7 1082 $submittedValues, $lineItem[$priceSetId]);
1083
6a488035
TO
1084 $submittedValues['total_amount'] = CRM_Utils_Array::value('amount', $submittedValues);
1085 }
1086 if ($this->_id) {
1087 //CRM-10964
5a230af7 1088 $pId = ($this->_compId && $this->_context == 'participant') ? $this->_compId : CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'participant_id', 'contribution_id');
6a488035 1089 }
8cc574cf 1090 if (!$priceSetId && !empty($submittedValues['total_amount']) && $this->_id) {
6a488035
TO
1091 // 10117 update th line items for participants
1092 if ($pId) {
1093 $entityTable = 'participant';
1094 $entityID = $pId;
1095 $participantParams = array(
1096 'fee_amount' => $submittedValues['total_amount'],
1097 'id' => $entityID
1098 );
1099 CRM_Event_BAO_Participant::add($participantParams);
1100 if (empty($this->_lineItems)) {
01e72fbc 1101 $this->_lineItems[] = CRM_Price_BAO_LineItem::getLineItems($entityID, 'participant', 1);
6a488035 1102 }
d37ade2e 1103 }
1104 else {
6a488035
TO
1105 $entityTable = 'contribution';
1106 $entityID = $this->_id;
1107 }
1108
1109 $lineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, $entityTable);
5af5956c
CB
1110 foreach (array_keys($lineItems) as $id) {
1111 $lineItems[$id]['id'] = $id;
1112 }
6a488035 1113 $itemId = key($lineItems);
8cc574cf 1114 if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) {
870f2eec
PJ
1115 $this->_priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
1116 }
1117
1118 if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) {
1119 $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('total_amount', $submittedValues));
04930780 1120
049db839 1121 // Update line total and total amount with tax on edit
04930780 1122 $financialItemsId = CRM_Core_PseudoConstant::getTaxRates();
1123 if (array_key_exists($submittedValues['financial_type_id'], $financialItemsId)) {
1124 $lineItems[$itemId]['tax_rate'] = $financialItemsId[$submittedValues['financial_type_id']];
1125 }
1126 else {
1127 $lineItems[$itemId]['tax_rate'] = $lineItems[$itemId]['tax_amount'] = "" ;
1128 $submittedValues['tax_amount'] = 'null';
1129 }
1130 if ($lineItems[$itemId]['tax_rate']) {
1131 $lineItems[$itemId]['tax_amount'] = ($lineItems[$itemId]['tax_rate']/100) * $lineItems[$itemId]['line_total'];
1132 $submittedValues['total_amount'] = $lineItems[$itemId]['line_total'] + $lineItems[$itemId]['tax_amount'];
1133 $submittedValues['tax_amount'] = $lineItems[$itemId]['tax_amount'];
049db839 1134 }
6a488035 1135 }
6a488035 1136 // 10117 update th line items for participants
32cb2feb 1137 if (!empty($lineItems[$itemId]['price_field_id'])) {
32cb2feb
CW
1138 $lineItem[$this->_priceSetId] = $lineItems;
1139 }
6a488035
TO
1140 }
1141 $isQuickConfig = 0;
9da8dc8c 1142 if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) {
6a488035
TO
1143 $isQuickConfig = 1;
1144 }
d77a0a58 1145 //CRM-11529 for quick config back office transactions
5a230af7 1146 //when financial_type_id is passed in form, update the
d77a0a58 1147 //line items with the financial type selected in form
8cc574cf 1148 if ($isQuickConfig && !empty($submittedValues['financial_type_id']) && CRM_Utils_Array::value($this->_priceSetId, $lineItem)
d37ade2e 1149 ) {
6a488035
TO
1150 foreach ($lineItem[$this->_priceSetId] as &$values) {
1151 $values['financial_type_id'] = $submittedValues['financial_type_id'];
1152 }
1153 }
5a230af7 1154
3b557cc8 1155 if (!isset($submittedValues['total_amount'])) {
6a488035
TO
1156 $submittedValues['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_values);
1157 }
1158 $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE);
1159
a7488080 1160 if (!empty($submittedValues['pcp_made_through_id'])) {
912e7aaa 1161 $pcp = array();
d37ade2e 1162 $fields = array(
1163 'pcp_made_through_id',
1164 'pcp_display_in_roll',
1165 'pcp_roll_nickname',
1166 'pcp_personal_note',
1167 );
ce8be7f2 1168 foreach ($fields as $f) {
1169 $pcp[$f] = CRM_Utils_Array::value($f, $submittedValues);
1170 }
cd0a30ac 1171 }
b6545333 1172
1173 $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id']));
1174 if ($this->_id && count($isEmpty) == 1 && key($isEmpty) == NULL) {
1175 //Delete existing soft credit records if soft credit list is empty on update
1176 CRM_Contribute_BAO_ContributionSoft::del(array('contribution_id' => $this->_id));
1177 }
cd0a30ac 1178 else {
b6545333 1179 //build soft credit params
b6545333 1180 foreach ($submittedValues['soft_credit_contact_id'] as $key => $val) {
1181 if ($val && $submittedValues['soft_credit_amount'][$key]) {
1182 $softParams[$key]['contact_id'] = $val;
1183 $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($submittedValues['soft_credit_amount'][$key]);
1184 $softParams[$key]['soft_credit_type_id'] = $submittedValues['soft_credit_type'][$key];
1185 if (!empty($submittedValues['soft_credit_id'][$key])) {
1186 $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key];
cd0a30ac 1187 }
1188 }
d37ade2e 1189 }
cd0a30ac 1190 }
1191
6a488035 1192 // set the contact, when contact is selected
ccec9d6b
CW
1193 if (!empty($submittedValues['contact_id'])) {
1194 $this->_contactID = $submittedValues['contact_id'];
6a488035
TO
1195 }
1196
1197 $config = CRM_Core_Config::singleton();
1198
1199 //Credit Card Contribution.
1200 if ($this->_mode) {
d77a0a58 1201 $this->processCreditCard($submittedValues, $config, $session, $lineItem);
6a488035
TO
1202 }
1203 else {
1204 //Offline Contribution.
1205 $submittedValues = $this->unsetCreditCardFields($submittedValues);
1206
1207 // get the required field value only.
1208 $formValues = $submittedValues;
1209 $params = $ids = array();
1210
1211 $params['contact_id'] = $this->_contactID;
1212
1213 $params['currency'] = $this->getCurrency($submittedValues);
1214
1215 $fields = array(
1216 'financial_type_id',
1217 'contribution_status_id',
1218 'payment_instrument_id',
1219 'cancel_reason',
1220 'source',
1221 'check_number',
6a488035
TO
1222 );
1223 foreach ($fields as $f) {
1224 $params[$f] = CRM_Utils_Array::value($f, $formValues);
1225 }
ce8be7f2 1226
1227 if (!empty($pcp)) {
3f662d9d 1228 $params['pcp'] = $pcp;
ce8be7f2 1229 }
cd0a30ac 1230 if (!empty($softParams)) {
3f662d9d 1231 $params['soft_credit'] = $softParams;
2cfc0f58 1232 $params['soft_credit_ids'] = $softIDs;
6a488035 1233 }
3f662d9d 1234
6a488035
TO
1235 //if priceset is used, no need to cleanup money
1236 //CRM-5740
1237 if ($priceSetId) {
1238 $params['skipCleanMoney'] = 1;
1239 }
1240
1241 $dates = array(
1242 'receive_date',
1243 'receipt_date',
1244 'cancel_date',
1245 );
1246
1247 foreach ($dates as $d) {
1248 $params[$d] = CRM_Utils_Date::processDate($formValues[$d], $formValues[$d . '_time'], TRUE);
1249 }
1250
a7488080 1251 if (!empty($formValues['is_email_receipt'])) {
6a488035
TO
1252 $params['receipt_date'] = date("Y-m-d");
1253 }
1254
1255 if ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Cancelled', 'name')
d37ade2e 1256 || $params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name')
1257 ) {
6a488035 1258 if (CRM_Utils_System::isNull(CRM_Utils_Array::value('cancel_date', $params))) {
d37ade2e 1259 $params['cancel_date'] = date('Y-m-d');
6a488035
TO
1260 }
1261 }
1262 else {
d37ade2e 1263 $params['cancel_date'] = $params['cancel_reason'] = 'null';
6a488035
TO
1264 }
1265
1266 // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996
5f6d46e1 1267 // else if contribution_status is changed to Completed is_pay_later flag is changed to 0, CRM-15041
6a488035
TO
1268 if ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name')) {
1269 $params['is_pay_later'] = 1;
1270 }
5f6d46e1 1271 elseif ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) {
1272 $params['is_pay_later'] = 0;
1273 }
6a488035
TO
1274
1275 $ids['contribution'] = $params['id'] = $this->_id;
1276
1277 //Add Additional common information to formatted params
1278 CRM_Contribute_Form_AdditionalInfo::postProcessCommon($formValues, $params, $this);
1279 if ($pId) {
1280 $params['contribution_mode'] = 'participant';
1281 $params['participant_id'] = $pId;
464bb009 1282 $params['skipLineItem'] = 1;
6a488035
TO
1283 }
1284 $params['line_item'] = $lineItem;
1285 $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array::value('id', $this->_paymentProcessor);
d91b8b33 1286 if (CRM_Utils_Array::value('tax_amount', $submittedValues)) {
1287 $params['tax_amount'] = $submittedValues['tax_amount'];
1288 }
6a488035
TO
1289 //create contribution.
1290 if ($isQuickConfig) {
1291 $params['is_quick_config'] = 1;
1292 }
1293
1294 // CRM-11956
d77a0a58 1295 // if non_deductible_amount exists i.e. Additional Details field set was opened [and staff typed something] -
6a488035
TO
1296 // if non_deductible_amount does NOT exist - then calculate it depending on:
1297 // $ContributionType->is_deductible and whether there is a product (premium).
d77a0a58 1298 if (!isset($params['non_deductible_amount']) || (!empty($params['non_deductible_amount']))) {
6a488035
TO
1299 $contributionType = new CRM_Financial_DAO_FinancialType();
1300 $contributionType->id = $params['financial_type_id'];
1301 if (!$contributionType->find(TRUE)) {
1302 CRM_Core_Error::fatal('Could not find a system table');
1303 }
1304 if ($contributionType->is_deductible) {
1305
1306 if (isset($formValues['product_name'][0])) {
1307 $selectProduct = $formValues['product_name'][0];
1308 }
1309 // if there is a product - compare the value to the contribution amount
d37ade2e 1310 if (isset($selectProduct)) {
6a488035
TO
1311 $productDAO = new CRM_Contribute_DAO_Product();
1312 $productDAO->id = $selectProduct;
1313 $productDAO->find(TRUE);
1314 // product value exceeds contribution amount
1315 if ($params['total_amount'] < $productDAO->price) {
1316 $params['non_deductible_amount'] = $params['total_amount'];
1317 }
1318 // product value does NOT exceed contribution amount
1319 else {
1320 $params['non_deductible_amount'] = $productDAO->price;
1321 }
1322 }
1323 // contribution is deductible - but there is no product
1324 else {
1325 $params['non_deductible_amount'] = '0.00';
1326 }
1327 }
1328 // contribution is NOT deductible
1329 else {
1330 $params['non_deductible_amount'] = $params['total_amount'];
1331 }
1332 }
1333
1334 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
1335
1336 // process associated membership / participant, CRM-4395
1337 $relatedComponentStatusMsg = NULL;
1338 if ($contribution->id && $this->_action & CRM_Core_Action::UPDATE) {
1339 $relatedComponentStatusMsg = $this->updateRelatedComponent($contribution->id,
1340 $contribution->contribution_status_id,
1341 CRM_Utils_Array::value('contribution_status_id',
1342 $this->_values
1343 )
1344 );
1345 }
1346
1347 //process note
1348 if ($contribution->id && isset($formValues['note'])) {
1349 CRM_Contribute_Form_AdditionalInfo::processNote($formValues, $this->_contactID, $contribution->id, $this->_noteID);
1350 }
1351
1352 //process premium
5a230af7 1353 if ($contribution->id && isset($formValues['product_name'][0])) {
6a488035
TO
1354 CRM_Contribute_Form_AdditionalInfo::processPremium($formValues, $contribution->id,
1355 $this->_premiumID, $this->_options
1356 );
1357 }
1358
1359 //send receipt mail.
8cc574cf 1360 if ($contribution->id && !empty($formValues['is_email_receipt'])) {
6a488035
TO
1361 $formValues['contact_id'] = $this->_contactID;
1362 $formValues['contribution_id'] = $contribution->id;
1363
1421174e 1364 $formValues += CRM_Contribute_BAO_ContributionSoft::getSoftContribution($contribution->id);
1365
6a488035
TO
1366 // to get 'from email id' for send receipt
1367 $this->fromEmailId = $formValues['from_email_address'];
1368 $sendReceipt = CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $formValues);
1369 }
1370
1371 $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1372 $contribution->id,
1373 'id',
1374 'contribution_id'
1375 );
1376
1377 //update pledge payment status.
1378 if ((($this->_ppID && $contribution->id) && $this->_action & CRM_Core_Action::ADD) ||
1379 (($pledgePaymentId) && $this->_action & CRM_Core_Action::UPDATE)
1380 ) {
1381
1382 if ($this->_ppID) {
1383 //store contribution id in payment record.
1384 CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id);
1385 }
1386 else {
1387 $this->_ppID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1388 $contribution->id,
1389 'id',
1390 'contribution_id'
1391 );
1392 $this->_pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1393 $contribution->id,
1394 'pledge_id',
1395 'contribution_id'
1396 );
1397 }
1398
1399 $adjustTotalAmount = FALSE;
1400 if (CRM_Utils_Array::value('option_type', $formValues) == 2) {
1401 $adjustTotalAmount = TRUE;
1402 }
1403
1404 $updatePledgePaymentStatus = FALSE;
1405 //do only if either the status or the amount has changed
1406 if ($this->_action & CRM_Core_Action::ADD) {
1407 $updatePledgePaymentStatus = TRUE;
1408 }
1409 elseif ($this->_action & CRM_Core_Action::UPDATE && (($this->_defaults['contribution_status_id'] != $formValues['contribution_status_id']) ||
1410 ($this->_defaults['total_amount'] != $formValues['total_amount']))
1411 ) {
1412 $updatePledgePaymentStatus = TRUE;
1413 }
1414
1415 if ($updatePledgePaymentStatus) {
1416 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID,
1417 array($this->_ppID),
1418 $contribution->contribution_status_id,
1419 NULL,
1420 $contribution->total_amount,
1421 $adjustTotalAmount
1422 );
1423 }
1424 }
5a230af7 1425
6a488035 1426 $statusMsg = ts('The contribution record has been saved.');
a7488080 1427 if (!empty($formValues['is_email_receipt']) && $sendReceipt) {
6a488035
TO
1428 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1429 }
5a230af7 1430
6a488035
TO
1431 if ($relatedComponentStatusMsg) {
1432 $statusMsg .= ' ' . $relatedComponentStatusMsg;
1433 }
5a230af7 1434
6a488035
TO
1435 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
1436 //Offline Contribution ends.
5a230af7 1437 }
6a488035
TO
1438 $buttonName = $this->controller->getButtonName();
1439 if ($this->_context == 'standalone') {
1440 if ($buttonName == $this->getButtonName('upload', 'new')) {
1441 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contribute/add',
1442 'reset=1&action=add&context=standalone'
1443 ));
1444 }
1445 else {
1446 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
1447 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
1448 ));
1449 }
1450 }
1451 elseif ($this->_context == 'contribution' && $this->_mode && $buttonName == $this->getButtonName('upload', 'new')) {
1452 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution',
1453 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}"
1454 ));
1455 }
1456 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
1457 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution',
1458 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}"
1459 ));
1460 }
28d44c9f
BS
1461
1462 //store contribution ID if not yet set (on create)
1463 if ( empty($this->_id) && !empty($contribution->id) ) {
1464 $this->_id = $contribution->id;
1465 }
6a488035 1466 }
5a230af7 1467
d77a0a58
EM
1468 /**
1469 * @param $submittedValues
1470 * @param $config
1471 * @param $session CRM_Core_Session
1472 * @param $lineItem
1473 *
1474 * @throws CRM_Core_Exception
1475 */
1476 public function processCreditCard($submittedValues, $config, $session, $lineItem) {
1477 $sendReceipt = $contribution = FALSE;
6a488035
TO
1478 $unsetParams = array(
1479 'trxn_id',
1480 'payment_instrument_id',
1481 'contribution_status_id',
1482 'cancel_date',
1483 'cancel_reason',
1484 );
1485 foreach ($unsetParams as $key) {
1486 if (isset($submittedValues[$key])) {
1487 unset($submittedValues[$key]);
1488 }
1489 }
4bd318e0 1490 $isTest = ($this->_mode == 'test') ? 1 : 0;
25e92485
PN
1491 // CRM-12680 set $_lineItem if its not set
1492 if (empty($this->_lineItem) && !empty($lineItem)) {
1493 $this->_lineItem = $lineItem;
1494 }
eea16664 1495
5032bf07 1496 //Get the require fields value only.
6a488035
TO
1497 $params = $this->_params = $submittedValues;
1498
1499 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'],
1500 $this->_mode
1501 );
1502
1503 //get the payment processor id as per mode.
8ef12e64 1504 $this->_params['payment_processor'] = $params['payment_processor_id'] =
78b79549 1505 $this->_params['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor['id'];
6a488035
TO
1506
1507 $now = date('YmdHis');
1508 $fields = array();
1509
1510 // we need to retrieve email address
8cc574cf 1511 if ($this->_context == 'standalone' && !empty($submittedValues['is_email_receipt'])) {
6a488035
TO
1512 list($this->userDisplayName,
1513 $this->userEmail
1514 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
1515 $this->assign('displayName', $this->userDisplayName);
1516 }
1517
1518 //set email for primary location.
1519 $fields['email-Primary'] = 1;
1520 $params['email-Primary'] = $this->userEmail;
1521
1522 // now set the values for the billing location.
d77a0a58 1523 foreach (array_keys($this->_fields) as $name) {
6a488035
TO
1524 $fields[$name] = 1;
1525 }
1526
1527 // also add location name to the array
1528 $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);
1529 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
1530 $fields["address_name-{$this->_bltID}"] = 1;
1531
1532 $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
1533 $this->_contactID,
1534 'contact_type'
1535 );
1536
1537 $nameFields = array('first_name', 'middle_name', 'last_name');
1538 foreach ($nameFields as $name) {
1539 $fields[$name] = 1;
1540 if (array_key_exists("billing_$name", $params)) {
1541 $params[$name] = $params["billing_{$name}"];
1542 $params['preserveDBName'] = TRUE;
1543 }
1544 }
1545
a7488080 1546 if (!empty($params['source'])) {
6a488035
TO
1547 unset($params['source']);
1548 }
1549 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields,
1550 $this->_contactID,
1551 NULL, NULL,
1552 $ctype
1553 );
1554
5032bf07 1555 // add all the additional payment params we need
6a488035
TO
1556 $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}"]);
1557 $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]);
1558
1559 if ($this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_CREDIT_CARD) {
1560 $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params);
1561 $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params);
1562 }
1563 $this->_params['ip_address'] = CRM_Utils_System::ipAddress();
1564 $this->_params['amount'] = $this->_params['total_amount'];
1565 $this->_params['amount_level'] = 0;
1566 $this->_params['currencyID'] = CRM_Utils_Array::value('currency',
1567 $this->_params,
1568 $config->defaultCurrency
1569 );
1570 $this->_params['payment_action'] = 'Sale';
a7488080 1571 if (!empty($this->_params['receive_date'])) {
6a488035
TO
1572 $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['receive_date'], $this->_params['receive_date_time']);
1573 }
1574
a7488080 1575 if (!empty($params['soft_credit_to'])) {
6a488035
TO
1576 $this->_params['soft_credit_to'] = $params['soft_credit_to'];
1577 $this->_params['pcp_made_through_id'] = $params['pcp_made_through_id'];
1578 }
1579
1580 $this->_params['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $params);
1581 $this->_params['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $params);
1582 $this->_params['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $params);
1583
1584 //Add common data to formatted params
1585 CRM_Contribute_Form_AdditionalInfo::postProcessCommon($params, $this->_params, $this);
1586
1587 if (empty($this->_params['invoice_id'])) {
1588 $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
1589 }
1590 else {
1591 $this->_params['invoiceID'] = $this->_params['invoice_id'];
1592 }
1593
1594 // at this point we've created a contact and stored its address etc
1595 // all the payment processors expect the name and address to be in the
1596 // so we copy stuff over to first_name etc.
1597 $paymentParams = $this->_params;
1598 $paymentParams['contactID'] = $this->_contactID;
1599 CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE);
1600
1601 $contributionType = new CRM_Financial_DAO_FinancialType();
1602 $contributionType->id = $params['financial_type_id'];
1603 if (!$contributionType->find(TRUE)) {
1604 CRM_Core_Error::fatal('Could not find a system table');
1605 }
1606
1607 // add some financial type details to the params list
1608 // if folks need to use it
1609 $paymentParams['contributionType_name'] = $this->_params['contributionType_name'] = $contributionType->name;
1610 $paymentParams['contributionPageID'] = NULL;
a7488080 1611 if (!empty($this->_params['is_email_receipt'])) {
6a488035
TO
1612 $paymentParams['email'] = $this->userEmail;
1613 $paymentParams['is_email_receipt'] = 1;
1614 }
1615 else {
1616 $paymentParams['is_email_receipt'] = 0;
1617 $this->_params['is_email_receipt'] = 0;
1618 }
a7488080 1619 if (!empty($this->_params['receive_date'])) {
6a488035
TO
1620 $paymentParams['receive_date'] = $this->_params['receive_date'];
1621 }
6a488035 1622
6a488035
TO
1623 // For recurring contribution, create Contribution Record first.
1624 // Contribution ID, Recurring ID and Contact ID needed
1625 // When we get a callback from the payment processor, CRM-7115
a7488080 1626 if (!empty($paymentParams['is_recur'])) {
6a488035
TO
1627 $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($this,
1628 $this->_params,
5901ddf9 1629 NULL,
6a488035
TO
1630 $this->_contactID,
1631 $contributionType,
6a488035 1632 TRUE,
4bd318e0
EM
1633 FALSE,
1634 $isTest,
1635 $this->_lineItem
6a488035
TO
1636 );
1637 $paymentParams['contributionID'] = $contribution->id;
1638 $paymentParams['contributionTypeID'] = $contribution->financial_type_id;
1639 $paymentParams['contributionPageID'] = $contribution->contribution_page_id;
1640 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id;
1641 }
5901ddf9 1642 $result = NULL;
6a488035 1643 if ($paymentParams['amount'] > 0.0) {
d77a0a58 1644 // force a re-get of the payment processor in case the form changed it, CRM-7179
6a488035
TO
1645 $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this, TRUE);
1646 $result = $payment->doDirectPayment($paymentParams);
1647 }
1648
1649 if (is_a($result, 'CRM_Core_Error')) {
1650 //make sure to cleanup db for recurring case.
a7488080 1651 if (!empty($paymentParams['contributionID'])) {
6a488035
TO
1652 CRM_Core_Error::debug_log_message(CRM_Core_Error::getMessages($result) . "contact id={$this->_contactID} (deleting contribution {$paymentParams['contributionID']}");
1653 CRM_Contribute_BAO_Contribution::deleteContribution($paymentParams['contributionID']);
1654 }
a7488080 1655 if (!empty($paymentParams['contributionRecurID'])) {
6a488035
TO
1656 CRM_Core_Error::debug_log_message(CRM_Core_Error::getMessages($result) . "contact id={$this->_contactID} (deleting recurring contribution {$paymentParams['contributionRecurID']}");
1657 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']);
1658 }
1659
1660 //set the contribution mode.
1661 $urlParams = "action=add&cid={$this->_contactID}";
1662 if ($this->_mode) {
1663 $urlParams .= "&mode={$this->_mode}";
1664 }
1665 if (!empty($this->_ppID)) {
1666 $urlParams .= "&context=pledge&ppid={$this->_ppID}";
1667 }
1668 CRM_Core_Error::displaySessionError($result);
1669 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams));
1670 }
1671
1672 if ($result) {
1673 $this->_params = array_merge($this->_params, $result);
1674 }
1675
1676 $this->_params['receive_date'] = $now;
1677
a7488080 1678 if (!empty($this->_params['is_email_receipt'])) {
6a488035
TO
1679 $this->_params['receipt_date'] = $now;
1680 }
1681 else {
1682 $this->_params['receipt_date'] = CRM_Utils_Date::processDate($this->_params['receipt_date'],
1683 $params['receipt_date_time'], TRUE
1684 );
1685 }
1686
1687 $this->set('params', $this->_params);
1688 $this->assign('trxn_id', $result['trxn_id']);
1689 $this->assign('receive_date', $this->_params['receive_date']);
1690
1691 // result has all the stuff we need
1692 // lets archive it to a financial transaction
1693 if ($contributionType->is_deductible) {
1694 $this->assign('is_deductible', TRUE);
1695 $this->set('is_deductible', TRUE);
1696 }
1697
1698 // set source if not set
1699 if (empty($this->_params['source'])) {
1700 $userID = $session->get('userID');
1701 $userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID,
1702 'sort_name'
1703 );
1704 $this->_params['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
1705 }
1706
1707 // build custom data getFields array
1708 $customFieldsContributionType = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE,
1709 CRM_Utils_Array::value('financial_type_id', $params)
1710 );
1711 $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsContributionType,
1712 CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, TRUE)
1713 );
1714 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
1715 $customFields,
1716 $this->_id,
1717 'Contribution'
1718 );
1719
1720
a7488080 1721 if (empty($paymentParams['is_recur'])) {
6a488035
TO
1722 $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($this,
1723 $this->_params,
1724 $result,
1725 $this->_contactID,
1726 $contributionType,
4bd318e0
EM
1727 FALSE, FALSE,
1728 $isTest,
1729 $this->_lineItem
6a488035
TO
1730 );
1731 }
1732
1733 //send receipt mail.
8cc574cf 1734 if ($contribution->id && !empty($this->_params['is_email_receipt'])) {
6a488035
TO
1735 $this->_params['trxn_id'] = CRM_Utils_Array::value('trxn_id', $result);
1736 $this->_params['contact_id'] = $this->_contactID;
1737 $this->_params['contribution_id'] = $contribution->id;
1738 $sendReceipt = CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $this->_params, TRUE);
1739 }
1740
1741 //process the note
1742 if ($contribution->id && isset($params['note'])) {
1743 CRM_Contribute_Form_AdditionalInfo::processNote($params, $contactID, $contribution->id, NULL);
1744 }
1745 //process premium
1746 if ($contribution->id && isset($params['product_name'][0])) {
1747 CRM_Contribute_Form_AdditionalInfo::processPremium($params, $contribution->id, NULL, $this->_options);
1748 }
1749
1750 //update pledge payment status.
1751 if ($this->_ppID && $contribution->id) {
1752 //store contribution id in payment record.
1753 CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id);
1754
1755 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID,
1756 array($this->_ppID),
1757 $contribution->contribution_status_id,
1758 NULL,
1759 $contribution->total_amount
1760 );
1761 }
1762
1763 if ($contribution->id) {
1764 $statusMsg = ts('The contribution record has been processed.');
a7488080 1765 if (!empty($this->_params['is_email_receipt']) && $sendReceipt) {
6a488035
TO
1766 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1767 }
1768 CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success');
1769 }
1770 }
1771}