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_AbstractEditPayment
extends CRM_Core_Form
{
49 public $_paymentProcessor;
50 public $_recurPaymentProcessors;
55 * the id of the contribution that we are proceessing
63 * the id of the premium that we are proceessing
68 public $_premiumID = NULL;
69 public $_productDAO = NULL;
80 * the id of the contact associated with this contribution
88 * the id of the pledge payment that we are processing
96 * the id of the pledge that we are processing
104 * is this contribution associated with an online
105 * financial transaction
110 public $_online = FALSE;
113 * Stores all product option
121 * stores the honor id
126 public $_honorID = NULL;
129 * Store the contribution Type ID
133 public $_contributionType;
136 * The contribution values if an existing contribution
141 * The pledge values if this contribution is associated with pledge
143 public $_pledgeValues;
145 public $_contributeMode = 'direct';
152 * Store the line items if price set used.
156 protected $_formType;
159 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
161 $params = array('id' => $id);
162 CRM_Contribute_BAO_Contribution
::getValues($params, $values, $ids);
164 //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL)
165 $this->_online
= FALSE;
166 $fids = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($id);
167 if (!empty($fids['financialTrxnId'])) {
168 $this->_online
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
171 // Also don't allow user to update some fields for recurring contributions.
172 if (!$this->_online
) {
173 $this->_online
= CRM_Utils_Array
::value('contribution_recur_id', $values);
176 $this->assign('isOnline', $this->_online ?
TRUE : FALSE);
179 $daoNote = new CRM_Core_BAO_Note();
180 $daoNote->entity_table
= 'civicrm_contribution';
181 $daoNote->entity_id
= $id;
182 if ($daoNote->find(TRUE)) {
183 $this->_noteID
= $daoNote->id
;
184 $values['note'] = $daoNote->note
;
186 $this->_contributionType
= $values['financial_type_id'];
190 * @param string $type eg 'Contribution'
191 * @param string $subType
192 * @param int $entityId
194 public function applyCustomData($type, $subType, $entityId) {
195 $this->set('type', $type);
196 $this->set('subType', $subType);
197 $this->set('entityId', $entityId);
199 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, 1, $type, $entityId);
200 CRM_Custom_Form_CustomData
::buildQuickForm($this);
201 CRM_Custom_Form_CustomData
::setDefaultValues($this);
204 public function assignPremiumProduct($id) { //to get Premium id
207 FROM civicrm_contribution_product
208 WHERE contribution_id = {$id}
210 $dao = CRM_Core_DAO
::executeQuery($sql,
211 CRM_Core_DAO
::$_nullArray
214 $this->_premiumID
= $dao->id
;
215 $this->_productDAO
= $dao;
221 * This function process contribution related objects.
223 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) {
225 if (!$contributionId ||
!$statusId) {
230 'contribution_id' => $contributionId,
231 'contribution_status_id' => $statusId,
232 'previous_contribution_status_id' => $previousStatusId,
235 $updateResult = CRM_Contribute_BAO_Contribution
::transitionComponents($params);
237 if (!is_array($updateResult) ||
238 !($updatedComponents = CRM_Utils_Array
::value('updatedComponents', $updateResult)) ||
239 !is_array($updatedComponents) ||
240 empty($updatedComponents)
245 // get the user display name.
247 SELECT display_name as displayName
249 LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
250 WHERE civicrm_contribution.id = {$contributionId}";
251 $userDisplayName = CRM_Core_DAO
::singleValueQuery($sql);
253 // get the status message for user.
254 foreach ($updatedComponents as $componentName => $updatedStatusId) {
256 if ($componentName == 'CiviMember') {
257 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
258 CRM_Member_PseudoConstant
::membershipStatus()
260 if ($updatedStatusName == 'Cancelled') {
261 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
263 elseif ($updatedStatusName == 'Expired') {
264 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
267 $endDate = CRM_Utils_Array
::value('membership_end_date', $updateResult);
269 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
271 1 => $userDisplayName,
279 if ($componentName == 'CiviEvent') {
280 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
281 CRM_Event_PseudoConstant
::participantStatus()
283 if ($updatedStatusName == 'Cancelled') {
284 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
286 elseif ($updatedStatusName == 'Registered') {
287 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
291 if ($componentName == 'CiviPledge') {
292 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
293 CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name')
295 if ($updatedStatusName == 'Cancelled') {
296 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
298 elseif ($updatedStatusName == 'Failed') {
299 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
301 elseif ($updatedStatusName == 'Completed') {
302 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
311 * @return array (0 => array(int $ppId => string $label), 1 => array(...payproc details...))
313 public function getValidProcessorsAndAssignFutureStartDate() {
314 $validProcessors = array();
315 $processors = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, "billing_mode IN ( 1, 3 )");
317 foreach ($processors as $ppID => $label) {
318 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ppID, $this->_mode
);
319 // at this stage only Authorize.net has been tested to support future start dates so if it's enabled let the template know
320 // to show receive date
321 $processorsSupportingFutureStartDate = array('AuthNet');
322 if (in_array($paymentProcessor['payment_processor_type'], $processorsSupportingFutureStartDate)) {
323 $this->assign('processorSupportsFutureStartDate', TRUE);
325 if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) {
328 elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode
== 'live') {
332 $paymentObject = CRM_Core_Payment
::singleton($this->_mode
, $paymentProcessor, $this);
333 $error = $paymentObject->checkConfig();
335 $validProcessors[$ppID] = $label;
337 $paymentObject = NULL;
340 if (empty($validProcessors)) {
341 CRM_Core_Error
::fatal(ts('You will need to configure the %1 settings for your Payment Processor before you can submit credit card transactions.', array(1 => $this->_mode
)));
344 return array($validProcessors, $paymentProcessor);
349 * Assign billing type id to bltID
353 public function assignBillingType() {
354 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
355 $this->_bltID
= array_search('Billing', $locationTypes);
356 if (!$this->_bltID
) {
357 CRM_Core_Error
::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
359 $this->set('bltID', $this->_bltID
);
360 $this->assign('bltID', $this->_bltID
);
364 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
366 public function assignProcessors() {
367 //ensure that processor has a valid config
368 //only valid processors get display to user
370 list($this->_processors
, $paymentProcessor) = $this->getValidProcessorsAndAssignFutureStartDate();
372 //get the valid recurring processors.
373 $recurring = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
374 $this->_recurPaymentProcessors
= array_intersect_assoc($this->_processors
, $recurring);
376 $this->assign('recurringPaymentProcessorIds',
377 empty($this->_recurPaymentProcessors
) ?
'' : implode(',', array_keys($this->_recurPaymentProcessors
))
380 // this required to show billing block
381 $this->assign_by_ref('paymentProcessor', $paymentProcessor);
382 $this->assign('hidePayPalExpress', TRUE);
385 public function getCurrency($submittedValues) { // get current currency from DB or use default currency
386 $config = CRM_Core_Config
::singleton();
388 $currentCurrency = CRM_Utils_Array
::value('currency',
390 $config->defaultCurrency
393 // use submitted currency if present else use current currency
394 $result = CRM_Utils_Array
::value('currency',
401 public function getFinancialAccounts($financialTypeId) {
402 $financialAccounts = array();
403 CRM_Core_PseudoConstant
::populate($financialAccounts,
404 'CRM_Financial_DAO_EntityFinancialAccount',
406 $retrieve = 'financial_account_id',
408 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
409 return $financialAccounts;
412 public function getFinancialAccount($financialTypeId, $relationTypeId) {
413 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
414 return CRM_Utils_Array
::value($relationTypeId, $financialAccounts);
417 public function preProcessPledge() {
418 //get the payment values associated with given pledge payment id OR check for payments due.
419 $this->_pledgeValues
= array();
421 $payParams = array('id' => $this->_ppID
);
423 CRM_Pledge_BAO_PledgePayment
::retrieve($payParams, $this->_pledgeValues
['pledgePayment']);
424 $this->_pledgeID
= CRM_Utils_Array
::value('pledge_id', $this->_pledgeValues
['pledgePayment']);
425 $paymentStatusID = CRM_Utils_Array
::value('status_id', $this->_pledgeValues
['pledgePayment']);
426 $this->_id
= CRM_Utils_Array
::value('contribution_id', $this->_pledgeValues
['pledgePayment']);
429 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
430 if (!($paymentStatusID == array_search('Pending', $allStatus) ||
$paymentStatusID == array_search('Overdue', $allStatus))) {
431 CRM_Core_Error
::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
434 //get the pledge values associated with given pledge payment.
437 $pledgeParams = array('id' => $this->_pledgeID
);
438 CRM_Pledge_BAO_Pledge
::getValues($pledgeParams, $this->_pledgeValues
, $ids);
439 $this->assign('ppID', $this->_ppID
);
442 // Not making a pledge payment, so if adding a new contribution we should check if pledge payment(s) are due for this contact so we can alert the user. CRM-5206
443 if (isset($this->_contactID
)) {
444 $contactPledges = CRM_Pledge_BAO_Pledge
::getContactPledges($this->_contactID
);
446 if (!empty($contactPledges)) {
447 $payments = $paymentsDue = NULL;
448 $multipleDue = FALSE;
449 foreach ($contactPledges as $key => $pledgeId) {
450 $payments = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
457 $paymentsDue = $payments;
462 // Show link to pledge tab since more than one pledge has a payment due
463 $pledgeTab = CRM_Utils_System
::url('civicrm/contact/view',
464 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
466 CRM_Core_Session
::setStatus(ts('This contact has pending or overdue pledge payments. <a href="%1">Click here to view their Pledges tab</a> and verify whether this contribution should be applied as a pledge payment.', array(1 => $pledgeTab)), ts('Notice'), 'alert');
468 elseif ($paymentsDue) {
469 // Show user link to oldest Pending or Overdue pledge payment
470 $ppAmountDue = CRM_Utils_Money
::format($payments['amount'], $payments['currency']);
471 $ppSchedDate = CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
473 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
474 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
478 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
479 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
482 CRM_Core_Session
::setStatus(ts('This contact has a pending or overdue pledge payment of %2 which is scheduled for %3. <a href="%1">Click here to enter a pledge payment</a>.', array(
486 )), ts('Notice'), 'alert');
493 public function unsetCreditCardFields($submittedValues) {
494 //Offline Contribution.
495 $unsetParams = array(
496 'payment_processor_id',
497 "email-{$this->_bltID}",
498 'hidden_buildCreditCard',
499 'hidden_buildDirectDebit',
500 'billing_first_name',
501 'billing_middle_name',
504 "city-{$this->_bltID}",
505 "state_province_id-{$this->_bltID}",
506 "postal_code-{$this->_bltID}",
507 "country_id-{$this->_bltID}",
508 'credit_card_number',
510 'credit_card_exp_date',
513 foreach ($unsetParams as $key) {
514 if (isset($submittedValues[$key])) {
515 unset($submittedValues[$key]);
518 return $submittedValues;