3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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;
71 * @var CRM_Contribute_DAO_ContributionProduct
73 public $_productDAO = NULL;
84 * the id of the contact associated with this contribution
92 * the id of the pledge payment that we are processing
100 * the id of the pledge that we are processing
108 * is this contribution associated with an online
109 * financial transaction
114 public $_online = FALSE;
117 * Stores all product option
125 * stores the honor id
130 public $_honorID = NULL;
133 * Store the contribution Type ID
137 public $_contributionType;
140 * The contribution values if an existing contribution
145 * The pledge values if this contribution is associated with pledge
147 public $_pledgeValues;
149 public $_contributeMode = 'direct';
156 * Store the line items if price set used.
160 protected $_formType;
163 public function showRecordLinkMesssage($id) {
164 $statusId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id');
165 if (CRM_Contribute_PseudoConstant
::contributionStatus($statusId, 'name') == 'Partially paid') {
166 if ($pid = CRM_Core_DAO
::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) {
167 $recordPaymentLink = CRM_Utils_System
::url('civicrm/payment',
168 "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event"
170 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');
175 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
177 $params = array('id' => $id);
178 CRM_Contribute_BAO_Contribution
::getValues($params, $values, $ids);
180 //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL)
181 $this->_online
= FALSE;
182 $fids = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($id);
183 if (!empty($fids['financialTrxnId'])) {
184 $this->_online
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
187 // Also don't allow user to update some fields for recurring contributions.
188 if (!$this->_online
) {
189 $this->_online
= CRM_Utils_Array
::value('contribution_recur_id', $values);
192 $this->assign('isOnline', $this->_online ?
TRUE : FALSE);
195 $daoNote = new CRM_Core_BAO_Note();
196 $daoNote->entity_table
= 'civicrm_contribution';
197 $daoNote->entity_id
= $id;
198 if ($daoNote->find(TRUE)) {
199 $this->_noteID
= $daoNote->id
;
200 $values['note'] = $daoNote->note
;
202 $this->_contributionType
= $values['financial_type_id'];
206 * @param string $type eg 'Contribution'
207 * @param string $subType
208 * @param int $entityId
210 public function applyCustomData($type, $subType, $entityId) {
211 $this->set('type', $type);
212 $this->set('subType', $subType);
213 $this->set('entityId', $entityId);
215 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, 1, $type, $entityId);
216 CRM_Custom_Form_CustomData
::buildQuickForm($this);
217 CRM_Custom_Form_CustomData
::setDefaultValues($this);
222 * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find();
224 public function assignPremiumProduct($id) { //to get Premium id
227 FROM civicrm_contribution_product
228 WHERE contribution_id = {$id}
230 $dao = CRM_Core_DAO
::executeQuery($sql,
231 CRM_Core_DAO
::$_nullArray
234 $this->_premiumID
= $dao->id
;
235 $this->_productDAO
= $dao;
241 * This function process contribution related objects.
243 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) {
245 if (!$contributionId ||
!$statusId) {
250 'contribution_id' => $contributionId,
251 'contribution_status_id' => $statusId,
252 'previous_contribution_status_id' => $previousStatusId,
255 $updateResult = CRM_Contribute_BAO_Contribution
::transitionComponents($params);
257 if (!is_array($updateResult) ||
258 !($updatedComponents = CRM_Utils_Array
::value('updatedComponents', $updateResult)) ||
259 !is_array($updatedComponents) ||
260 empty($updatedComponents)
265 // get the user display name.
267 SELECT display_name as displayName
269 LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
270 WHERE civicrm_contribution.id = {$contributionId}";
271 $userDisplayName = CRM_Core_DAO
::singleValueQuery($sql);
273 // get the status message for user.
274 foreach ($updatedComponents as $componentName => $updatedStatusId) {
276 if ($componentName == 'CiviMember') {
277 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
278 CRM_Member_PseudoConstant
::membershipStatus()
280 if ($updatedStatusName == 'Cancelled') {
281 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
283 elseif ($updatedStatusName == 'Expired') {
284 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
287 $endDate = CRM_Utils_Array
::value('membership_end_date', $updateResult);
289 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
291 1 => $userDisplayName,
299 if ($componentName == 'CiviEvent') {
300 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
301 CRM_Event_PseudoConstant
::participantStatus()
303 if ($updatedStatusName == 'Cancelled') {
304 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
306 elseif ($updatedStatusName == 'Registered') {
307 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
311 if ($componentName == 'CiviPledge') {
312 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
313 CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name')
315 if ($updatedStatusName == 'Cancelled') {
316 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
318 elseif ($updatedStatusName == 'Failed') {
319 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
321 elseif ($updatedStatusName == 'Completed') {
322 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
331 * @return array (0 => array(int $ppId => string $label), 1 => array(...payproc details...))
333 public function getValidProcessorsAndAssignFutureStartDate() {
334 $validProcessors = array();
335 // restrict to payment_type = 1 (credit card only) and billing mode 1 and 3
336 $processors = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, "billing_mode IN ( 1, 3 ) AND payment_type = 1");
338 foreach ($processors as $ppID => $label) {
339 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ppID, $this->_mode
);
340 // at this stage only Authorize.net has been tested to support future start dates so if it's enabled let the template know
341 // to show receive date
342 $processorsSupportingFutureStartDate = array('AuthNet');
343 if (in_array($paymentProcessor['payment_processor_type'], $processorsSupportingFutureStartDate)) {
344 $this->assign('processorSupportsFutureStartDate', TRUE);
346 if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) {
349 elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode
== 'live') {
353 $paymentObject = CRM_Core_Payment
::singleton($this->_mode
, $paymentProcessor, $this);
354 $error = $paymentObject->checkConfig();
356 $validProcessors[$ppID] = $label;
358 $paymentObject = NULL;
361 if (empty($validProcessors)) {
362 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
)));
365 return array($validProcessors, $paymentProcessor);
370 * Assign billing type id to bltID
374 public function assignBillingType() {
375 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
376 $this->_bltID
= array_search('Billing', $locationTypes);
377 if (!$this->_bltID
) {
378 CRM_Core_Error
::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
380 $this->set('bltID', $this->_bltID
);
381 $this->assign('bltID', $this->_bltID
);
385 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
387 public function assignProcessors() {
388 //ensure that processor has a valid config
389 //only valid processors get display to user
391 list($this->_processors
, $paymentProcessor) = $this->getValidProcessorsAndAssignFutureStartDate();
393 //get the valid recurring processors.
394 $recurring = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
395 $this->_recurPaymentProcessors
= array_intersect_assoc($this->_processors
, $recurring);
397 $this->assign('recurringPaymentProcessorIds',
398 empty($this->_recurPaymentProcessors
) ?
'' : implode(',', array_keys($this->_recurPaymentProcessors
))
401 // this required to show billing block
402 $this->assign_by_ref('paymentProcessor', $paymentProcessor);
403 $this->assign('hidePayPalExpress', TRUE);
406 public function getCurrency($submittedValues) { // get current currency from DB or use default currency
407 $config = CRM_Core_Config
::singleton();
409 $currentCurrency = CRM_Utils_Array
::value('currency',
411 $config->defaultCurrency
414 // use submitted currency if present else use current currency
415 $result = CRM_Utils_Array
::value('currency',
422 public function getFinancialAccounts($financialTypeId) {
423 $financialAccounts = array();
424 CRM_Core_PseudoConstant
::populate($financialAccounts,
425 'CRM_Financial_DAO_EntityFinancialAccount',
427 $retrieve = 'financial_account_id',
429 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
430 return $financialAccounts;
433 public function getFinancialAccount($financialTypeId, $relationTypeId) {
434 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
435 return CRM_Utils_Array
::value($relationTypeId, $financialAccounts);
438 public function preProcessPledge() {
439 //get the payment values associated with given pledge payment id OR check for payments due.
440 $this->_pledgeValues
= array();
442 $payParams = array('id' => $this->_ppID
);
444 CRM_Pledge_BAO_PledgePayment
::retrieve($payParams, $this->_pledgeValues
['pledgePayment']);
445 $this->_pledgeID
= CRM_Utils_Array
::value('pledge_id', $this->_pledgeValues
['pledgePayment']);
446 $paymentStatusID = CRM_Utils_Array
::value('status_id', $this->_pledgeValues
['pledgePayment']);
447 $this->_id
= CRM_Utils_Array
::value('contribution_id', $this->_pledgeValues
['pledgePayment']);
450 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
451 if (!($paymentStatusID == array_search('Pending', $allStatus) ||
$paymentStatusID == array_search('Overdue', $allStatus))) {
452 CRM_Core_Error
::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
455 //get the pledge values associated with given pledge payment.
458 $pledgeParams = array('id' => $this->_pledgeID
);
459 CRM_Pledge_BAO_Pledge
::getValues($pledgeParams, $this->_pledgeValues
, $ids);
460 $this->assign('ppID', $this->_ppID
);
463 // 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
464 if (isset($this->_contactID
)) {
465 $contactPledges = CRM_Pledge_BAO_Pledge
::getContactPledges($this->_contactID
);
467 if (!empty($contactPledges)) {
468 $payments = $paymentsDue = NULL;
469 $multipleDue = FALSE;
470 foreach ($contactPledges as $key => $pledgeId) {
471 $payments = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
478 $paymentsDue = $payments;
483 // Show link to pledge tab since more than one pledge has a payment due
484 $pledgeTab = CRM_Utils_System
::url('civicrm/contact/view',
485 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
487 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');
489 elseif ($paymentsDue) {
490 // Show user link to oldest Pending or Overdue pledge payment
491 $ppAmountDue = CRM_Utils_Money
::format($payments['amount'], $payments['currency']);
492 $ppSchedDate = CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
494 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
495 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
499 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
500 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
503 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(
507 )), ts('Notice'), 'alert');
514 public function unsetCreditCardFields($submittedValues) {
515 //Offline Contribution.
516 $unsetParams = array(
517 'payment_processor_id',
518 "email-{$this->_bltID}",
519 'hidden_buildCreditCard',
520 'hidden_buildDirectDebit',
521 'billing_first_name',
522 'billing_middle_name',
525 "city-{$this->_bltID}",
526 "state_province_id-{$this->_bltID}",
527 "postal_code-{$this->_bltID}",
528 "country_id-{$this->_bltID}",
529 'credit_card_number',
531 'credit_card_exp_date',
534 foreach ($unsetParams as $key) {
535 if (isset($submittedValues[$key])) {
536 unset($submittedValues[$key]);
539 return $submittedValues;