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