From fd633b30c12872e4ba29d75424dd4a614425dc48 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 6 Jul 2016 03:15:54 +0530 Subject: [PATCH] =?utf8?q?[ready-for-core-team-review]CRM-16189,=20added?= =?utf8?q?=20function=20to=20retrieve=20all=20financial=20account=20with?= =?utf8?q?=20Defe=E2=80=A6=20(#8655)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * CRM-16189, added function to retrieve all financial account with Deferred revenue relationship ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Added unit test for getAllDeferredFinancialAccount function ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Fixed jenkins errors ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 --- CRM/Financial/BAO/FinancialAccount.php | 21 +++++ .../Financial/BAO/FinancialAccountTest.php | 83 ++++++++++++------- 2 files changed, 75 insertions(+), 29 deletions(-) diff --git a/CRM/Financial/BAO/FinancialAccount.php b/CRM/Financial/BAO/FinancialAccount.php index b650fa8d5f..894f5e9937 100644 --- a/CRM/Financial/BAO/FinancialAccount.php +++ b/CRM/Financial/BAO/FinancialAccount.php @@ -497,4 +497,25 @@ In other words, please create deferred revenue accounts at Administer > CiviCont return NULL; } + /** + * Retrieve all Deferred Financial Accounts. + * + * + * @return array of Deferred Financial Account + * + */ + public static function getAllDeferredFinancialAccount() { + $query = "SELECT cfa.id, cfa.name FROM civicrm_entity_financial_account ce +INNER JOIN civicrm_financial_account cfa ON ce.financial_account_id = cfa.id +WHERE `entity_table` = 'civicrm_financial_type' AND cfa.is_active = 1 AND ce.account_relationship = %1 GROUP BY cfa.id"; + $deferredAccountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' ")); + $queryParams = array(1 => array($deferredAccountRel, 'Integer')); + $dao = CRM_Core_DAO::executeQuery($query, $queryParams); + $financialAccount = array(); + while ($dao->fetch()) { + $financialAccount[$dao->id] = $dao->name; + } + return $financialAccount; + } + } diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php index 47fa9a5280..1a987f1e95 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php @@ -218,35 +218,7 @@ class CRM_Financial_BAO_FinancialAccountTest extends CiviUnitTestCase { * Test getting deferred financial type. */ public function testGetDeferredFinancialType() { - $params = array( - 'name' => 'TestFinancialAccount_1', - 'accounting_code' => 4800, - 'contact_id' => 1, - 'is_deductible' => 0, - 'is_active' => 1, - 'is_reserved' => 0, - ); - - $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', $params); - $params['name'] = 'test_financialType1'; - $financialType = $this->callAPISuccess('FinancialType', 'create', $params); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' ")); - $financialParams = array( - 'entity_table' => 'civicrm_financial_type', - 'entity_id' => $financialType['id'], - 'account_relationship' => $relationTypeId, - 'financial_account_id' => $financialAccount['id'], - ); - - $this->callAPISuccess('EntityFinancialAccount', 'create', $financialParams); - $result = $this->assertDBNotNull( - 'CRM_Financial_DAO_EntityFinancialAccount', - $financialAccount['id'], - 'entity_id', - 'financial_account_id', - 'Database check on added financial type record.' - ); - $this->assertEquals($result, $financialType['id'], 'Verify Account Type'); + $result = $this->_createDeferredFinancialAccount(); $financialTypes = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType(); $this->assertTrue(array_key_exists($result, $financialTypes), "The financial type created does not have a deferred account relationship"); } @@ -376,4 +348,57 @@ class CRM_Financial_BAO_FinancialAccountTest extends CiviUnitTestCase { $this->membershipTypeDelete(array('id' => $membershipType->id)); } + /** + * Test testGetAllDeferredFinancialAccount. + */ + public function testGetAllDeferredFinancialAccount() { + $financialAccount = CRM_Financial_BAO_FinancialAccount::getAllDeferredFinancialAccount(); + // The two deferred financial accounts which are created by default. + $expected = array( + "Deferred Revenue - Event Fee", + "Deferred Revenue - Member Dues", + ); + $this->assertEquals(array_count_values($expected), array_count_values($financialAccount), "The two arrays are not the same"); + $this->_createDeferredFinancialAccount(); + $financialAccount = CRM_Financial_BAO_FinancialAccount::getAllDeferredFinancialAccount(); + $expected[] = "TestFinancialAccount_1"; + $this->assertEquals(array_count_values($expected), array_count_values($financialAccount), "The two arrays are not the same"); + } + + /** + * Helper function to create deferred financial account. + */ + public function _createDeferredFinancialAccount() { + $params = array( + 'name' => 'TestFinancialAccount_1', + 'accounting_code' => 4800, + 'contact_id' => 1, + 'is_deductible' => 0, + 'is_active' => 1, + 'is_reserved' => 0, + ); + + $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', $params); + $params['name'] = 'test_financialType1'; + $financialType = $this->callAPISuccess('FinancialType', 'create', $params); + $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' ")); + $financialParams = array( + 'entity_table' => 'civicrm_financial_type', + 'entity_id' => $financialType['id'], + 'account_relationship' => $relationTypeId, + 'financial_account_id' => $financialAccount['id'], + ); + + $this->callAPISuccess('EntityFinancialAccount', 'create', $financialParams); + $result = $this->assertDBNotNull( + 'CRM_Financial_DAO_EntityFinancialAccount', + $financialAccount['id'], + 'entity_id', + 'financial_account_id', + 'Database check on added financial type record.' + ); + $this->assertEquals($result, $financialType['id'], 'Verify Account Type'); + return $result; + } + } -- 2.25.1