remove now unused function
[civicrm-core.git] / CRM / Contribute / Form / AbstractEditPayment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for processing a contribution
38 *
39 */
40class CRM_Contribute_Form_AbstractEditPayment extends CRM_Core_Form {
41 public $_mode;
42
43 public $_action;
44
45 public $_bltID;
46
47 public $_fields;
48
49 public $_paymentProcessor;
50 public $_recurPaymentProcessors;
51
dc913073
EM
52 /**
53 * array of processor options in the format id => array($id => $label)
54 * WARNING it appears that the format used to differ to this and there are places in the code that
55 * expect the old format. $this->_paymentProcessors provides the additional data which this
56 * array seems to have provided in the past
57 * @var array
58 */
6a488035
TO
59 public $_processors;
60
61 /**
dc913073
EM
62 * available payment processors with full details including the key 'object' indexed by their id
63 * @var array
64 */
65 protected $_paymentProcessors = array();
66 /**
67 * the id of the contribution that we are processing
6a488035
TO
68 *
69 * @var int
70 * @public
71 */
72 public $_id;
73
74 /**
75 * the id of the premium that we are proceessing
76 *
77 * @var int
78 * @public
79 */
80 public $_premiumID = NULL;
4691b077
EM
81
82 /**
83 * @var CRM_Contribute_DAO_ContributionProduct
84 */
6a488035
TO
85 public $_productDAO = NULL;
86
87 /**
88 * the id of the note
89 *
90 * @var int
91 * @public
92 */
93 public $_noteID;
94
95 /**
96 * the id of the contact associated with this contribution
97 *
98 * @var int
99 * @public
100 */
101 public $_contactID;
102
103 /**
104 * the id of the pledge payment that we are processing
105 *
106 * @var int
107 * @public
108 */
109 public $_ppID;
110
111 /**
112 * the id of the pledge that we are processing
113 *
114 * @var int
115 * @public
116 */
117 public $_pledgeID;
118
119 /**
120 * is this contribution associated with an online
121 * financial transaction
122 *
123 * @var boolean
124 * @public
125 */
126 public $_online = FALSE;
127
128 /**
129 * Stores all product option
130 *
131 * @var array
132 * @public
133 */
134 public $_options;
135
136 /**
137 * stores the honor id
138 *
139 * @var int
140 * @public
141 */
142 public $_honorID = NULL;
143
144 /**
145 * Store the contribution Type ID
146 *
147 * @var array
148 */
149 public $_contributionType;
150
151 /**
152 * The contribution values if an existing contribution
153 */
154 public $_values;
155
156 /**
157 * The pledge values if this contribution is associated with pledge
158 */
159 public $_pledgeValues;
160
161 public $_contributeMode = 'direct';
162
163 public $_context;
164
165 public $_compId;
166
167 /*
168 * Store the line items if price set used.
169 */
170 public $_lineItems;
171
172 protected $_formType;
173 protected $_cdType;
174
186c9c17
EM
175 /**
176 * @param $id
177 */
d5397f2f
PJ
178 public function showRecordLinkMesssage($id) {
179 $statusId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $id, 'contribution_status_id');
180 if (CRM_Contribute_PseudoConstant::contributionStatus($statusId, 'name') == 'Partially paid') {
181 if ($pid = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'participant_id', 'contribution_id')) {
182 $recordPaymentLink = CRM_Utils_System::url('civicrm/payment',
183 "reset=1&id={$pid}&cid={$this->_contactID}&action=add&component=event"
184 );
185 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');
186 }
187 }
188 }
189
186c9c17
EM
190 /**
191 * @param $id
192 * @param $values
193 */
6a488035
TO
194 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
195 $ids = array();
196 $params = array('id' => $id);
197 CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids);
198
199 //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL)
200 $this->_online = FALSE;
201 $fids = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($id);
a7488080 202 if (!empty($fids['financialTrxnId'])) {
6a488035
TO
203 $this->_online = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
204 }
eea16664 205
6a488035
TO
206 // Also don't allow user to update some fields for recurring contributions.
207 if (!$this->_online) {
208 $this->_online = CRM_Utils_Array::value('contribution_recur_id', $values);
209 }
eea16664 210
6a488035
TO
211 $this->assign('isOnline', $this->_online ? TRUE : FALSE);
212
6a488035
TO
213 //to get note id
214 $daoNote = new CRM_Core_BAO_Note();
215 $daoNote->entity_table = 'civicrm_contribution';
216 $daoNote->entity_id = $id;
217 if ($daoNote->find(TRUE)) {
218 $this->_noteID = $daoNote->id;
219 $values['note'] = $daoNote->note;
220 }
221 $this->_contributionType = $values['financial_type_id'];
6a488035
TO
222 }
223
224 /**
225 * @param string $type eg 'Contribution'
226 * @param string $subType
227 * @param int $entityId
228 */
229 public function applyCustomData($type, $subType, $entityId) {
230 $this->set('type', $type);
231 $this->set('subType', $subType);
232 $this->set('entityId', $entityId);
233
234 CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId);
235 CRM_Custom_Form_CustomData::buildQuickForm($this);
236 CRM_Custom_Form_CustomData::setDefaultValues($this);
237 }
238
4691b077
EM
239 /**
240 * @param $id
241 * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find();
242 */
6a488035
TO
243 public function assignPremiumProduct($id) { //to get Premium id
244 $sql = "
245SELECT *
246FROM civicrm_contribution_product
247WHERE contribution_id = {$id}
248";
249 $dao = CRM_Core_DAO::executeQuery($sql,
250 CRM_Core_DAO::$_nullArray
251 );
252 if ($dao->fetch()) {
253 $this->_premiumID = $dao->id;
254 $this->_productDAO = $dao;
255 }
256 $dao->free();
257 }
258
259 /**
260 * This function process contribution related objects.
03110609
EM
261 *
262 * @param integer $contributionId
263 * @param integer $statusId
264 * @param integer|null $previousStatusId
265 *
266 * @return null|string
6a488035
TO
267 */
268 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) {
269 $statusMsg = NULL;
270 if (!$contributionId || !$statusId) {
271 return $statusMsg;
272 }
273
274 $params = array(
275 'contribution_id' => $contributionId,
276 'contribution_status_id' => $statusId,
277 'previous_contribution_status_id' => $previousStatusId,
278 );
279
280 $updateResult = CRM_Contribute_BAO_Contribution::transitionComponents($params);
281
282 if (!is_array($updateResult) ||
283 !($updatedComponents = CRM_Utils_Array::value('updatedComponents', $updateResult)) ||
284 !is_array($updatedComponents) ||
285 empty($updatedComponents)
286 ) {
287 return $statusMsg;
288 }
289
290 // get the user display name.
291 $sql = "
292 SELECT display_name as displayName
293 FROM civicrm_contact
294LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
295 WHERE civicrm_contribution.id = {$contributionId}";
296 $userDisplayName = CRM_Core_DAO::singleValueQuery($sql);
297
298 // get the status message for user.
299 foreach ($updatedComponents as $componentName => $updatedStatusId) {
300
301 if ($componentName == 'CiviMember') {
302 $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
303 CRM_Member_PseudoConstant::membershipStatus()
304 );
305 if ($updatedStatusName == 'Cancelled') {
306 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
307 }
308 elseif ($updatedStatusName == 'Expired') {
309 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
310 }
d008a264 311 else {
13299a2b
JM
312 $endDate = CRM_Utils_Array::value('membership_end_date', $updateResult);
313 if ($endDate) {
d008a264
JM
314 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
315 array(
316 1 => $userDisplayName,
317 2 => $endDate
318 )
319 );
320 }
6a488035
TO
321 }
322 }
323
324 if ($componentName == 'CiviEvent') {
325 $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
326 CRM_Event_PseudoConstant::participantStatus()
327 );
328 if ($updatedStatusName == 'Cancelled') {
329 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
330 }
331 elseif ($updatedStatusName == 'Registered') {
332 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
333 }
334 }
335
336 if ($componentName == 'CiviPledge') {
337 $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
338 CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')
339 );
340 if ($updatedStatusName == 'Cancelled') {
341 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
342 }
343 elseif ($updatedStatusName == 'Failed') {
344 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
345 }
346 elseif ($updatedStatusName == 'Completed') {
347 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
348 }
349 }
350 }
351
352 return $statusMsg;
353 }
354
355 /**
dc913073 356 * @return array of valid processors. The array resembles the DB table but also has 'object' as a key
fbcb6fba
EM
357 * @throws Exception
358 */
359 public function getValidProcessors() {
fbcb6fba
EM
360 $defaultID = NULL;
361 $capabilities = array('BackOffice');
362 if ($this->_mode == 'live') {
363 $capabilities[] = 'LiveMode';
364 }
365 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors($capabilities);
44b6505d
EM
366 return $processors;
367
6a488035
TO
368 }
369
370 /**
371 * Assign billing type id to bltID
372 *
44b6505d 373 * @throws CRM_Core_Exception
6a488035
TO
374 * @return void
375 */
376 public function assignBillingType() {
180409a4 377 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
6a488035
TO
378 $this->_bltID = array_search('Billing', $locationTypes);
379 if (!$this->_bltID) {
44b6505d 380 throw new CRM_Core_Exception(ts('Please set a location type of %1', array(1 => 'Billing')));
6a488035
TO
381 }
382 $this->set('bltID', $this->_bltID);
383 $this->assign('bltID', $this->_bltID);
384 }
385
386 /**
387 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
388 */
389 public function assignProcessors() {
390 //ensure that processor has a valid config
391 //only valid processors get display to user
44b6505d 392
6a488035 393 if ($this->_mode) {
fbcb6fba 394 $this->assign(CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('supportsFutureRecurStartDate')), TRUE);
dc913073
EM
395 $this->_paymentProcessors = $this->getValidProcessors();
396 if (!isset($this->_paymentProcessor['id'])) {
397 // if the payment processor isn't set yet (as indicated by the presence of an id,) we'll grab the first one which should be the default
398 $this->_paymentProcessor = reset($this->_paymentProcessors);
399 }
400 if (empty($this->_paymentProcessors)) {
44b6505d
EM
401 throw new CRM_Core_Exception(ts('You will need to configure the %1 settings for your Payment Processor before you can submit a credit card transactions.', array(1 => $this->_mode)));
402 }
403 $this->_processors = array();
dc913073 404 foreach ($this->_paymentProcessors as $id => $processor) {
44b6505d
EM
405 $this->_processors[$id] = ts($processor['name']);
406 }
6a488035
TO
407 //get the valid recurring processors.
408 $recurring = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
409 $this->_recurPaymentProcessors = array_intersect_assoc($this->_processors, $recurring);
410 }
411 $this->assign('recurringPaymentProcessorIds',
412 empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors))
413 );
414
415 // this required to show billing block
03110609 416 // @todo remove this assignment the billing block is now designed to be always included but will not show fieldsets unless those sets of fields are assigned
44b6505d 417 $this->assign_by_ref('paymentProcessor', $processor);
6a488035
TO
418 }
419
186c9c17
EM
420 /**
421 * @param $submittedValues
422 *
423 * @return mixed
424 */
6a488035
TO
425 public function getCurrency($submittedValues) { // get current currency from DB or use default currency
426 $config = CRM_Core_Config::singleton();
427
428 $currentCurrency = CRM_Utils_Array::value('currency',
429 $this->_values,
430 $config->defaultCurrency
431 );
432
433 // use submitted currency if present else use current currency
434 $result = CRM_Utils_Array::value('currency',
435 $submittedValues,
436 $currentCurrency
437 );
438 return $result;
439 }
440
186c9c17
EM
441 /**
442 * @param $financialTypeId
443 *
444 * @return array
445 */
6a488035
TO
446 public function getFinancialAccounts($financialTypeId) {
447 $financialAccounts = array();
448 CRM_Core_PseudoConstant::populate($financialAccounts,
449 'CRM_Financial_DAO_EntityFinancialAccount',
450 $all = TRUE,
451 $retrieve = 'financial_account_id',
452 $filter = NULL,
453 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
454 return $financialAccounts;
455 }
456
186c9c17
EM
457 /**
458 * @param $financialTypeId
459 * @param $relationTypeId
460 *
461 * @return mixed
462 */
6a488035
TO
463 public function getFinancialAccount($financialTypeId, $relationTypeId) {
464 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
465 return CRM_Utils_Array::value($relationTypeId, $financialAccounts);
466 }
467
468 public function preProcessPledge() {
469 //get the payment values associated with given pledge payment id OR check for payments due.
470 $this->_pledgeValues = array();
471 if ($this->_ppID) {
472 $payParams = array('id' => $this->_ppID);
473
474 CRM_Pledge_BAO_PledgePayment::retrieve($payParams, $this->_pledgeValues['pledgePayment']);
475 $this->_pledgeID = CRM_Utils_Array::value('pledge_id', $this->_pledgeValues['pledgePayment']);
476 $paymentStatusID = CRM_Utils_Array::value('status_id', $this->_pledgeValues['pledgePayment']);
477 $this->_id = CRM_Utils_Array::value('contribution_id', $this->_pledgeValues['pledgePayment']);
478
479 //get all status
480 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
481 if (!($paymentStatusID == array_search('Pending', $allStatus) || $paymentStatusID == array_search('Overdue', $allStatus))) {
482 CRM_Core_Error::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'."));
483 }
484
485 //get the pledge values associated with given pledge payment.
486
487 $ids = array();
488 $pledgeParams = array('id' => $this->_pledgeID);
489 CRM_Pledge_BAO_Pledge::getValues($pledgeParams, $this->_pledgeValues, $ids);
490 $this->assign('ppID', $this->_ppID);
491 }
492 else {
493 // 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
494 if (isset($this->_contactID)) {
495 $contactPledges = CRM_Pledge_BAO_Pledge::getContactPledges($this->_contactID);
496
497 if (!empty($contactPledges)) {
498 $payments = $paymentsDue = NULL;
499 $multipleDue = FALSE;
500 foreach ($contactPledges as $key => $pledgeId) {
501 $payments = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
502 if ($payments) {
503 if ($paymentsDue) {
504 $multipleDue = TRUE;
505 break;
506 }
507 else {
508 $paymentsDue = $payments;
509 }
510 }
511 }
512 if ($multipleDue) {
513 // Show link to pledge tab since more than one pledge has a payment due
514 $pledgeTab = CRM_Utils_System::url('civicrm/contact/view',
515 "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge"
516 );
517 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');
518 }
519 elseif ($paymentsDue) {
520 // Show user link to oldest Pending or Overdue pledge payment
521 $ppAmountDue = CRM_Utils_Money::format($payments['amount'], $payments['currency']);
522 $ppSchedDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date'));
523 if ($this->_mode) {
524 $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution',
525 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live"
526 );
527 }
528 else {
529 $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution',
530 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
531 );
532 }
533 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(
534 1 => $ppUrl,
535 2 => $ppAmountDue,
536 3 => $ppSchedDate
537 )), ts('Notice'), 'alert');
538 }
539 }
540 }
541 }
542 }
543
186c9c17
EM
544 /**
545 * @param $submittedValues
546 *
547 * @return mixed
548 */
6a488035
TO
549 public function unsetCreditCardFields($submittedValues) {
550 //Offline Contribution.
551 $unsetParams = array(
552 'payment_processor_id',
553 "email-{$this->_bltID}",
554 'hidden_buildCreditCard',
555 'hidden_buildDirectDebit',
556 'billing_first_name',
557 'billing_middle_name',
558 'billing_last_name',
559 'street_address-5',
560 "city-{$this->_bltID}",
561 "state_province_id-{$this->_bltID}",
562 "postal_code-{$this->_bltID}",
563 "country_id-{$this->_bltID}",
564 'credit_card_number',
565 'cvv2',
566 'credit_card_exp_date',
567 'credit_card_type',
568 );
569 foreach ($unsetParams as $key) {
570 if (isset($submittedValues[$key])) {
571 unset($submittedValues[$key]);
572 }
573 }
574 return $submittedValues;
575 }
576
cc984198
EM
577 /**
578 * common block for setting up the parts of a form that relate to credit / debit card
579 * @throws Exception
580 */
581 protected function assignPaymentRelatedVariables() {
582 try {
583 if ($this->_mode) {
584 $this->assignProcessors();
585 if ($this->_contactID) {
586 list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
587 $this->assign('displayName', $this->userDisplayName);
588 }
589
590 $this->assignBillingType();
591
592 $this->_fields = array();
593 CRM_Core_Payment_Form::setPaymentFieldsByProcessor($this, $this->_paymentProcessor);
594 }
595 }
596 catch (CRM_Core_Exception $e) {
597 CRM_Core_Error::fatal($e->getMessage());
598 }
599 }
6a488035 600}