Merge pull request #14249 from yashodha/959_dev
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialItemTest.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_Financial_BAO_FinancialItemTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class CRM_Financial_BAO_FinancialItemTest extends CiviUnitTestCase {
33
00be9182 34 public function setUp() {
6a488035
TO
35 parent::setUp();
36 }
37
ffa4c203
PN
38 /**
39 * Clean up after each test.
40 */
41 public function tearDown() {
42 $this->quickCleanUpFinancialEntities();
43 parent::tearDown();
44 }
45
6a488035 46 /**
100fef9d 47 * Check method add()
6a488035 48 */
00be9182 49 public function testAdd() {
307dc80a 50 $params = [
51 'first_name' => 'Shane',
52 'last_name' => 'Whatson',
6a488035 53 'contact_type' => 'Individual',
307dc80a 54 ];
6a488035 55
307dc80a 56 $contact = $this->callAPISuccess('Contact', 'create', $params);
6a488035
TO
57
58 $price = 100;
307dc80a 59 $cParams = [
60 'contact_id' => $contact['id'],
6a488035
TO
61 'total_amount' => $price,
62 'financial_type_id' => 1,
92915c55
TO
63 'is_active' => 1,
64 'skipLineItem' => 1,
307dc80a 65 ];
6a488035 66
307dc80a 67 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
6a488035 68 $lParams = array(
307dc80a 69 'entity_id' => $contribution['id'],
6a488035
TO
70 'entity_table' => 'civicrm_contribution',
71 'price_field_id' => 1,
72 'qty' => 1,
73 'label' => 'Contribution Amount',
74 'unit_price' => $price,
75 'line_total' => $price,
76 'price_field_value_id' => 1,
77 'financial_type_id' => 1,
78 );
79
80 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
307dc80a 81 $contributionObj = $this->getContributionObject($contribution['id']);
82
83 CRM_Financial_BAO_FinancialItem::add($lineItem, $contributionObj);
6a488035
TO
84 $result = $this->assertDBNotNull(
85 'CRM_Financial_DAO_FinancialItem',
6c6e6187 86 $lineItem->id,
6a488035
TO
87 'amount',
88 'entity_id',
89 'Database check on added financial item record.'
90 );
481a74f4 91 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
6a488035
TO
92 }
93
94 /**
100fef9d 95 * Check method retrive()
6a488035 96 */
00be9182 97 public function testRetrieve() {
307dc80a 98 $params = [
99 'first_name' => 'Shane',
100 'last_name' => 'Whatson',
6a488035 101 'contact_type' => 'Individual',
307dc80a 102 ];
6a488035 103
307dc80a 104 $contact = $this->callAPISuccess('Contact', 'create', $params);
6a488035 105 $price = 100.00;
307dc80a 106 $cParams = [
107 'contact_id' => $contact['id'],
6a488035
TO
108 'total_amount' => $price,
109 'financial_type_id' => 1,
92915c55
TO
110 'is_active' => 1,
111 'skipLineItem' => 1,
307dc80a 112 ];
6a488035 113
307dc80a 114 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
6a488035 115 $lParams = array(
307dc80a 116 'entity_id' => $contribution['id'],
6a488035
TO
117 'entity_table' => 'civicrm_contribution',
118 'price_field_id' => 1,
119 'qty' => 1,
120 'label' => 'Contribution Amount',
121 'unit_price' => $price,
122 'line_total' => $price,
123 'price_field_value_id' => 1,
124 'financial_type_id' => 1,
125 );
126
307dc80a 127 $contributionObj = $this->getContributionObject($contribution['id']);
6a488035 128 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
307dc80a 129 CRM_Financial_BAO_FinancialItem::add($lineItem, $contributionObj);
6a488035
TO
130 $values = array();
131 $fParams = array(
132 'entity_id' => $lineItem->id,
133 'entity_table' => 'civicrm_line_item',
134 );
135 $financialItem = CRM_Financial_BAO_FinancialItem::retrieve($fParams, $values);
481a74f4 136 $this->assertEquals($financialItem->amount, $price, 'Verify financial item amount.');
6a488035
TO
137 }
138
139 /**
100fef9d 140 * Check method create()
6a488035 141 */
00be9182 142 public function testCreate() {
307dc80a 143 $params = [
144 'first_name' => 'Shane',
145 'last_name' => 'Whatson',
6a488035 146 'contact_type' => 'Individual',
307dc80a 147 ];
6a488035 148
307dc80a 149 $contact = $this->callAPISuccess('Contact', 'create', $params);
6a488035
TO
150 $price = 100.00;
151 $cParams = array(
307dc80a 152 'contact_id' => $contact['id'],
6a488035
TO
153 'total_amount' => $price,
154 'financial_type_id' => 1,
92915c55
TO
155 'is_active' => 1,
156 'skipLineItem' => 1,
6a488035
TO
157 );
158
307dc80a 159 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
6a488035 160 $lParams = array(
307dc80a 161 'entity_id' => $contribution['id'],
6a488035
TO
162 'entity_table' => 'civicrm_contribution',
163 'price_field_id' => 1,
164 'qty' => 1,
165 'label' => 'Contribution Amount',
166 'unit_price' => $price,
167 'line_total' => $price,
168 'price_field_value_id' => 1,
169 'financial_type_id' => 1,
170 );
171
172 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
173 $fParams = array(
307dc80a 174 'contact_id' => $contact['id'],
6a488035
TO
175 'description' => 'Contribution Amount',
176 'amount' => $price,
177 'financial_account_id' => 1,
178 'status_id' => 1,
179 'transaction_date' => date('YmdHis'),
180 'entity_id' => $lineItem->id,
181 'entity_table' => 'civicrm_line_item',
182 );
183
184 CRM_Financial_BAO_FinancialItem::create($fParams);
185 $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
186 $entityTrxn->entity_table = 'civicrm_contribution';
307dc80a 187 $entityTrxn->entity_id = $contribution['id'];
6a488035
TO
188 $entityTrxn->amount = $price;
189 if ($entityTrxn->find(TRUE)) {
190 $entityId = $entityTrxn->entity_id;
191 }
192
193 $result = $this->assertDBNotNull(
194 'CRM_Financial_DAO_FinancialItem',
6c6e6187 195 $lineItem->id,
6a488035
TO
196 'amount',
197 'entity_id',
198 'Database check on added financial item record.'
199 );
200
481a74f4 201 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
6a488035
TO
202 $entityResult = $this->assertDBNotNull(
203 'CRM_Financial_DAO_EntityFinancialTrxn',
6c6e6187 204 $entityId,
6a488035
TO
205 'amount',
206 'entity_id',
207 'Database check on added entity financial trxn record.'
208 );
481a74f4 209 $this->assertEquals($entityResult, $price, 'Verify Amount for Financial Item');
6a488035
TO
210 }
211
212 /**
100fef9d 213 * Check method del()
6a488035 214 */
00be9182 215 public function testCreateEntityTrxn() {
6a488035 216 $fParams = array(
92fcb95f 217 'name' => 'Donations' . substr(sha1(rand()), 0, 7),
6a488035
TO
218 'is_deductible' => 0,
219 'is_active' => 1,
220 );
221
222 $amount = 200;
223 $ids = array();
224 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($fParams, $ids);
225 $financialTrxn = new CRM_Financial_DAO_FinancialTrxn();
226 $financialTrxn->to_financial_account_id = $financialAccount->id;
227 $financialTrxn->total_amount = $amount;
228 $financialTrxn->save();
229 $params = array(
230 'entity_table' => 'civicrm_contribution',
231 'entity_id' => 1,
232 'financial_trxn_id' => $financialTrxn->id,
233 'amount' => $amount,
234 );
235
236 $entityTrxn = CRM_Financial_BAO_FinancialItem::createEntityTrxn($params);
237 $entityResult = $this->assertDBNotNull(
238 'CRM_Financial_DAO_EntityFinancialTrxn',
6c6e6187 239 $financialTrxn->id,
6a488035
TO
240 'amount',
241 'financial_trxn_id',
242 'Database check on added entity financial trxn record.'
243 );
481a74f4 244 $this->assertEquals($entityResult, $amount, 'Verify Amount for Financial Item');
6a488035
TO
245 return $entityTrxn;
246 }
247
248 /**
100fef9d 249 * Check method retrieveEntityFinancialTrxn()
6a488035 250 */
00be9182 251 public function testRetrieveEntityFinancialTrxn() {
6a488035
TO
252 $entityTrxn = self::testCreateEntityTrxn();
253 $params = array(
254 'entity_table' => 'civicrm_contribution',
255 'entity_id' => 1,
256 'financial_trxn_id' => $entityTrxn->financial_trxn_id,
257 'amount' => $entityTrxn->amount,
258 );
259
260 CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params);
261 $entityResult = $this->assertDBNotNull(
262 'CRM_Financial_DAO_EntityFinancialTrxn',
263 $entityTrxn->financial_trxn_id,
264 'amount',
265 'financial_trxn_id',
266 'Database check on added entity financial trxn record.'
267 );
481a74f4 268 $this->assertEquals($entityResult, $entityTrxn->amount, 'Verify Amount for Financial Item');
6a488035 269 }
96025800 270
00b4d571
PN
271 /**
272 * Check method getPreviousFinancialItem().
273 */
274 public function testGetPreviousFinancialItem() {
275 $contactId = $this->individualCreate();
276
277 $params = array(
278 'contact_id' => $contactId,
279 'currency' => 'USD',
280 'financial_type_id' => 1,
281 'contribution_status_id' => 1,
282 'payment_instrument_id' => 1,
283 'source' => 'STUDENT',
284 'receive_date' => '20160522000000',
285 'receipt_date' => '20160522000000',
286 'non_deductible_amount' => 0.00,
287 'total_amount' => 100.00,
288 'trxn_id' => '22ereerwww444444',
289 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
290 );
291
d0c97775 292 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
00b4d571
PN
293
294 $params = array(
d0c97775 295 'id' => $contribution['id'],
00b4d571
PN
296 'total_amount' => 300.00,
297 );
298
d0c97775 299 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
300 $financialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
cf28d075 301 $params = array('id' => $financialItem['id']);
00b4d571 302 $financialItem = $this->callAPISuccess('FinancialItem', 'get', $params);
cf28d075 303 $this->assertEquals(200.00, $financialItem['values'][$financialItem['id']]['amount'], "The amounts do not match.");
00b4d571
PN
304 }
305
ffa4c203
PN
306 /**
307 * Check method getPreviousFinancialItem() with tax entry.
83644f47 308 *
309 * @param string $thousandSeparator
310 * punctuation used to refer to thousands.
311 *
312 * @dataProvider getThousandSeparators
ffa4c203 313 */
83644f47 314 public function testGetPreviousFinancialItemHavingTax($thousandSeparator) {
315 $this->setCurrencySeparators($thousandSeparator);
ffa4c203
PN
316 $contactId = $this->individualCreate();
317 $this->enableTaxAndInvoicing();
318 $this->relationForFinancialTypeWithFinancialAccount(1);
319 $form = new CRM_Contribute_Form_Contribution();
320 $form->testSubmit(array(
39b959db
SL
321 'total_amount' => 100,
322 'financial_type_id' => 1,
323 'contact_id' => $contactId,
324 'contribution_status_id' => 1,
325 'price_set_id' => 0,
326 ), CRM_Core_Action::ADD);
ffa4c203
PN
327 $contribution = $this->callAPISuccessGetSingle('Contribution',
328 array(
329 'contact_id' => $contactId,
330 'return' => array('id'),
331 )
332 );
333 $financialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
334 $params = array(
335 'id' => $financialItem['id'],
336 'return' => array(
337 'description',
338 'status_id',
339 'amount',
340 'financial_account_id',
341 ),
342 );
343 $checkAgainst = array(
344 'id' => $financialItem['id'],
345 'description' => 'Contribution Amount',
346 'status_id' => '1',
347 'amount' => '100.00',
348 'financial_account_id' => '1',
349 );
350 $this->callAPISuccessGetSingle('FinancialItem', $params, $checkAgainst);
351 }
352
232624b1 353}