3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 * Store the contribution Type ID
112 public $_contributionType;
115 * The contribution values if an existing contribution
120 * The pledge values if this contribution is associated with pledge
122 public $_pledgeValues;
124 public $_contributeMode = 'direct';
131 * Store the line items if price set used.
136 * @var soft credit info
138 public $_softCreditInfo;
140 protected $_formType;
144 * Function to set variables up before form is built
149 public function preProcess() {
151 //check permission for action.
152 if (!CRM_Core_Permission
::checkActionPermission('CiviContribute', $this->_action
)) {
153 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
156 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
158 $this->assign('cdType', FALSE);
159 if ($this->_cdType
) {
160 $this->assign('cdType', TRUE);
161 CRM_Custom_Form_CustomData
::preProcess($this);
165 $config = CRM_Core_Config
::singleton();
167 $this->_formType
= CRM_Utils_Array
::value('formType', $_GET);
170 $this->_priceSetId
= CRM_Utils_Array
::value('priceSetId', $_GET);
171 $this->set('priceSetId', $this->_priceSetId
);
172 $this->assign('priceSetId', $this->_priceSetId
);
174 //get the pledge payment id
175 $this->_ppID
= CRM_Utils_Request
::retrieve('ppid', 'Positive', $this);
178 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
181 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'add');
182 $this->assign('action', $this->_action
);
184 //get the contribution id if update
185 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
186 if (!empty($this->_id
)) {
187 $this->assign('contribID', $this->_id
);
190 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
191 $this->assign('context', $this->_context
);
193 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
195 $this->_compContext
= CRM_Utils_Request
::retrieve('compContext', 'String', $this);
197 //set the contribution mode.
198 $this->_mode
= CRM_Utils_Request
::retrieve('mode', 'String', $this);
200 $this->assign('contributionMode', $this->_mode
);
202 $this->_paymentProcessor
= array('billing_mode' => 1);
204 $this->assign('showCheckNumber', TRUE);
206 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
207 $this->assignProcessors();
209 if ($this->_contactID
) {
210 list($this->userDisplayName
, $this->userEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
211 $this->assign('displayName', $this->userDisplayName
);
214 // also check for billing information
215 // get the billing location type
216 $this->assignBillingType();
218 $this->_fields
= array();
220 CRM_Core_Payment_Form
::setPaymentFieldsByType(CRM_Utils_Array
::value('payment_type', $this->_processors
), $this);
222 if ($this->_action
& CRM_Core_Action
::DELETE
) {
226 if (in_array('CiviPledge', $config->enableComponents
) && !$this->_formType
) {
227 $this->preProcessPledge();
230 $this->_values
= array();
232 // current contribution id
234 $this->assignPremiumProduct($this->_id
);
235 $this->buildValuesAndAssignOnline_Note_Type($this->_id
, $this->_values
);
238 // when custom data is included in this page
239 if (!empty($_POST['hidden_custom'])) {
240 $this->applyCustomData('Contribution', CRM_Utils_Array
::value('financial_type_id', $_POST), $this->_id
);
243 $this->_lineItems
= array();
245 if (!empty($this->_compId
) && $this->_compContext
== 'participant') {
246 $this->assign('compId', $this->_compId
);
247 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_compId
);
250 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution', 1);
252 empty($lineItem) ?
NULL : $this->_lineItems
[] = $lineItem;
255 $this->assign('lineItem', empty($this->_lineItems
) ?
FALSE : $this->_lineItems
);
258 if ($this->_contactID
) {
259 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactID
);
261 // Check if this is default domain contact CRM-10482
262 if (CRM_Contact_BAO_Contact
::checkDomainContact($this->_contactID
)) {
263 $displayName .= ' (' . ts('default organization') . ')';
266 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
267 CRM_Utils_System
::setTitle(ts('Contribution from') . ' ' . $displayName);
271 function setDefaultValues() {
272 if ($this->_cdType
) {
273 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
276 $defaults = $this->_values
;
278 //set defaults for pledge payment.
280 $defaults['total_amount'] = CRM_Utils_Array
::value('scheduled_amount', $this->_pledgeValues
['pledgePayment']);
281 $defaults['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $this->_pledgeValues
);
282 $defaults['currency'] = CRM_Utils_Array
::value('currency', $this->_pledgeValues
);
283 $defaults['option_type'] = 1;
287 if ($this->_action
& CRM_Core_Action
::DELETE
) {
291 // set soft credit defaults
292 CRM_Contribute_Form_SoftCredit
::setDefaultValues($defaults, $this);
295 $config = CRM_Core_Config
::singleton();
296 // set default country from config if no country set
297 if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
298 $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
301 if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) {
302 $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince
;
305 $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID
);
306 $defaults = array_merge($defaults, $billingDefaults);
308 // now fix all state country selectors, set correct state based on country
309 CRM_Core_BAO_Address
::fixAllStateSelects($this, $defaults);
313 $this->_contactID
= $defaults['contact_id'];
316 // Set $newCredit variable in template to control whether link to credit card mode is included
317 CRM_Core_Payment
::allowBackofficeCreditCard($this);
319 // fix the display of the monetary value, CRM-4038
320 if (isset($defaults['total_amount'])) {
321 $defaults['total_amount'] = CRM_Utils_Money
::format($defaults['total_amount'], NULL, '%a');
324 if (isset($defaults['non_deductible_amount'])) {
325 $defaults['non_deductible_amount'] = CRM_Utils_Money
::format($defaults['non_deductible_amount'], NULL, '%a');
328 if (isset($defaults['fee_amount'])) {
329 $defaults['fee_amount'] = CRM_Utils_Money
::format($defaults['fee_amount'], NULL, '%a');
332 if (isset($defaults['net_amount'])) {
333 $defaults['net_amount'] = CRM_Utils_Money
::format($defaults['net_amount'], NULL, '%a');
336 if ($this->_contributionType
) {
337 $defaults['financial_type_id'] = $this->_contributionType
;
340 if (empty($defaults['payment_instrument_id'])) {
341 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
344 if (!empty($defaults['is_test'])) {
345 $this->assign('is_test', TRUE);
348 $this->assign('showOption', TRUE);
349 // for Premium section
350 if ($this->_premiumID
) {
351 $this->assign('showOption', FALSE);
352 $options = isset($this->_options
[$this->_productDAO
->product_id
]) ?
$this->_options
[$this->_productDAO
->product_id
] : "";
354 $this->assign('showOption', TRUE);
356 $options_key = CRM_Utils_Array
::key($this->_productDAO
->product_option
, $options);
358 $defaults['product_name'] = array($this->_productDAO
->product_id
, trim($options_key));
361 $defaults['product_name'] = array($this->_productDAO
->product_id
);
363 if ($this->_productDAO
->fulfilled_date
) {
364 list($defaults['fulfilled_date']) = CRM_Utils_Date
::setDateDefaults($this->_productDAO
->fulfilled_date
);
368 if (isset($this->userEmail
)) {
369 $this->assign('email', $this->userEmail
);
372 if (!empty($defaults['is_pay_later'])) {
373 $this->assign('is_pay_later', TRUE);
375 $this->assign('contribution_status_id', CRM_Utils_Array
::value('contribution_status_id', $defaults));
377 $dates = array('receive_date', 'receipt_date', 'cancel_date', 'thankyou_date');
378 foreach ($dates as $key) {
379 if (!empty($defaults[$key])) {
380 list($defaults[$key],
381 $defaults[$key . '_time']
382 ) = CRM_Utils_Date
::setDateDefaults(CRM_Utils_Array
::value($key, $defaults),
388 if (!$this->_id
&& empty($defaults['receive_date'])) {
389 list($defaults['receive_date'],
390 $defaults['receive_date_time']
391 ) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
394 $this->assign('receive_date', CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('receive_date', $defaults),
395 CRM_Utils_Array
::value('receive_date_time', $defaults)
397 $currency = CRM_Utils_Array
::value('currency', $defaults);
398 $this->assign('currency', $currency);
399 // Hack to get currency info to the js layer. CRM-11440
400 CRM_Utils_Money
::format(1);
401 $this->assign('currencySymbol', CRM_Utils_Array
::value($currency, CRM_Utils_Money
::$_currencySymbols));
402 $this->assign('totalAmount', CRM_Utils_Array
::value('total_amount', $defaults));
404 //inherit campaign from pledge.
405 if ($this->_ppID
&& !empty($this->_pledgeValues
['campaign_id'])) {
406 $defaults['campaign_id'] = $this->_pledgeValues
['campaign_id'];
409 $this->_defaults
= $defaults;
414 * Function to build the form
419 public function buildQuickForm() {
420 if ($this->_cdType
) {
421 CRM_Custom_Form_CustomData
::buildQuickForm($this);
425 // build price set form.
426 $buildPriceSet = FALSE;
427 if (empty($this->_lineItems
) &&
428 ($this->_priceSetId ||
!empty($_POST['price_set_id']))
430 $buildPriceSet = TRUE;
431 $getOnlyPriceSetElements = TRUE;
432 if (!$this->_priceSetId
) {
433 $this->_priceSetId
= $_POST['price_set_id'];
434 $getOnlyPriceSetElements = FALSE;
437 $this->set('priceSetId', $this->_priceSetId
);
438 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
440 // get only price set form elements.
441 if ($getOnlyPriceSetElements) {
445 // use to build form during form rule.
446 $this->assign('buildPriceSet', $buildPriceSet);
448 $showAdditionalInfo = FALSE;
450 $defaults = $this->_values
;
451 $additionalDetailFields = array(
455 'non_deductible_amount',
459 foreach ($additionalDetailFields as $key) {
460 if (!empty($defaults[$key])) {
461 $defaults['hidden_AdditionalDetail'] = 1;
466 if ($this->_productDAO
) {
467 if ($this->_productDAO
->product_id
) {
468 $defaults['hidden_Premium'] = 1;
472 if ($this->_noteID
&&
473 isset($this->_values
['note'])
475 $defaults['hidden_AdditionalDetail'] = 1;
479 ts('Additional Details') => 'AdditionalDetail',
482 //Add Premium pane only if Premium is exists.
483 $dao = new CRM_Contribute_DAO_Product();
486 if ($dao->find(TRUE)) {
487 $paneNames[ts('Premium Information')] = 'Premium';
492 if (CRM_Utils_Array
::value('payment_type', $this->_processors
) & CRM_Core_Payment
::PAYMENT_TYPE_DIRECT_DEBIT
494 $ccPane = array(ts('Direct Debit Information') => 'DirectDebit');
497 $ccPane = array(ts('Credit Card Information') => 'CreditCard');
500 if (is_array($ccPane)) {
501 $paneNames = array_merge($ccPane, $paneNames);
504 $buildRecurBlock = FALSE;
505 foreach ($paneNames as $name => $type) {
506 $urlParams = "snippet=4&formType={$type}";
508 $urlParams .= "&mode={$this->_mode}";
512 if ($type == 'CreditCard' ||
513 $type == 'DirectDebit'
518 $allPanes[$name] = array(
519 'url' => CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams),
524 // see if we need to include this paneName in the current form
525 if ($this->_formType
== $type ||
!empty($_POST["hidden_{$type}"]) ||
526 CRM_Utils_Array
::value("hidden_{$type}", $defaults)
528 $showAdditionalInfo = TRUE;
529 $allPanes[$name]['open'] = 'true';
532 if ($type == 'CreditCard') {
533 $buildRecurBlock = TRUE;
534 $this->add('hidden', 'hidden_CreditCard', 1);
535 CRM_Core_Payment_Form
::buildCreditCard($this, TRUE);
537 elseif ($type == 'DirectDebit') {
538 $buildRecurBlock = TRUE;
539 $this->add('hidden', 'hidden_DirectDebit', 1);
540 CRM_Core_Payment_Form
::buildDirectDebit($this, TRUE);
543 $additionalInfoFormFunction = 'build' . $type;
544 CRM_Contribute_Form_AdditionalInfo
::$additionalInfoFormFunction($this);
547 if (empty($this->_recurPaymentProcessors
)) {
548 $buildRecurBlock = FALSE;
550 if ($buildRecurBlock) {
551 CRM_Contribute_Form_Contribution_Main
::buildRecur($this);
552 $this->setDefaults(array('is_recur' => 0));
554 $this->assign('buildRecurBlock', $buildRecurBlock);
555 $qfKey = $this->controller
->_key
;
556 $this->assign('qfKey', $qfKey);
557 $this->assign('allPanes', $allPanes);
558 $this->assign('showAdditionalInfo', $showAdditionalInfo);
560 if ($this->_formType
) {
561 $this->assign('formType', $this->_formType
);
565 $this->applyFilter('__ALL__', 'trim');
567 if ($this->_action
& CRM_Core_Action
::DELETE
) {
568 $this->addButtons(array(
571 'name' => ts('Delete'),
572 'spacing' => ' ',
577 'name' => ts('Cancel')
584 //need to assign custom data type and subtype to the template
585 $this->assign('customDataType', 'Contribution');
586 $this->assign('customDataSubType', $this->_contributionType
);
587 $this->assign('entityID', $this->_id
);
589 if ($this->_context
== 'standalone') {
590 CRM_Contact_Form_NewContact
::buildQuickForm($this);
593 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution');
595 $financialType = $this->add('select', 'financial_type_id',
596 ts('Financial Type'),
597 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType(),
599 array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );")
603 $paymentInstrument = $this->add('select', 'payment_instrument_id',
605 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
606 TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
610 $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), $attributes['trxn_id']);
612 //add receipt for offline contribution
613 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
615 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
);
617 $status = CRM_Contribute_PseudoConstant
::contributionStatus();
619 // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169)
620 $statusName = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
627 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
630 elseif ((!$this->_ppID
&& $this->_id
) ||
!$this->_id
) {
635 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
639 $contributionStatus = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id
, 'contribution_status_id');
640 $name = CRM_Utils_Array
::value($contributionStatus, $statusName);
645 unset($status[CRM_Utils_Array
::key('Pending', $statusName)]);
646 unset($status[CRM_Utils_Array
::key('Failed', $statusName)]);
649 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
658 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
664 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
667 $this->add('select', 'contribution_status_id',
668 ts('Contribution Status'),
674 $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
676 if ($this->_online
) {
677 $this->assign('hideCalender', TRUE);
679 $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']);
681 $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime'));
682 $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime'));
684 $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']);
687 if ($buildRecurBlock) {
688 $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
690 $element = $this->add('select',
691 'payment_processor_id',
692 ts('Payment Processor'),
698 if ($this->_online
) {
702 if (empty($this->_lineItems
)) {
703 $buildPriceSet = FALSE;
704 $priceSets = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviContribute');
705 if (!empty($priceSets) && !$this->_ppID
) {
706 $buildPriceSet = TRUE;
709 // don't allow price set for contribution if it is related to participant, or if it is a pledge payment
710 // and if we already have line items for that participant. CRM-5095
711 if ($buildPriceSet && $this->_id
) {
712 $componentDetails = CRM_Contribute_BAO_Contribution
::getComponentDetails($this->_id
);
713 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
718 if ($pledgePaymentId) {
719 $buildPriceSet = FALSE;
721 if ($participantID = CRM_Utils_Array
::value('participant', $componentDetails)) {
722 $participantLI = CRM_Price_BAO_LineItem
::getLineItems($participantID);
723 if (!CRM_Utils_System
::isNull($participantLI)) {
724 $buildPriceSet = FALSE;
729 $hasPriceSets = FALSE;
730 if ($buildPriceSet) {
731 $hasPriceSets = TRUE;
732 $element = $this->add('select', 'price_set_id', ts('Choose price set'),
734 '' => ts('Choose price set')
736 NULL, array('onchange' => "buildAmount( this.value );")
738 if ($this->_online
&& !($this->_action
& CRM_Core_Action
::UPDATE
)) {
742 $this->assign('hasPriceSets', $hasPriceSets);
743 $currencyFreeze = FALSE;
744 if (!($this->_action
& CRM_Core_Action
::UPDATE
)) {
745 if ($this->_online ||
$this->_ppID
) {
746 $attributes['total_amount'] = array_merge($attributes['total_amount'], array(
748 'style' => "background-color:#EBECE4"
750 $optionTypes = array(
751 '1' => ts('Adjust Pledge Payment Schedule?'),
752 '2' => ts('Adjust Total Pledge Amount?'),
754 $this->addRadio('option_type',
760 $currencyFreeze = TRUE;
764 $totalAmount = $this->addMoney('total_amount',
766 ($hasPriceSets) ?
FALSE : TRUE,
767 $attributes['total_amount'],
768 TRUE, 'currency', NULL, $currencyFreeze
772 $this->add('text', 'source', ts('Source'), CRM_Utils_Array
::value('source', $attributes));
774 //CRM-7362 --add campaigns.
775 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
777 CRM_Contribute_Form_SoftCredit
::buildQuickForm($this);
781 $js = array('onclick' => "return verify( );");
784 $mailingInfo = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
787 $this->assign('outBound_option', $mailingInfo['outBound_option']);
789 $this->addButtons(array(
792 'name' => ts('Save'),
798 'name' => ts('Save and New'),
804 'name' => ts('Cancel')
809 // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type,
810 // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail
811 if ($this->_id
&& $this->_values
['contribution_status_id'] == array_search('Cancelled', $statusName)) {
813 $totalAmount->freeze();
815 $checkNumber->freeze();
816 $paymentInstrument->freeze();
818 $financialType->freeze();
821 $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this);
823 if ($this->_action
& CRM_Core_Action
::VIEW
) {
831 * @param array $fields the input form values
832 * @param array $files the uploaded files if any
833 * @param array $options additional user data
835 * @return true if no errors, else array of errors
839 static function formRule($fields, $files, $self) {
842 //check if contact is selected in standalone mode
843 if (isset($fields['contact_select_id'][1]) && !$fields['contact_select_id'][1]) {
844 $errors['contact[1]'] = ts('Please select a contact or create new contact');
847 //check for Credit Card Contribution.
849 if (empty($fields['payment_processor_id'])) {
850 $errors['payment_processor_id'] = ts('Payment Processor is a required field.');
854 // do the amount validations.
855 if (empty($fields['total_amount']) && empty($self->_lineItems
)) {
856 if ($priceSetId = CRM_Utils_Array
::value('price_set_id', $fields)) {
857 CRM_Price_BAO_PriceField
::priceSetValidation($priceSetId, $fields, $errors);
861 $softErrors = CRM_Contribute_Form_SoftCredit
::formRule($fields, $errors, $self);
863 if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) ||
!empty($fields['fee_amount']))) {
864 $sum = CRM_Utils_Rule
::cleanMoney($fields['net_amount']) + CRM_Utils_Rule
::cleanMoney($fields['fee_amount']);
865 if (CRM_Utils_Rule
::cleanMoney($fields['total_amount']) != $sum) {
866 $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
869 //form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
870 if ($self->_id
&& $self->_values
['contribution_status_id'] != $fields['contribution_status_id']) {
871 CRM_Contribute_BAO_Contribution
::checkStatusValidation($self->_values
, $fields, $errors);
874 //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
875 if (!empty($fields['fee_amount']) && $financialType = CRM_Contribute_BAO_Contribution
::validateFinancialType($fields['financial_type_id'])) {
876 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
879 // $trxn_id must be unique CRM-13919
880 if (!empty($fields['trxn_id'])) {
881 $queryParams = array(1 => array($fields['trxn_id'], 'String'));
882 $query = 'select count(*) from civicrm_contribution where trxn_id = %1';
884 $queryParams[2] = array((int)$self->_id
, 'Integer');
885 $query .= ' and id !=%2';
887 $tCnt = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
889 $errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id']));
893 $errors = array_merge($errors, $softErrors);
898 * Function to process the form
904 public function postProcess() {
905 $session = CRM_Core_Session
::singleton();
906 if ($this->_action
& CRM_Core_Action
::DELETE
) {
907 CRM_Contribute_BAO_Contribution
::deleteContribution($this->_id
);
908 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
909 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
914 // get the submitted form values.
915 $submittedValues = $this->controller
->exportValues($this->_name
);
916 if (!empty($submittedValues['price_set_id']) && $this->_action
& CRM_Core_Action
::UPDATE
) {
917 $line = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution');
918 $lineID = key($line);
919 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $line[$lineID]), 'price_set_id');
920 $quickConfig = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
922 CRM_Price_BAO_LineItem
::deleteLineItems($this->_id
, 'civicrm_contribution');
926 // process price set and get total amount and line items.
928 $priceSetId = $pId = NULL;
929 $priceSetId = CRM_Utils_Array
::value('price_set_id', $submittedValues);
930 if (empty($priceSetId) && !$this->_id
) {
931 $this->_priceSetId
= $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
932 $this->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
933 $fieldID = key($this->_priceSet
['fields']);
934 $fieldValueId = key($this->_priceSet
['fields'][$fieldID]['options']);
935 $this->_priceSet
['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount'];
936 $submittedValues['price_' . $fieldID] = 1;
940 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
941 $submittedValues, $lineItem[$priceSetId]);
943 $submittedValues['total_amount'] = CRM_Utils_Array
::value('amount', $submittedValues);
947 $pId = ($this->_compId
&& $this->_context
== 'participant') ?
$this->_compId
: CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id
, 'participant_id', 'contribution_id');
949 if (!$priceSetId && !empty($submittedValues['total_amount']) && $this->_id
) {
950 // 10117 update th line items for participants
952 $entityTable = 'participant';
954 $participantParams = array(
955 'fee_amount' => $submittedValues['total_amount'],
958 CRM_Event_BAO_Participant
::add($participantParams);
959 if (empty($this->_lineItems
)) {
960 $this->_lineItems
= CRM_Price_BAO_LineItem
::getLineItems($entityID, 'participant', 1);
964 $entityTable = 'contribution';
965 $entityID = $this->_id
;
968 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($entityID, $entityTable);
969 foreach (array_keys($lineItems) as $id) {
970 $lineItems[$id]['id'] = $id;
972 $itemId = key($lineItems);
974 if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) {
975 $fieldType = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'html_type');
977 $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule
::cleanMoney(CRM_Utils_Array
::value('total_amount', $submittedValues));
978 // 10117 update th line items for participants
979 if (!empty($lineItems[$itemId]['price_field_id'])) {
980 $this->_priceSetId
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
981 $lineItem[$this->_priceSetId
] = $lineItems;
985 if ($this->_priceSetId
&& CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config')) {
988 //CRM-11529 for quick config backoffice transactions
989 //when financial_type_id is passed in form, update the
990 //lineitems with the financial type selected in form
991 if ($isQuickConfig && !empty($submittedValues['financial_type_id']) && CRM_Utils_Array
::value($this->_priceSetId
, $lineItem)
993 foreach ($lineItem[$this->_priceSetId
] as &$values) {
994 $values['financial_type_id'] = $submittedValues['financial_type_id'];
998 if (!isset($submittedValues['total_amount'])) {
999 $submittedValues['total_amount'] = CRM_Utils_Array
::value('total_amount', $this->_values
);
1001 $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ?
$lineItem : FALSE);
1003 if (!empty($submittedValues['pcp_made_through_id'])) {
1006 'pcp_made_through_id',
1007 'pcp_display_in_roll',
1008 'pcp_roll_nickname',
1009 'pcp_personal_note',
1011 foreach ($fields as $f) {
1012 $pcp[$f] = CRM_Utils_Array
::value($f, $submittedValues);
1016 //build soft credit params
1017 if (!empty($submittedValues['soft_credit_contact_select_id'])) {
1018 $softParams = $softIDs =array();
1019 foreach ($submittedValues['soft_credit_contact_select_id'] as $key => $val) {
1020 if ($val && $submittedValues['soft_credit_amount'][$key]) {
1021 $softParams[$key]['contact_id'] = $val;
1022 $softParams[$key]['amount'] = CRM_Utils_Rule
::cleanMoney($submittedValues['soft_credit_amount'][$key]);
1023 $softParams[$key]['soft_credit_type_id'] = $submittedValues['soft_credit_type'][$key];
1024 if (!empty($submittedValues['soft_credit_id'][$key])) {
1025 $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key];
1032 // set the contact, when contact is selected
1033 if (!empty($submittedValues['contact_select_id'])) {
1034 $this->_contactID
= $submittedValues['contact_select_id'][1];
1037 $config = CRM_Core_Config
::singleton();
1039 //Credit Card Contribution.
1041 $this->processCreditCard($submittedValues, $config, $session, $pId, $lineItem);
1044 //Offline Contribution.
1045 $submittedValues = $this->unsetCreditCardFields($submittedValues);
1047 // get the required field value only.
1048 $formValues = $submittedValues;
1049 $params = $ids = array();
1051 $params['contact_id'] = $this->_contactID
;
1053 $params['currency'] = $this->getCurrency($submittedValues);
1056 'financial_type_id',
1057 'contribution_status_id',
1058 'payment_instrument_id',
1063 foreach ($fields as $f) {
1064 $params[$f] = CRM_Utils_Array
::value($f, $formValues);
1068 $params['pcp'] = $pcp;
1070 if (!empty($softParams)) {
1071 $params['soft_credit'] = $softParams;
1072 $params['soft_credit_ids'] = $softIDs;
1075 //if priceset is used, no need to cleanup money
1078 $params['skipCleanMoney'] = 1;
1087 foreach ($dates as $d) {
1088 $params[$d] = CRM_Utils_Date
::processDate($formValues[$d], $formValues[$d . '_time'], TRUE);
1091 if (!empty($formValues['is_email_receipt'])) {
1092 $params['receipt_date'] = date("Y-m-d");
1095 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Cancelled', 'name')
1096 ||
$params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Refunded', 'name')
1098 if (CRM_Utils_System
::isNull(CRM_Utils_Array
::value('cancel_date', $params))) {
1099 $params['cancel_date'] = date('Y-m-d');
1103 $params['cancel_date'] = $params['cancel_reason'] = 'null';
1106 // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996
1107 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Pending', 'name')) {
1108 $params['is_pay_later'] = 1;
1111 $ids['contribution'] = $params['id'] = $this->_id
;
1113 //Add Additional common information to formatted params
1114 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($formValues, $params, $this);
1116 $params['contribution_mode'] = 'participant';
1117 $params['participant_id'] = $pId;
1118 $params['skipLineItem'] = 1;
1120 $params['line_item'] = $lineItem;
1121 $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array
::value('id', $this->_paymentProcessor
);
1122 //create contribution.
1123 if ($isQuickConfig) {
1124 $params['is_quick_config'] = 1;
1128 // if non_deductible_amount exists i.e. Additional Details fieldset was opened [and staff typed something] -> keep it.
1129 if (isset($params['non_deductible_amount']) && (!empty($params['non_deductible_amount']))) {
1130 $params['non_deductible_amount'] = $params['non_deductible_amount'];
1132 // if non_deductible_amount does NOT exist - then calculate it depending on:
1133 // $ContributionType->is_deductible and whether there is a product (premium).
1135 $contributionType = new CRM_Financial_DAO_FinancialType();
1136 $contributionType->id
= $params['financial_type_id'];
1137 if (!$contributionType->find(TRUE)) {
1138 CRM_Core_Error
::fatal('Could not find a system table');
1140 if ($contributionType->is_deductible
) {
1142 if (isset($formValues['product_name'][0])) {
1143 $selectProduct = $formValues['product_name'][0];
1145 // if there is a product - compare the value to the contribution amount
1146 if (isset($selectProduct)) {
1147 $productDAO = new CRM_Contribute_DAO_Product();
1148 $productDAO->id
= $selectProduct;
1149 $productDAO->find(TRUE);
1150 // product value exceeds contribution amount
1151 if ($params['total_amount'] < $productDAO->price
) {
1152 $params['non_deductible_amount'] = $params['total_amount'];
1154 // product value does NOT exceed contribution amount
1156 $params['non_deductible_amount'] = $productDAO->price
;
1159 // contribution is deductible - but there is no product
1161 $params['non_deductible_amount'] = '0.00';
1164 // contribution is NOT deductible
1166 $params['non_deductible_amount'] = $params['total_amount'];
1170 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
1172 // process associated membership / participant, CRM-4395
1173 $relatedComponentStatusMsg = NULL;
1174 if ($contribution->id
&& $this->_action
& CRM_Core_Action
::UPDATE
) {
1175 $relatedComponentStatusMsg = $this->updateRelatedComponent($contribution->id
,
1176 $contribution->contribution_status_id
,
1177 CRM_Utils_Array
::value('contribution_status_id',
1184 if ($contribution->id
&& isset($formValues['note'])) {
1185 CRM_Contribute_Form_AdditionalInfo
::processNote($formValues, $this->_contactID
, $contribution->id
, $this->_noteID
);
1189 if ($contribution->id
&& isset($formValues['product_name'][0])) {
1190 CRM_Contribute_Form_AdditionalInfo
::processPremium($formValues, $contribution->id
,
1191 $this->_premiumID
, $this->_options
1195 //send receipt mail.
1196 if ($contribution->id
&& !empty($formValues['is_email_receipt'])) {
1197 $formValues['contact_id'] = $this->_contactID
;
1198 $formValues['contribution_id'] = $contribution->id
;
1200 // to get 'from email id' for send receipt
1201 $this->fromEmailId
= $formValues['from_email_address'];
1202 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $formValues);
1205 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1211 //update pledge payment status.
1212 if ((($this->_ppID
&& $contribution->id
) && $this->_action
& CRM_Core_Action
::ADD
) ||
1213 (($pledgePaymentId) && $this->_action
& CRM_Core_Action
::UPDATE
)
1217 //store contribution id in payment record.
1218 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1221 $this->_ppID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1226 $this->_pledgeID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1233 $adjustTotalAmount = FALSE;
1234 if (CRM_Utils_Array
::value('option_type', $formValues) == 2) {
1235 $adjustTotalAmount = TRUE;
1238 $updatePledgePaymentStatus = FALSE;
1239 //do only if either the status or the amount has changed
1240 if ($this->_action
& CRM_Core_Action
::ADD
) {
1241 $updatePledgePaymentStatus = TRUE;
1243 elseif ($this->_action
& CRM_Core_Action
::UPDATE
&& (($this->_defaults
['contribution_status_id'] != $formValues['contribution_status_id']) ||
1244 ($this->_defaults
['total_amount'] != $formValues['total_amount']))
1246 $updatePledgePaymentStatus = TRUE;
1249 if ($updatePledgePaymentStatus) {
1250 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1251 array($this->_ppID
),
1252 $contribution->contribution_status_id
,
1254 $contribution->total_amount
,
1260 $statusMsg = ts('The contribution record has been saved.');
1261 if (!empty($formValues['is_email_receipt']) && $sendReceipt) {
1262 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1265 if ($relatedComponentStatusMsg) {
1266 $statusMsg .= ' ' . $relatedComponentStatusMsg;
1269 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');
1270 //Offline Contribution ends.
1272 $buttonName = $this->controller
->getButtonName();
1273 if ($this->_context
== 'standalone') {
1274 if ($buttonName == $this->getButtonName('upload', 'new')) {
1275 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contribute/add',
1276 'reset=1&action=add&context=standalone'
1280 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
1281 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
1285 elseif ($this->_context
== 'contribution' && $this->_mode
&& $buttonName == $this->getButtonName('upload', 'new')) {
1286 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1287 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}"
1290 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
1291 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1292 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}"
1297 public function processCreditCard($submittedValues, $config, $session, $pId, $lineItem) {
1298 $unsetParams = array(
1300 'payment_instrument_id',
1301 'contribution_status_id',
1305 foreach ($unsetParams as $key) {
1306 if (isset($submittedValues[$key])) {
1307 unset($submittedValues[$key]);
1311 // CRM-12680 set $_lineItem if its not set
1312 if (empty($this->_lineItem
) && !empty($lineItem)) {
1313 $this->_lineItem
= $lineItem;
1316 //Get the rquire fields value only.
1317 $params = $this->_params
= $submittedValues;
1319 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($this->_params
['payment_processor_id'],
1323 //get the payment processor id as per mode.
1324 $this->_params
['payment_processor'] = $params['payment_processor_id'] =
1325 $this->_params
['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor
['id'];
1327 $now = date('YmdHis');
1330 // we need to retrieve email address
1331 if ($this->_context
== 'standalone' && !empty($submittedValues['is_email_receipt'])) {
1332 list($this->userDisplayName
,
1334 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
1335 $this->assign('displayName', $this->userDisplayName
);
1338 //set email for primary location.
1339 $fields['email-Primary'] = 1;
1340 $params['email-Primary'] = $this->userEmail
;
1342 // now set the values for the billing location.
1343 foreach ($this->_fields
as $name => $dontCare) {
1347 // also add location name to the array
1348 $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);
1349 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
1350 $fields["address_name-{$this->_bltID}"] = 1;
1352 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1357 $nameFields = array('first_name', 'middle_name', 'last_name');
1358 foreach ($nameFields as $name) {
1360 if (array_key_exists("billing_$name", $params)) {
1361 $params[$name] = $params["billing_{$name}"];
1362 $params['preserveDBName'] = TRUE;
1366 if (!empty($params['source'])) {
1367 unset($params['source']);
1369 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $fields,
1375 // add all the additioanl payment params we need
1376 $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}"]);
1377 $this->_params
["country-{$this->_bltID}"] = $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
1379 if ($this->_paymentProcessor
['payment_type'] & CRM_Core_Payment
::PAYMENT_TYPE_CREDIT_CARD
) {
1380 $this->_params
['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($this->_params
);
1381 $this->_params
['month'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($this->_params
);
1383 $this->_params
['ip_address'] = CRM_Utils_System
::ipAddress();
1384 $this->_params
['amount'] = $this->_params
['total_amount'];
1385 $this->_params
['amount_level'] = 0;
1386 $this->_params
['currencyID'] = CRM_Utils_Array
::value('currency',
1388 $config->defaultCurrency
1390 $this->_params
['payment_action'] = 'Sale';
1391 if (!empty($this->_params
['receive_date'])) {
1392 $this->_params
['receive_date'] = CRM_Utils_Date
::processDate($this->_params
['receive_date'], $this->_params
['receive_date_time']);
1395 if (!empty($params['soft_credit_to'])) {
1396 $this->_params
['soft_credit_to'] = $params['soft_credit_to'];
1397 $this->_params
['pcp_made_through_id'] = $params['pcp_made_through_id'];
1400 $this->_params
['pcp_display_in_roll'] = CRM_Utils_Array
::value('pcp_display_in_roll', $params);
1401 $this->_params
['pcp_roll_nickname'] = CRM_Utils_Array
::value('pcp_roll_nickname', $params);
1402 $this->_params
['pcp_personal_note'] = CRM_Utils_Array
::value('pcp_personal_note', $params);
1404 //Add common data to formatted params
1405 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($params, $this->_params
, $this);
1407 if (empty($this->_params
['invoice_id'])) {
1408 $this->_params
['invoiceID'] = md5(uniqid(rand(), TRUE));
1411 $this->_params
['invoiceID'] = $this->_params
['invoice_id'];
1414 // at this point we've created a contact and stored its address etc
1415 // all the payment processors expect the name and address to be in the
1416 // so we copy stuff over to first_name etc.
1417 $paymentParams = $this->_params
;
1418 $paymentParams['contactID'] = $this->_contactID
;
1419 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $this->_params
, $paymentParams, TRUE);
1421 $contributionType = new CRM_Financial_DAO_FinancialType();
1422 $contributionType->id
= $params['financial_type_id'];
1423 if (!$contributionType->find(TRUE)) {
1424 CRM_Core_Error
::fatal('Could not find a system table');
1427 // add some financial type details to the params list
1428 // if folks need to use it
1429 $paymentParams['contributionType_name'] = $this->_params
['contributionType_name'] = $contributionType->name
;
1430 $paymentParams['contributionPageID'] = NULL;
1431 if (!empty($this->_params
['is_email_receipt'])) {
1432 $paymentParams['email'] = $this->userEmail
;
1433 $paymentParams['is_email_receipt'] = 1;
1436 $paymentParams['is_email_receipt'] = 0;
1437 $this->_params
['is_email_receipt'] = 0;
1439 if (!empty($this->_params
['receive_date'])) {
1440 $paymentParams['receive_date'] = $this->_params
['receive_date'];
1442 if (!empty($this->_params
['receive_date'])) {
1443 $paymentParams['receive_date'] = $this->_params
['receive_date'];
1448 // For recurring contribution, create Contribution Record first.
1449 // Contribution ID, Recurring ID and Contact ID needed
1450 // When we get a callback from the payment processor, CRM-7115
1451 if (!empty($paymentParams['is_recur'])) {
1452 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1461 $paymentParams['contributionID'] = $contribution->id
;
1462 $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
1463 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
1464 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
1467 if ($paymentParams['amount'] > 0.0) {
1468 // force a reget of the payment processor in case the form changed it, CRM-7179
1469 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this, TRUE);
1470 $result = $payment->doDirectPayment($paymentParams);
1473 if (is_a($result, 'CRM_Core_Error')) {
1474 //make sure to cleanup db for recurring case.
1475 if (!empty($paymentParams['contributionID'])) {
1476 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting contribution {$paymentParams['contributionID']}");
1477 CRM_Contribute_BAO_Contribution
::deleteContribution($paymentParams['contributionID']);
1479 if (!empty($paymentParams['contributionRecurID'])) {
1480 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting recurring contribution {$paymentParams['contributionRecurID']}");
1481 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
1484 //set the contribution mode.
1485 $urlParams = "action=add&cid={$this->_contactID}";
1487 $urlParams .= "&mode={$this->_mode}";
1489 if (!empty($this->_ppID
)) {
1490 $urlParams .= "&context=pledge&ppid={$this->_ppID}";
1492 CRM_Core_Error
::displaySessionError($result);
1493 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams));
1497 $this->_params
= array_merge($this->_params
, $result);
1500 $this->_params
['receive_date'] = $now;
1502 if (!empty($this->_params
['is_email_receipt'])) {
1503 $this->_params
['receipt_date'] = $now;
1506 $this->_params
['receipt_date'] = CRM_Utils_Date
::processDate($this->_params
['receipt_date'],
1507 $params['receipt_date_time'], TRUE
1511 $this->set('params', $this->_params
);
1512 $this->assign('trxn_id', $result['trxn_id']);
1513 $this->assign('receive_date', $this->_params
['receive_date']);
1515 // result has all the stuff we need
1516 // lets archive it to a financial transaction
1517 if ($contributionType->is_deductible
) {
1518 $this->assign('is_deductible', TRUE);
1519 $this->set('is_deductible', TRUE);
1522 // set source if not set
1523 if (empty($this->_params
['source'])) {
1524 $userID = $session->get('userID');
1525 $userSortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $userID,
1528 $this->_params
['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
1531 // build custom data getFields array
1532 $customFieldsContributionType = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE,
1533 CRM_Utils_Array
::value('financial_type_id', $params)
1535 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsContributionType,
1536 CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, TRUE)
1538 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
1545 if (empty($paymentParams['is_recur'])) {
1546 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1555 //send receipt mail.
1556 if ($contribution->id
&& !empty($this->_params
['is_email_receipt'])) {
1557 $this->_params
['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $result);
1558 $this->_params
['contact_id'] = $this->_contactID
;
1559 $this->_params
['contribution_id'] = $contribution->id
;
1560 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $this->_params
, TRUE);
1564 if ($contribution->id
&& isset($params['note'])) {
1565 CRM_Contribute_Form_AdditionalInfo
::processNote($params, $contactID, $contribution->id
, NULL);
1568 if ($contribution->id
&& isset($params['product_name'][0])) {
1569 CRM_Contribute_Form_AdditionalInfo
::processPremium($params, $contribution->id
, NULL, $this->_options
);
1572 //update pledge payment status.
1573 if ($this->_ppID
&& $contribution->id
) {
1574 //store contribution id in payment record.
1575 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1577 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1578 array($this->_ppID
),
1579 $contribution->contribution_status_id
,
1581 $contribution->total_amount
1585 if ($contribution->id
) {
1586 $statusMsg = ts('The contribution record has been processed.');
1587 if (!empty($this->_params
['is_email_receipt']) && $sendReceipt) {
1588 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1590 CRM_Core_Session
::setStatus($statusMsg, ts('Complete'), 'success');