Merge branch 'master' into 5.15
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / FinancialTrxnTest.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 * Class CRM_Core_BAO_FinancialTrxnTest
30 * @group headless
31 */
32 class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 }
37
38 /**
39 * Check method create().
40 */
41 public function testCreate() {
42 $contactId = $this->individualCreate();
43 $financialTypeId = 1;
44 $this->contributionCreate(array(
45 'contact_id' => $contactId,
46 'financial_type_id' => $financialTypeId,
47 ));
48 $params = array(
49 'contribution_id' => $financialTypeId,
50 'to_financial_account_id' => 1,
51 'trxn_date' => 20091021184930,
52 'trxn_type' => 'Debit',
53 'total_amount' => 10,
54 'net_amount' => 90.00,
55 'currency' => 'USD',
56 'payment_processor' => 'Dummy',
57 'trxn_id' => 'test_01014000',
58 );
59 $FinancialTrxn = CRM_Core_BAO_FinancialTrxn::create($params);
60
61 $result = $this->assertDBNotNull('CRM_Core_BAO_FinancialTrxn', $FinancialTrxn->id,
62 'total_amount', 'id',
63 'Database check on updated financial trxn record.'
64 );
65
66 $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
67 }
68
69 /**
70 * Test getTotalPayments function.
71 */
72 public function testGetTotalPayments() {
73 $contactId = $this->individualCreate();
74
75 $params = array(
76 'contact_id' => $contactId,
77 'currency' => 'USD',
78 'financial_type_id' => 1,
79 'contribution_status_id' => 2,
80 'payment_instrument_id' => 1,
81 'source' => 'STUDENT',
82 'is_pay_later' => 1,
83 'receive_date' => '20080522000000',
84 'receipt_date' => '20080522000000',
85 'non_deductible_amount' => 0.00,
86 'total_amount' => 200.00,
87 'fee_amount' => 5,
88 'net_amount' => 195,
89 'trxn_id' => '22ereerwwe4444yy',
90 'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
91 'thankyou_date' => '20080522',
92 );
93
94 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
95 $contribution = $contribution['values'][$contribution['id']];
96
97 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
98 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
99
100 $params['id'] = $contribution['id'];
101 $params['contribution_status_id'] = 1;
102
103 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
104
105 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
106 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
107 }
108
109 /**
110 * Tests the lines of code that used to be in the getPartialPaymentTrxn fn.
111 */
112 public function testGetExPartialPaymentTrxn() {
113 $contributionTest = new CRM_Contribute_BAO_ContributionTest();
114 list($lineItems, $contribution) = $contributionTest->addParticipantWithContribution();
115 $contribution = (array) $contribution;
116 $params = [
117 'contribution_id' => $contribution['id'],
118 'total_amount' => 100.00,
119 ];
120 $this->callAPISuccess('Payment', 'create', $params);
121 $paid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($params['contribution_id']);
122 $total = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution_id'], 'total_amount');
123 $cmp = bccomp($total, $paid, 5);
124 // If paid amount is greater or equal to total amount
125 if ($cmp == 0 || $cmp == -1) {
126 civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id']));
127 }
128
129 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
130 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
131 }
132
133 /**
134 * Test for createDeferredTrxn().
135 */
136 public function testCreateDeferredTrxn() {
137 Civi::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
138 $cid = $this->individualCreate();
139 $params = array(
140 'contact_id' => $cid,
141 'receive_date' => '2016-01-20',
142 'total_amount' => 622,
143 'financial_type_id' => 4,
144 'line_items' => array(
145 array(
146 'line_item' => array(
147 array(
148 'entity_table' => 'civicrm_contribution',
149 'price_field_id' => 8,
150 'price_field_value_id' => 16,
151 'label' => 'test 1',
152 'qty' => 1,
153 'unit_price' => 100,
154 'line_total' => 100,
155 'financial_type_id' => 4,
156 ),
157 ),
158 'params' => array(),
159 ),
160 ),
161 );
162 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
163 $lineItems[1] = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
164 $lineItemId = key($lineItems[1]);
165 $lineItems[1][$lineItemId]['financial_item_id'] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_item WHERE entity_table = 'civicrm_line_item' AND entity_id = {$lineItemId}");
166 // Get financial trxns for contribution
167 $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622));
168 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('2016-01-20')));
169 $contributionObj = $this->getContributionObject($contribution['id']);
170 $contributionObj->revenue_recognition_date = date('Ymd', strtotime("+1 month"));
171 CRM_Core_BAO_FinancialTrxn::createDeferredTrxn($lineItems, $contributionObj);
172 $trxn = $this->callAPISuccess("FinancialTrxn", "get", array('total_amount' => 622, 'id' => array("NOT IN" => array($trxn['id']))));
173 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime("+1 month")));
174 }
175
176 /**
177 * Test for updateCreditCardDetails().
178 */
179 public function testUpdateCreditCardDetailsUsingContributionAPI() {
180 $cid = $this->individualCreate();
181 $params = array(
182 'contact_id' => $cid,
183 'receive_date' => '2016-01-20',
184 'total_amount' => 100,
185 'financial_type_id' => 1,
186 );
187 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
188 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
189 $financialTrxn = $this->callAPISuccessGetSingle(
190 'FinancialTrxn',
191 array(
192 'id' => $lastFinancialTrxnId['financialTrxnId'],
193 'return' => array('card_type_id', 'pan_truncation'),
194 )
195 );
196 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
197 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
198 $params = array(
199 'card_type_id' => 2,
200 'pan_truncation' => 4567,
201 'id' => $contribution['id'],
202 );
203 $this->callAPISuccess("Contribution", "create", $params);
204 $financialTrxn = $this->callAPISuccessGetSingle(
205 'FinancialTrxn',
206 array(
207 'id' => $lastFinancialTrxnId['financialTrxnId'],
208 'return' => array('card_type_id', 'pan_truncation'),
209 )
210 );
211 $this->assertEquals($financialTrxn['card_type_id'], 2);
212 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
213 }
214
215 /**
216 * Test for updateCreditCardDetails().
217 */
218 public function testUpdateCreditCardDetails() {
219 $cid = $this->individualCreate();
220 $params = array(
221 'contact_id' => $cid,
222 'receive_date' => '2016-01-20',
223 'total_amount' => 100,
224 'financial_type_id' => 1,
225 );
226 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
227 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
228 $financialTrxn = $this->callAPISuccessGetSingle(
229 'FinancialTrxn',
230 array(
231 'id' => $lastFinancialTrxnId['financialTrxnId'],
232 'return' => array('card_type_id', 'pan_truncation'),
233 )
234 );
235 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
236 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
237 CRM_Core_BAO_FinancialTrxn::updateCreditCardDetails($contribution['id'], 4567, 2);
238 $financialTrxn = $this->callAPISuccessGetSingle(
239 'FinancialTrxn',
240 array(
241 'id' => $lastFinancialTrxnId['financialTrxnId'],
242 'return' => array('card_type_id', 'pan_truncation'),
243 )
244 );
245 $this->assertEquals($financialTrxn['card_type_id'], 2);
246 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
247 }
248
249 /**
250 * Test getPartialPaymentWithType function.
251 */
252 public function testGetPartialPaymentWithType() {
253 //create the contribution that isn't paid yet
254 $contactId = $this->individualCreate();
255 $params = array(
256 'contact_id' => $contactId,
257 'currency' => 'USD',
258 'financial_type_id' => 1,
259 'contribution_status_id' => 8,
260 'payment_instrument_id' => 4,
261 'total_amount' => 300.00,
262 'fee_amount' => 0.00,
263 'net_amount' => 300.00,
264 );
265 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][7];
266 //make a payment one cent short
267 $params = array(
268 'contribution_id' => $contribution['id'],
269 'total_amount' => 299.99,
270 );
271 $this->callAPISuccess('Payment', 'create', $params);
272 //amount owed should be one cent
273 $amountOwed = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($contribution['id'], 'contribution')['amount_owed'];
274 $this->assertTrue(0.01 == $amountOwed, 'Amount does not match');
275 }
276
277 }