some comment block fixes
[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
52 public $_processors;
53
54 /**
55 * the id of the contribution that we are proceessing
56 *
57 * @var int
58 * @public
59 */
60 public $_id;
61
62 /**
63 * the id of the premium that we are proceessing
64 *
65 * @var int
66 * @public
67 */
68 public $_premiumID = NULL;
4691b077
EM
69
70 /**
71 * @var CRM_Contribute_DAO_ContributionProduct
72 */
6a488035
TO
73 public $_productDAO = NULL;
74
75 /**
76 * the id of the note
77 *
78 * @var int
79 * @public
80 */
81 public $_noteID;
82
83 /**
84 * the id of the contact associated with this contribution
85 *
86 * @var int
87 * @public
88 */
89 public $_contactID;
90
91 /**
92 * the id of the pledge payment that we are processing
93 *
94 * @var int
95 * @public
96 */
97 public $_ppID;
98
99 /**
100 * the id of the pledge that we are processing
101 *
102 * @var int
103 * @public
104 */
105 public $_pledgeID;
106
107 /**
108 * is this contribution associated with an online
109 * financial transaction
110 *
111 * @var boolean
112 * @public
113 */
114 public $_online = FALSE;
115
116 /**
117 * Stores all product option
118 *
119 * @var array
120 * @public
121 */
122 public $_options;
123
124 /**
125 * stores the honor id
126 *
127 * @var int
128 * @public
129 */
130 public $_honorID = NULL;
131
132 /**
133 * Store the contribution Type ID
134 *
135 * @var array
136 */
137 public $_contributionType;
138
139 /**
140 * The contribution values if an existing contribution
141 */
142 public $_values;
143
144 /**
145 * The pledge values if this contribution is associated with pledge
146 */
147 public $_pledgeValues;
148
149 public $_contributeMode = 'direct';
150
151 public $_context;
152
153 public $_compId;
154
155 /*
156 * Store the line items if price set used.
157 */
158 public $_lineItems;
159
160 protected $_formType;
161 protected $_cdType;
162
d5397f2f
PJ
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"
169 );
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');
171 }
172 }
173 }
174
6a488035
TO
175 public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
176 $ids = array();
177 $params = array('id' => $id);
178 CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids);
179
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);
a7488080 183 if (!empty($fids['financialTrxnId'])) {
6a488035
TO
184 $this->_online = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id');
185 }
eea16664 186
6a488035
TO
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);
190 }
eea16664 191
6a488035
TO
192 $this->assign('isOnline', $this->_online ? TRUE : FALSE);
193
6a488035
TO
194 //to get note id
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;
201 }
202 $this->_contributionType = $values['financial_type_id'];
6a488035
TO
203 }
204
205 /**
206 * @param string $type eg 'Contribution'
207 * @param string $subType
208 * @param int $entityId
209 */
210 public function applyCustomData($type, $subType, $entityId) {
211 $this->set('type', $type);
212 $this->set('subType', $subType);
213 $this->set('entityId', $entityId);
214
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);
218 }
219
4691b077
EM
220 /**
221 * @param $id
222 * @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find();
223 */
6a488035
TO
224 public function assignPremiumProduct($id) { //to get Premium id
225 $sql = "
226SELECT *
227FROM civicrm_contribution_product
228WHERE contribution_id = {$id}
229";
230 $dao = CRM_Core_DAO::executeQuery($sql,
231 CRM_Core_DAO::$_nullArray
232 );
233 if ($dao->fetch()) {
234 $this->_premiumID = $dao->id;
235 $this->_productDAO = $dao;
236 }
237 $dao->free();
238 }
239
240 /**
241 * This function process contribution related objects.
242 */
243 protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) {
244 $statusMsg = NULL;
245 if (!$contributionId || !$statusId) {
246 return $statusMsg;
247 }
248
249 $params = array(
250 'contribution_id' => $contributionId,
251 'contribution_status_id' => $statusId,
252 'previous_contribution_status_id' => $previousStatusId,
253 );
254
255 $updateResult = CRM_Contribute_BAO_Contribution::transitionComponents($params);
256
257 if (!is_array($updateResult) ||
258 !($updatedComponents = CRM_Utils_Array::value('updatedComponents', $updateResult)) ||
259 !is_array($updatedComponents) ||
260 empty($updatedComponents)
261 ) {
262 return $statusMsg;
263 }
264
265 // get the user display name.
266 $sql = "
267 SELECT display_name as displayName
268 FROM civicrm_contact
269LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
270 WHERE civicrm_contribution.id = {$contributionId}";
271 $userDisplayName = CRM_Core_DAO::singleValueQuery($sql);
272
273 // get the status message for user.
274 foreach ($updatedComponents as $componentName => $updatedStatusId) {
275
276 if ($componentName == 'CiviMember') {
277 $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
278 CRM_Member_PseudoConstant::membershipStatus()
279 );
280 if ($updatedStatusName == 'Cancelled') {
281 $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName));
282 }
283 elseif ($updatedStatusName == 'Expired') {
284 $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName));
285 }
d008a264 286 else {
13299a2b
JM
287 $endDate = CRM_Utils_Array::value('membership_end_date', $updateResult);
288 if ($endDate) {
d008a264
JM
289 $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.",
290 array(
291 1 => $userDisplayName,
292 2 => $endDate
293 )
294 );
295 }
6a488035
TO
296 }
297 }
298
299 if ($componentName == 'CiviEvent') {
300 $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
301 CRM_Event_PseudoConstant::participantStatus()
302 );
303 if ($updatedStatusName == 'Cancelled') {
304 $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName));
305 }
306 elseif ($updatedStatusName == 'Registered') {
307 $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName));
308 }
309 }
310
311 if ($componentName == 'CiviPledge') {
312 $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
313 CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')
314 );
315 if ($updatedStatusName == 'Cancelled') {
316 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName));
317 }
318 elseif ($updatedStatusName == 'Failed') {
319 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName));
320 }
321 elseif ($updatedStatusName == 'Completed') {
322 $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName));
323 }
324 }
325 }
326
327 return $statusMsg;
328 }
329
330 /**
331 * @return array (0 => array(int $ppId => string $label), 1 => array(...payproc details...))
332 */
333 public function getValidProcessorsAndAssignFutureStartDate() {
334 $validProcessors = array();
1193f906
DG
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");
6a488035
TO
337
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);
345 }
346 if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) {
347 continue;
348 }
349 elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode == 'live') {
350 continue;
351 }
352 else {
353 $paymentObject = CRM_Core_Payment::singleton($this->_mode, $paymentProcessor, $this);
354 $error = $paymentObject->checkConfig();
355 if (empty($error)) {
356 $validProcessors[$ppID] = $label;
357 }
358 $paymentObject = NULL;
359 }
360 }
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)));
363 }
364 else {
365 return array($validProcessors, $paymentProcessor);
366 }
367 }
368
369 /**
370 * Assign billing type id to bltID
371 *
372 * @return void
373 */
374 public function assignBillingType() {
180409a4 375 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
6a488035
TO
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')));
379 }
380 $this->set('bltID', $this->_bltID);
381 $this->assign('bltID', $this->_bltID);
382 }
383
384 /**
385 * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables
386 */
387 public function assignProcessors() {
388 //ensure that processor has a valid config
389 //only valid processors get display to user
390 if ($this->_mode) {
391 list($this->_processors, $paymentProcessor) = $this->getValidProcessorsAndAssignFutureStartDate();
392
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);
396 }
397 $this->assign('recurringPaymentProcessorIds',
398 empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors))
399 );
400
401 // this required to show billing block
402 $this->assign_by_ref('paymentProcessor', $paymentProcessor);
403 $this->assign('hidePayPalExpress', TRUE);
404 }
405
406 public function getCurrency($submittedValues) { // get current currency from DB or use default currency
407 $config = CRM_Core_Config::singleton();
408
409 $currentCurrency = CRM_Utils_Array::value('currency',
410 $this->_values,
411 $config->defaultCurrency
412 );
413
414 // use submitted currency if present else use current currency
415 $result = CRM_Utils_Array::value('currency',
416 $submittedValues,
417 $currentCurrency
418 );
419 return $result;
420 }
421
422 public function getFinancialAccounts($financialTypeId) {
423 $financialAccounts = array();
424 CRM_Core_PseudoConstant::populate($financialAccounts,
425 'CRM_Financial_DAO_EntityFinancialAccount',
426 $all = TRUE,
427 $retrieve = 'financial_account_id',
428 $filter = NULL,
429 " entity_id = {$financialTypeId} ", NULL, 'account_relationship');
430 return $financialAccounts;
431 }
432
433 public function getFinancialAccount($financialTypeId, $relationTypeId) {
434 $financialAccounts = $this->getFinancialAccounts($financialTypeId);
435 return CRM_Utils_Array::value($relationTypeId, $financialAccounts);
436 }
437
438 public function preProcessPledge() {
439 //get the payment values associated with given pledge payment id OR check for payments due.
440 $this->_pledgeValues = array();
441 if ($this->_ppID) {
442 $payParams = array('id' => $this->_ppID);
443
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']);
448
449 //get all status
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'."));
453 }
454
455 //get the pledge values associated with given pledge payment.
456
457 $ids = array();
458 $pledgeParams = array('id' => $this->_pledgeID);
459 CRM_Pledge_BAO_Pledge::getValues($pledgeParams, $this->_pledgeValues, $ids);
460 $this->assign('ppID', $this->_ppID);
461 }
462 else {
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);
466
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);
472 if ($payments) {
473 if ($paymentsDue) {
474 $multipleDue = TRUE;
475 break;
476 }
477 else {
478 $paymentsDue = $payments;
479 }
480 }
481 }
482 if ($multipleDue) {
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"
486 );
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');
488 }
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'));
493 if ($this->_mode) {
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"
496 );
497 }
498 else {
499 $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution',
500 "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge"
501 );
502 }
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(
504 1 => $ppUrl,
505 2 => $ppAmountDue,
506 3 => $ppSchedDate
507 )), ts('Notice'), 'alert');
508 }
509 }
510 }
511 }
512 }
513
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',
523 'billing_last_name',
524 'street_address-5',
525 "city-{$this->_bltID}",
526 "state_province_id-{$this->_bltID}",
527 "postal_code-{$this->_bltID}",
528 "country_id-{$this->_bltID}",
529 'credit_card_number',
530 'cvv2',
531 'credit_card_exp_date',
532 'credit_card_type',
533 );
534 foreach ($unsetParams as $key) {
535 if (isset($submittedValues[$key])) {
536 unset($submittedValues[$key]);
537 }
538 }
539 return $submittedValues;
540 }
541
542}