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;
166 public function showRecordLinkMesssage($id) {
167 $statusId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id');
168 if (CRM_Contribute_PseudoConstant
::contributionStatus($statusId, 'name') == 'Partially paid') {
169 if ($pid = CRM_Core_DAO
::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) {
170 $recordPaymentLink = CRM_Utils_System
::url('civicrm/payment',
171 "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event"
173 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');
182 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
184 $params = array('id' => $id);
185 CRM_Contribute_BAO_Contribution
::getValues($params, $values, $ids);
187 //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL)
188 $this->_online
= FALSE;
189 $fids = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($id);
190 if (!empty($fids['financialTrxnId'])) {
191 $this->_online
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
194 // Also don't allow user to update some fields for recurring contributions.
195 if (!$this->_online
) {
196 $this->_online
= CRM_Utils_Array
::value('contribution_recur_id', $values);
199 $this->assign('isOnline', $this->_online ?
TRUE : FALSE);
202 $daoNote = new CRM_Core_BAO_Note();
203 $daoNote->entity_table
= 'civicrm_contribution';
204 $daoNote->entity_id
= $id;
205 if ($daoNote->find(TRUE)) {
206 $this->_noteID
= $daoNote->id
;
207 $values['note'] = $daoNote->note
;
209 $this->_contributionType
= $values['financial_type_id'];
213 * @param string $type eg 'Contribution'
214 * @param string $subType
215 * @param int $entityId
217 public function applyCustomData($type, $subType, $entityId) {
218 $this->set('type', $type);
219 $this->set('subType', $subType);
220 $this->set('entityId', $entityId);
222 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, 1, $type, $entityId);
223 CRM_Custom_Form_CustomData
::buildQuickForm($this);
224 CRM_Custom_Form_CustomData
::setDefaultValues($this);
229 * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find();
231 public function assignPremiumProduct($id) { //to get Premium id
234 FROM civicrm_contribution_product
235 WHERE contribution_id = {$id}
237 $dao = CRM_Core_DAO
::executeQuery($sql,
238 CRM_Core_DAO
::$_nullArray
241 $this->_premiumID
= $dao->id
;
242 $this->_productDAO
= $dao;
248 * This function process contribution related objects.
250 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) {
252 if (!$contributionId ||
!$statusId) {
257 'contribution_id' => $contributionId,
258 'contribution_status_id' => $statusId,
259 'previous_contribution_status_id' => $previousStatusId,
262 $updateResult = CRM_Contribute_BAO_Contribution
::transitionComponents($params);
264 if (!is_array($updateResult) ||
265 !($updatedComponents = CRM_Utils_Array
::value('updatedComponents', $updateResult)) ||
266 !is_array($updatedComponents) ||
267 empty($updatedComponents)
272 // get the user display name.
274 SELECT display_name as displayName
276 LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
277 WHERE civicrm_contribution.id = {$contributionId}";
278 $userDisplayName = CRM_Core_DAO
::singleValueQuery($sql);
280 // get the status message for user.
281 foreach ($updatedComponents as $componentName => $updatedStatusId) {
283 if ($componentName == 'CiviMember') {
284 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
285 CRM_Member_PseudoConstant
::membershipStatus()
287 if ($updatedStatusName == 'Cancelled') {
288 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
290 elseif ($updatedStatusName == 'Expired') {
291 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
294 $endDate = CRM_Utils_Array
::value('membership_end_date', $updateResult);
296 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
298 1 => $userDisplayName,
306 if ($componentName == 'CiviEvent') {
307 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
308 CRM_Event_PseudoConstant
::participantStatus()
310 if ($updatedStatusName == 'Cancelled') {
311 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
313 elseif ($updatedStatusName == 'Registered') {
314 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
318 if ($componentName == 'CiviPledge') {
319 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
320 CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name')
322 if ($updatedStatusName == 'Cancelled') {
323 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
325 elseif ($updatedStatusName == 'Failed') {
326 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
328 elseif ($updatedStatusName == 'Completed') {
329 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
338 * @return array (0 => array(int $ppId => string $label), 1 => array(...payproc details...))
340 public function getValidProcessorsAndAssignFutureStartDate() {
341 $validProcessors = array();
342 // restrict to payment_type = 1 (credit card only) and billing mode 1 and 3
343 $processors = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, "billing_mode IN ( 1, 3 ) AND payment_type = 1");
345 foreach ($processors as $ppID => $label) {
346 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ppID, $this->_mode
);
347 // at this stage only Authorize.net has been tested to support future start dates so if it's enabled let the template know
348 // to show receive date
349 $processorsSupportingFutureStartDate = array('AuthNet');
350 if (in_array($paymentProcessor['payment_processor_type'], $processorsSupportingFutureStartDate)) {
351 $this->assign('processorSupportsFutureStartDate', TRUE);
353 if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) {
356 elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode
== 'live') {
360 $paymentObject = CRM_Core_Payment
::singleton($this->_mode
, $paymentProcessor, $this);
361 $error = $paymentObject->checkConfig();
363 $validProcessors[$ppID] = $label;
365 $paymentObject = NULL;
368 if (empty($validProcessors)) {
369 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
)));
372 return array($validProcessors, $paymentProcessor);
377 * Assign billing type id to bltID
381 public function assignBillingType() {
382 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
383 $this->_bltID
= array_search('Billing', $locationTypes);
384 if (!$this->_bltID
) {
385 CRM_Core_Error
::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
387 $this->set('bltID', $this->_bltID
);
388 $this->assign('bltID', $this->_bltID
);
392 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
394 public function assignProcessors() {
395 //ensure that processor has a valid config
396 //only valid processors get display to user
398 list($this->_processors
, $paymentProcessor) = $this->getValidProcessorsAndAssignFutureStartDate();
400 //get the valid recurring processors.
401 $recurring = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
402 $this->_recurPaymentProcessors
= array_intersect_assoc($this->_processors
, $recurring);
404 $this->assign('recurringPaymentProcessorIds',
405 empty($this->_recurPaymentProcessors
) ?
'' : implode(',', array_keys($this->_recurPaymentProcessors
))
408 // this required to show billing block
409 $this->assign_by_ref('paymentProcessor', $paymentProcessor);
410 $this->assign('hidePayPalExpress', TRUE);
414 * @param $submittedValues
418 public function getCurrency($submittedValues) { // get current currency from DB or use default currency
419 $config = CRM_Core_Config
::singleton();
421 $currentCurrency = CRM_Utils_Array
::value('currency',
423 $config->defaultCurrency
426 // use submitted currency if present else use current currency
427 $result = CRM_Utils_Array
::value('currency',
435 * @param $financialTypeId
439 public function getFinancialAccounts($financialTypeId) {
440 $financialAccounts = array();
441 CRM_Core_PseudoConstant
::populate($financialAccounts,
442 'CRM_Financial_DAO_EntityFinancialAccount',
444 $retrieve = 'financial_account_id',
446 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
447 return $financialAccounts;
451 * @param $financialTypeId
452 * @param $relationTypeId
456 public function getFinancialAccount($financialTypeId, $relationTypeId) {
457 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
458 return CRM_Utils_Array
::value($relationTypeId, $financialAccounts);
461 public function preProcessPledge() {
462 //get the payment values associated with given pledge payment id OR check for payments due.
463 $this->_pledgeValues
= array();
465 $payParams = array('id' => $this->_ppID
);
467 CRM_Pledge_BAO_PledgePayment
::retrieve($payParams, $this->_pledgeValues
['pledgePayment']);
468 $this->_pledgeID
= CRM_Utils_Array
::value('pledge_id', $this->_pledgeValues
['pledgePayment']);
469 $paymentStatusID = CRM_Utils_Array
::value('status_id', $this->_pledgeValues
['pledgePayment']);
470 $this->_id
= CRM_Utils_Array
::value('contribution_id', $this->_pledgeValues
['pledgePayment']);
473 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
474 if (!($paymentStatusID == array_search('Pending', $allStatus) ||
$paymentStatusID == array_search('Overdue', $allStatus))) {
475 CRM_Core_Error
::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
478 //get the pledge values associated with given pledge payment.
481 $pledgeParams = array('id' => $this->_pledgeID
);
482 CRM_Pledge_BAO_Pledge
::getValues($pledgeParams, $this->_pledgeValues
, $ids);
483 $this->assign('ppID', $this->_ppID
);
486 // 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
487 if (isset($this->_contactID
)) {
488 $contactPledges = CRM_Pledge_BAO_Pledge
::getContactPledges($this->_contactID
);
490 if (!empty($contactPledges)) {
491 $payments = $paymentsDue = NULL;
492 $multipleDue = FALSE;
493 foreach ($contactPledges as $key => $pledgeId) {
494 $payments = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
501 $paymentsDue = $payments;
506 // Show link to pledge tab since more than one pledge has a payment due
507 $pledgeTab = CRM_Utils_System
::url('civicrm/contact/view',
508 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
510 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');
512 elseif ($paymentsDue) {
513 // Show user link to oldest Pending or Overdue pledge payment
514 $ppAmountDue = CRM_Utils_Money
::format($payments['amount'], $payments['currency']);
515 $ppSchedDate = CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
517 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
518 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
522 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
523 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
526 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(
530 )), ts('Notice'), 'alert');
538 * @param $submittedValues
542 public function unsetCreditCardFields($submittedValues) {
543 //Offline Contribution.
544 $unsetParams = array(
545 'payment_processor_id',
546 "email-{$this->_bltID}",
547 'hidden_buildCreditCard',
548 'hidden_buildDirectDebit',
549 'billing_first_name',
550 'billing_middle_name',
553 "city-{$this->_bltID}",
554 "state_province_id-{$this->_bltID}",
555 "postal_code-{$this->_bltID}",
556 "country_id-{$this->_bltID}",
557 'credit_card_number',
559 'credit_card_exp_date',
562 foreach ($unsetParams as $key) {
563 if (isset($submittedValues[$key])) {
564 unset($submittedValues[$key]);
567 return $submittedValues;