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 (CRM_Utils_Array
::value('financialTrxnId', $fids)) {
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);
178 //unset the honor type id:when delete the honor_contact_id
179 //and edit the contribution, honoree infomation pane open
180 //since honor_type_id is present
181 if (!CRM_Utils_Array
::value('honor_contact_id', $values)) {
182 unset($values['honor_type_id']);
185 $daoNote = new CRM_Core_BAO_Note();
186 $daoNote->entity_table
= 'civicrm_contribution';
187 $daoNote->entity_id
= $id;
188 if ($daoNote->find(TRUE)) {
189 $this->_noteID
= $daoNote->id
;
190 $values['note'] = $daoNote->note
;
192 $this->_contributionType
= $values['financial_type_id'];
196 * @param string $type eg 'Contribution'
197 * @param string $subType
198 * @param int $entityId
200 public function applyCustomData($type, $subType, $entityId) {
201 $this->set('type', $type);
202 $this->set('subType', $subType);
203 $this->set('entityId', $entityId);
205 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $subType, 1, $type, $entityId);
206 CRM_Custom_Form_CustomData
::buildQuickForm($this);
207 CRM_Custom_Form_CustomData
::setDefaultValues($this);
210 public function assignPremiumProduct($id) { //to get Premium id
213 FROM civicrm_contribution_product
214 WHERE contribution_id = {$id}
216 $dao = CRM_Core_DAO
::executeQuery($sql,
217 CRM_Core_DAO
::$_nullArray
220 $this->_premiumID
= $dao->id
;
221 $this->_productDAO
= $dao;
227 * This function process contribution related objects.
229 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) {
231 if (!$contributionId ||
!$statusId) {
236 'contribution_id' => $contributionId,
237 'contribution_status_id' => $statusId,
238 'previous_contribution_status_id' => $previousStatusId,
241 $updateResult = CRM_Contribute_BAO_Contribution
::transitionComponents($params);
243 if (!is_array($updateResult) ||
244 !($updatedComponents = CRM_Utils_Array
::value('updatedComponents', $updateResult)) ||
245 !is_array($updatedComponents) ||
246 empty($updatedComponents)
251 // get the user display name.
253 SELECT display_name as displayName
255 LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
256 WHERE civicrm_contribution.id = {$contributionId}";
257 $userDisplayName = CRM_Core_DAO
::singleValueQuery($sql);
259 // get the status message for user.
260 foreach ($updatedComponents as $componentName => $updatedStatusId) {
262 if ($componentName == 'CiviMember') {
263 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
264 CRM_Member_PseudoConstant
::membershipStatus()
266 if ($updatedStatusName == 'Cancelled') {
267 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
269 elseif ($updatedStatusName == 'Expired') {
270 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
272 elseif ($endDate = CRM_Utils_Array
::value('membership_end_date', $updateResult)) {
273 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
275 1 => $userDisplayName,
282 if ($componentName == 'CiviEvent') {
283 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
284 CRM_Event_PseudoConstant
::participantStatus()
286 if ($updatedStatusName == 'Cancelled') {
287 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
289 elseif ($updatedStatusName == 'Registered') {
290 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
294 if ($componentName == 'CiviPledge') {
295 $updatedStatusName = CRM_Utils_Array
::value($updatedStatusId,
296 CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name')
298 if ($updatedStatusName == 'Cancelled') {
299 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
301 elseif ($updatedStatusName == 'Failed') {
302 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
304 elseif ($updatedStatusName == 'Completed') {
305 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
314 * @return array (0 => array(int $ppId => string $label), 1 => array(...payproc details...))
316 public function getValidProcessorsAndAssignFutureStartDate() {
317 $validProcessors = array();
318 // restrict to payment_type = 1 (credit card only) and billing mode 1 and 3
319 $processors = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, "billing_mode IN ( 1, 3 ) AND payment_type = 1");
321 foreach ($processors as $ppID => $label) {
322 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($ppID, $this->_mode
);
323 // at this stage only Authorize.net has been tested to support future start dates so if it's enabled let the template know
324 // to show receive date
325 $processorsSupportingFutureStartDate = array('AuthNet');
326 if (in_array($paymentProcessor['payment_processor_type'], $processorsSupportingFutureStartDate)) {
327 $this->assign('processorSupportsFutureStartDate', TRUE);
329 if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) {
332 elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode
== 'live') {
336 $paymentObject = CRM_Core_Payment
::singleton($this->_mode
, $paymentProcessor, $this);
337 $error = $paymentObject->checkConfig();
339 $validProcessors[$ppID] = $label;
341 $paymentObject = NULL;
344 if (empty($validProcessors)) {
345 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
)));
348 return array($validProcessors, $paymentProcessor);
353 * Assign billing type id to bltID
357 public function assignBillingType() {
358 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
359 $this->_bltID
= array_search('Billing', $locationTypes);
360 if (!$this->_bltID
) {
361 CRM_Core_Error
::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
363 $this->set('bltID', $this->_bltID
);
364 $this->assign('bltID', $this->_bltID
);
368 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
370 public function assignProcessors() {
371 //ensure that processor has a valid config
372 //only valid processors get display to user
374 list($this->_processors
, $paymentProcessor) = $this->getValidProcessorsAndAssignFutureStartDate();
376 //get the valid recurring processors.
377 $recurring = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
378 $this->_recurPaymentProcessors
= array_intersect_assoc($this->_processors
, $recurring);
380 $this->assign('recurringPaymentProcessorIds',
381 empty($this->_recurPaymentProcessors
) ?
'' : implode(',', array_keys($this->_recurPaymentProcessors
))
384 // this required to show billing block
385 $this->assign_by_ref('paymentProcessor', $paymentProcessor);
386 $this->assign('hidePayPalExpress', TRUE);
389 public function getCurrency($submittedValues) { // get current currency from DB or use default currency
390 $config = CRM_Core_Config
::singleton();
392 $currentCurrency = CRM_Utils_Array
::value('currency',
394 $config->defaultCurrency
397 // use submitted currency if present else use current currency
398 $result = CRM_Utils_Array
::value('currency',
405 public function getFinancialAccounts($financialTypeId) {
406 $financialAccounts = array();
407 CRM_Core_PseudoConstant
::populate($financialAccounts,
408 'CRM_Financial_DAO_EntityFinancialAccount',
410 $retrieve = 'financial_account_id',
412 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
413 return $financialAccounts;
416 public function getFinancialAccount($financialTypeId, $relationTypeId) {
417 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
418 return CRM_Utils_Array
::value($relationTypeId, $financialAccounts);
421 public function preProcessPledge() {
422 //get the payment values associated with given pledge payment id OR check for payments due.
423 $this->_pledgeValues
= array();
425 $payParams = array('id' => $this->_ppID
);
427 CRM_Pledge_BAO_PledgePayment
::retrieve($payParams, $this->_pledgeValues
['pledgePayment']);
428 $this->_pledgeID
= CRM_Utils_Array
::value('pledge_id', $this->_pledgeValues
['pledgePayment']);
429 $paymentStatusID = CRM_Utils_Array
::value('status_id', $this->_pledgeValues
['pledgePayment']);
430 $this->_id
= CRM_Utils_Array
::value('contribution_id', $this->_pledgeValues
['pledgePayment']);
433 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
434 if (!($paymentStatusID == array_search('Pending', $allStatus) ||
$paymentStatusID == array_search('Overdue', $allStatus))) {
435 CRM_Core_Error
::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
438 //get the pledge values associated with given pledge payment.
441 $pledgeParams = array('id' => $this->_pledgeID
);
442 CRM_Pledge_BAO_Pledge
::getValues($pledgeParams, $this->_pledgeValues
, $ids);
443 $this->assign('ppID', $this->_ppID
);
446 // 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
447 if (isset($this->_contactID
)) {
448 $contactPledges = CRM_Pledge_BAO_Pledge
::getContactPledges($this->_contactID
);
450 if (!empty($contactPledges)) {
451 $payments = $paymentsDue = NULL;
452 $multipleDue = FALSE;
453 foreach ($contactPledges as $key => $pledgeId) {
454 $payments = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
461 $paymentsDue = $payments;
466 // Show link to pledge tab since more than one pledge has a payment due
467 $pledgeTab = CRM_Utils_System
::url('civicrm/contact/view',
468 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
470 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');
472 elseif ($paymentsDue) {
473 // Show user link to oldest Pending or Overdue pledge payment
474 $ppAmountDue = CRM_Utils_Money
::format($payments['amount'], $payments['currency']);
475 $ppSchedDate = CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
477 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
478 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
482 $ppUrl = CRM_Utils_System
::url('civicrm/contact/view/contribution',
483 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
486 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(
490 )), ts('Notice'), 'alert');
497 public function unsetCreditCardFields($submittedValues) {
498 //Offline Contribution.
499 $unsetParams = array(
500 'payment_processor_id',
501 "email-{$this->_bltID}",
502 'hidden_buildCreditCard',
503 'hidden_buildDirectDebit',
504 'billing_first_name',
505 'billing_middle_name',
508 "city-{$this->_bltID}",
509 "state_province_id-{$this->_bltID}",
510 "postal_code-{$this->_bltID}",
511 "country_id-{$this->_bltID}",
512 'credit_card_number',
514 'credit_card_exp_date',
517 foreach ($unsetParams as $key) {
518 if (isset($submittedValues[$key])) {
519 unset($submittedValues[$key]);
522 return $submittedValues;