3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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_FinancialAccountTest
32 class CRM_Financial_BAO_FinancialAccountTest
extends CiviUnitTestCase
{
34 public function setUp() {
35 $this->useTransaction(TRUE);
37 $this->organizationCreate();
43 public function testAdd() {
45 'name' => 'Donations',
50 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params, $ids);
52 $result = $this->assertDBNotNull(
53 'CRM_Financial_BAO_FinancialAccount',
54 $financialAccount->id
,
57 'Database check on updated financial type record.'
60 $this->assertEquals($result, 'Donations', 'Verify financial type name.');
64 * Check method retrive()
66 public function testRetrieve() {
68 'name' => 'Donations',
72 $ids = $defaults = array();
73 CRM_Financial_BAO_FinancialAccount
::add($params);
75 $result = CRM_Financial_BAO_FinancialAccount
::retrieve($params, $defaults);
77 $this->assertEquals($result->name
, 'Donations', 'Verify financial account name.');
81 * Check method setIsActive()
83 public function testSetIsActive() {
85 'name' => 'Donations',
90 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params, $ids);
91 $result = CRM_Financial_BAO_FinancialAccount
::setIsActive($financialAccount->id
, 0);
92 $this->assertEquals($result, TRUE, 'Verify financial account record updation for is_active.');
94 $isActive = $this->assertDBNotNull(
95 'CRM_Financial_BAO_FinancialAccount',
96 $financialAccount->id
,
99 'Database check on updated for financial account is_active.'
101 $this->assertEquals($isActive, 0, 'Verify financial account is_active.');
107 public function testdel() {
109 'name' => 'Donations',
110 'is_deductible' => 0,
114 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params, $ids);
116 CRM_Financial_BAO_FinancialAccount
::del($financialAccount->id
);
117 $params = array('id' => $financialAccount->id
);
118 $result = CRM_Financial_BAO_FinancialAccount
::retrieve($params, $defaults);
119 $this->assertEquals(empty($result), TRUE, 'Verify financial account record deletion.');
125 public function testdelIfHasContribution() {
127 'name' => 'Donation Test',
129 'is_deductible' => 1,
132 $financialType = CRM_Financial_BAO_FinancialType
::add($params);
135 'name' => 'Donation Test',
138 $result = CRM_Financial_BAO_FinancialAccount
::retrieve($params, $defaults);
140 $contactId = $this->individualCreate();
141 $contributionParams = array(
142 'total_amount' => 300,
144 'contact_id' => $contactId,
145 'financial_type_id' => $financialType->id
,
146 'contribution_status_id' => 1,
148 $this->callAPISuccess('Contribution', 'create', $contributionParams);
149 CRM_Financial_BAO_FinancialAccount
::del($result->id
);
150 $params = array('id' => $result->id
);
151 $result = CRM_Financial_BAO_FinancialAccount
::retrieve($params, $defaults);
152 $this->assertEquals(empty($result), FALSE, 'Verify financial account record deletion.');
156 * Check method getAccountingCode()
158 public function testGetAccountingCode() {
160 'name' => 'Donations',
166 $financialType = CRM_Financial_BAO_FinancialType
::add($params, $ids);
167 $financialAccountid = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Donations', 'id', 'name');
168 CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccountid, 'accounting_code', '4800');
169 $accountingCode = CRM_Financial_BAO_FinancialAccount
::getAccountingCode($financialType->id
);
170 $this->assertEquals($accountingCode, 4800, 'Verify accounting code.');
174 * Test getting financial account for a given financial Type with a particular relationship.
176 public function testGetFinancialAccountByFinancialTypeAndRelationshipBuiltIn() {
177 $this->assertEquals(2, CRM_Financial_BAO_FinancialAccount
::getFinancialAccountForFinancialTypeByRelationship(2, 'Income Account Is'));
181 * Test getting financial account for a given financial Type with a particular relationship.
183 public function testGetFinancialAccountByFinancialTypeAndRelationshipBuiltInRefunded() {
184 $this->assertEquals(2, CRM_Financial_BAO_FinancialAccount
::getFinancialAccountForFinancialTypeByRelationship(2, 'Credit/Contra Revenue Account Is'));
188 * Test getting financial account for a given financial Type with a particular relationship.
190 public function testGetFinancialAccountByFinancialTypeAndRelationshipBuiltInChargeBack() {
191 $this->assertEquals(2, CRM_Financial_BAO_FinancialAccount
::getFinancialAccountForFinancialTypeByRelationship(2, 'Chargeback Account Is'));
195 * Test getting financial account for a given financial Type with a particular relationship.
197 public function testGetFinancialAccountByFinancialTypeAndRelationshipCustomAddedRefunded() {
198 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
199 'name' => 'Refund Account',
203 $this->callAPISuccess('EntityFinancialAccount', 'create', array(
205 'entity_table' => 'civicrm_financial_type',
206 'account_relationship' => 'Credit/Contra Revenue Account is',
207 'financial_account_id' => 'Refund Account',
209 $this->assertEquals($financialAccount['id'],
210 CRM_Financial_BAO_FinancialAccount
::getFinancialAccountForFinancialTypeByRelationship(2, 'Credit/Contra Revenue Account is'));
214 * Test getting financial account relations for a given financial type.
216 public function testGetFinancialAccountRelations() {
217 $fAccounts = $rAccounts = array();
218 $relations = CRM_Financial_BAO_FinancialAccount
::getfinancialAccountRelations();
220 'Expense Account is' => 'Expenses',
221 'Accounts Receivable Account is' => 'Asset',
222 'Income Account is' => 'Revenue',
223 'Asset Account is' => 'Asset',
224 'Cost of Sales Account is' => 'Cost of Sales',
225 'Premiums Inventory Account is' => 'Asset',
226 'Discounts Account is' => 'Revenue',
227 'Sales Tax Account is' => 'Liability',
228 'Deferred Revenue Account is' => 'Liability',
230 $dao = CRM_Core_DAO
::executeQuery("SELECT ov.value, ov.name
231 FROM civicrm_option_value ov
232 INNER JOIN civicrm_option_group og ON og.id = ov.option_group_id
233 AND og.name = 'financial_account_type'");
234 while ($dao->fetch()) {
235 $fAccounts[$dao->value
] = $dao->name
;
237 $dao = CRM_Core_DAO
::executeQuery("SELECT ov.value, ov.name
238 FROM civicrm_option_value ov
239 INNER JOIN civicrm_option_group og ON og.id = ov.option_group_id
240 AND og.name = 'account_relationship'");
241 while ($dao->fetch()) {
242 $rAccounts[$dao->value
] = $dao->name
;
244 foreach ($links as $accountRelation => $accountType) {
245 $financialAccountLinks[array_search($accountRelation, $rAccounts)] = array_search($accountType, $fAccounts);
247 $this->assertTrue(($relations == $financialAccountLinks), "The two arrays are not the same");
251 * Test getting deferred financial type.
253 public function testGetDeferredFinancialType() {
254 $result = $this->_createDeferredFinancialAccount();
255 $financialTypes = CRM_Financial_BAO_FinancialAccount
::getDeferredFinancialType();
256 $this->assertTrue(array_key_exists($result, $financialTypes), "The financial type created does not have a deferred account relationship");
260 * Test getting financial account for a given financial Type with a particular relationship.
262 public function testValidateFinancialAccount() {
263 // Create a record with financial item having financial account as Event Fee.
264 $this->createParticipantWithContribution();
265 $financialAccounts = CRM_Contribute_PseudoConstant
::financialAccount();
266 $financialAccountId = array_search('Event Fee', $financialAccounts);
267 $message = CRM_Financial_BAO_FinancialAccount
::validateFinancialAccount($financialAccountId);
268 $this->assertTrue($message, "The financial account cannot be deleted. Failed asserting this was true.");
269 $financialAccountId = array_search('Member Dues', $financialAccounts);
270 $message = CRM_Financial_BAO_FinancialAccount
::validateFinancialAccount($financialAccountId);
271 $this->assertFalse($message, "The financial account can be deleted. Failed asserting this was true.");
275 * Test for validating financial type has deferred revenue account relationship.
277 public function testcheckFinancialTypeHasDeferred() {
278 Civi
::settings()->set('deferred_revenue_enabled', 1);
280 $valid = CRM_Financial_BAO_FinancialAccount
::checkFinancialTypeHasDeferred($params);
281 $this->assertFalse($valid, "This should have been false");
282 $cid = $this->individualCreate();
284 'contact_id' => $cid,
285 'receive_date' => '2016-01-20',
286 'total_amount' => 100,
287 'financial_type_id' => 4,
288 'revenue_recognition_date' => date('Ymd', strtotime("+1 month")),
289 'line_items' => array(
291 'line_item' => array(
293 'entity_table' => 'civicrm_contribution',
294 'price_field_id' => 8,
295 'price_field_value_id' => 16,
300 'financial_type_id' => 4,
303 'entity_table' => 'civicrm_contribution',
304 'price_field_id' => 8,
305 'price_field_value_id' => 17,
310 'financial_type_id' => 4,
317 CRM_Financial_BAO_FinancialAccount
::checkFinancialTypeHasDeferred($params);
319 catch (CRM_Core_Exception
$e) {
320 $this->fail("Missed expected exception");
323 'contact_id' => $cid,
324 'receive_date' => '2016-01-20',
325 'total_amount' => 100,
326 'financial_type_id' => 1,
327 'revenue_recognition_date' => date('Ymd', strtotime("+1 month")),
330 CRM_Financial_BAO_FinancialAccount
::checkFinancialTypeHasDeferred($params);
331 $this->fail("Missed expected exception");
333 catch (CRM_Core_Exception
$e) {
334 $this->assertEquals('Revenue Recognition Date cannot be processed unless there is a Deferred Revenue account setup for the Financial Type. Please remove Revenue Recognition Date, select a different Financial Type with a Deferred Revenue account setup for it, or setup a Deferred Revenue account for this Financial Type.', $e->getMessage());
339 * Test testGetAllDeferredFinancialAccount.
341 public function testGetAllDeferredFinancialAccount() {
342 $financialAccount = CRM_Financial_BAO_FinancialAccount
::getAllDeferredFinancialAccount();
343 // The two deferred financial accounts which are created by default.
345 "Deferred Revenue - Member Dues (2740)",
346 "Deferred Revenue - Event Fee (2730)",
348 $this->assertEquals(array_count_values($expected), array_count_values($financialAccount), "The two arrays are not the same");
349 $this->_createDeferredFinancialAccount();
350 $financialAccount = CRM_Financial_BAO_FinancialAccount
::getAllDeferredFinancialAccount();
351 $expected[] = "TestFinancialAccount_1 (4800)";
352 $this->assertEquals(array_count_values($expected), array_count_values($financialAccount), "The two arrays are not the same");
356 * CRM-20037: Test balance due amount, if contribution is done using deferred Financial Type
358 public function testBalanceDueIfDeferredRevenueEnabled() {
359 Civi
::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
360 $deferredFinancialTypeID = $this->_createDeferredFinancialAccount();
362 $totalAmount = 100.00;
363 $contribution = $this->callAPISuccess('Contribution', 'create', array(
364 'contact_id' => $this->individualCreate(),
365 'receive_date' => '20120511',
366 'total_amount' => $totalAmount,
367 'financial_type_id' => $deferredFinancialTypeID,
368 'non_deductible_amount' => 10.00,
369 'fee_amount' => 5.00,
370 'net_amount' => 95.00,
372 'contribution_status_id' => 1,
374 $balance = CRM_Contribute_BAO_Contribution
::getContributionBalance($contribution['id'], $totalAmount);
375 $this->assertEquals(0.0, $balance);
376 Civi
::settings()->revert('contribution_invoice_settings');
380 * Helper function to create deferred financial account.
382 public function _createDeferredFinancialAccount() {
384 'name' => 'TestFinancialAccount_1',
385 'accounting_code' => 4800,
387 'is_deductible' => 0,
392 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', $params);
393 $params['name'] = 'test_financialType1';
394 $financialType = $this->callAPISuccess('FinancialType', 'create', $params);
395 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' "));
396 $financialParams = array(
397 'entity_table' => 'civicrm_financial_type',
398 'entity_id' => $financialType['id'],
399 'account_relationship' => $relationTypeId,
400 'financial_account_id' => $financialAccount['id'],
403 $this->callAPISuccess('EntityFinancialAccount', 'create', $financialParams);
404 $result = $this->assertDBNotNull(
405 'CRM_Financial_DAO_EntityFinancialAccount',
406 $financialAccount['id'],
408 'financial_account_id',
409 'Database check on added financial type record.'
411 $this->assertEquals($result, $financialType['id'], 'Verify Account Type');