3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class CRM_Financial_BAO_FinancialItemTest
32 class CRM_Financial_BAO_FinancialItemTest
extends CiviUnitTestCase
{
34 public function setUp() {
39 * Clean up after each test.
41 public function tearDown() {
42 $this->quickCleanUpFinancialEntities();
49 public function testAdd() {
51 'first_name' => 'Shane',
52 'last_name' => 'Whatson',
53 'contact_type' => 'Individual',
56 $contact = $this->callAPISuccess('Contact', 'create', $params);
60 'contact_id' => $contact['id'],
61 'total_amount' => $price,
62 'financial_type_id' => 1,
67 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
69 'entity_id' => $contribution['id'],
70 'entity_table' => 'civicrm_contribution',
71 'price_field_id' => 1,
73 'label' => 'Contribution Amount',
74 'unit_price' => $price,
75 'line_total' => $price,
76 'price_field_value_id' => 1,
77 'financial_type_id' => 1,
80 $lineItem = CRM_Price_BAO_LineItem
::create($lParams);
81 $contributionObj = $this->getContributionObject($contribution['id']);
83 CRM_Financial_BAO_FinancialItem
::add($lineItem, $contributionObj);
84 $result = $this->assertDBNotNull(
85 'CRM_Financial_DAO_FinancialItem',
89 'Database check on added financial item record.'
91 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
95 * Check method retrive()
97 public function testRetrieve() {
99 'first_name' => 'Shane',
100 'last_name' => 'Whatson',
101 'contact_type' => 'Individual',
104 $contact = $this->callAPISuccess('Contact', 'create', $params);
107 'contact_id' => $contact['id'],
108 'total_amount' => $price,
109 'financial_type_id' => 1,
114 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
116 'entity_id' => $contribution['id'],
117 'entity_table' => 'civicrm_contribution',
118 'price_field_id' => 1,
120 'label' => 'Contribution Amount',
121 'unit_price' => $price,
122 'line_total' => $price,
123 'price_field_value_id' => 1,
124 'financial_type_id' => 1,
127 $contributionObj = $this->getContributionObject($contribution['id']);
128 $lineItem = CRM_Price_BAO_LineItem
::create($lParams);
129 CRM_Financial_BAO_FinancialItem
::add($lineItem, $contributionObj);
132 'entity_id' => $lineItem->id
,
133 'entity_table' => 'civicrm_line_item',
135 $financialItem = CRM_Financial_BAO_FinancialItem
::retrieve($fParams, $values);
136 $this->assertEquals($financialItem->amount
, $price, 'Verify financial item amount.');
140 * Check method create()
142 public function testCreate() {
144 'first_name' => 'Shane',
145 'last_name' => 'Whatson',
146 'contact_type' => 'Individual',
149 $contact = $this->callAPISuccess('Contact', 'create', $params);
152 'contact_id' => $contact['id'],
153 'total_amount' => $price,
154 'financial_type_id' => 1,
159 $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
161 'entity_id' => $contribution['id'],
162 'entity_table' => 'civicrm_contribution',
163 'price_field_id' => 1,
165 'label' => 'Contribution Amount',
166 'unit_price' => $price,
167 'line_total' => $price,
168 'price_field_value_id' => 1,
169 'financial_type_id' => 1,
172 $lineItem = CRM_Price_BAO_LineItem
::create($lParams);
174 'contact_id' => $contact['id'],
175 'description' => 'Contribution Amount',
177 'financial_account_id' => 1,
179 'transaction_date' => date('YmdHis'),
180 'entity_id' => $lineItem->id
,
181 'entity_table' => 'civicrm_line_item',
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
;
193 $result = $this->assertDBNotNull(
194 'CRM_Financial_DAO_FinancialItem',
198 'Database check on added financial item record.'
201 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
202 $entityResult = $this->assertDBNotNull(
203 'CRM_Financial_DAO_EntityFinancialTrxn',
207 'Database check on added entity financial trxn record.'
209 $this->assertEquals($entityResult, $price, 'Verify Amount for Financial Item');
215 public function testCreateEntityTrxn() {
217 'name' => 'Donations' . substr(sha1(rand()), 0, 7),
218 'is_deductible' => 0,
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();
230 'entity_table' => 'civicrm_contribution',
232 'financial_trxn_id' => $financialTrxn->id
,
236 $entityTrxn = CRM_Financial_BAO_FinancialItem
::createEntityTrxn($params);
237 $entityResult = $this->assertDBNotNull(
238 'CRM_Financial_DAO_EntityFinancialTrxn',
242 'Database check on added entity financial trxn record.'
244 $this->assertEquals($entityResult, $amount, 'Verify Amount for Financial Item');
249 * Check method retrieveEntityFinancialTrxn()
251 public function testRetrieveEntityFinancialTrxn() {
252 $entityTrxn = self
::testCreateEntityTrxn();
254 'entity_table' => 'civicrm_contribution',
256 'financial_trxn_id' => $entityTrxn->financial_trxn_id
,
257 'amount' => $entityTrxn->amount
,
260 CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params);
261 $entityResult = $this->assertDBNotNull(
262 'CRM_Financial_DAO_EntityFinancialTrxn',
263 $entityTrxn->financial_trxn_id
,
266 'Database check on added entity financial trxn record.'
268 $this->assertEquals($entityResult, $entityTrxn->amount
, 'Verify Amount for Financial Item');
272 * Check method getPreviousFinancialItem().
274 public function testGetPreviousFinancialItem() {
275 $contactId = $this->individualCreate();
278 'contact_id' => $contactId,
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',
292 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
295 'id' => $contribution['id'],
296 'total_amount' => 300.00,
299 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
300 $financialItem = CRM_Financial_BAO_FinancialItem
::getPreviousFinancialItem($contribution['id']);
301 $params = ['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.");
307 * Check method getPreviousFinancialItem() with tax entry.
309 * @param string $thousandSeparator
310 * punctuation used to refer to thousands.
312 * @dataProvider getThousandSeparators
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();
321 'total_amount' => 100,
322 'financial_type_id' => 1,
323 'contact_id' => $contactId,
324 'contribution_status_id' => 1,
326 ], CRM_Core_Action
::ADD
);
327 $contribution = $this->callAPISuccessGetSingle('Contribution',
329 'contact_id' => $contactId,
333 $financialItem = CRM_Financial_BAO_FinancialItem
::getPreviousFinancialItem($contribution['id']);
335 'id' => $financialItem['id'],
340 'financial_account_id',
344 'id' => $financialItem['id'],
345 'description' => 'Contribution Amount',
347 'amount' => '100.00',
348 'financial_account_id' => '1',
350 $this->callAPISuccessGetSingle('FinancialItem', $params, $checkAgainst);