Merge pull request #13833 from civicrm/5.12
[civicrm-core.git] / CRM / Financial / BAO / Payment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * This class contains payment related functions.
36 */
37 class CRM_Financial_BAO_Payment {
38
39 /**
40 * Function to process additional payment for partial and refund contributions.
41 *
42 * This function is called via API payment.create function. All forms that add payments
43 * should use this.
44 *
45 * @param array $params
46 * - contribution_id
47 * - total_amount
48 * - line_item
49 *
50 * @return \CRM_Financial_DAO_FinancialTrxn
51 *
52 * @throws \API_Exception
53 * @throws \CRM_Core_Exception
54 */
55 public static function create($params) {
56 $contribution = civicrm_api3('Contribution', 'getsingle', ['id' => $params['contribution_id']]);
57 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus($contribution['contribution_status_id'], 'name');
58
59 $isPaymentCompletesContribution = self::isPaymentCompletesContribution($params['contribution_id'], $params['total_amount']);
60
61 // For legacy reasons Pending payments are completed through completetransaction.
62 // @todo completetransaction should transition components but financial transactions
63 // should be handled through Payment.create.
64 $isSkipRecordingPaymentHereForLegacyHandlingReasons = ($contributionStatus == 'Pending' && $isPaymentCompletesContribution);
65
66 if (!$isSkipRecordingPaymentHereForLegacyHandlingReasons) {
67 $trxn = CRM_Contribute_BAO_Contribution::recordPartialPayment($contribution, $params);
68
69 if (CRM_Utils_Array::value('line_item', $params) && !empty($trxn)) {
70 foreach ($params['line_item'] as $values) {
71 foreach ($values as $id => $amount) {
72 $p = ['id' => $id];
73 $check = CRM_Price_BAO_LineItem::retrieve($p, $defaults);
74 if (empty($check)) {
75 throw new API_Exception('Please specify a valid Line Item.');
76 }
77 // get financial item
78 $sql = "SELECT fi.id
79 FROM civicrm_financial_item fi
80 INNER JOIN civicrm_line_item li ON li.id = fi.entity_id and fi.entity_table = 'civicrm_line_item'
81 WHERE li.contribution_id = %1 AND li.id = %2";
82 $sqlParams = [
83 1 => [$params['contribution_id'], 'Integer'],
84 2 => [$id, 'Integer'],
85 ];
86 $fid = CRM_Core_DAO::singleValueQuery($sql, $sqlParams);
87 // Record Entity Financial Trxn
88 $eftParams = [
89 'entity_table' => 'civicrm_financial_item',
90 'financial_trxn_id' => $trxn->id,
91 'amount' => $amount,
92 'entity_id' => $fid,
93 ];
94 civicrm_api3('EntityFinancialTrxn', 'create', $eftParams);
95 }
96 }
97 }
98 elseif (!empty($trxn)) {
99 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution['total_amount']);
100 }
101 }
102
103 if ($isPaymentCompletesContribution) {
104 civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id']));
105 // Get the trxn
106 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
107 $ftParams = ['id' => $trxnId['financialTrxnId']];
108 $trxn = CRM_Core_BAO_FinancialTrxn::retrieve($ftParams, CRM_Core_DAO::$_nullArray);
109 }
110 elseif ($contributionStatus === 'Pending') {
111 civicrm_api3('Contribution', 'create',
112 [
113 'id' => $contribution['id'],
114 'contribution_status_id' => 'Partially paid',
115 ]
116 );
117 }
118
119 return $trxn;
120 }
121
122 /**
123 * Send an email confirming a payment that has been received.
124 *
125 * @param array $params
126 *
127 * @return array
128 */
129 public static function sendConfirmation($params) {
130
131 $entities = self::loadRelatedEntities($params['id']);
132 $sendTemplateParams = array(
133 'groupName' => 'msg_tpl_workflow_contribution',
134 'valueName' => 'payment_or_refund_notification',
135 'PDFFilename' => ts('notification') . '.pdf',
136 'contactId' => $entities['contact']['id'],
137 'toName' => $entities['contact']['display_name'],
138 'toEmail' => $entities['contact']['email'],
139 'tplParams' => self::getConfirmationTemplateParameters($entities),
140 );
141 return CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
142 }
143
144 /**
145 * Load entities related to the current payment id.
146 *
147 * This gives us all the data we need to send an email confirmation but avoiding
148 * getting anything not tested for the confirmations. We retrieve the 'full' event as
149 * it has been traditionally assigned in full.
150 *
151 * @param int $id
152 *
153 * @return array
154 * - contact = ['id' => x, 'display_name' => y, 'email' => z]
155 * - event = [.... full event details......]
156 * - contribution = ['id' => x],
157 * - payment = [payment info + payment summary info]
158 */
159 protected static function loadRelatedEntities($id) {
160 $entities = [];
161 $contributionID = (int) civicrm_api3('EntityFinancialTrxn', 'getvalue', [
162 'financial_trxn_id' => $id,
163 'entity_table' => 'civicrm_contribution',
164 'return' => 'entity_id',
165 ]);
166 $entities['contribution'] = ['id' => $contributionID];
167 $entities['payment'] = array_merge(civicrm_api3('FinancialTrxn', 'getsingle', ['id' => $id]),
168 CRM_Contribute_BAO_Contribution::getPaymentInfo($contributionID)
169 );
170
171 $contactID = self::getPaymentContactID($contributionID);
172 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
173 $entities['contact'] = ['id' => $contactID, 'display_name' => $displayName, 'email' => $email];
174 $contact = civicrm_api3('Contact', 'getsingle', ['id' => $contactID, 'return' => 'email_greeting']);
175 $entities['contact']['email_greeting'] = $contact['email_greeting_display'];
176
177 $participantRecords = civicrm_api3('ParticipantPayment', 'get', [
178 'contribution_id' => $contributionID,
179 'api.Participant.get' => ['return' => 'event_id'],
180 'sequential' => 1,
181 ])['values'];
182 if (!empty($participantRecords)) {
183 $entities['event'] = civicrm_api3('Event', 'getsingle', ['id' => $participantRecords[0]['api.Participant.get']['values'][0]['event_id']]);
184 if (!empty($entities['event']['is_show_location'])) {
185 $locationParams = [
186 'entity_id' => $entities['event']['id'],
187 'entity_table' => 'civicrm_event',
188 ];
189 $entities['location'] = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
190 }
191 }
192
193 return $entities;
194 }
195
196 /**
197 * @param int $contributionID
198 *
199 * @return int
200 */
201 public static function getPaymentContactID($contributionID) {
202 $contribution = civicrm_api3('Contribution', 'getsingle', [
203 'id' => $contributionID ,
204 'return' => ['contact_id'],
205 ]);
206 return (int) $contribution['contact_id'];
207 }
208 /**
209 * @param array $entities
210 * Related entities as an array keyed by the various entities.
211 *
212 * @return array
213 * Values required for the notification
214 * - contact_id
215 * - template_variables
216 * - event (DAO of event if relevant)
217 */
218 public static function getConfirmationTemplateParameters($entities) {
219 $templateVariables = [
220 'contactDisplayName' => $entities['contact']['display_name'],
221 'emailGreeting' => $entities['contact']['email_greeting'],
222 'totalAmount' => $entities['payment']['total'],
223 'amountOwed' => $entities['payment']['balance'],
224 'totalPaid' => $entities['payment']['paid'],
225 'paymentAmount' => $entities['payment']['total_amount'],
226 'checkNumber' => CRM_Utils_Array::value('check_number', $entities['payment']),
227 'receive_date' => $entities['payment']['trxn_date'],
228 'paidBy' => CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $entities['payment']['payment_instrument_id']),
229 'isShowLocation' => (!empty($entities['event']) ? $entities['event']['is_show_location'] : FALSE),
230 'location' => CRM_Utils_Array::value('location', $entities),
231 'event' => CRM_Utils_Array::value('event', $entities),
232 'component' => (!empty($entities['event']) ? 'event' : 'contribution'),
233 'isRefund' => $entities['payment']['total_amount'] < 0,
234 'isAmountzero' => $entities['payment']['total_amount'] === 0,
235 'refundAmount' => ($entities['payment']['total_amount'] < 0 ? $entities['payment']['total_amount'] : NULL),
236 'paymentsComplete' => ($entities['payment']['balance'] == 0),
237 ];
238
239 return self::filterUntestedTemplateVariables($templateVariables);
240 }
241
242 /**
243 * Filter out any untested variables.
244 *
245 * This just serves to highlight if any variables are added without a unit test also being added.
246 *
247 * (if hit then add a unit test for the param & add to this array).
248 *
249 * @param array $params
250 *
251 * @return array
252 */
253 public static function filterUntestedTemplateVariables($params) {
254 $testedTemplateVariables = [
255 'contactDisplayName',
256 'totalAmount',
257 'amountOwed',
258 'paymentAmount',
259 'event',
260 'component',
261 'checkNumber',
262 'receive_date',
263 'paidBy',
264 'isShowLocation',
265 'location',
266 'isRefund',
267 'isAmountzero',
268 'refundAmount',
269 'totalPaid',
270 'paymentsComplete',
271 'emailGreeting'
272 ];
273 // These are assigned by the payment form - they still 'get through' from the
274 // form for now without being in here but we should ideally load
275 // and assign. Note we should update the tpl to use {if $billingName}
276 // and ditch contributeMode - although it might need to be deprecated rather than removed.
277 $todoParams = [
278 'contributeMode',
279 'billingName',
280 'address',
281 'credit_card_type',
282 'credit_card_number',
283 'credit_card_exp_date',
284 ];
285 $filteredParams = [];
286 foreach ($testedTemplateVariables as $templateVariable) {
287 // This will cause an a-notice if any are NOT set - by design. Ensuring
288 // they are set prevents leakage.
289 $filteredParams[$templateVariable] = $params[$templateVariable];
290 }
291 return $filteredParams;
292 }
293
294 /**
295 * @param $contributionId
296 * @param $trxnData
297 * @param $updateStatus
298 * - deprecate this param
299 *
300 * @todo - make this protected once recordAdditionalPayment no longer calls it.
301 *
302 * @return CRM_Financial_DAO_FinancialTrxn
303 */
304 public static function recordRefundPayment($contributionId, $trxnData, $updateStatus) {
305 list($contributionDAO, $params) = self::getContributionAndParamsInFormatForRecordFinancialTransaction($contributionId);
306
307 $params['payment_instrument_id'] = CRM_Utils_Array::value('payment_instrument_id', $trxnData, CRM_Utils_Array::value('payment_instrument_id', $params));
308
309 $paidStatus = CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Paid');
310 $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is');
311 $completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
312
313 $trxnData['total_amount'] = $trxnData['net_amount'] = -$trxnData['total_amount'];
314 $trxnData['from_financial_account_id'] = $arAccountId;
315 $trxnData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');
316 // record the entry
317 $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnData);
318
319 // note : not using the self::add method,
320 // the reason because it performs 'status change' related code execution for financial records
321 // which in 'Pending Refund' => 'Completed' is not useful, instead specific financial record updates
322 // are coded below i.e. just updating financial_item status to 'Paid'
323 if ($updateStatus) {
324 CRM_Core_DAO::setFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'contribution_status_id', $completedStatusId);
325 }
326 // add financial item entry
327 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionDAO->id);
328 if (!empty($lineItems)) {
329 foreach ($lineItems as $lineItemId => $lineItemValue) {
330 // don't record financial item for cancelled line-item
331 if ($lineItemValue['qty'] == 0) {
332 continue;
333 }
334 $paid = $lineItemValue['line_total'] * ($financialTrxn->total_amount / $contributionDAO->total_amount);
335 $addFinancialEntry = [
336 'transaction_date' => $financialTrxn->trxn_date,
337 'contact_id' => $contributionDAO->contact_id,
338 'amount' => round($paid, 2),
339 'currency' => $contributionDAO->currency,
340 'status_id' => $paidStatus,
341 'entity_id' => $lineItemId,
342 'entity_table' => 'civicrm_line_item',
343 ];
344 $trxnIds = ['id' => $financialTrxn->id];
345 CRM_Financial_BAO_FinancialItem::create($addFinancialEntry, NULL, $trxnIds);
346 }
347 }
348 return $financialTrxn;
349 }
350
351 /**
352 * @param int $contributionId
353 * @param array $trxnData
354 * @param int $participantId
355 *
356 * @return \CRM_Core_BAO_FinancialTrxn
357 */
358 public static function recordPayment($contributionId, $trxnData, $participantId) {
359 list($contributionDAO, $params) = self::getContributionAndParamsInFormatForRecordFinancialTransaction($contributionId);
360
361 $trxnData['trxn_date'] = !empty($trxnData['trxn_date']) ? $trxnData['trxn_date'] : date('YmdHis');
362 $params['payment_instrument_id'] = CRM_Utils_Array::value('payment_instrument_id', $trxnData, CRM_Utils_Array::value('payment_instrument_id', $params));
363
364 $paidStatus = CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Paid');
365 $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is');
366 $completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
367
368 $params['partial_payment_total'] = $contributionDAO->total_amount;
369 $params['partial_amount_to_pay'] = $trxnData['total_amount'];
370 $trxnData['net_amount'] = !empty($trxnData['net_amount']) ? $trxnData['net_amount'] : $trxnData['total_amount'];
371 $params['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $trxnData);
372 $params['card_type_id'] = CRM_Utils_Array::value('card_type_id', $trxnData);
373 $params['check_number'] = CRM_Utils_Array::value('check_number', $trxnData);
374
375 // record the entry
376 $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnData);
377 $toFinancialAccount = $arAccountId;
378 $trxnId = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId, $contributionDAO->financial_type_id);
379 if (!empty($trxnId)) {
380 $trxnId = $trxnId['trxn_id'];
381 }
382 elseif (!empty($contributionDAO->payment_instrument_id)) {
383 $trxnId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($contributionDAO->payment_instrument_id);
384 }
385 else {
386 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
387 $queryParams = [1 => [$relationTypeId, 'Integer']];
388 $trxnId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1", $queryParams);
389 }
390
391 // update statuses
392 // criteria for updates contribution total_amount == financial_trxns of partial_payments
393 $sql = "SELECT SUM(ft.total_amount) as sum_of_payments, SUM(ft.net_amount) as net_amount_total
394 FROM civicrm_financial_trxn ft
395 LEFT JOIN civicrm_entity_financial_trxn eft
396 ON (ft.id = eft.financial_trxn_id)
397 WHERE eft.entity_table = 'civicrm_contribution'
398 AND eft.entity_id = {$contributionId}
399 AND ft.to_financial_account_id != {$toFinancialAccount}
400 AND ft.status_id = {$completedStatusId}
401 ";
402 $query = CRM_Core_DAO::executeQuery($sql);
403 $query->fetch();
404 $sumOfPayments = $query->sum_of_payments;
405
406 // update statuses
407 if ($contributionDAO->total_amount == $sumOfPayments) {
408 // update contribution status and
409 // clean cancel info (if any) if prev. contribution was updated in case of 'Refunded' => 'Completed'
410 $contributionDAO->contribution_status_id = $completedStatusId;
411 $contributionDAO->cancel_date = 'null';
412 $contributionDAO->cancel_reason = NULL;
413 $netAmount = !empty($trxnData['net_amount']) ? NULL : $trxnData['total_amount'];
414 $contributionDAO->net_amount = $query->net_amount_total + $netAmount;
415 $contributionDAO->fee_amount = $contributionDAO->total_amount - $contributionDAO->net_amount;
416 $contributionDAO->save();
417
418 //Change status of financial record too
419 $financialTrxn->status_id = $completedStatusId;
420 $financialTrxn->save();
421
422 // note : not using the self::add method,
423 // the reason because it performs 'status change' related code execution for financial records
424 // which in 'Partial Paid' => 'Completed' is not useful, instead specific financial record updates
425 // are coded below i.e. just updating financial_item status to 'Paid'
426
427 if (!$participantId) {
428 $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $contributionId, 'participant_id', 'contribution_id');
429 }
430 if ($participantId) {
431 // update participant status
432 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
433 $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
434 foreach ($ids as $val) {
435 $participantUpdate['id'] = $val;
436 $participantUpdate['status_id'] = array_search('Registered', $participantStatuses);
437 CRM_Event_BAO_Participant::add($participantUpdate);
438 }
439 }
440
441 // Remove this - completeOrder does it.
442 CRM_Contribute_BAO_Contribution::updateMembershipBasedOnCompletionOfContribution(
443 $contributionDAO,
444 $contributionId,
445 $trxnData['trxn_date']
446 );
447
448 // update financial item statuses
449 $baseTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contributionId);
450 $sqlFinancialItemUpdate = "
451 UPDATE civicrm_financial_item fi
452 LEFT JOIN civicrm_entity_financial_trxn eft
453 ON (eft.entity_id = fi.id AND eft.entity_table = 'civicrm_financial_item')
454 SET status_id = {$paidStatus}
455 WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']})
456 ";
457 CRM_Core_DAO::executeQuery($sqlFinancialItemUpdate);
458 }
459 return $financialTrxn;
460 }
461
462 /**
463 * The recordFinancialTransactions function has capricious requirements for input parameters - load them.
464 *
465 * The function needs rework but for now we need to give it what it wants.
466 *
467 * @param int $contributionId
468 *
469 * @return array
470 */
471 protected static function getContributionAndParamsInFormatForRecordFinancialTransaction($contributionId) {
472 $getInfoOf['id'] = $contributionId;
473 $defaults = [];
474 $contributionDAO = CRM_Contribute_BAO_Contribution::retrieve($getInfoOf, $defaults, CRM_Core_DAO::$_nullArray);
475
476 // build params for recording financial trxn entry
477 $params['contribution'] = $contributionDAO;
478 $params = array_merge($defaults, $params);
479 $params['skipLineItem'] = TRUE;
480 return [$contributionDAO, $params];
481 }
482
483 /**
484 * Does this payment complete the contribution
485 *
486 * @param int $contributionID
487 * @param float $paymentAmount
488 *
489 * @return bool
490 */
491 protected static function isPaymentCompletesContribution($contributionID, $paymentAmount) {
492 $outstandingBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($contributionID);
493 $cmp = bccomp($paymentAmount, $outstandingBalance, 5);
494 return ($cmp == 0 || $cmp == 1);
495 }
496
497 }