Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / FinancialTrxnTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
e9479dcf
EM
12/**
13 * Class CRM_Core_BAO_FinancialTrxnTest
acb109b7 14 * @group headless
e9479dcf 15 */
6a488035 16class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
39b959db 17
00be9182 18 public function setUp() {
6a488035
TO
19 parent::setUp();
20 }
21
22 /**
78ab0ca4 23 * Check method create().
f150189b 24 *
25 * @throws \CRM_Core_Exception
6a488035 26 */
00be9182 27 public function testCreate() {
92915c55 28 $contactId = $this->individualCreate();
e6ff1593 29 $financialTypeId = 1;
9099cab3 30 $this->contributionCreate([
78ab0ca4 31 'contact_id' => $contactId,
32 'financial_type_id' => $financialTypeId,
9099cab3
CW
33 ]);
34 $params = [
e6ff1593 35 'contribution_id' => $financialTypeId,
6a488035
TO
36 'to_financial_account_id' => 1,
37 'trxn_date' => 20091021184930,
38 'trxn_type' => 'Debit',
39 'total_amount' => 10,
40 'net_amount' => 90.00,
41 'currency' => 'USD',
42 'payment_processor' => 'Dummy',
43 'trxn_id' => 'test_01014000',
9099cab3 44 ];
6a488035
TO
45 $FinancialTrxn = CRM_Core_BAO_FinancialTrxn::create($params);
46
47 $result = $this->assertDBNotNull('CRM_Core_BAO_FinancialTrxn', $FinancialTrxn->id,
48 'total_amount', 'id',
49 'Database check on updated financial trxn record.'
50 );
51
52 $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
53 }
96025800 54
3efd9c58 55 /**
76c28c8d 56 * Test getTotalPayments function.
f150189b 57 *
58 * @throws \CRM_Core_Exception
3efd9c58 59 */
76c28c8d
DG
60 public function testGetTotalPayments() {
61 $contactId = $this->individualCreate();
3efd9c58 62
9099cab3 63 $params = [
3efd9c58
DG
64 'contact_id' => $contactId,
65 'currency' => 'USD',
66 'financial_type_id' => 1,
67 'contribution_status_id' => 2,
68 'payment_instrument_id' => 1,
69 'source' => 'STUDENT',
70 'is_pay_later' => 1,
71 'receive_date' => '20080522000000',
72 'receipt_date' => '20080522000000',
73 'non_deductible_amount' => 0.00,
74 'total_amount' => 200.00,
75 'fee_amount' => 5,
76 'net_amount' => 195,
77 'trxn_id' => '22ereerwwe4444yy',
78 'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
79 'thankyou_date' => '20080522',
9099cab3 80 ];
3efd9c58 81
3ca4bd1b 82 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
83 $contribution = $contribution['values'][$contribution['id']];
3efd9c58 84
3ca4bd1b 85 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
3efd9c58 86 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
3efd9c58 87
3ca4bd1b 88 $params['id'] = $contribution['id'];
89 $params['contribution_status_id'] = 1;
3efd9c58 90
3ca4bd1b 91 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
3efd9c58 92
3ca4bd1b 93 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
3efd9c58 94 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
3efd9c58
DG
95 }
96
78c99516 97 /**
74e4c3c2 98 * Tests the lines of code that used to be in the getPartialPaymentTrxn fn.
f150189b 99 *
100 * @throws \CRM_Core_Exception
78c99516 101 */
f150189b 102 public function testGetTotalPaymentsParticipantOrder() {
103 $orderID = $this->createPartiallyPaidParticipantOrder()['id'];
88a20030 104 $params = [
f150189b 105 'contribution_id' => $orderID,
78c99516 106 'total_amount' => 100.00,
88a20030 107 ];
108 $this->callAPISuccess('Payment', 'create', $params);
f150189b 109 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($orderID);
78c99516
PN
110 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
111 }
112
6419695f
PN
113 /**
114 * Test for createDeferredTrxn().
f150189b 115 *
116 * @throws \CRM_Core_Exception
6419695f
PN
117 */
118 public function testCreateDeferredTrxn() {
fc7106d2 119 Civi::settings()->set('deferred_revenue_enabled', TRUE);
6419695f 120 $cid = $this->individualCreate();
9099cab3 121 $params = [
6419695f
PN
122 'contact_id' => $cid,
123 'receive_date' => '2016-01-20',
124 'total_amount' => 622,
125 'financial_type_id' => 4,
f150189b 126 'contribution_status_id' => 'Pending',
127 'api.Payment.create' => ['total_amount' => 622, 'trxn_date' => '2016-01-20'],
9099cab3 128 ];
f150189b 129 $contribution = $this->callAPISuccess('Order', 'create', $params);
3ca4bd1b 130 $lineItems[1] = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
6419695f
PN
131 $lineItemId = key($lineItems[1]);
132 $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}");
f150189b 133
134 $trxn = $this->callAPISuccess('FinancialTrxn', 'get', ['total_amount' => 622]);
135 $this->assertEquals(date('Ymd', strtotime('2016-01-20')), date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])));
136
3ca4bd1b 137 $contributionObj = $this->getContributionObject($contribution['id']);
f150189b 138 $contributionObj->revenue_recognition_date = date('Ymd', strtotime('+1 month'));
3ca4bd1b 139 CRM_Core_BAO_FinancialTrxn::createDeferredTrxn($lineItems, $contributionObj);
f150189b 140 $trxn = $this->callAPISuccess('FinancialTrxn', 'get', ['total_amount' => 622, 'id' => ['NOT IN' => [$trxn['id']]]]);
141
142 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('+1 month')));
6419695f
PN
143 }
144
2c4a6dc8
PN
145 /**
146 * Test for updateCreditCardDetails().
f150189b 147 *
148 * @throws \CRM_Core_Exception
2c4a6dc8
PN
149 */
150 public function testUpdateCreditCardDetailsUsingContributionAPI() {
151 $cid = $this->individualCreate();
9099cab3 152 $params = [
2c4a6dc8
PN
153 'contact_id' => $cid,
154 'receive_date' => '2016-01-20',
155 'total_amount' => 100,
156 'financial_type_id' => 1,
9099cab3 157 ];
3ca4bd1b 158 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
159 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
2c4a6dc8
PN
160 $financialTrxn = $this->callAPISuccessGetSingle(
161 'FinancialTrxn',
9099cab3 162 [
2c4a6dc8 163 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
164 'return' => ['card_type_id', 'pan_truncation'],
165 ]
2c4a6dc8 166 );
d72b084a 167 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
2c4a6dc8 168 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
9099cab3 169 $params = [
d72b084a 170 'card_type_id' => 2,
2c4a6dc8 171 'pan_truncation' => 4567,
3ca4bd1b 172 'id' => $contribution['id'],
9099cab3 173 ];
f150189b 174 $this->callAPISuccess('Contribution', 'create', $params);
2c4a6dc8
PN
175 $financialTrxn = $this->callAPISuccessGetSingle(
176 'FinancialTrxn',
9099cab3 177 [
2c4a6dc8 178 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
179 'return' => ['card_type_id', 'pan_truncation'],
180 ]
2c4a6dc8 181 );
d72b084a 182 $this->assertEquals($financialTrxn['card_type_id'], 2);
2c4a6dc8
PN
183 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
184 }
185
186 /**
187 * Test for updateCreditCardDetails().
f150189b 188 *
189 * @throws \CRM_Core_Exception
2c4a6dc8
PN
190 */
191 public function testUpdateCreditCardDetails() {
192 $cid = $this->individualCreate();
9099cab3 193 $params = [
2c4a6dc8
PN
194 'contact_id' => $cid,
195 'receive_date' => '2016-01-20',
196 'total_amount' => 100,
197 'financial_type_id' => 1,
9099cab3 198 ];
3ca4bd1b 199 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
200 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
2c4a6dc8
PN
201 $financialTrxn = $this->callAPISuccessGetSingle(
202 'FinancialTrxn',
9099cab3 203 [
2c4a6dc8 204 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
205 'return' => ['card_type_id', 'pan_truncation'],
206 ]
2c4a6dc8 207 );
d72b084a 208 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
2c4a6dc8 209 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
3ca4bd1b 210 CRM_Core_BAO_FinancialTrxn::updateCreditCardDetails($contribution['id'], 4567, 2);
2c4a6dc8
PN
211 $financialTrxn = $this->callAPISuccessGetSingle(
212 'FinancialTrxn',
9099cab3 213 [
2c4a6dc8 214 'id' => $lastFinancialTrxnId['financialTrxnId'],
9099cab3
CW
215 'return' => ['card_type_id', 'pan_truncation'],
216 ]
2c4a6dc8 217 );
d72b084a 218 $this->assertEquals($financialTrxn['card_type_id'], 2);
2c4a6dc8
PN
219 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
220 }
221
c10c4749 222 /**
a168b222 223 * Test testGetContributionBalance function.
f150189b 224 *
225 * @throws \CRM_Core_Exception
c10c4749 226 */
a168b222 227 public function testGetContributionBalance() {
c10c4749
EL
228 //create the contribution that isn't paid yet
229 $contactId = $this->individualCreate();
9099cab3 230 $params = [
c10c4749
EL
231 'contact_id' => $contactId,
232 'currency' => 'USD',
233 'financial_type_id' => 1,
5bb1a08a 234 'contribution_status_id' => 'Pending',
c10c4749
EL
235 'payment_instrument_id' => 4,
236 'total_amount' => 300.00,
237 'fee_amount' => 0.00,
238 'net_amount' => 300.00,
9099cab3 239 ];
9d0b81fc 240 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
c10c4749 241 //make a payment one cent short
9099cab3 242 $params = [
c10c4749
EL
243 'contribution_id' => $contribution['id'],
244 'total_amount' => 299.99,
9099cab3 245 ];
c10c4749
EL
246 $this->callAPISuccess('Payment', 'create', $params);
247 //amount owed should be one cent
a168b222 248 $amountOwed = CRM_Contribute_BAO_Contribution::getContributionBalance($contribution['id']);
f150189b 249 $this->assertEquals(0.01, $amountOwed, 'Amount does not match');
c10c4749
EL
250 }
251
6a488035 252}