Merge pull request #14249 from yashodha/959_dev
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialItemTest.php
... / ...
CommitLineData
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_Financial_BAO_FinancialItemTest
30 * @group headless
31 */
32class CRM_Financial_BAO_FinancialItemTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 }
37
38 /**
39 * Clean up after each test.
40 */
41 public function tearDown() {
42 $this->quickCleanUpFinancialEntities();
43 parent::tearDown();
44 }
45
46 /**
47 * Check method add()
48 */
49 public function testAdd() {
50 $params = [
51 'first_name' => 'Shane',
52 'last_name' => 'Whatson',
53 'contact_type' => 'Individual',
54 ];
55
56 $contact = $this->callAPISuccess('Contact', 'create', $params);
57
58 $price = 100;
59 $cParams = [
60 'contact_id' => $contact['id'],
61 'total_amount' => $price,
62 'financial_type_id' => 1,
63 'is_active' => 1,
64 'skipLineItem' => 1,
65 ];
66
67 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
68 $lParams = array(
69 'entity_id' => $contribution['id'],
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);
81 $contributionObj = $this->getContributionObject($contribution['id']);
82
83 CRM_Financial_BAO_FinancialItem::add($lineItem, $contributionObj);
84 $result = $this->assertDBNotNull(
85 'CRM_Financial_DAO_FinancialItem',
86 $lineItem->id,
87 'amount',
88 'entity_id',
89 'Database check on added financial item record.'
90 );
91 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
92 }
93
94 /**
95 * Check method retrive()
96 */
97 public function testRetrieve() {
98 $params = [
99 'first_name' => 'Shane',
100 'last_name' => 'Whatson',
101 'contact_type' => 'Individual',
102 ];
103
104 $contact = $this->callAPISuccess('Contact', 'create', $params);
105 $price = 100.00;
106 $cParams = [
107 'contact_id' => $contact['id'],
108 'total_amount' => $price,
109 'financial_type_id' => 1,
110 'is_active' => 1,
111 'skipLineItem' => 1,
112 ];
113
114 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
115 $lParams = array(
116 'entity_id' => $contribution['id'],
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
127 $contributionObj = $this->getContributionObject($contribution['id']);
128 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
129 CRM_Financial_BAO_FinancialItem::add($lineItem, $contributionObj);
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);
136 $this->assertEquals($financialItem->amount, $price, 'Verify financial item amount.');
137 }
138
139 /**
140 * Check method create()
141 */
142 public function testCreate() {
143 $params = [
144 'first_name' => 'Shane',
145 'last_name' => 'Whatson',
146 'contact_type' => 'Individual',
147 ];
148
149 $contact = $this->callAPISuccess('Contact', 'create', $params);
150 $price = 100.00;
151 $cParams = array(
152 'contact_id' => $contact['id'],
153 'total_amount' => $price,
154 'financial_type_id' => 1,
155 'is_active' => 1,
156 'skipLineItem' => 1,
157 );
158
159 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
160 $lParams = array(
161 'entity_id' => $contribution['id'],
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(
174 'contact_id' => $contact['id'],
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';
187 $entityTrxn->entity_id = $contribution['id'];
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',
195 $lineItem->id,
196 'amount',
197 'entity_id',
198 'Database check on added financial item record.'
199 );
200
201 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
202 $entityResult = $this->assertDBNotNull(
203 'CRM_Financial_DAO_EntityFinancialTrxn',
204 $entityId,
205 'amount',
206 'entity_id',
207 'Database check on added entity financial trxn record.'
208 );
209 $this->assertEquals($entityResult, $price, 'Verify Amount for Financial Item');
210 }
211
212 /**
213 * Check method del()
214 */
215 public function testCreateEntityTrxn() {
216 $fParams = array(
217 'name' => 'Donations' . substr(sha1(rand()), 0, 7),
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',
239 $financialTrxn->id,
240 'amount',
241 'financial_trxn_id',
242 'Database check on added entity financial trxn record.'
243 );
244 $this->assertEquals($entityResult, $amount, 'Verify Amount for Financial Item');
245 return $entityTrxn;
246 }
247
248 /**
249 * Check method retrieveEntityFinancialTrxn()
250 */
251 public function testRetrieveEntityFinancialTrxn() {
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 );
268 $this->assertEquals($entityResult, $entityTrxn->amount, 'Verify Amount for Financial Item');
269 }
270
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
292 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
293
294 $params = array(
295 'id' => $contribution['id'],
296 'total_amount' => 300.00,
297 );
298
299 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
300 $financialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
301 $params = array('id' => $financialItem['id']);
302 $financialItem = $this->callAPISuccess('FinancialItem', 'get', $params);
303 $this->assertEquals(200.00, $financialItem['values'][$financialItem['id']]['amount'], "The amounts do not match.");
304 }
305
306 /**
307 * Check method getPreviousFinancialItem() with tax entry.
308 *
309 * @param string $thousandSeparator
310 * punctuation used to refer to thousands.
311 *
312 * @dataProvider getThousandSeparators
313 */
314 public function testGetPreviousFinancialItemHavingTax($thousandSeparator) {
315 $this->setCurrencySeparators($thousandSeparator);
316 $contactId = $this->individualCreate();
317 $this->enableTaxAndInvoicing();
318 $this->relationForFinancialTypeWithFinancialAccount(1);
319 $form = new CRM_Contribute_Form_Contribution();
320 $form->testSubmit(array(
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);
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
353}