Create pseudo bao CRM_Financial_BAO_Payment & move create function to it.
[civicrm-core.git] / CRM / Financial / BAO / Payment.php
CommitLineData
0c9b306a 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 */
37class 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 if ($contributionStatus != 'Partially paid'
59 && !($contributionStatus == 'Pending' && $contribution['is_pay_later'] == TRUE)
60 ) {
61 throw new API_Exception('Please select a contribution which has a partial or pending payment');
62 }
63 else {
64 // Check if pending contribution
65 $fullyPaidPayLater = FALSE;
66 if ($contributionStatus == 'Pending') {
67 $cmp = bccomp($contribution['total_amount'], $params['total_amount'], 5);
68 // Total payment amount is the whole amount paid against pending contribution
69 if ($cmp == 0 || $cmp == -1) {
70 civicrm_api3('Contribution', 'completetransaction', ['id' => $contribution['id']]);
71 // Get the trxn
72 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
73 $ftParams = ['id' => $trxnId['financialTrxnId']];
74 $trxn = CRM_Core_BAO_FinancialTrxn::retrieve($ftParams, CRM_Core_DAO::$_nullArray);
75 $fullyPaidPayLater = TRUE;
76 }
77 else {
78 civicrm_api3('Contribution', 'create',
79 [
80 'id' => $contribution['id'],
81 'contribution_status_id' => 'Partially paid',
82 ]
83 );
84 }
85 }
86 if (!$fullyPaidPayLater) {
87 $trxn = CRM_Core_BAO_FinancialTrxn::getPartialPaymentTrxn($contribution, $params);
88 if (CRM_Utils_Array::value('line_item', $params) && !empty($trxn)) {
89 foreach ($params['line_item'] as $values) {
90 foreach ($values as $id => $amount) {
91 $p = ['id' => $id];
92 $check = CRM_Price_BAO_LineItem::retrieve($p, $defaults);
93 if (empty($check)) {
94 throw new API_Exception('Please specify a valid Line Item.');
95 }
96 // get financial item
97 $sql = "SELECT fi.id
98 FROM civicrm_financial_item fi
99 INNER JOIN civicrm_line_item li ON li.id = fi.entity_id and fi.entity_table = 'civicrm_line_item'
100 WHERE li.contribution_id = %1 AND li.id = %2";
101 $sqlParams = [
102 1 => [$params['contribution_id'], 'Integer'],
103 2 => [$id, 'Integer'],
104 ];
105 $fid = CRM_Core_DAO::singleValueQuery($sql, $sqlParams);
106 // Record Entity Financial Trxn
107 $eftParams = [
108 'entity_table' => 'civicrm_financial_item',
109 'financial_trxn_id' => $trxn->id,
110 'amount' => $amount,
111 'entity_id' => $fid,
112 ];
113 civicrm_api3('EntityFinancialTrxn', 'create', $eftParams);
114 }
115 }
116 }
117 elseif (!empty($trxn)) {
118 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution['total_amount']);
119 }
120 }
121 }
122 return $trxn;
123 }
124
125}