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 * 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
;
318 $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID
);
319 $defaults = array_merge($defaults, $billingDefaults);
321 // now fix all state country selectors, set correct state based on country
322 CRM_Core_BAO_Address
::fixAllStateSelects($this, $defaults);
326 $this->_contactID
= $defaults['contact_id'];
329 // Set $newCredit variable in template to control whether link to credit card mode is included
330 CRM_Core_Payment
::allowBackofficeCreditCard($this);
332 // fix the display of the monetary value, CRM-4038
333 if (isset($defaults['total_amount'])) {
334 $defaults['total_amount'] = CRM_Utils_Money
::format($defaults['total_amount'], NULL, '%a');
337 if (isset($defaults['non_deductible_amount'])) {
338 $defaults['non_deductible_amount'] = CRM_Utils_Money
::format($defaults['non_deductible_amount'], NULL, '%a');
341 if (isset($defaults['fee_amount'])) {
342 $defaults['fee_amount'] = CRM_Utils_Money
::format($defaults['fee_amount'], NULL, '%a');
345 if (isset($defaults['net_amount'])) {
346 $defaults['net_amount'] = CRM_Utils_Money
::format($defaults['net_amount'], NULL, '%a');
349 if ($this->_contributionType
) {
350 $defaults['financial_type_id'] = $this->_contributionType
;
353 if (CRM_Utils_Array
::value('is_test', $defaults)) {
354 $this->assign('is_test', TRUE);
357 if (isset($defaults['honor_contact_id'])) {
358 $honorDefault = $ids = array();
359 $this->_honorID
= $defaults['honor_contact_id'];
360 $honorType = CRM_Core_PseudoConstant
::get('CRM_Contribute_DAO_Contribution', 'honor_type_id');
362 'id' => $defaults['honor_contact_id'],
363 'contact_id' => $defaults['honor_contact_id'],
365 CRM_Contact_BAO_Contact
::retrieve($idParams, $honorDefault, $ids);
367 $defaults['honor_prefix_id'] = CRM_Utils_Array
::value('prefix_id', $honorDefault);
368 $defaults['honor_first_name'] = CRM_Utils_Array
::value('first_name', $honorDefault);
369 $defaults['honor_last_name'] = CRM_Utils_Array
::value('last_name', $honorDefault);
370 $defaults['honor_email'] = CRM_Utils_Array
::value('email', $honorDefault['email'][1]);
371 $defaults['honor_type'] = $honorType[$defaults['honor_type_id']];
374 $this->assign('showOption', TRUE);
375 // for Premium section
376 if ($this->_premiumID
) {
377 $this->assign('showOption', FALSE);
378 $options = isset($this->_options
[$this->_productDAO
->product_id
]) ?
$this->_options
[$this->_productDAO
->product_id
] : "";
380 $this->assign('showOption', TRUE);
382 $options_key = CRM_Utils_Array
::key($this->_productDAO
->product_option
, $options);
384 $defaults['product_name'] = array($this->_productDAO
->product_id
, trim($options_key));
387 $defaults['product_name'] = array($this->_productDAO
->product_id
);
389 if ($this->_productDAO
->fulfilled_date
) {
390 list($defaults['fulfilled_date']) = CRM_Utils_Date
::setDateDefaults($this->_productDAO
->fulfilled_date
);
394 if (isset($this->userEmail
)) {
395 $this->assign('email', $this->userEmail
);
398 if (CRM_Utils_Array
::value('is_pay_later', $defaults)) {
399 $this->assign('is_pay_later', TRUE);
401 $this->assign('contribution_status_id', CRM_Utils_Array
::value('contribution_status_id', $defaults));
403 $dates = array('receive_date', 'receipt_date', 'cancel_date', 'thankyou_date');
404 foreach ($dates as $key) {
405 if (CRM_Utils_Array
::value($key, $defaults)) {
406 list($defaults[$key],
407 $defaults[$key . '_time']
408 ) = CRM_Utils_Date
::setDateDefaults(CRM_Utils_Array
::value($key, $defaults),
414 if (!$this->_id
&& !CRM_Utils_Array
::value('receive_date', $defaults)) {
415 list($defaults['receive_date'],
416 $defaults['receive_date_time']
417 ) = CRM_Utils_Date
::setDateDefaults(NULL, 'activityDateTime');
420 $this->assign('receive_date', CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('receive_date', $defaults),
421 CRM_Utils_Array
::value('receive_date_time', $defaults)
423 $currency = CRM_Utils_Array
::value('currency', $defaults);
424 $this->assign('currency', $currency);
425 // Hack to get currency info to the js layer. CRM-11440
426 CRM_Utils_Money
::format(1);
427 $this->assign('currencySymbol', CRM_Utils_Array
::value($currency, CRM_Utils_Money
::$_currencySymbols));
428 $this->assign('totalAmount', CRM_Utils_Array
::value('total_amount', $defaults));
430 //inherit campaign from pledge.
431 if ($this->_ppID
&& CRM_Utils_Array
::value('campaign_id', $this->_pledgeValues
)) {
432 $defaults['campaign_id'] = $this->_pledgeValues
['campaign_id'];
435 $this->_defaults
= $defaults;
440 * Function to build the form
445 public function buildQuickForm() {
446 if ($this->_cdType
) {
447 CRM_Custom_Form_CustomData
::buildQuickForm($this);
451 // build price set form.
452 $buildPriceSet = FALSE;
453 if (empty($this->_lineItems
) &&
454 ($this->_priceSetId || CRM_Utils_Array
::value('price_set_id', $_POST))
456 $buildPriceSet = TRUE;
457 $getOnlyPriceSetElements = TRUE;
458 if (!$this->_priceSetId
) {
459 $this->_priceSetId
= $_POST['price_set_id'];
460 $getOnlyPriceSetElements = FALSE;
463 $this->set('priceSetId', $this->_priceSetId
);
464 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
466 // get only price set form elements.
467 if ($getOnlyPriceSetElements) {
471 // use to build form during form rule.
472 $this->assign('buildPriceSet', $buildPriceSet);
474 $showAdditionalInfo = FALSE;
476 $defaults = $this->_values
;
477 $additionalDetailFields = array(
481 'non_deductible_amount',
485 foreach ($additionalDetailFields as $key) {
486 if (!empty($defaults[$key])) {
487 $defaults['hidden_AdditionalDetail'] = 1;
492 $honorFields = array(
499 foreach ($honorFields as $key) {
500 if (!empty($defaults[$key])) {
501 $defaults['hidden_Honoree'] = 1;
506 //check for honoree pane.
507 if ($this->_ppID
&& CRM_Utils_Array
::value('honor_contact_id', $this->_pledgeValues
)) {
508 $defaults['hidden_Honoree'] = 1;
511 if ($this->_productDAO
) {
512 if ($this->_productDAO
->product_id
) {
513 $defaults['hidden_Premium'] = 1;
517 if ($this->_noteID
&&
518 isset($this->_values
['note'])
520 $defaults['hidden_AdditionalDetail'] = 1;
524 ts('Additional Details') => 'AdditionalDetail',
525 ts('Honoree Information') => 'Honoree'
528 //Add Premium pane only if Premium is exists.
529 $dao = new CRM_Contribute_DAO_Product();
532 if ($dao->find(TRUE)) {
533 $paneNames[ts('Premium Information')] = 'Premium';
538 if (CRM_Utils_Array
::value('payment_type', $this->_processors
) & CRM_Core_Payment
::PAYMENT_TYPE_DIRECT_DEBIT
540 $ccPane = array(ts('Direct Debit Information') => 'DirectDebit');
543 $ccPane = array(ts('Credit Card Information') => 'CreditCard');
546 if (is_array($ccPane)) {
547 $paneNames = array_merge($ccPane, $paneNames);
550 $buildRecurBlock = FALSE;
551 foreach ($paneNames as $name => $type) {
552 $urlParams = "snippet=4&formType={$type}";
554 $urlParams .= "&mode={$this->_mode}";
558 if ($type == 'CreditCard' ||
559 $type == 'DirectDebit'
564 $allPanes[$name] = array(
565 'url' => CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams),
570 // see if we need to include this paneName in the current form
571 if ($this->_formType
== $type ||
572 CRM_Utils_Array
::value("hidden_{$type}", $_POST) ||
573 CRM_Utils_Array
::value("hidden_{$type}", $defaults)
575 $showAdditionalInfo = TRUE;
576 $allPanes[$name]['open'] = 'true';
579 if ($type == 'CreditCard') {
580 $buildRecurBlock = TRUE;
581 $this->add('hidden', 'hidden_CreditCard', 1);
582 CRM_Core_Payment_Form
::buildCreditCard($this, TRUE);
584 elseif ($type == 'DirectDebit') {
585 $buildRecurBlock = TRUE;
586 $this->add('hidden', 'hidden_DirectDebit', 1);
587 CRM_Core_Payment_Form
::buildDirectDebit($this, TRUE);
590 $additionalInfoFormFunction = 'build' . $type;
591 CRM_Contribute_Form_AdditionalInfo
::$additionalInfoFormFunction($this);
594 if (empty($this->_recurPaymentProcessors
)) {
595 $buildRecurBlock = FALSE;
597 if ($buildRecurBlock) {
598 CRM_Contribute_Form_Contribution_Main
::buildRecur($this);
599 $this->setDefaults(array('is_recur' => 0));
601 $this->assign('buildRecurBlock', $buildRecurBlock);
602 $qfKey = $this->controller
->_key
;
603 $this->assign('qfKey', $qfKey);
604 $this->assign('allPanes', $allPanes);
605 $this->assign('showAdditionalInfo', $showAdditionalInfo);
607 if ($this->_formType
) {
608 $this->assign('formType', $this->_formType
);
612 $this->applyFilter('__ALL__', 'trim');
614 if ($this->_action
& CRM_Core_Action
::DELETE
) {
615 $this->addButtons(array(
618 'name' => ts('Delete'),
619 'spacing' => ' ',
624 'name' => ts('Cancel')
631 //need to assign custom data type and subtype to the template
632 $this->assign('customDataType', 'Contribution');
633 $this->assign('customDataSubType', $this->_contributionType
);
634 $this->assign('entityID', $this->_id
);
636 if ($this->_context
== 'standalone') {
637 CRM_Contact_Form_NewContact
::buildQuickForm($this);
640 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution');
642 $financialType = $this->add('select', 'financial_type_id',
643 ts('Financial Type'),
644 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType(),
646 array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );")
650 $paymentInstrument = $this->add('select', 'payment_instrument_id',
652 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
653 FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
657 $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), $attributes['trxn_id']);
659 //add receipt for offline contribution
660 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
662 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
);
664 $status = CRM_Contribute_PseudoConstant
::contributionStatus();
666 // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169)
667 $statusName = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
674 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
677 elseif ((!$this->_ppID
&& $this->_id
) ||
!$this->_id
) {
682 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
686 $contributionStatus = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id
, 'contribution_status_id');
687 $name = CRM_Utils_Array
::value($contributionStatus, $statusName);
692 unset($status[CRM_Utils_Array
::key('Pending', $statusName)]);
693 unset($status[CRM_Utils_Array
::key('Failed', $statusName)]);
696 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
705 unset($status[CRM_Utils_Array
::key($suppress, $statusName)]);
711 unset($status[CRM_Utils_Array
::key('Refunded', $statusName)]);
714 $this->add('select', 'contribution_status_id',
715 ts('Contribution Status'),
721 $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
723 if ($this->_online
) {
724 $this->assign('hideCalender', TRUE);
726 $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']);
728 $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime'));
729 $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime'));
731 $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']);
734 if ($buildRecurBlock) {
735 $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
737 $element = $this->add('select',
738 'payment_processor_id',
739 ts('Payment Processor'),
745 if ($this->_online
) {
749 if (empty($this->_lineItems
)) {
750 $buildPriceSet = FALSE;
751 $priceSets = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviContribute');
752 if (!empty($priceSets) && !$this->_ppID
) {
753 $buildPriceSet = TRUE;
756 // don't allow price set for contribution if it is related to participant, or if it is a pledge payment
757 // and if we already have line items for that participant. CRM-5095
758 if ($buildPriceSet && $this->_id
) {
759 $componentDetails = CRM_Contribute_BAO_Contribution
::getComponentDetails($this->_id
);
760 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
765 if ($pledgePaymentId) {
766 $buildPriceSet = FALSE;
768 if ($participantID = CRM_Utils_Array
::value('participant', $componentDetails)) {
769 $participantLI = CRM_Price_BAO_LineItem
::getLineItems($participantID);
770 if (!CRM_Utils_System
::isNull($participantLI)) {
771 $buildPriceSet = FALSE;
776 $hasPriceSets = FALSE;
777 if ($buildPriceSet) {
778 $hasPriceSets = TRUE;
779 $element = $this->add('select', 'price_set_id', ts('Choose price set'),
781 '' => ts('Choose price set')
783 NULL, array('onchange' => "buildAmount( this.value );")
785 if ($this->_online
&& !($this->_action
& CRM_Core_Action
::UPDATE
)) {
789 $this->assign('hasPriceSets', $hasPriceSets);
790 $currencyFreeze = FALSE;
791 if (!($this->_action
& CRM_Core_Action
::UPDATE
)) {
792 if ($this->_online ||
$this->_ppID
) {
793 $attributes['total_amount'] = array_merge($attributes['total_amount'], array(
795 'style' => "background-color:#EBECE4"
797 $optionTypes = array(
798 '1' => ts('Adjust Pledge Payment Schedule?'),
799 '2' => ts('Adjust Total Pledge Amount?'),
801 $this->addRadio('option_type',
807 $currencyFreeze = TRUE;
811 $totalAmount = $this->addMoney('total_amount',
813 ($hasPriceSets) ?
FALSE : TRUE,
814 $attributes['total_amount'],
815 TRUE, 'currency', NULL, $currencyFreeze
819 $this->add('text', 'source', ts('Source'), CRM_Utils_Array
::value('source', $attributes));
821 //CRM-7362 --add campaigns.
822 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
824 CRM_Contribute_Form_SoftCredit
::buildQuickForm($this);
828 $js = array('onclick' => "return verify( );");
831 $mailingInfo = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
834 $this->assign('outBound_option', $mailingInfo['outBound_option']);
836 $this->addButtons(array(
839 'name' => ts('Save'),
845 'name' => ts('Save and New'),
851 'name' => ts('Cancel')
856 // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type,
857 // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail
858 if ($this->_id
&& $this->_values
['contribution_status_id'] == array_search('Cancelled', $statusName)) {
860 $totalAmount->freeze();
862 $checkNumber->freeze();
863 $paymentInstrument->freeze();
865 $financialType->freeze();
868 $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this);
870 if ($this->_action
& CRM_Core_Action
::VIEW
) {
878 * @param array $fields the input form values
879 * @param array $files the uploaded files if any
880 * @param array $options additional user data
882 * @return true if no errors, else array of errors
886 static function formRule($fields, $files, $self) {
889 //check if contact is selected in standalone mode
890 if (isset($fields['contact_select_id'][1]) && !$fields['contact_select_id'][1]) {
891 $errors['contact[1]'] = ts('Please select a contact or create new contact');
894 if (isset($fields['honor_type_id'])) {
895 if (!((CRM_Utils_Array
::value('honor_first_name', $fields) &&
896 CRM_Utils_Array
::value('honor_last_name', $fields)
898 CRM_Utils_Array
::value('honor_email', $fields)
901 $errors['honor_first_name'] = ts('Honor First Name and Last Name OR an email should be set.');
905 //check for Credit Card Contribution.
907 if (empty($fields['payment_processor_id'])) {
908 $errors['payment_processor_id'] = ts('Payment Processor is a required field.');
912 // do the amount validations.
913 if (!CRM_Utils_Array
::value('total_amount', $fields) && empty($self->_lineItems
)) {
914 if ($priceSetId = CRM_Utils_Array
::value('price_set_id', $fields)) {
915 CRM_Price_BAO_PriceField
::priceSetValidation($priceSetId, $fields, $errors);
919 $softErrors = CRM_Contribute_Form_SoftCredit
::formRule($fields);
921 if (CRM_Utils_Array
::value('total_amount', $fields) && (CRM_Utils_Array
::value('net_amount', $fields) || CRM_Utils_Array
::value('fee_amount', $fields))) {
922 $sum = CRM_Utils_Rule
::cleanMoney($fields['net_amount']) + CRM_Utils_Rule
::cleanMoney($fields['fee_amount']);
923 if (CRM_Utils_Rule
::cleanMoney($fields['total_amount']) != $sum) {
924 $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
927 //form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
928 if ($self->_id
&& $self->_values
['contribution_status_id'] != $fields['contribution_status_id']) {
929 CRM_Contribute_BAO_Contribution
::checkStatusValidation($self->_values
, $fields, $errors);
932 //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
933 if (CRM_Utils_Array
::value('fee_amount', $fields)
934 && $financialType = CRM_Contribute_BAO_Contribution
::validateFinancialType($fields['financial_type_id'])) {
935 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
938 $errors = array_merge($errors, $softErrors);
943 * Function to process the form
949 public function postProcess() {
950 $session = CRM_Core_Session
::singleton();
951 if ($this->_action
& CRM_Core_Action
::DELETE
) {
952 CRM_Contribute_BAO_Contribution
::deleteContribution($this->_id
);
953 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
954 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
959 // get the submitted form values.
960 $submittedValues = $this->controller
->exportValues($this->_name
);
961 if (CRM_Utils_Array
::value('price_set_id', $submittedValues) && $this->_action
& CRM_Core_Action
::UPDATE
) {
962 $line = CRM_Price_BAO_LineItem
::getLineItems($this->_id
, 'contribution');
963 $lineID = key($line);
964 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array
::value('price_field_id', $line[$lineID]), 'price_set_id');
965 $quickConfig = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config');
967 CRM_Price_BAO_LineItem
::deleteLineItems($this->_id
, 'civicrm_contribution');
971 // process price set and get total amount and line items.
973 $priceSetId = $pId = NULL;
974 $priceSetId = CRM_Utils_Array
::value('price_set_id', $submittedValues);
975 if (empty($priceSetId) && !$this->_id
) {
976 $this->_priceSetId
= $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
977 $this->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSetId));
978 $fieldID = key($this->_priceSet
['fields']);
979 $fieldValueId = key($this->_priceSet
['fields'][$fieldID]['options']);
980 $this->_priceSet
['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount'];
981 $submittedValues['price_' . $fieldID] = 1;
985 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
986 $submittedValues, $lineItem[$priceSetId]);
988 $submittedValues['total_amount'] = CRM_Utils_Array
::value('amount', $submittedValues);
992 $pId = ($this->_compId
&& $this->_context
== 'participant') ?
$this->_compId
: CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id
, 'participant_id', 'contribution_id');
994 if (!$priceSetId && CRM_Utils_Array
::value('total_amount', $submittedValues) && $this->_id
) {
995 // 10117 update th line items for participants
997 $entityTable = 'participant';
999 $participantParams = array(
1000 'fee_amount' => $submittedValues['total_amount'],
1003 CRM_Event_BAO_Participant
::add($participantParams);
1004 if (empty($this->_lineItems
)) {
1005 $this->_lineItems
= CRM_Price_BAO_LineItem
::getLineItems($entityID, 'participant', 1);
1009 $entityTable = 'contribution';
1010 $entityID = $this->_id
;
1013 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($entityID, $entityTable);
1014 foreach (array_keys($lineItems) as $id) {
1015 $lineItems[$id]['id'] = $id;
1017 $itemId = key($lineItems);
1019 if ($itemId && CRM_Utils_Array
::value('price_field_id', $lineItems[$itemId])) {
1020 $fieldType = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'html_type');
1022 $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule
::cleanMoney(CRM_Utils_Array
::value('total_amount', $submittedValues));
1023 // 10117 update th line items for participants
1024 if (!empty($lineItems[$itemId]['price_field_id'])) {
1025 $this->_priceSetId
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');
1026 $lineItem[$this->_priceSetId
] = $lineItems;
1030 if ($this->_priceSetId
&& CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId
, 'is_quick_config')) {
1033 //CRM-11529 for quick config backoffice transactions
1034 //when financial_type_id is passed in form, update the
1035 //lineitems with the financial type selected in form
1036 if ($isQuickConfig && CRM_Utils_Array
::value('financial_type_id', $submittedValues)
1037 && CRM_Utils_Array
::value($this->_priceSetId
, $lineItem)
1039 foreach ($lineItem[$this->_priceSetId
] as &$values) {
1040 $values['financial_type_id'] = $submittedValues['financial_type_id'];
1044 if (!isset($submittedValues['total_amount'])) {
1045 $submittedValues['total_amount'] = CRM_Utils_Array
::value('total_amount', $this->_values
);
1047 $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ?
$lineItem : FALSE);
1049 if (CRM_Utils_Array
::value('pcp_made_through_id', $submittedValues)) {
1052 'pcp_made_through_id',
1053 'pcp_display_in_roll',
1054 'pcp_roll_nickname',
1055 'pcp_personal_note',
1057 foreach ($fields as $f) {
1058 $pcp[$f] = CRM_Utils_Array
::value($f, $submittedValues);
1062 //build soft credit params
1063 if (!empty($submittedValues['soft_credit_contact_select_id'])) {
1064 $softParams = $softIDs =array();
1065 foreach ($submittedValues['soft_credit_contact_select_id'] as $key => $val) {
1066 if ($val && $submittedValues['soft_credit_amount'][$key]) {
1067 $softParams[$key]['contact_id'] = $val;
1068 $softParams[$key]['amount'] = CRM_Utils_Rule
::cleanMoney($submittedValues['soft_credit_amount'][$key]);
1069 if (!empty($submittedValues['soft_credit_id'][$key])) {
1070 $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key];
1077 // set the contact, when contact is selected
1078 if (CRM_Utils_Array
::value('contact_select_id', $submittedValues)) {
1079 $this->_contactID
= $submittedValues['contact_select_id'][1];
1082 $config = CRM_Core_Config
::singleton();
1084 //Credit Card Contribution.
1086 $this->processCreditCard($submittedValues, $config, $session, $pId, $lineItem);
1089 //Offline Contribution.
1090 $submittedValues = $this->unsetCreditCardFields($submittedValues);
1092 // get the required field value only.
1093 $formValues = $submittedValues;
1094 $params = $ids = array();
1096 $params['contact_id'] = $this->_contactID
;
1098 $params['currency'] = $this->getCurrency($submittedValues);
1101 'financial_type_id',
1102 'contribution_status_id',
1103 'payment_instrument_id',
1108 foreach ($fields as $f) {
1109 $params[$f] = CRM_Utils_Array
::value($f, $formValues);
1113 $params['pcp'] = $pcp;
1115 if (!empty($softParams)) {
1116 $params['soft_credit'] = $softParams;
1117 $params['soft_credit_ids'] = $softIDs;
1120 //if priceset is used, no need to cleanup money
1123 $params['skipCleanMoney'] = 1;
1132 foreach ($dates as $d) {
1133 $params[$d] = CRM_Utils_Date
::processDate($formValues[$d], $formValues[$d . '_time'], TRUE);
1136 if (CRM_Utils_Array
::value('is_email_receipt', $formValues)) {
1137 $params['receipt_date'] = date("Y-m-d");
1140 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Cancelled', 'name')
1141 ||
$params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Refunded', 'name')
1143 if (CRM_Utils_System
::isNull(CRM_Utils_Array
::value('cancel_date', $params))) {
1144 $params['cancel_date'] = date('Y-m-d');
1148 $params['cancel_date'] = $params['cancel_reason'] = 'null';
1151 // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996
1152 if ($params['contribution_status_id'] == CRM_Core_OptionGroup
::getValue('contribution_status', 'Pending', 'name')) {
1153 $params['is_pay_later'] = 1;
1156 $ids['contribution'] = $params['id'] = $this->_id
;
1158 //Add Additional common information to formatted params
1159 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($formValues, $params, $this);
1161 $params['contribution_mode'] = 'participant';
1162 $params['participant_id'] = $pId;
1163 $params['skipLineItem'] = 1;
1165 $params['line_item'] = $lineItem;
1166 $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array
::value('id', $this->_paymentProcessor
);
1167 //create contribution.
1168 if ($isQuickConfig) {
1169 $params['is_quick_config'] = 1;
1173 // if non_deductible_amount exists i.e. Additional Details fieldset was opened [and staff typed something] -> keep it.
1174 if (isset($params['non_deductible_amount']) && (!empty($params['non_deductible_amount']))) {
1175 $params['non_deductible_amount'] = $params['non_deductible_amount'];
1177 // if non_deductible_amount does NOT exist - then calculate it depending on:
1178 // $ContributionType->is_deductible and whether there is a product (premium).
1180 $contributionType = new CRM_Financial_DAO_FinancialType();
1181 $contributionType->id
= $params['financial_type_id'];
1182 if (!$contributionType->find(TRUE)) {
1183 CRM_Core_Error
::fatal('Could not find a system table');
1185 if ($contributionType->is_deductible
) {
1187 if (isset($formValues['product_name'][0])) {
1188 $selectProduct = $formValues['product_name'][0];
1190 // if there is a product - compare the value to the contribution amount
1191 if (isset($selectProduct)) {
1192 $productDAO = new CRM_Contribute_DAO_Product();
1193 $productDAO->id
= $selectProduct;
1194 $productDAO->find(TRUE);
1195 // product value exceeds contribution amount
1196 if ($params['total_amount'] < $productDAO->price
) {
1197 $params['non_deductible_amount'] = $params['total_amount'];
1199 // product value does NOT exceed contribution amount
1201 $params['non_deductible_amount'] = $productDAO->price
;
1204 // contribution is deductible - but there is no product
1206 $params['non_deductible_amount'] = '0.00';
1209 // contribution is NOT deductible
1211 $params['non_deductible_amount'] = $params['total_amount'];
1215 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
1217 // process associated membership / participant, CRM-4395
1218 $relatedComponentStatusMsg = NULL;
1219 if ($contribution->id
&& $this->_action
& CRM_Core_Action
::UPDATE
) {
1220 $relatedComponentStatusMsg = $this->updateRelatedComponent($contribution->id
,
1221 $contribution->contribution_status_id
,
1222 CRM_Utils_Array
::value('contribution_status_id',
1229 if ($contribution->id
&& isset($formValues['note'])) {
1230 CRM_Contribute_Form_AdditionalInfo
::processNote($formValues, $this->_contactID
, $contribution->id
, $this->_noteID
);
1234 if ($contribution->id
&& isset($formValues['product_name'][0])) {
1235 CRM_Contribute_Form_AdditionalInfo
::processPremium($formValues, $contribution->id
,
1236 $this->_premiumID
, $this->_options
1240 //send receipt mail.
1241 if ($contribution->id
&& CRM_Utils_Array
::value('is_email_receipt', $formValues)) {
1242 $formValues['contact_id'] = $this->_contactID
;
1243 $formValues['contribution_id'] = $contribution->id
;
1245 // to get 'from email id' for send receipt
1246 $this->fromEmailId
= $formValues['from_email_address'];
1247 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $formValues);
1250 $pledgePaymentId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1256 //update pledge payment status.
1257 if ((($this->_ppID
&& $contribution->id
) && $this->_action
& CRM_Core_Action
::ADD
) ||
1258 (($pledgePaymentId) && $this->_action
& CRM_Core_Action
::UPDATE
)
1262 //store contribution id in payment record.
1263 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1266 $this->_ppID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1271 $this->_pledgeID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
1278 $adjustTotalAmount = FALSE;
1279 if (CRM_Utils_Array
::value('option_type', $formValues) == 2) {
1280 $adjustTotalAmount = TRUE;
1283 $updatePledgePaymentStatus = FALSE;
1284 //do only if either the status or the amount has changed
1285 if ($this->_action
& CRM_Core_Action
::ADD
) {
1286 $updatePledgePaymentStatus = TRUE;
1288 elseif ($this->_action
& CRM_Core_Action
::UPDATE
&& (($this->_defaults
['contribution_status_id'] != $formValues['contribution_status_id']) ||
1289 ($this->_defaults
['total_amount'] != $formValues['total_amount']))
1291 $updatePledgePaymentStatus = TRUE;
1294 if ($updatePledgePaymentStatus) {
1295 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1296 array($this->_ppID
),
1297 $contribution->contribution_status_id
,
1299 $contribution->total_amount
,
1305 $statusMsg = ts('The contribution record has been saved.');
1306 if (CRM_Utils_Array
::value('is_email_receipt', $formValues) && $sendReceipt) {
1307 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1310 if ($relatedComponentStatusMsg) {
1311 $statusMsg .= ' ' . $relatedComponentStatusMsg;
1314 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');
1315 //Offline Contribution ends.
1317 $buttonName = $this->controller
->getButtonName();
1318 if ($this->_context
== 'standalone') {
1319 if ($buttonName == $this->getButtonName('upload', 'new')) {
1320 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contribute/add',
1321 'reset=1&action=add&context=standalone'
1325 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
1326 "reset=1&cid={$this->_contactID}&selectedChild=contribute"
1330 elseif ($this->_context
== 'contribution' && $this->_mode
&& $buttonName == $this->getButtonName('upload', 'new')) {
1331 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1332 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}"
1335 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
1336 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/contribution',
1337 "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}"
1342 public function processCreditCard($submittedValues, $config, $session, $pId, $lineItem) {
1343 $unsetParams = array(
1345 'payment_instrument_id',
1346 'contribution_status_id',
1350 foreach ($unsetParams as $key) {
1351 if (isset($submittedValues[$key])) {
1352 unset($submittedValues[$key]);
1356 // CRM-12680 set $_lineItem if its not set
1357 if (empty($this->_lineItem
) && !empty($lineItem)) {
1358 $this->_lineItem
= $lineItem;
1361 //Get the rquire fields value only.
1362 $params = $this->_params
= $submittedValues;
1364 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($this->_params
['payment_processor_id'],
1368 //get the payment processor id as per mode.
1369 $this->_params
['payment_processor'] = $params['payment_processor_id'] =
1370 $this->_params
['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor
['id'];
1372 $now = date('YmdHis');
1375 // we need to retrieve email address
1376 if ($this->_context
== 'standalone' && CRM_Utils_Array
::value('is_email_receipt', $submittedValues)) {
1377 list($this->userDisplayName
,
1379 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
1380 $this->assign('displayName', $this->userDisplayName
);
1383 //set email for primary location.
1384 $fields['email-Primary'] = 1;
1385 $params['email-Primary'] = $this->userEmail
;
1387 // now set the values for the billing location.
1388 foreach ($this->_fields
as $name => $dontCare) {
1392 // also add location name to the array
1393 $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);
1394 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
1395 $fields["address_name-{$this->_bltID}"] = 1;
1397 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
1402 $nameFields = array('first_name', 'middle_name', 'last_name');
1403 foreach ($nameFields as $name) {
1405 if (array_key_exists("billing_$name", $params)) {
1406 $params[$name] = $params["billing_{$name}"];
1407 $params['preserveDBName'] = TRUE;
1411 if (CRM_Utils_Array
::value('source', $params)) {
1412 unset($params['source']);
1414 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $fields,
1420 // add all the additioanl payment params we need
1421 $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}"]);
1422 $this->_params
["country-{$this->_bltID}"] = $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
1424 if ($this->_paymentProcessor
['payment_type'] & CRM_Core_Payment
::PAYMENT_TYPE_CREDIT_CARD
) {
1425 $this->_params
['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($this->_params
);
1426 $this->_params
['month'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($this->_params
);
1428 $this->_params
['ip_address'] = CRM_Utils_System
::ipAddress();
1429 $this->_params
['amount'] = $this->_params
['total_amount'];
1430 $this->_params
['amount_level'] = 0;
1431 $this->_params
['currencyID'] = CRM_Utils_Array
::value('currency',
1433 $config->defaultCurrency
1435 $this->_params
['payment_action'] = 'Sale';
1436 if (CRM_Utils_Array
::value('receive_date', $this->_params
)) {
1437 $this->_params
['receive_date'] = CRM_Utils_Date
::processDate($this->_params
['receive_date'], $this->_params
['receive_date_time']);
1440 if (CRM_Utils_Array
::value('soft_credit_to', $params)) {
1441 $this->_params
['soft_credit_to'] = $params['soft_credit_to'];
1442 $this->_params
['pcp_made_through_id'] = $params['pcp_made_through_id'];
1445 $this->_params
['pcp_display_in_roll'] = CRM_Utils_Array
::value('pcp_display_in_roll', $params);
1446 $this->_params
['pcp_roll_nickname'] = CRM_Utils_Array
::value('pcp_roll_nickname', $params);
1447 $this->_params
['pcp_personal_note'] = CRM_Utils_Array
::value('pcp_personal_note', $params);
1449 //Add common data to formatted params
1450 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($params, $this->_params
, $this);
1452 if (empty($this->_params
['invoice_id'])) {
1453 $this->_params
['invoiceID'] = md5(uniqid(rand(), TRUE));
1456 $this->_params
['invoiceID'] = $this->_params
['invoice_id'];
1459 // at this point we've created a contact and stored its address etc
1460 // all the payment processors expect the name and address to be in the
1461 // so we copy stuff over to first_name etc.
1462 $paymentParams = $this->_params
;
1463 $paymentParams['contactID'] = $this->_contactID
;
1464 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $this->_params
, $paymentParams, TRUE);
1466 $contributionType = new CRM_Financial_DAO_FinancialType();
1467 $contributionType->id
= $params['financial_type_id'];
1468 if (!$contributionType->find(TRUE)) {
1469 CRM_Core_Error
::fatal('Could not find a system table');
1472 // add some financial type details to the params list
1473 // if folks need to use it
1474 $paymentParams['contributionType_name'] = $this->_params
['contributionType_name'] = $contributionType->name
;
1475 $paymentParams['contributionPageID'] = NULL;
1476 if (CRM_Utils_Array
::value('is_email_receipt', $this->_params
)) {
1477 $paymentParams['email'] = $this->userEmail
;
1478 $paymentParams['is_email_receipt'] = 1;
1481 $paymentParams['is_email_receipt'] = 0;
1482 $this->_params
['is_email_receipt'] = 0;
1484 if (CRM_Utils_Array
::value('receive_date', $this->_params
)) {
1485 $paymentParams['receive_date'] = $this->_params
['receive_date'];
1487 if (CRM_Utils_Array
::value('receive_date', $this->_params
)) {
1488 $paymentParams['receive_date'] = $this->_params
['receive_date'];
1493 // For recurring contribution, create Contribution Record first.
1494 // Contribution ID, Recurring ID and Contact ID needed
1495 // When we get a callback from the payment processor, CRM-7115
1496 if (CRM_Utils_Array
::value('is_recur', $paymentParams)) {
1497 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1506 $paymentParams['contributionID'] = $contribution->id
;
1507 $paymentParams['contributionTypeID'] = $contribution->financial_type_id
;
1508 $paymentParams['contributionPageID'] = $contribution->contribution_page_id
;
1509 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id
;
1512 if ($paymentParams['amount'] > 0.0) {
1513 // force a reget of the payment processor in case the form changed it, CRM-7179
1514 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this, TRUE);
1515 $result = $payment->doDirectPayment($paymentParams);
1518 if (is_a($result, 'CRM_Core_Error')) {
1519 //make sure to cleanup db for recurring case.
1520 if (CRM_Utils_Array
::value('contributionID', $paymentParams)) {
1521 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting contribution {$paymentParams['contributionID']}");
1522 CRM_Contribute_BAO_Contribution
::deleteContribution($paymentParams['contributionID']);
1524 if (CRM_Utils_Array
::value('contributionRecurID', $paymentParams)) {
1525 CRM_Core_Error
::debug_log_message(CRM_Core_Error
::getMessages($result) . "contact id={$this->_contactID} (deleting recurring contribution {$paymentParams['contributionRecurID']}");
1526 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($paymentParams['contributionRecurID']);
1529 //set the contribution mode.
1530 $urlParams = "action=add&cid={$this->_contactID}";
1532 $urlParams .= "&mode={$this->_mode}";
1534 if (!empty($this->_ppID
)) {
1535 $urlParams .= "&context=pledge&ppid={$this->_ppID}";
1537 CRM_Core_Error
::displaySessionError($result);
1538 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams));
1542 $this->_params
= array_merge($this->_params
, $result);
1545 $this->_params
['receive_date'] = $now;
1547 if (CRM_Utils_Array
::value('is_email_receipt', $this->_params
)) {
1548 $this->_params
['receipt_date'] = $now;
1551 $this->_params
['receipt_date'] = CRM_Utils_Date
::processDate($this->_params
['receipt_date'],
1552 $params['receipt_date_time'], TRUE
1556 $this->set('params', $this->_params
);
1557 $this->assign('trxn_id', $result['trxn_id']);
1558 $this->assign('receive_date', $this->_params
['receive_date']);
1560 // result has all the stuff we need
1561 // lets archive it to a financial transaction
1562 if ($contributionType->is_deductible
) {
1563 $this->assign('is_deductible', TRUE);
1564 $this->set('is_deductible', TRUE);
1567 // set source if not set
1568 if (empty($this->_params
['source'])) {
1569 $userID = $session->get('userID');
1570 $userSortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $userID,
1573 $this->_params
['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
1576 // build custom data getFields array
1577 $customFieldsContributionType = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE,
1578 CRM_Utils_Array
::value('financial_type_id', $params)
1580 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsContributionType,
1581 CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, TRUE)
1583 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
1590 if (!CRM_Utils_Array
::value('is_recur', $paymentParams)) {
1591 $contribution = CRM_Contribute_Form_Contribution_Confirm
::processContribution($this,
1600 //send receipt mail.
1601 if ($contribution->id
&&
1602 CRM_Utils_Array
::value('is_email_receipt', $this->_params
)
1604 $this->_params
['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $result);
1605 $this->_params
['contact_id'] = $this->_contactID
;
1606 $this->_params
['contribution_id'] = $contribution->id
;
1607 $sendReceipt = CRM_Contribute_Form_AdditionalInfo
::emailReceipt($this, $this->_params
, TRUE);
1611 if ($contribution->id
&& isset($params['note'])) {
1612 CRM_Contribute_Form_AdditionalInfo
::processNote($params, $contactID, $contribution->id
, NULL);
1615 if ($contribution->id
&& isset($params['product_name'][0])) {
1616 CRM_Contribute_Form_AdditionalInfo
::processPremium($params, $contribution->id
, NULL, $this->_options
);
1619 //update pledge payment status.
1620 if ($this->_ppID
&& $contribution->id
) {
1621 //store contribution id in payment record.
1622 CRM_Core_DAO
::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID
, 'contribution_id', $contribution->id
);
1624 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($this->_pledgeID
,
1625 array($this->_ppID
),
1626 $contribution->contribution_status_id
,
1628 $contribution->total_amount
1632 if ($contribution->id
) {
1633 $statusMsg = ts('The contribution record has been processed.');
1634 if (CRM_Utils_Array
::value('is_email_receipt', $this->_params
) && $sendReceipt) {
1635 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
1637 CRM_Core_Session
::setStatus($statusMsg, ts('Complete'), 'success');