Merge pull request #15448 from artfulrobot/replace-openflash-charts
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / FinancialTrxnTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class CRM_Core_BAO_FinancialTrxnTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035 32class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
39b959db 33
00be9182 34 public function setUp() {
6a488035
TO
35 parent::setUp();
36 }
37
38 /**
78ab0ca4 39 * Check method create().
6a488035 40 */
00be9182 41 public function testCreate() {
92915c55 42 $contactId = $this->individualCreate();
e6ff1593 43 $financialTypeId = 1;
9099cab3 44 $this->contributionCreate([
78ab0ca4 45 'contact_id' => $contactId,
46 'financial_type_id' => $financialTypeId,
9099cab3
CW
47 ]);
48 $params = [
e6ff1593 49 'contribution_id' => $financialTypeId,
6a488035
TO
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',
9099cab3 58 ];
6a488035
TO
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 }
96025800 68
3efd9c58 69 /**
76c28c8d 70 * Test getTotalPayments function.
3efd9c58 71 */
76c28c8d
DG
72 public function testGetTotalPayments() {
73 $contactId = $this->individualCreate();
3efd9c58 74
9099cab3 75 $params = [
3efd9c58
DG
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',
9099cab3 92 ];
3efd9c58 93
3ca4bd1b 94 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
95 $contribution = $contribution['values'][$contribution['id']];
3efd9c58 96
3ca4bd1b 97 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
3efd9c58 98 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
3efd9c58 99
3ca4bd1b 100 $params['id'] = $contribution['id'];
101 $params['contribution_status_id'] = 1;
3efd9c58 102
3ca4bd1b 103 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
3efd9c58 104
3ca4bd1b 105 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
3efd9c58 106 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
3efd9c58
DG
107 }
108
78c99516 109 /**
74e4c3c2 110 * Tests the lines of code that used to be in the getPartialPaymentTrxn fn.
78c99516 111 */
74e4c3c2 112 public function testGetExPartialPaymentTrxn() {
78c99516
PN
113 $contributionTest = new CRM_Contribute_BAO_ContributionTest();
114 list($lineItems, $contribution) = $contributionTest->addParticipantWithContribution();
115 $contribution = (array) $contribution;
88a20030 116 $params = [
78c99516
PN
117 'contribution_id' => $contribution['id'],
118 'total_amount' => 100.00,
88a20030 119 ];
120 $this->callAPISuccess('Payment', 'create', $params);
74e4c3c2 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);
39b959db
SL
124 // If paid amount is greater or equal to total amount
125 if ($cmp == 0 || $cmp == -1) {
9099cab3 126 civicrm_api3('Contribution', 'completetransaction', ['id' => $contribution['id']]);
74e4c3c2 127 }
78c99516 128
78c99516
PN
129 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
130 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
131 }
132
6419695f
PN
133 /**
134 * Test for createDeferredTrxn().
135 */
136 public function testCreateDeferredTrxn() {
9099cab3 137 Civi::settings()->set('contribution_invoice_settings', ['deferred_revenue_enabled' => '1']);
6419695f 138 $cid = $this->individualCreate();
9099cab3 139 $params = [
6419695f
PN
140 'contact_id' => $cid,
141 'receive_date' => '2016-01-20',
142 'total_amount' => 622,
143 'financial_type_id' => 4,
9099cab3
CW
144 'line_items' => [
145 [
146 'line_item' => [
147 [
6419695f
PN
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,
9099cab3
CW
156 ],
157 ],
158 'params' => [],
159 ],
160 ],
161 ];
3ca4bd1b 162 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
163 $lineItems[1] = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
6419695f
PN
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
9099cab3 167 $trxn = $this->callAPISuccess("FinancialTrxn", "get", ['total_amount' => 622]);
6419695f 168 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('2016-01-20')));
3ca4bd1b 169 $contributionObj = $this->getContributionObject($contribution['id']);
170 $contributionObj->revenue_recognition_date = date('Ymd', strtotime("+1 month"));
171 CRM_Core_BAO_FinancialTrxn::createDeferredTrxn($lineItems, $contributionObj);
9099cab3 172 $trxn = $this->callAPISuccess("FinancialTrxn", "get", ['total_amount' => 622, 'id' => ["NOT IN" => [$trxn['id']]]]);
6419695f
PN
173 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime("+1 month")));
174 }
175
2c4a6dc8
PN
176 /**
177 * Test for updateCreditCardDetails().
178 */
179 public function testUpdateCreditCardDetailsUsingContributionAPI() {
180 $cid = $this->individualCreate();
9099cab3 181 $params = [
2c4a6dc8
PN
182 'contact_id' => $cid,
183 'receive_date' => '2016-01-20',
184 'total_amount' => 100,
185 'financial_type_id' => 1,
9099cab3 186 ];
3ca4bd1b 187 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
188 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
2c4a6dc8
PN
189 $financialTrxn = $this->callAPISuccessGetSingle(
190 'FinancialTrxn',
9099cab3 191 [
2c4a6dc8 192 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
193 'return' => ['card_type_id', 'pan_truncation'],
194 ]
2c4a6dc8 195 );
d72b084a 196 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
2c4a6dc8 197 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
9099cab3 198 $params = [
d72b084a 199 'card_type_id' => 2,
2c4a6dc8 200 'pan_truncation' => 4567,
3ca4bd1b 201 'id' => $contribution['id'],
9099cab3 202 ];
2c4a6dc8
PN
203 $this->callAPISuccess("Contribution", "create", $params);
204 $financialTrxn = $this->callAPISuccessGetSingle(
205 'FinancialTrxn',
9099cab3 206 [
2c4a6dc8 207 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
208 'return' => ['card_type_id', 'pan_truncation'],
209 ]
2c4a6dc8 210 );
d72b084a 211 $this->assertEquals($financialTrxn['card_type_id'], 2);
2c4a6dc8
PN
212 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
213 }
214
215 /**
216 * Test for updateCreditCardDetails().
217 */
218 public function testUpdateCreditCardDetails() {
219 $cid = $this->individualCreate();
9099cab3 220 $params = [
2c4a6dc8
PN
221 'contact_id' => $cid,
222 'receive_date' => '2016-01-20',
223 'total_amount' => 100,
224 'financial_type_id' => 1,
9099cab3 225 ];
3ca4bd1b 226 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
227 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
2c4a6dc8
PN
228 $financialTrxn = $this->callAPISuccessGetSingle(
229 'FinancialTrxn',
9099cab3 230 [
2c4a6dc8 231 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
232 'return' => ['card_type_id', 'pan_truncation'],
233 ]
2c4a6dc8 234 );
d72b084a 235 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
2c4a6dc8 236 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
3ca4bd1b 237 CRM_Core_BAO_FinancialTrxn::updateCreditCardDetails($contribution['id'], 4567, 2);
2c4a6dc8
PN
238 $financialTrxn = $this->callAPISuccessGetSingle(
239 'FinancialTrxn',
9099cab3 240 [
2c4a6dc8 241 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
242 'return' => ['card_type_id', 'pan_truncation'],
243 ]
2c4a6dc8 244 );
d72b084a 245 $this->assertEquals($financialTrxn['card_type_id'], 2);
2c4a6dc8
PN
246 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
247 }
248
c10c4749
EL
249 /**
250 * Test getPartialPaymentWithType function.
251 */
252 public function testGetPartialPaymentWithType() {
253 //create the contribution that isn't paid yet
254 $contactId = $this->individualCreate();
9099cab3 255 $params = [
c10c4749
EL
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,
9099cab3 264 ];
c10c4749
EL
265 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][7];
266 //make a payment one cent short
9099cab3 267 $params = [
c10c4749
EL
268 'contribution_id' => $contribution['id'],
269 'total_amount' => 299.99,
9099cab3 270 ];
c10c4749
EL
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
6a488035 277}