[ready-for-core-team-review]CRM-16189, added function to retrieve all financial accou...
authorPradeep Nayak <pradpnayak@gmail.com>
Tue, 5 Jul 2016 21:45:54 +0000 (03:15 +0530)
committercolemanw <coleman@civicrm.org>
Tue, 5 Jul 2016 21:45:54 +0000 (17:45 -0400)
* 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
tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php

index b650fa8d5f385dbeb72286108fd0ef2799f98723..894f5e9937c92600c3008888c05efd27fc701e25 100644 (file)
@@ -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;
+  }
+
 }
index 47fa9a5280eba105f4fa54cea79a5f8538f755b4..1a987f1e954a2058b97a5d1a3dd19de293027d9a 100644 (file)
@@ -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;
+  }
+
 }