3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class generates form components for processing a contribution
40 class CRM_Contribute_Form_Contribution
extends CRM_Contribute_Form_AbstractEditPayment
{
42 * the id of the contribution that we are proceessing
50 * the id of the premium that we are proceessing
55 public $_premiumID = NULL;
56 public $_productDAO = NULL;
67 * the id of the contact associated with this contribution
75 * the id of the pledge payment that we are processing
83 * the id of the pledge that we are processing
91 * is this contribution associated with an online
92 * financial transaction
97 public $_online = FALSE;
100 * Stores all product option
108 * stores the honor id
113 public $_honorID = NULL;
116 * Store the contribution Type ID
120 public $_contributionType;
123 * The contribution values if an existing contribution
128 * The pledge values if this contribution is associated with pledge
130 public $_pledgeValues;
132 public $_contributeMode = 'direct';
139 * Store the line items if price set used.
144 * @var soft credit info
146 public $_softCreditInfo;
148 protected $_formType;
152 * Function to set variables up before form is built
157 public function preProcess() {
159 //check permission for action.
160 if (!CRM_Core_Permission
::checkActionPermission('CiviContribute', $this->_action
)) {
161 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
164 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
166 $this->assign('cdType', FALSE);
167 if ($this->_cdType
) {
168 $this->assign('cdType', TRUE);
169 CRM_Custom_Form_CustomData
::preProcess($this);
173 $config = CRM_Core_Config
::singleton();
174 $resources = CRM_Core_Resources
::singleton();
175 $resources->addScriptFile('civicrm', 'templates/CRM/Contribute/Form/SoftCredit.js');
176 $resources->addSetting(array('monetaryThousandSeparator' => $config->monetaryThousandSeparator
));
178 $this->_formType
= CRM_Utils_Array
::value('formType', $_GET);
181 $this->_priceSetId
= CRM_Utils_Array
::value('priceSetId', $_GET);
182 $this->set('priceSetId', $this->_priceSetId
);
183 $this->assign('priceSetId', $this->_priceSetId
);
185 //get the pledge payment id
186 $this->_ppID
= CRM_Utils_Request
::retrieve('ppid', 'Positive', $this);
189 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
192 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
193 $this->assign('action', $this->_action
);
195 //get the contribution id if update
196 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
197 if (!empty($this->_id
)) {
198 $this->assign('contribID', $this->_id
);
201 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
202 $this->assign('context', $this->_context
);
204 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
206 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
208 //set the contribution mode.
209 $this->_mode
= CRM_Utils_Request
::retrieve('mode', 'String', $this);
211 $this->assign('contributionMode', $this->_mode
);
213 $this->_paymentProcessor
= array('billing_mode' => 1);
215 $this->assign('showCheckNumber', TRUE);
217 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
218 $this->assignProcessors();
220 if ($this->_contactID
) {
221 list($this->userDisplayName
, $this->userEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
222 $this->assign('displayName', $this->userDisplayName
);
225 // also check for billing information
226 // get the billing location type
227 $this->assignBillingType();
229 $this->_fields
= array();
231 CRM_Core_Payment_Form
::setPaymentFieldsByType(CRM_Utils_Array
::value('payment_type', $this->_processors
), $this);
233 if ($this->_action
& CRM_Core_Action
::DELETE
) {
237 if (in_array('CiviPledge', $config->enableComponents
) && !$this->_formType
) {
238 $this->preProcessPledge();
241 $this->_values
= array();
243 // current contribution id
245 $this->assignPremiumProduct($this->_id
);
246 $this->buildValuesAndAssignOnline_Note_Type($this->_id
, $this->_values
);
249 // when custom data is included in this page
250 if (CRM_Utils_Array
::value('hidden_custom', $_POST)) {
251 $this->applyCustomData('Contribution', CRM_Utils_Array
::value('financial_type_id', $_POST), $this->_id
);
254 $this->_lineItems
= array();
256 if (!empty($this->_compId
) && $this->_compContext
== 'participant') {
257 $this->assign('compId', $this->_compId
);
258 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_compId
);
261 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution', 1);
263 empty($lineItem) ?
NULL : $this->_lineItems
[] = $lineItem;
266 $this->assign('lineItem', empty($this->_lineItems
) ?
FALSE : $this->_lineItems
);
269 if ($this->_contactID
) {
270 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactID
);
272 // Check if this is default domain contact CRM-10482
273 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_contactID
)) {
274 $displayName .= ' (' . ts('default organization') . ')';
277 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
278 CRM_Utils_System
::setTitle(ts('Contribution from') . ' ' . $displayName);
282 function setDefaultValues() {
283 if ($this->_cdType
) {
284 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
287 $defaults = $this->_values
;
289 //set defaults for pledge payment.
291 $defaults['total_amount'] = CRM_Utils_Array
::value('scheduled_amount', $this->_pledgeValues
['pledgePayment']);
292 $defaults['honor_type_id'] = CRM_Utils_Array
::value('honor_type_id', $this->_pledgeValues
);
293 $defaults['honor_contact_id'] = CRM_Utils_Array
::value('honor_contact_id', $this->_pledgeValues
);
294 $defaults['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $this->_pledgeValues
);
295 $defaults['currency'] = CRM_Utils_Array
::value('currency', $this->_pledgeValues
);
296 $defaults['option_type'] = 1;
300 if ($this->_action
& CRM_Core_Action
::DELETE
) {
304 // set soft credit defaults
305 CRM_Contribute_Form_SoftCredit
::setDefaultValues($defaults, $this);
308 $config = CRM_Core_Config
::singleton();
309 // set default country from config if no country set
310 if (!CRM_Utils_Array
::value("billing_country_id-{$this->_bltID}", $defaults)) {
311 $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
314 if (!CRM_Utils_Array
::value("billing_state_province_id-{$this->_bltID}", $defaults)) {
315 $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince
;
322 "street_address-{$this->_bltID}",
323 "city-{$this->_bltID}",
324 "postal_code-{$this->_bltID}",
325 "country_id-{$this->_bltID}",
326 "state_province_id-{$this->_bltID}",
327 "state_province-{$this->_bltID}",
328 "country-{$this->_bltID}"
331 foreach ($names as $name) {
335 if ($this->_contactID
) {
336 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactID
, $fields, $defaults);
339 foreach ($names as $name) {
340 if (!empty($defaults[$name])) {
341 $defaults['billing_' . $name] = $defaults[$name];
345 // now fix all state country selectors
346 CRM_Core_BAO_Address
::fixAllStateSelects($this, $defaults);
350 $this->_contactID
= $defaults['contact_id'];
353 // Set $newCredit variable in template to control whether link to credit card mode is included
354 CRM_Core_Payment
::allowBackofficeCreditCard($this);
356 // fix the display of the monetary value, CRM-4038
357 if (isset($defaults['total_amount'])) {
358 $defaults['total_amount'] = CRM_Utils_Money
::format($defaults['total_amount'], NULL, '%a');
361 if (isset($defaults['non_deductible_amount'])) {
362 $defaults['non_deductible_amount'] = CRM_Utils_Money
::format($defaults['non_deductible_amount'], NULL, '%a');
365 if (isset($defaults['fee_amount'])) {
366 $defaults['fee_amount'] = CRM_Utils_Money
::format($defaults['fee_amount'], NULL, '%a');
369 if (isset($defaults['net_amount'])) {
370 $defaults['net_amount'] = CRM_Utils_Money
::format($defaults['net_amount'], NULL, '%a');
373 if ($this->_contributionType
) {
374 $defaults['financial_type_id'] = $this->_contributionType
;
377 if (CRM_Utils_Array
::value('is_test', $defaults)) {
378 $this->assign('is_test', TRUE);
381 if (isset($defaults['honor_contact_id'])) {
382 $honorDefault = $ids = array();
383 $this->_honorID
= $defaults['honor_contact_id'];
384 $honorType = CRM_Core_PseudoConstant
::get('CRM_Contribute_DAO_Contribution', 'honor_type_id');
386 'id' => $defaults['honor_contact_id'],
387 'contact_id' => $defaults['honor_contact_id'],
389 CRM_Contact_BAO_Contact
::retrieve($idParams, $honorDefault, $ids);
391 $defaults['honor_prefix_id'] = CRM_Utils_Array
::value('prefix_id', $honorDefault);
392 $defaults['honor_first_name'] = CRM_Utils_Array
::value('first_name', $honorDefault);
393 $defaults['honor_last_name'] = CRM_Utils_Array
::value('last_name', $honorDefault);
394 $defaults['honor_email'] = CRM_Utils_Array
::value('email', $honorDefault['email'][1]);
395 $defaults['honor_type'] = $honorType[$defaults['honor_type_id']];
398 $this->assign('showOption', TRUE);
399 // for Premium section
400 if ($this->_premiumID
) {
401 $this->assign('showOption', FALSE);
402 $options = isset($this->_options
[$this->_productDAO
->product_id
]) ?
$this->_options
[$this->_productDAO
->product_id
] : "";
404 $this->assign('showOption', TRUE);
406 $options_key = CRM_Utils_Array
::key($this->_productDAO
->product_option
, $options);
408 $defaults['product_name'] = array($this->_productDAO
->product_id
, trim($options_key));
411 $defaults['product_name'] = array($this->_productDAO
->product_id
);
413 if ($this->_productDAO
->fulfilled_date
) {
414 list($defaults['fulfilled_date']) = CRM_Utils_Date
::setDateDefaults($this->_productDAO
->fulfilled_date
);
418 if (isset($this->userEmail
)) {
419 $this->assign('email', $this->userEmail
);
422 if (CRM_Utils_Array
::value('is_pay_later', $defaults)) {
423 $this->assign('is_pay_later', TRUE);
425 $this->assign('contribution_status_id', CRM_Utils_Array
::value('contribution_status_id', $defaults));
427 $dates = array('receive_date', 'receipt_date', 'cancel_date', 'thankyou_date');
428 foreach ($dates as $key) {
429 if (CRM_Utils_Array
::value($key, $defaults)) {
430 list($defaults[$key],
431 $defaults[$key . '_time']
432 ) = CRM_Utils_Date
::setDateDefaults(CRM_Utils_Array
::value($key, $defaults),
438 if (!$this->_id
&& !CRM_Utils_Array
::value('receive_date', $defaults)) {
439 list($defaults['receive_date'],
440 $defaults['receive_date_time']
441 ) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
444 $this->assign('receive_date', CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('receive_date', $defaults),
445 CRM_Utils_Array
::value('receive_date_time', $defaults)
447 $currency = CRM_Utils_Array
::value('currency', $defaults);
448 $this->assign('currency', $currency);
449 // Hack to get currency info to the js layer. CRM-11440
450 CRM_Utils_Money
::format(1);
451 $this->assign('currencySymbol', CRM_Utils_Array
::value($currency, CRM_Utils_Money
::$_currencySymbols));
452 $this->assign('totalAmount', CRM_Utils_Array
::value('total_amount', $defaults));
454 //inherit campaign from pledge.
455 if ($this->_ppID
&& CRM_Utils_Array
::value('campaign_id', $this->_pledgeValues
)) {
456 $defaults['campaign_id'] = $this->_pledgeValues
['campaign_id'];
459 $this->_defaults
= $defaults;
464 * Function to build the form
469 public function buildQuickForm() {
470 if ($this->_cdType
) {
471 CRM_Custom_Form_CustomData
::buildQuickForm($this);
475 // build price set form.
476 $buildPriceSet = FALSE;
477 if (empty($this->_lineItems
) &&
478 ($this->_priceSetId || CRM_Utils_Array
::value('price_set_id', $_POST))
480 $buildPriceSet = TRUE;
481 $getOnlyPriceSetElements = TRUE;
482 if (!$this->_priceSetId
) {
483 $this->_priceSetId
= $_POST['price_set_id'];
484 $getOnlyPriceSetElements = FALSE;
487 $this->set('priceSetId', $this->_priceSetId
);
488 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
490 // get only price set form elements.
491 if ($getOnlyPriceSetElements) {
495 // use to build form during form rule.
496 $this->assign('buildPriceSet', $buildPriceSet);
498 $showAdditionalInfo = FALSE;
500 $defaults = $this->_values
;
501 $additionalDetailFields = array(
505 'non_deductible_amount',
509 foreach ($additionalDetailFields as $key) {
510 if (!empty($defaults[$key])) {
511 $defaults['hidden_AdditionalDetail'] = 1;
516 $honorFields = array(
523 foreach ($honorFields as $key) {
524 if (!empty($defaults[$key])) {
525 $defaults['hidden_Honoree'] = 1;
530 //check for honoree pane.
531 if ($this->_ppID
&& CRM_Utils_Array
::value('honor_contact_id', $this->_pledgeValues
)) {
532 $defaults['hidden_Honoree'] = 1;
535 if ($this->_productDAO
) {
536 if ($this->_productDAO
->product_id
) {
537 $defaults['hidden_Premium'] = 1;
541 if ($this->_noteID
&&
542 isset($this->_values
['note'])
544 $defaults['hidden_AdditionalDetail'] = 1;
548 ts('Additional Details') => 'AdditionalDetail',
549 ts('Honoree Information') => 'Honoree'
552 //Add Premium pane only if Premium is exists.
553 $dao = new CRM_Contribute_DAO_Product();
556 if ($dao->find(TRUE)) {
557 $paneNames[ts('Premium Information')] = 'Premium';
562 if (CRM_Utils_Array
::value('payment_type', $this->_processors
) & CRM_Core_Payment
::PAYMENT_TYPE_DIRECT_DEBIT
564 $ccPane = array(ts('Direct Debit Information') => 'DirectDebit');
567 $ccPane = array(ts('Credit Card Information') => 'CreditCard');
570 if (is_array($ccPane)) {
571 $paneNames = array_merge($ccPane, $paneNames);
574 $buildRecurBlock = FALSE;
575 foreach ($paneNames as $name => $type) {
576 $urlParams = "snippet=4&formType={$type}";
578 $urlParams .= "&mode={$this->_mode}";
582 if ($type == 'CreditCard' ||
583 $type == 'DirectDebit'
588 $allPanes[$name] = array(
589 'url' => CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams),
594 // see if we need to include this paneName in the current form
595 if ($this->_formType
== $type ||
596 CRM_Utils_Array
::value("hidden_{$type}", $_POST) ||
597 CRM_Utils_Array
::value("hidden_{$type}", $defaults)
599 $showAdditionalInfo = TRUE;
600 $allPanes[$name]['open'] = 'true';
603 if ($type == 'CreditCard') {
604 $buildRecurBlock = TRUE;
605 $this->add('hidden', 'hidden_CreditCard', 1);
606 CRM_Core_Payment_Form
::buildCreditCard($this, TRUE);
608 elseif ($type == 'DirectDebit') {
609 $buildRecurBlock = TRUE;
610 $this->add('hidden', 'hidden_DirectDebit', 1);
611 CRM_Core_Payment_Form
::buildDirectDebit($this, TRUE);
614 $additionalInfoFormFunction = 'build' . $type;
615 CRM_Contribute_Form_AdditionalInfo
::$additionalInfoFormFunction($this);
618 if (empty($this->_recurPaymentProcessors
)) {
619 $buildRecurBlock = FALSE;
621 if ($buildRecurBlock) {
622 CRM_Contribute_Form_Contribution_Main
::buildRecur($this);
623 $this->setDefaults(array('is_recur' => 0));
625 $this->assign('buildRecurBlock', $buildRecurBlock);
626 $qfKey = $this->controller
->_key
;
627 $this->assign('qfKey', $qfKey);
628 $this->assign('allPanes', $allPanes);
629 $this->assign('showAdditionalInfo', $showAdditionalInfo);
631 if ($this->_formType
) {
632 $this->assign('formType', $this->_formType
);
636 $this->applyFilter('__ALL__', 'trim');
638 if ($this->_action
& CRM_Core_Action
::DELETE
) {
639 $this->addButtons(array(
642 'name' => ts('Delete'),
643 'spacing' => ' ',
648 'name' => ts('Cancel')
655 //need to assign custom data type and subtype to the template
656 $this->assign('customDataType', 'Contribution');
657 $this->assign('customDataSubType', $this->_contributionType
);
658 $this->assign('entityID', $this->_id
);
660 if ($this->_context
== 'standalone') {
661 CRM_Contact_Form_NewContact
::buildQuickForm($this);
664 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution');
666 $financialType = $this->add('select', 'financial_type_id',
667 ts('Financial Type'),
668 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType(),
670 array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );")
674 $paymentInstrument = $this->add('select', 'payment_instrument_id',
676 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
677 FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
681 $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), $attributes['trxn_id']);
683 //add receipt for offline contribution
684 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
686 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
);
688 $status = CRM_Contribute_PseudoConstant
::contributionStatus();
690 // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169)
691 $statusName = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
698 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
701 elseif ((!$this->_ppID
&& $this->_id
) ||
!$this->_id
) {
706 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
710 $contributionStatus = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id
, 'contribution_status_id');
711 $name = CRM_Utils_Array
::value($contributionStatus, $statusName);
716 unset($status[CRM_Utils_Array
::key('Pending', $statusName)]);
717 unset($status[CRM_Utils_Array
::key('Failed', $statusName)]);
720 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
729 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
735 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
738 $this->add('select', 'contribution_status_id',
739 ts('Contribution Status'),
745 $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
747 if ($this->_online
) {
748 $this->assign('hideCalender', TRUE);
750 $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']);
752 $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime'));
753 $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime'));
755 $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']);
758 if ($buildRecurBlock) {
759 $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
761 $element = $this->add('select',
762 'payment_processor_id',
763 ts('Payment Processor'),
769 if ($this->_online
) {
773 if (empty($this->_lineItems
)) {
774 $buildPriceSet = FALSE;
775 $priceSets = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviContribute');
776 if (!empty($priceSets) && !$this->_ppID
) {
777 $buildPriceSet = TRUE;
780 // don't allow price set for contribution if it is related to participant, or if it is a pledge payment
781 // and if we already have line items for that participant. CRM-5095
782 if ($buildPriceSet && $this->_id
) {
783 $componentDetails = CRM_Contribute_BAO_Contribution
::getComponentDetails($this->_id
);
784 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
789 if ($pledgePaymentId) {
790 $buildPriceSet = FALSE;
792 if ($participantID = CRM_Utils_Array
::value('participant', $componentDetails)) {
793 $participantLI = CRM_Price_BAO_LineItem
::getLineItems($participantID);
794 if (!CRM_Utils_System
::isNull($participantLI)) {
795 $buildPriceSet = FALSE;
800 $hasPriceSets = FALSE;
801 if ($buildPriceSet) {
802 $hasPriceSets = TRUE;
803 $element = $this->add('select', 'price_set_id', ts('Choose price set'),
805 '' => ts('Choose price set')
807 NULL, array('onchange' => "buildAmount( this.value );")
809 if ($this->_online
&& !($this->_action
& CRM_Core_Action
::UPDATE
)) {
813 $this->assign('hasPriceSets', $hasPriceSets);
814 $currencyFreeze = FALSE;
815 if (!($this->_action
& CRM_Core_Action
::UPDATE
)) {
816 if ($this->_online ||
$this->_ppID
) {
817 $attributes['total_amount'] = array_merge($attributes['total_amount'], array(
819 'style' => "background-color:#EBECE4"
821 $optionTypes = array(
822 '1' => ts('Adjust Pledge Payment Schedule?'),
823 '2' => ts('Adjust Total Pledge Amount?'),
825 $this->addRadio('option_type',
831 $currencyFreeze = TRUE;
835 $totalAmount = $this->addMoney('total_amount',
837 ($hasPriceSets) ?
FALSE : TRUE,
838 $attributes['total_amount'],
839 TRUE, 'currency', NULL, $currencyFreeze
843 $this->add('text', 'source', ts('Source'), CRM_Utils_Array
::value('source', $attributes));
845 //CRM-7362 --add campaigns.
846 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
848 CRM_Contribute_Form_SoftCredit
::buildQuickForm($this);
852 $js = array('onclick' => "return verify( );");
855 $mailingInfo = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
858 $this->assign('outBound_option', $mailingInfo['outBound_option']);
860 $this->addButtons(array(
863 'name' => ts('Save'),
869 'name' => ts('Save and New'),
875 'name' => ts('Cancel')
880 // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type,
881 // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail
882 if ($this->_id
&& $this->_values
['contribution_status_id'] == array_search('Cancelled', $statusName)) {
884 $totalAmount->freeze();
886 $checkNumber->freeze();
887 $paymentInstrument->freeze();
889 $financialType->freeze();
892 $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this);
894 if ($this->_action
& CRM_Core_Action
::VIEW
) {
902 * @param array $fields the input form values
903 * @param array $files the uploaded files if any
904 * @param array $options additional user data
906 * @return true if no errors, else array of errors
910 static function formRule($fields, $files, $self) {
913 //check if contact is selected in standalone mode
914 if (isset($fields['contact_select_id'][1]) && !$fields['contact_select_id'][1]) {
915 $errors['contact[1]'] = ts('Please select a contact or create new contact');
918 if (isset($fields['honor_type_id'])) {
919 if (!((CRM_Utils_Array
::value('honor_first_name', $fields) &&
920 CRM_Utils_Array
::value('honor_last_name', $fields)
922 CRM_Utils_Array
::value('honor_email', $fields)
925 $errors['honor_first_name'] = ts('Honor First Name and Last Name OR an email should be set.');
929 //check for Credit Card Contribution.
931 if (empty($fields['payment_processor_id'])) {
932 $errors['payment_processor_id'] = ts('Payment Processor is a required field.');
936 // do the amount validations.
937 if (!CRM_Utils_Array
::value('total_amount', $fields) && empty($self->_lineItems
)) {
938 if ($priceSetId = CRM_Utils_Array
::value('price_set_id', $fields)) {
939 CRM_Price_BAO_PriceField
::priceSetValidation($priceSetId, $fields, $errors);
943 $softErrors = CRM_Contribute_Form_SoftCredit
::formRule($fields);
945 if (CRM_Utils_Array
::value('total_amount', $fields) && (CRM_Utils_Array
::value('net_amount', $fields) || CRM_Utils_Array
::value('fee_amount', $fields))) {
946 $sum = CRM_Utils_Rule
::cleanMoney($fields['net_amount']) + CRM_Utils_Rule
::cleanMoney($fields['fee_amount']);
947 if (CRM_Utils_Rule
::cleanMoney($fields['total_amount']) != $sum) {
948 $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
951 //form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
952 if ($self->_id
&& $self->_values
['contribution_status_id'] != $fields['contribution_status_id']) {
953 CRM_Contribute_BAO_Contribution
::checkStatusValidation($self->_values
, $fields, $errors);
956 //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
957 if (CRM_Utils_Array
::value('fee_amount', $fields)) {
958 $financialAccount = array();
959 CRM_Core_PseudoConstant
::populate($financialAccount,
960 'CRM_Financial_DAO_EntityFinancialAccount',
962 $retrieve = 'financial_account_id',
964 " account_relationship = 5 AND entity_id = {$fields['financial_type_id']} ");
965 if (!current($financialAccount)) {
966 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for this Financial Type");
970 $errors = array_merge($errors, $softErrors);
975 * Function to process the form
981 public function postProcess() {
982 $session = CRM_Core_Session
::singleton();
983 if ($this->_action
& CRM_Core_Action
::DELETE
) {
984 CRM_Contribute_BAO_Contribution
::deleteContribution($this->_id
);
985 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
986 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
991 // get the submitted form values.
992 $submittedValues = $this->controller
->exportValues($this->_name
);
993 if (CRM_Utils_Array
::value('price_set_id', $submittedValues) && $this->_action
& CRM_Core_Action
::UPDATE
) {
994 $line = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution');
995 $lineID = key($line);
996 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $line[$lineID]), 'price_set_id');
997 $quickConfig = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
999 CRM_Price_BAO_LineItem
::deleteLineItems($this->_id
, 'civicrm_contribution');
1003 // process price set and get total amount and line items.
1004 $lineItem = array();
1005 $priceSetId = $pId = NULL;
1006 $priceSetId = CRM_Utils_Array
::value('price_set_id', $submittedValues);
1007 if (empty($priceSetId) && !$this->_id
) {
1008 $this->_priceSetId
= $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
1009 $this->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
1010 $fieldID = key($this->_priceSet
['fields']);
1011 $fieldValueId = key($this->_priceSet
['fields'][$fieldID]['options']);
1012 $this->_priceSet
['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount'];
1013 $submittedValues['price_' . $fieldID] = 1;
1017 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
1018 $submittedValues, $lineItem[$priceSetId]);
1020 $submittedValues['total_amount'] = CRM_Utils_Array
::value('amount', $submittedValues);
1024 $pId = ($this->_compId
&& $this->_context
== 'participant') ?
$this->_compId
: CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id
, 'participant_id', 'contribution_id');
1026 if (!$priceSetId && CRM_Utils_Array
::value('total_amount', $submittedValues) && $this->_id
) {
1027 // 10117 update th line items for participants
1029 $entityTable = 'participant';
1031 $participantParams = array(
1032 'fee_amount' => $submittedValues['total_amount'],
1035 CRM_Event_BAO_Participant
::add($participantParams);
1036 if (empty($this->_lineItems
)) {
1037 $this->_lineItems
= CRM_Price_BAO_LineItem
::getLineItems($entityID, 'participant', 1);
1041 $entityTable = 'contribution';
1042 $entityID = $this->_id
;
1045 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($entityID, $entityTable);
1046 foreach (array_keys($lineItems) as $id) {
1047 $lineItems[$id]['id'] = $id;
1049 $itemId = key($lineItems);
1051 if ($itemId && CRM_Utils_Array
::value('price_field_id', $lineItems[$itemId])) {
1052 $fieldType = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'html_type');
1054 $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule
::cleanMoney(CRM_Utils_Array
::value('total_amount', $submittedValues));
1055 // 10117 update th line items for participants
1056 $this->_priceSetId
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
1057 $lineItem[$this->_priceSetId
] = $lineItems;
1060 if ($this->_priceSetId
&& CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config')) {
1063 //CRM-11529 for quick config backoffice transactions
1064 //when financial_type_id is passed in form, update the
1065 //lineitems with the financial type selected in form
1066 if ($isQuickConfig && CRM_Utils_Array
::value('financial_type_id', $submittedValues)
1067 && CRM_Utils_Array
::value($this->_priceSetId
, $lineItem)
1069 foreach ($lineItem[$this->_priceSetId
] as &$values) {
1070 $values['financial_type_id'] = $submittedValues['financial_type_id'];
1074 if (!isset($submittedValues['total_amount'])) {
1075 $submittedValues['total_amount'] = CRM_Utils_Array
::value('total_amount', $this->_values
);
1077 $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ?
$lineItem : FALSE);
1079 if (CRM_Utils_Array
::value('pcp_made_through_id', $submittedValues)) {
1082 'pcp_made_through_id',
1083 'pcp_display_in_roll',
1084 'pcp_roll_nickname',
1085 'pcp_personal_note',
1087 foreach ($fields as $f) {
1088 $pcp[$f] = CRM_Utils_Array
::value($f, $submittedValues);
1092 //build soft credit params
1093 if (!empty($submittedValues['soft_credit_contact_select_id'])) {
1094 $softParams = $softIDs =array();
1095 foreach ($submittedValues['soft_credit_contact_select_id'] as $key => $val) {
1096 if ($val && $submittedValues['soft_credit_amount'][$key]) {
1097 $softParams[$key]['contact_id'] = $val;
1098 $softParams[$key]['amount'] = CRM_Utils_Rule
::cleanMoney($submittedValues['soft_credit_amount'][$key]);
1099 if (!empty($submittedValues['soft_credit_id'][$key])) {
1100 $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key];
1107 // set the contact, when contact is selected
1108 if (CRM_Utils_Array
::value('contact_select_id', $submittedValues)) {
1109 $this->_contactID
= $submittedValues['contact_select_id'][1];
1112 $config = CRM_Core_Config
::singleton();
1114 //Credit Card Contribution.
1116 $this->processCreditCard($submittedValues, $config, $session, $pId, $lineItem);
1119 //Offline Contribution.
1120 $submittedValues = $this->unsetCreditCardFields($submittedValues);
1122 // get the required field value only.
1123 $formValues = $submittedValues;
1124 $params = $ids = array();
1126 $params['contact_id'] = $this->_contactID
;
1128 $params['currency'] = $this->getCurrency($submittedValues);
1131 'financial_type_id',
1132 'contribution_status_id',
1133 'payment_instrument_id',
1138 foreach ($fields as $f) {
1139 $params[$f] = CRM_Utils_Array
::value($f, $formValues);
1143 $params['pcp'] = $pcp;
1145 if (!empty($softParams)) {
1146 $params['soft_credit'] = $softParams;
1147 $params['soft_credit_ids'] = $softIDs;
1150 //if priceset is used, no need to cleanup money
1153 $params['skipCleanMoney'] = 1;
1162 foreach ($dates as $d) {
1163 $params[$d] = CRM_Utils_Date
::processDate($formValues[$d], $formValues[$d . '_time'], TRUE);
1166 if (CRM_Utils_Array
::value('is_email_receipt', $formValues)) {
1167 $params['receipt_date'] = date("Y-m-d");
1170 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Cancelled', 'name')
1171 ||
$params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Refunded', 'name')
1173 if (CRM_Utils_System
::isNull(CRM_Utils_Array
::value('cancel_date', $params))) {
1174 $params['cancel_date'] = date('Y-m-d');
1178 $params['cancel_date'] = $params['cancel_reason'] = 'null';
1181 // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996
1182 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Pending', 'name')) {
1183 $params['is_pay_later'] = 1;
1186 $ids['contribution'] = $params['id'] = $this->_id
;
1188 //Add Additional common information to formatted params
1189 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($formValues, $params, $this);
1191 $params['contribution_mode'] = 'participant';
1192 $params['participant_id'] = $pId;
1193 $params['skipLineItem'] = 1;
1195 $params['line_item'] = $lineItem;
1196 $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array
::value('id', $this->_paymentProcessor
);
1197 //create contribution.
1198 if ($isQuickConfig) {
1199 $params['is_quick_config'] = 1;
1203 // if non_deductible_amount exists i.e. Additional Details fieldset was opened [and staff typed something] -> keep it.
1204 if (isset($params['non_deductible_amount']) && (!empty($params['non_deductible_amount']))) {
1205 $params['non_deductible_amount'] = $params['non_deductible_amount'];
1207 // if non_deductible_amount does NOT exist - then calculate it depending on:
1208 // $ContributionType->is_deductible and whether there is a product (premium).
1210 $contributionType = new CRM_Financial_DAO_FinancialType();
1211 $contributionType->id
= $params['financial_type_id'];
1212 if (!$contributionType->find(TRUE)) {
1213 CRM_Core_Error
::fatal('Could not find a system table');
1215 if ($contributionType->is_deductible
) {
1217 if (isset($formValues['product_name'][0])) {
1218 $selectProduct = $formValues['product_name'][0];
1220 // if there is a product - compare the value to the contribution amount
1221 if (isset($selectProduct)) {
1222 $productDAO = new CRM_Contribute_DAO_Product();
1223 $productDAO->id
= $selectProduct;
1224 $productDAO->find(TRUE);
1225 // product value exceeds contribution amount
1226 if ($params['total_amount'] < $productDAO->price
) {
1227 $params['non_deductible_amount'] = $params['total_amount'];
1229 // product value does NOT exceed contribution amount
1231 $params['non_deductible_amount'] = $productDAO->price
;
1234 // contribution is deductible - but there is no product
1236 $params['non_deductible_amount'] = '0.00';
1239 // contribution is NOT deductible
1241 $params['non_deductible_amount'] = $params['total_amount'];
1245 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
1247 // process associated membership / participant, CRM-4395
1248 $relatedComponentStatusMsg = NULL;
1249 if ($contribution->id
&& $this->_action
& CRM_Core_Action
::UPDATE
) {
1250 $relatedComponentStatusMsg = $this->updateRelatedComponent($contribution->id
,
1251 $contribution->contribution_status_id
,
1252 CRM_Utils_Array
::value('contribution_status_id',
1259 if ($contribution->id
&& isset($formValues['note'])) {
1260 CRM_Contribute_Form_AdditionalInfo
::processNote($formValues, $this->_contactID
, $contribution->id
, $this->_noteID
);
1264 if ($contribution->id
&& isset($formValues['product_name'][0])) {
1265 CRM_Contribute_Form_AdditionalInfo
::processPremium($formValues, $contribution->id
,
1266 $this->_premiumID
, $this->_options
1270 //send receipt mail.
1271 if ($contribution->id
&& CRM_Utils_Array
::value('is_email_receipt', $formValues)) {
1272 $formValues['contact_id'] = $this->_contactID
;
1273 $formValues['contribution_id'] = $contribution->id
;
1275 // to get 'from email id' for send receipt
1276 $this->fromEmailId
= $formValues['from_email_address'];
1277 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $formValues);
1280 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1286 //update pledge payment status.
1287 if ((($this->_ppID
&& $contribution->id
) && $this->_action
& CRM_Core_Action
::ADD
) ||
1288 (($pledgePaymentId) && $this->_action
& CRM_Core_Action
::UPDATE
)
1292 //store contribution id in payment record.
1293 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1296 $this->_ppID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1301 $this->_pledgeID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1308 $adjustTotalAmount = FALSE;
1309 if (CRM_Utils_Array
::value('option_type', $formValues) == 2) {
1310 $adjustTotalAmount = TRUE;
1313 $updatePledgePaymentStatus = FALSE;
1314 //do only if either the status or the amount has changed
1315 if ($this->_action
& CRM_Core_Action
::ADD
) {
1316 $updatePledgePaymentStatus = TRUE;
1318 elseif ($this->_action
& CRM_Core_Action
::UPDATE
&& (($this->_defaults
['contribution_status_id'] != $formValues['contribution_status_id']) ||
1319 ($this->_defaults
['total_amount'] != $formValues['total_amount']))
1321 $updatePledgePaymentStatus = TRUE;
1324 if ($updatePledgePaymentStatus) {
1325 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1326 array($this->_ppID
),
1327 $contribution->contribution_status_id
,
1329 $contribution->total_amount
,
1335 $statusMsg = ts('The contribution record has been saved.');
1336 if (CRM_Utils_Array
::value('is_email_receipt', $formValues) && $sendReceipt) {
1337 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1340 if ($relatedComponentStatusMsg) {
1341 $statusMsg .= ' ' . $relatedComponentStatusMsg;
1344 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');
1345 //Offline Contribution ends.
1347 $buttonName = $this->controller
->getButtonName();
1348 if ($this->_context
== 'standalone') {
1349 if ($buttonName == $this->getButtonName('upload', 'new')) {
1350 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contribute/add',
1351 'reset=1&action=add&context=standalone'
1355 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
1356 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
1360 elseif ($this->_context
== 'contribution' && $this->_mode
&& $buttonName == $this->getButtonName('upload', 'new')) {
1361 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1362 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}"
1365 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
1366 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1367 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}"
1372 public function processCreditCard($submittedValues, $config, $session, $pId, $lineItem) {
1373 $unsetParams = array(
1375 'payment_instrument_id',
1376 'contribution_status_id',
1380 foreach ($unsetParams as $key) {
1381 if (isset($submittedValues[$key])) {
1382 unset($submittedValues[$key]);
1386 // CRM-12680 set $_lineItem if its not set
1387 if (empty($this->_lineItem
) && !empty($lineItem)) {
1388 $this->_lineItem
= $lineItem;
1391 //Get the rquire fields value only.
1392 $params = $this->_params
= $submittedValues;
1394 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($this->_params
['payment_processor_id'],
1398 //get the payment processor id as per mode.
1399 $this->_params
['payment_processor'] = $params['payment_processor_id'] =
1400 $this->_params
['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor
['id'];
1402 $now = date('YmdHis');
1405 // we need to retrieve email address
1406 if ($this->_context
== 'standalone' && CRM_Utils_Array
::value('is_email_receipt', $submittedValues)) {
1407 list($this->userDisplayName
,
1409 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
1410 $this->assign('displayName', $this->userDisplayName
);
1413 //set email for primary location.
1414 $fields['email-Primary'] = 1;
1415 $params['email-Primary'] = $this->userEmail
;
1417 // now set the values for the billing location.
1418 foreach ($this->_fields
as $name => $dontCare) {
1422 // also add location name to the array
1423 $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);
1424 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
1425 $fields["address_name-{$this->_bltID}"] = 1;
1427 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1432 $nameFields = array('first_name', 'middle_name', 'last_name');
1433 foreach ($nameFields as $name) {
1435 if (array_key_exists("billing_$name", $params)) {
1436 $params[$name] = $params["billing_{$name}"];
1437 $params['preserveDBName'] = TRUE;
1441 if (CRM_Utils_Array
::value('source', $params)) {
1442 unset($params['source']);
1444 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $fields,
1450 // add all the additioanl payment params we need
1451 $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}"]);
1452 $this->_params
["country-{$this->_bltID}"] = $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
1454 if ($this->_paymentProcessor
['payment_type'] & CRM_Core_Payment
::PAYMENT_TYPE_CREDIT_CARD
) {
1455 $this->_params
['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($this->_params
);
1456 $this->_params
['month'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($this->_params
);
1458 $this->_params
['ip_address'] = CRM_Utils_System
::ipAddress();
1459 $this->_params
['amount'] = $this->_params
['total_amount'];
1460 $this->_params
['amount_level'] = 0;
1461 $this->_params
['currencyID'] = CRM_Utils_Array
::value('currency',
1463 $config->defaultCurrency
1465 $this->_params
['payment_action'] = 'Sale';
1466 if (CRM_Utils_Array
::value('receive_date', $this->_params
)) {
1467 $this->_params
['receive_date'] = CRM_Utils_Date
::processDate($this->_params
['receive_date'], $this->_params
['receive_date_time']);
1470 if (CRM_Utils_Array
::value('soft_credit_to', $params)) {
1471 $this->_params
['soft_credit_to'] = $params['soft_credit_to'];
1472 $this->_params
['pcp_made_through_id'] = $params['pcp_made_through_id'];
1475 $this->_params
['pcp_display_in_roll'] = CRM_Utils_Array
::value('pcp_display_in_roll', $params);
1476 $this->_params
['pcp_roll_nickname'] = CRM_Utils_Array
::value('pcp_roll_nickname', $params);
1477 $this->_params
['pcp_personal_note'] = CRM_Utils_Array
::value('pcp_personal_note', $params);
1479 //Add common data to formatted params
1480 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($params, $this->_params
, $this);
1482 if (empty($this->_params
['invoice_id'])) {
1483 $this->_params
['invoiceID'] = md5(uniqid(rand(), TRUE));
1486 $this->_params
['invoiceID'] = $this->_params
['invoice_id'];
1489 // at this point we've created a contact and stored its address etc
1490 // all the payment processors expect the name and address to be in the
1491 // so we copy stuff over to first_name etc.
1492 $paymentParams = $this->_params
;
1493 $paymentParams['contactID'] = $this->_contactID
;
1494 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $this->_params
, $paymentParams, TRUE);
1496 $contributionType = new CRM_Financial_DAO_FinancialType();
1497 $contributionType->id
= $params['financial_type_id'];
1498 if (!$contributionType->find(TRUE)) {
1499 CRM_Core_Error
::fatal('Could not find a system table');
1502 // add some financial type details to the params list
1503 // if folks need to use it
1504 $paymentParams['contributionType_name'] = $this->_params
['contributionType_name'] = $contributionType->name
;
1505 $paymentParams['contributionPageID'] = NULL;
1506 if (CRM_Utils_Array
::value('is_email_receipt', $this->_params
)) {
1507 $paymentParams['email'] = $this->userEmail
;
1508 $paymentParams['is_email_receipt'] = 1;
1511 $paymentParams['is_email_receipt'] = 0;
1512 $this->_params
['is_email_receipt'] = 0;
1514 if (CRM_Utils_Array
::value('receive_date', $this->_params
)) {
1515 $paymentParams['receive_date'] = $this->_params
['receive_date'];
1517 if (CRM_Utils_Array
::value('receive_date', $this->_params
)) {
1518 $paymentParams['receive_date'] = $this->_params
['receive_date'];
1523 // For recurring contribution, create Contribution Record first.
1524 // Contribution ID, Recurring ID and Contact ID needed
1525 // When we get a callback from the payment processor, CRM-7115
1526 if (CRM_Utils_Array
::value('is_recur', $paymentParams)) {
1527 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1536 $paymentParams['contributionID'] = $contribution->id
;
1537 $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
1538 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
1539 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
1542 if ($paymentParams['amount'] > 0.0) {
1543 // force a reget of the payment processor in case the form changed it, CRM-7179
1544 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this, TRUE);
1545 $result = $payment->doDirectPayment($paymentParams);
1548 if (is_a($result, 'CRM_Core_Error')) {
1549 //make sure to cleanup db for recurring case.
1550 if (CRM_Utils_Array
::value('contributionID', $paymentParams)) {
1551 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting contribution {$paymentParams['contributionID']}");
1552 CRM_Contribute_BAO_Contribution
::deleteContribution($paymentParams['contributionID']);
1554 if (CRM_Utils_Array
::value('contributionRecurID', $paymentParams)) {
1555 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting recurring contribution {$paymentParams['contributionRecurID']}");
1556 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
1559 //set the contribution mode.
1560 $urlParams = "action=add&cid={$this->_contactID}";
1562 $urlParams .= "&mode={$this->_mode}";
1564 if (!empty($this->_ppID
)) {
1565 $urlParams .= "&context=pledge&ppid={$this->_ppID}";
1567 CRM_Core_Error
::displaySessionError($result);
1568 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams));
1572 $this->_params
= array_merge($this->_params
, $result);
1575 $this->_params
['receive_date'] = $now;
1577 if (CRM_Utils_Array
::value('is_email_receipt', $this->_params
)) {
1578 $this->_params
['receipt_date'] = $now;
1581 $this->_params
['receipt_date'] = CRM_Utils_Date
::processDate($this->_params
['receipt_date'],
1582 $params['receipt_date_time'], TRUE
1586 $this->set('params', $this->_params
);
1587 $this->assign('trxn_id', $result['trxn_id']);
1588 $this->assign('receive_date', $this->_params
['receive_date']);
1590 // result has all the stuff we need
1591 // lets archive it to a financial transaction
1592 if ($contributionType->is_deductible
) {
1593 $this->assign('is_deductible', TRUE);
1594 $this->set('is_deductible', TRUE);
1597 // set source if not set
1598 if (empty($this->_params
['source'])) {
1599 $userID = $session->get('userID');
1600 $userSortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $userID,
1603 $this->_params
['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
1606 // build custom data getFields array
1607 $customFieldsContributionType = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE,
1608 CRM_Utils_Array
::value('financial_type_id', $params)
1610 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsContributionType,
1611 CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, TRUE)
1613 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
1620 if (!CRM_Utils_Array
::value('is_recur', $paymentParams)) {
1621 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1630 //send receipt mail.
1631 if ($contribution->id
&&
1632 CRM_Utils_Array
::value('is_email_receipt', $this->_params
)
1634 $this->_params
['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $result);
1635 $this->_params
['contact_id'] = $this->_contactID
;
1636 $this->_params
['contribution_id'] = $contribution->id
;
1637 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $this->_params
, TRUE);
1641 if ($contribution->id
&& isset($params['note'])) {
1642 CRM_Contribute_Form_AdditionalInfo
::processNote($params, $contactID, $contribution->id
, NULL);
1645 if ($contribution->id
&& isset($params['product_name'][0])) {
1646 CRM_Contribute_Form_AdditionalInfo
::processPremium($params, $contribution->id
, NULL, $this->_options
);
1649 //update pledge payment status.
1650 if ($this->_ppID
&& $contribution->id
) {
1651 //store contribution id in payment record.
1652 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1654 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1655 array($this->_ppID
),
1656 $contribution->contribution_status_id
,
1658 $contribution->total_amount
1662 if ($contribution->id
) {
1663 $statusMsg = ts('The contribution record has been processed.');
1664 if (CRM_Utils_Array
::value('is_email_receipt', $this->_params
) && $sendReceipt) {
1665 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1667 CRM_Core_Session
::setStatus($statusMsg, ts('Complete'), 'success');