CRM-20936: Hide statuses on backoffice contribution form
[civicrm-core.git] / CRM / Contribute / BAO / Contribution / Utils.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 useful, 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
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33 class CRM_Contribute_BAO_Contribution_Utils {
34
35 /**
36 * Process payment after confirmation.
37 *
38 * @param CRM_Core_Form $form
39 * Form object.
40 * @param array $paymentParams
41 * Array with payment related key.
42 * value pairs
43 * @param int $contactID
44 * Contact id.
45 * @param int $financialTypeID
46 * Financial type id.
47 * @param bool $isTest
48 * @param bool $isRecur
49 *
50 * @throws CRM_Core_Exception
51 * @throws Exception
52 * @return array
53 * associated array
54 *
55 */
56 public static function processConfirm(
57 &$form,
58 &$paymentParams,
59 $contactID,
60 $financialTypeID,
61 $isTest,
62 $isRecur
63 ) {
64 CRM_Core_Payment_Form::mapParams($form->_bltID, $form->_params, $paymentParams, TRUE);
65 $isPaymentTransaction = self::isPaymentTransaction($form);
66
67 $financialType = new CRM_Financial_DAO_FinancialType();
68 $financialType->id = $financialTypeID;
69 $financialType->find(TRUE);
70 if ($financialType->is_deductible) {
71 $form->assign('is_deductible', TRUE);
72 $form->set('is_deductible', TRUE);
73 }
74
75 // add some financial type details to the params list
76 // if folks need to use it
77 //CRM-15297 - contributionType is obsolete - pass financial type as well so people can deprecate it
78 $paymentParams['financialType_name'] = $paymentParams['contributionType_name'] = $form->_params['contributionType_name'] = $financialType->name;
79 //CRM-11456
80 $paymentParams['financialType_accounting_code'] = $paymentParams['contributionType_accounting_code'] = $form->_params['contributionType_accounting_code'] = CRM_Financial_BAO_FinancialAccount::getAccountingCode($financialTypeID);
81 $paymentParams['contributionPageID'] = $form->_params['contributionPageID'] = $form->_values['id'];
82 $paymentParams['contactID'] = $form->_params['contactID'] = $contactID;
83
84 //fix for CRM-16317
85 if (empty($form->_params['receive_date'])) {
86 $form->_params['receive_date'] = date('YmdHis');
87 }
88 if (!empty($form->_params['start_date'])) {
89 $form->_params['start_date'] = date('YmdHis');
90 }
91 $form->assign('receive_date',
92 CRM_Utils_Date::mysqlToIso($form->_params['receive_date'])
93 );
94
95 if (empty($form->_values['amount'])) {
96 // If the amount is not in _values[], set it
97 $form->_values['amount'] = $form->_params['amount'];
98 }
99
100 if ($isPaymentTransaction) {
101 $contributionParams = array(
102 'id' => CRM_Utils_Array::value('contribution_id', $paymentParams),
103 'contact_id' => $contactID,
104 'is_test' => $isTest,
105 'campaign_id' => CRM_Utils_Array::value('campaign_id', $paymentParams, CRM_Utils_Array::value('campaign_id', $form->_values)),
106 'contribution_page_id' => $form->_id,
107 'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)),
108 );
109 if (isset($paymentParams['line_item'])) {
110 // @todo make sure this is consisently set at this point.
111 $contributionParams['line_item'] = $paymentParams['line_item'];
112 }
113 if (!empty($form->_paymentProcessor)) {
114 $contributionParams['payment_instrument_id'] = $paymentParams['payment_instrument_id'] = $form->_paymentProcessor['payment_instrument_id'];
115 }
116
117 $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution(
118 $form,
119 $paymentParams,
120 NULL,
121 $contributionParams,
122 $financialType,
123 TRUE,
124 $form->_bltID,
125 $isRecur
126 );
127
128 $paymentParams['item_name'] = $form->_params['description'];
129
130 $paymentParams['qfKey'] = $form->controller->_key;
131 if ($paymentParams['skipLineItem']) {
132 // We are not processing the line item here because we are processing a membership.
133 // Do not continue with contribution processing in this function.
134 return array('contribution' => $contribution);
135 }
136
137 $paymentParams['contributionID'] = $contribution->id;
138 //CRM-15297 deprecate contributionTypeID
139 $paymentParams['financialTypeID'] = $paymentParams['contributionTypeID'] = $contribution->financial_type_id;
140 $paymentParams['contributionPageID'] = $contribution->contribution_page_id;
141 if (isset($paymentParams['contribution_source'])) {
142 $paymentParams['source'] = $paymentParams['contribution_source'];
143 }
144
145 if (CRM_Utils_Array::value('is_recur', $form->_params) && $contribution->contribution_recur_id) {
146 $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id;
147 }
148 if (isset($paymentParams['contribution_source'])) {
149 $form->_params['source'] = $paymentParams['contribution_source'];
150 }
151
152 // get the price set values for receipt.
153 if ($form->_priceSetId && $form->_lineItem) {
154 $form->_values['lineItem'] = $form->_lineItem;
155 $form->_values['priceSetID'] = $form->_priceSetId;
156 }
157
158 $form->_values['contribution_id'] = $contribution->id;
159 $form->_values['contribution_page_id'] = $contribution->contribution_page_id;
160
161 if (!empty($form->_paymentProcessor)) {
162 try {
163 $payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor);
164 if ($form->_contributeMode == 'notify') {
165 // We want to get rid of this & make it generic - eg. by making payment processing the last thing
166 // and always calling it first.
167 $form->postProcessHook();
168 }
169 $result = $payment->doPayment($paymentParams);
170 $form->_params = array_merge($form->_params, $result);
171 $form->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $result));
172 if (!empty($result['trxn_id'])) {
173 $contribution->trxn_id = $result['trxn_id'];
174 }
175 if (!empty($result['payment_status_id'])) {
176 $contribution->payment_status_id = $result['payment_status_id'];
177 }
178 $result['contribution'] = $contribution;
179 if ($result['payment_status_id'] == CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution',
180 'status_id', 'Pending') && $payment->isSendReceiptForPending()) {
181 CRM_Contribute_BAO_ContributionPage::sendMail($contactID,
182 $form->_values,
183 $contribution->is_test
184 );
185 }
186 return $result;
187 }
188 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
189 // Clean up DB as appropriate.
190 if (!empty($paymentParams['contributionID'])) {
191 CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'],
192 $paymentParams['contactID'], $e->getMessage());
193 }
194 if (!empty($paymentParams['contributionRecurID'])) {
195 CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']);
196 }
197
198 $result['is_payment_failure'] = TRUE;
199 $result['error'] = $e;
200 return $result;
201 }
202 }
203 }
204
205 // Only pay later or unpaid should reach this point, although pay later likely does not & is handled via the
206 // manual processor, so it's unclear what this set is for and whether the following send ever fires.
207 $form->set('params', $form->_params);
208
209 if ($form->_params['amount'] == 0) {
210 // This is kind of a back-up for pay-later $0 transactions.
211 // In other flows they pick up the manual processor & get dealt with above (I
212 // think that might be better...).
213 return array(
214 'payment_status_id' => 1,
215 'contribution' => $contribution,
216 'payment_processor_id' => 0,
217 );
218 }
219
220 CRM_Contribute_BAO_ContributionPage::sendMail($contactID,
221 $form->_values,
222 $contribution->is_test
223 );
224 }
225
226 /**
227 * Is a payment being made.
228 * Note that setting is_monetary on the form is somewhat legacy and the behaviour around this setting is confusing. It would be preferable
229 * to look for the amount only (assuming this cannot refer to payment in goats or other non-monetary currency
230 * @param CRM_Core_Form $form
231 *
232 * @return bool
233 */
234 static protected function isPaymentTransaction($form) {
235 return ($form->_amount >= 0.0) ? TRUE : FALSE;
236 }
237
238 /**
239 * Get the contribution details by month of the year.
240 *
241 * @param int $param
242 * Year.
243 *
244 * @return array
245 * associated array
246 */
247 public static function contributionChartMonthly($param) {
248 if ($param) {
249 $param = array(1 => array($param, 'Integer'));
250 }
251 else {
252 $param = date("Y");
253 $param = array(1 => array($param, 'Integer'));
254 }
255
256 $query = "
257 SELECT sum(contrib.total_amount) AS ctAmt,
258 MONTH( contrib.receive_date) AS contribMonth
259 FROM civicrm_contribution AS contrib
260 INNER JOIN civicrm_contact AS contact ON ( contact.id = contrib.contact_id )
261 WHERE contrib.contact_id = contact.id
262 AND ( contrib.is_test = 0 OR contrib.is_test IS NULL )
263 AND contrib.contribution_status_id = 1
264 AND date_format(contrib.receive_date,'%Y') = %1
265 AND contact.is_deleted = 0
266 GROUP BY contribMonth
267 ORDER BY month(contrib.receive_date)";
268
269 $dao = CRM_Core_DAO::executeQuery($query, $param);
270
271 $params = NULL;
272 while ($dao->fetch()) {
273 if ($dao->contribMonth) {
274 $params['By Month'][$dao->contribMonth] = $dao->ctAmt;
275 }
276 }
277 return $params;
278 }
279
280 /**
281 * Get the contribution details by year.
282 *
283 * @return array
284 * associated array
285 */
286 public static function contributionChartYearly() {
287 $config = CRM_Core_Config::singleton();
288 $yearClause = "year(contrib.receive_date) as contribYear";
289 if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] != 1 || $config->fiscalYearStart['d'] != 1)) {
290 $yearClause = "CASE
291 WHEN (MONTH(contrib.receive_date)>= " . $config->fiscalYearStart['M'] . "
292 && DAYOFMONTH(contrib.receive_date)>= " . $config->fiscalYearStart['d'] . " )
293 THEN
294 concat(YEAR(contrib.receive_date), '-',YEAR(contrib.receive_date)+1)
295 ELSE
296 concat(YEAR(contrib.receive_date)-1,'-', YEAR(contrib.receive_date))
297 END AS contribYear";
298 }
299
300 $query = "
301 SELECT sum(contrib.total_amount) AS ctAmt,
302 {$yearClause}
303 FROM civicrm_contribution AS contrib
304 INNER JOIN civicrm_contact contact ON ( contact.id = contrib.contact_id )
305 WHERE ( contrib.is_test = 0 OR contrib.is_test IS NULL )
306 AND contrib.contribution_status_id = 1
307 AND contact.is_deleted = 0
308 GROUP BY contribYear
309 ORDER BY contribYear";
310 $dao = CRM_Core_DAO::executeQuery($query);
311
312 $params = NULL;
313 while ($dao->fetch()) {
314 if (!empty($dao->contribYear)) {
315 $params['By Year'][$dao->contribYear] = $dao->ctAmt;
316 }
317 }
318 return $params;
319 }
320
321 /**
322 * @param array $params
323 * @param int $contactID
324 * @param $mail
325 */
326 public static function createCMSUser(&$params, $contactID, $mail) {
327 // lets ensure we only create one CMS user
328 static $created = FALSE;
329
330 if ($created) {
331 return;
332 }
333 $created = TRUE;
334
335 if (!empty($params['cms_create_account'])) {
336 $params['contactID'] = !empty($params['onbehalf_contact_id']) ? $params['onbehalf_contact_id'] : $contactID;
337 if (!CRM_Core_BAO_CMSUser::create($params, $mail)) {
338 CRM_Core_Error::statusBounce(ts('Your profile is not saved and Account is not created.'));
339 }
340 }
341 }
342
343 /**
344 * @param array $params
345 * @param string $type
346 *
347 * @return bool
348 */
349 public static function _fillCommonParams(&$params, $type = 'paypal') {
350 if (array_key_exists('transaction', $params)) {
351 $transaction = &$params['transaction'];
352 }
353 else {
354 $transaction = &$params;
355 }
356
357 $params['contact_type'] = 'Individual';
358
359 $billingLocTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', 'Billing', 'id', 'name');
360 if (!$billingLocTypeId) {
361 $billingLocTypeId = 1;
362 }
363 if (!CRM_Utils_System::isNull($params['address'])) {
364 $params['address'][1]['is_primary'] = 1;
365 $params['address'][1]['location_type_id'] = $billingLocTypeId;
366 }
367 if (!CRM_Utils_System::isNull($params['email'])) {
368 $params['email'] = array(
369 1 => array(
370 'email' => $params['email'],
371 'location_type_id' => $billingLocTypeId,
372 ),
373 );
374 }
375
376 if (isset($transaction['trxn_id'])) {
377 // set error message if transaction has already been processed.
378 $contribution = new CRM_Contribute_DAO_Contribution();
379 $contribution->trxn_id = $transaction['trxn_id'];
380 if ($contribution->find(TRUE)) {
381 $params['error'][] = ts('transaction already processed.');
382 }
383 }
384 else {
385 // generate a new transaction id, if not already exist
386 $transaction['trxn_id'] = md5(uniqid(rand(), TRUE));
387 }
388
389 if (!isset($transaction['financial_type_id'])) {
390 $contributionTypes = array_keys(CRM_Contribute_PseudoConstant::financialType());
391 $transaction['financial_type_id'] = $contributionTypes[0];
392 }
393
394 if (($type == 'paypal') && (!isset($transaction['net_amount']))) {
395 $transaction['net_amount'] = $transaction['total_amount'] - CRM_Utils_Array::value('fee_amount', $transaction, 0);
396 }
397
398 if (!isset($transaction['invoice_id'])) {
399 $transaction['invoice_id'] = $transaction['trxn_id'];
400 }
401
402 $source = ts('ContributionProcessor: %1 API',
403 array(1 => ucfirst($type))
404 );
405 if (isset($transaction['source'])) {
406 $transaction['source'] = $source . ':: ' . $transaction['source'];
407 }
408 else {
409 $transaction['source'] = $source;
410 }
411
412 return TRUE;
413 }
414
415 /**
416 * @param int $contactID
417 *
418 * @return mixed
419 */
420 public static function getFirstLastDetails($contactID) {
421 static $_cache;
422
423 if (!$_cache) {
424 $_cache = array();
425 }
426
427 if (!isset($_cache[$contactID])) {
428 $sql = "
429 SELECT total_amount, receive_date
430 FROM civicrm_contribution c
431 WHERE contact_id = %1
432 ORDER BY receive_date ASC
433 LIMIT 1
434 ";
435 $params = array(1 => array($contactID, 'Integer'));
436
437 $dao = CRM_Core_DAO::executeQuery($sql, $params);
438 $details = array(
439 'first' => NULL,
440 'last' => NULL,
441 );
442 if ($dao->fetch()) {
443 $details['first'] = array(
444 'total_amount' => $dao->total_amount,
445 'receive_date' => $dao->receive_date,
446 );
447 }
448
449 // flip asc and desc to get the last query
450 $sql = str_replace('ASC', 'DESC', $sql);
451 $dao = CRM_Core_DAO::executeQuery($sql, $params);
452 if ($dao->fetch()) {
453 $details['last'] = array(
454 'total_amount' => $dao->total_amount,
455 'receive_date' => $dao->receive_date,
456 );
457 }
458
459 $_cache[$contactID] = $details;
460 }
461 return $_cache[$contactID];
462 }
463
464 /**
465 * Calculate the tax amount based on given tax rate.
466 *
467 * @param float $amount
468 * Amount of field.
469 * @param float $taxRate
470 * Tax rate of selected financial account for field.
471 *
472 * @return array
473 * array of tax amount
474 *
475 */
476 public static function calculateTaxAmount($amount, $taxRate) {
477 $taxAmount = array();
478 // There can not be any rounding at this stage - as this is prior to quantity multiplication
479 $taxAmount['tax_amount'] = ($taxRate / 100) * CRM_Utils_Rule::cleanMoney($amount);
480
481 return $taxAmount;
482 }
483
484 /**
485 * Format monetary amount: round and return to desired decimal place
486 * CRM-20145
487 *
488 * @param float $amount
489 * Monetary amount
490 * @param int $decimals
491 * How many decimal places to round to and return
492 *
493 * @return float
494 * Amount rounded and returned with the desired decimal places
495 */
496 public static function formatAmount($amount, $decimals = 2) {
497 return number_format((float) round($amount, (int) $decimals), (int) $decimals, '.', '');
498 }
499
500 /**
501 * Get contribution statuses by entity e.g. contribution, membership or 'participant'
502 *
503 * @param string $usedFor
504 * @param int $id
505 * Contribution ID
506 *
507 * @return array
508 * Array of contribution statuses in array('status id' => 'label') format
509 */
510 public static function getContributionStatuses($usedFor = 'contribution', $id = NULL) {
511 if ($usedFor == 'pledge') {
512 $statusNames = CRM_Core_OptionGroup::values('pledge_status', FALSE, FALSE, FALSE, NULL, 'name');
513 }
514 else {
515 $statusNames = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
516 }
517
518 $statusNamesToUnset = array();
519
520 // on create fetch statuses on basis of component
521 if (!$id) {
522 $statusNamesToUnset = array(
523 'Refunded',
524 'Chargeback',
525 'Pending refund',
526 );
527 // Event registration and New Membership backoffice form support partially paid payment,
528 // so exclude this status only for 'New Contribution' form
529 if ($usedFor == 'contribution') {
530 $statusNamesToUnset = array_merge($statusNamesToUnset, array(
531 'In Progress',
532 'Overdue',
533 'Partially paid',
534 ));
535 }
536 elseif ($usedFor == 'participant') {
537 $statusNamesToUnset = array_merge($statusNamesToUnset, array(
538 'Cancelled',
539 'Failed',
540 ));
541 }
542 }
543 else {
544 $contributionStatus = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $id, 'contribution_status_id');
545 $name = CRM_Utils_Array::value($contributionStatus, $statusNames);
546 switch ($name) {
547 case 'Completed':
548 // [CRM-17498] Removing unsupported status change options.
549 $statusNamesToUnset = array_merge($statusNamesToUnset, array(
550 'Pending',
551 'Failed',
552 'Partially paid',
553 'Pending refund',
554 ));
555 break;
556
557 case 'Cancelled':
558 case 'Chargeback':
559 case 'Refunded':
560 $statusNamesToUnset = array_merge($statusNamesToUnset, array(
561 'Pending',
562 'Failed',
563 ));
564 break;
565
566 case 'Pending':
567 case 'In Progress':
568 $statusNamesToUnset = array_merge($statusNamesToUnset, array(
569 'Refunded',
570 'Chargeback',
571 ));
572 break;
573
574 case 'Failed':
575 $statusNamesToUnset = array_merge($statusNamesToUnset, array(
576 'Pending',
577 'Refunded',
578 'Chargeback',
579 'Completed',
580 'In Progress',
581 'Cancelled',
582 ));
583 break;
584 }
585 }
586
587 foreach ($statusNamesToUnset as $name) {
588 unset($statusNames[CRM_Utils_Array::key($name, $statusNames)]);
589 }
590
591 // based on filtered statuse names fetch the final list of statuses in array('id' => 'label') format
592 if ($usedFor == 'pledge') {
593 $statuses = CRM_Core_OptionGroup::values('pledge_status');
594 }
595 else {
596 $statuses = CRM_Contribute_PseudoConstant::contributionStatus();
597 }
598 foreach ($statuses as $statusID => $label) {
599 if (!array_key_exists($statusID, $statusNames)) {
600 unset($statuses[$statusID]);
601 }
602 }
603
604 return $statuses;
605 }
606
607 }