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 showRecordLinkMesssage($id) {
160 $statusId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id');
161 if (CRM_Contribute_PseudoConstant
::contributionStatus($statusId, 'name') == 'Partially paid') {
162 if ($pid = CRM_Core_DAO
::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) {
163 $recordPaymentLink = CRM_Utils_System
::url('civicrm/payment',
164 "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event"
166 CRM_Core_Session
::setStatus(ts('Please use the <a href="%1">Record Payment</a> form if you have received an additional payment for this Partially paid contribution record.', array(1 => $recordPaymentLink)), ts('Notice'), 'alert');
171 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
173 $params = array('id' => $id);
174 CRM_Contribute_BAO_Contribution
::getValues($params, $values, $ids);
176 //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL)
177 $this->_online
= FALSE;
178 $fids = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($id);
179 if (!empty($fids['financialTrxnId'])) {
180 $this->_online
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
183 // Also don't allow user to update some fields for recurring contributions.
184 if (!$this->_online
) {
185 $this->_online
= CRM_Utils_Array
::value('contribution_recur_id', $values);
188 $this->assign('isOnline', $this->_online ?
TRUE : FALSE);
191 $daoNote = new CRM_Core_BAO_Note();
192 $daoNote->entity_table
= 'civicrm_contribution';
193 $daoNote->entity_id
= $id;
194 if ($daoNote->find(TRUE)) {
195 $this->_noteID
= $daoNote->id
;
196 $values['note'] = $daoNote->note
;
198 $this->_contributionType
= $values['financial_type_id'];
202 * @param string $type eg 'Contribution'
203 * @param string $subType
204 * @param int $entityId
206 public function applyCustomData($type, $subType, $entityId) {
207 $this->set('type', $type);
208 $this->set('subType', $subType);
209 $this->set('entityId', $entityId);
211 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, 1, $type, $entityId);
212 CRM_Custom_Form_CustomData
::buildQuickForm($this);
213 CRM_Custom_Form_CustomData
::setDefaultValues($this);
216 public function assignPremiumProduct($id) { //to get Premium id
219 FROM civicrm_contribution_product
220 WHERE contribution_id = {$id}
222 $dao = CRM_Core_DAO
::executeQuery($sql,
223 CRM_Core_DAO
::$_nullArray
226 $this->_premiumID
= $dao->id
;
227 $this->_productDAO
= $dao;
233 * This function process contribution related objects.
235 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) {
237 if (!$contributionId ||
!$statusId) {
242 'contribution_id' => $contributionId,
243 'contribution_status_id' => $statusId,
244 'previous_contribution_status_id' => $previousStatusId,
247 $updateResult = CRM_Contribute_BAO_Contribution
::transitionComponents($params);
249 if (!is_array($updateResult) ||
250 !($updatedComponents = CRM_Utils_Array
::value('updatedComponents', $updateResult)) ||
251 !is_array($updatedComponents) ||
252 empty($updatedComponents)
257 // get the user display name.
259 SELECT display_name as displayName
261 LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
262 WHERE civicrm_contribution.id = {$contributionId}";
263 $userDisplayName = CRM_Core_DAO
::singleValueQuery($sql);
265 // get the status message for user.
266 foreach ($updatedComponents as $componentName => $updatedStatusId) {
268 if ($componentName == 'CiviMember') {
269 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
270 CRM_Member_PseudoConstant
::membershipStatus()
272 if ($updatedStatusName == 'Cancelled') {
273 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
275 elseif ($updatedStatusName == 'Expired') {
276 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
279 $endDate = CRM_Utils_Array
::value('membership_end_date', $updateResult);
281 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
283 1 => $userDisplayName,
291 if ($componentName == 'CiviEvent') {
292 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
293 CRM_Event_PseudoConstant
::participantStatus()
295 if ($updatedStatusName == 'Cancelled') {
296 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
298 elseif ($updatedStatusName == 'Registered') {
299 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
303 if ($componentName == 'CiviPledge') {
304 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
305 CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name')
307 if ($updatedStatusName == 'Cancelled') {
308 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
310 elseif ($updatedStatusName == 'Failed') {
311 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
313 elseif ($updatedStatusName == 'Completed') {
314 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
323 * @return array (0 => array(int $ppId => string $label), 1 => array(...payproc details...))
325 public function getValidProcessorsAndAssignFutureStartDate() {
326 $validProcessors = array();
327 $processors = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, "billing_mode IN ( 1, 3 )");
329 foreach ($processors as $ppID => $label) {
330 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ppID, $this->_mode
);
331 // at this stage only Authorize.net has been tested to support future start dates so if it's enabled let the template know
332 // to show receive date
333 $processorsSupportingFutureStartDate = array('AuthNet');
334 if (in_array($paymentProcessor['payment_processor_type'], $processorsSupportingFutureStartDate)) {
335 $this->assign('processorSupportsFutureStartDate', TRUE);
337 if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) {
340 elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode
== 'live') {
344 $paymentObject = CRM_Core_Payment
::singleton($this->_mode
, $paymentProcessor, $this);
345 $error = $paymentObject->checkConfig();
347 $validProcessors[$ppID] = $label;
349 $paymentObject = NULL;
352 if (empty($validProcessors)) {
353 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
)));
356 return array($validProcessors, $paymentProcessor);
361 * Assign billing type id to bltID
365 public function assignBillingType() {
366 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
367 $this->_bltID
= array_search('Billing', $locationTypes);
368 if (!$this->_bltID
) {
369 CRM_Core_Error
::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
371 $this->set('bltID', $this->_bltID
);
372 $this->assign('bltID', $this->_bltID
);
376 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
378 public function assignProcessors() {
379 //ensure that processor has a valid config
380 //only valid processors get display to user
382 list($this->_processors
, $paymentProcessor) = $this->getValidProcessorsAndAssignFutureStartDate();
384 //get the valid recurring processors.
385 $recurring = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
386 $this->_recurPaymentProcessors
= array_intersect_assoc($this->_processors
, $recurring);
388 $this->assign('recurringPaymentProcessorIds',
389 empty($this->_recurPaymentProcessors
) ?
'' : implode(',', array_keys($this->_recurPaymentProcessors
))
392 // this required to show billing block
393 $this->assign_by_ref('paymentProcessor', $paymentProcessor);
394 $this->assign('hidePayPalExpress', TRUE);
397 public function getCurrency($submittedValues) { // get current currency from DB or use default currency
398 $config = CRM_Core_Config
::singleton();
400 $currentCurrency = CRM_Utils_Array
::value('currency',
402 $config->defaultCurrency
405 // use submitted currency if present else use current currency
406 $result = CRM_Utils_Array
::value('currency',
413 public function getFinancialAccounts($financialTypeId) {
414 $financialAccounts = array();
415 CRM_Core_PseudoConstant
::populate($financialAccounts,
416 'CRM_Financial_DAO_EntityFinancialAccount',
418 $retrieve = 'financial_account_id',
420 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
421 return $financialAccounts;
424 public function getFinancialAccount($financialTypeId, $relationTypeId) {
425 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
426 return CRM_Utils_Array
::value($relationTypeId, $financialAccounts);
429 public function preProcessPledge() {
430 //get the payment values associated with given pledge payment id OR check for payments due.
431 $this->_pledgeValues
= array();
433 $payParams = array('id' => $this->_ppID
);
435 CRM_Pledge_BAO_PledgePayment
::retrieve($payParams, $this->_pledgeValues
['pledgePayment']);
436 $this->_pledgeID
= CRM_Utils_Array
::value('pledge_id', $this->_pledgeValues
['pledgePayment']);
437 $paymentStatusID = CRM_Utils_Array
::value('status_id', $this->_pledgeValues
['pledgePayment']);
438 $this->_id
= CRM_Utils_Array
::value('contribution_id', $this->_pledgeValues
['pledgePayment']);
441 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
442 if (!($paymentStatusID == array_search('Pending', $allStatus) ||
$paymentStatusID == array_search('Overdue', $allStatus))) {
443 CRM_Core_Error
::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
446 //get the pledge values associated with given pledge payment.
449 $pledgeParams = array('id' => $this->_pledgeID
);
450 CRM_Pledge_BAO_Pledge
::getValues($pledgeParams, $this->_pledgeValues
, $ids);
451 $this->assign('ppID', $this->_ppID
);
454 // 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
455 if (isset($this->_contactID
)) {
456 $contactPledges = CRM_Pledge_BAO_Pledge
::getContactPledges($this->_contactID
);
458 if (!empty($contactPledges)) {
459 $payments = $paymentsDue = NULL;
460 $multipleDue = FALSE;
461 foreach ($contactPledges as $key => $pledgeId) {
462 $payments = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
469 $paymentsDue = $payments;
474 // Show link to pledge tab since more than one pledge has a payment due
475 $pledgeTab = CRM_Utils_System
::url('civicrm/contact/view',
476 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
478 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');
480 elseif ($paymentsDue) {
481 // Show user link to oldest Pending or Overdue pledge payment
482 $ppAmountDue = CRM_Utils_Money
::format($payments['amount'], $payments['currency']);
483 $ppSchedDate = CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
485 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
486 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
490 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
491 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
494 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(
498 )), ts('Notice'), 'alert');
505 public function unsetCreditCardFields($submittedValues) {
506 //Offline Contribution.
507 $unsetParams = array(
508 'payment_processor_id',
509 "email-{$this->_bltID}",
510 'hidden_buildCreditCard',
511 'hidden_buildDirectDebit',
512 'billing_first_name',
513 'billing_middle_name',
516 "city-{$this->_bltID}",
517 "state_province_id-{$this->_bltID}",
518 "postal_code-{$this->_bltID}",
519 "country_id-{$this->_bltID}",
520 'credit_card_number',
522 'credit_card_exp_date',
525 foreach ($unsetParams as $key) {
526 if (isset($submittedValues[$key])) {
527 unset($submittedValues[$key]);
530 return $submittedValues;