CRM-16189, added function to validate financial type (#8570)
authorPradeep Nayak <pradpnayak@gmail.com>
Mon, 27 Jun 2016 14:54:33 +0000 (20:24 +0530)
committercolemanw <coleman@civicrm.org>
Mon, 27 Jun 2016 14:54:33 +0000 (10:54 -0400)
* CRM-16189, added function to validate financial type

----------------------------------------
* CRM-16189: Improve support for Accrual Method bookkeeping
  https://issues.civicrm.org/jira/browse/CRM-16189

Conflicts:

CRM/Financial/BAO/FinancialAccount.php

* CRM-16189 Added unit test for ValidateFinancialType

----------------------------------------
* CRM-16189: Improve support for Accrual Method bookkeeping
  https://issues.civicrm.org/jira/browse/CRM-16189

Conflicts:

tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php

* CRM-16189, fixed stylings

----------------------------------------
* CRM-16189: Improve support for Accrual Method bookkeeping
  https://issues.civicrm.org/jira/browse/CRM-16189

Conflicts:

CRM/Financial/BAO/FinancialAccount.php

* --CRM-16189, updated code and removed unwanted checks

* CRM-16189, updated class name

----------------------------------------
* CRM-16189: Improve support for Accrual Method bookkeeping
  https://issues.civicrm.org/jira/browse/CRM-16189

* --CRM-16189, fixed array indentation

* --CRM-16189, removed conflicts

* --CRM-16189, fixed return value to use it as bool

CRM/Financial/BAO/FinancialAccount.php
tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php

index 19147b11f7726f7a80aa0add5799b67b0e199219..fb551938f8344bf5f74834b36d0843b8d8ed2e85 100644 (file)
@@ -396,4 +396,45 @@ LIMIT 1";
     return FALSE;
   }
 
+  /**
+   * Check if financial type has Deferred Revenue Account is relationship
+   * with Financial Account.
+   *
+   * @param int $financialTypeId
+   *
+   * @return bool
+   *
+   */
+  public static function validateFinancialType($financialTypeId, $entityID = NULL, $entity = NULL) {
+    if (!CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
+      return FALSE;
+    }
+    if ($entityID) {
+      $query = ' SELECT ps.extends FROM civicrm_price_set ps %3 WHERE %1.id = %2';
+      $params = array(
+        1 => array('ps', 'Text'),
+        2 => array($entityID, 'Integer'),
+      );
+      if ($entity == 'PriceField') {
+        $params[1] = array('pf', 'Text');
+        $params[3] = array(
+          ' INNER JOIN civicrm_price_field pf ON pf.price_set_id = ps.id ',
+          'Text',
+        );
+      }
+      $extends = CRM_Core_DAO::singleValueQuery($query, $params);
+      $extends = explode('\ 1', $extends);
+      if (!(in_array(CRM_Core_Component::getComponentID('CiviEvent'), $extends)
+        || in_array(CRM_Core_Component::getComponentID('CiviMember'), $extends))
+      ) {
+        return FALSE;
+      }
+    }
+    $deferredFinancialType = self::getDeferredFinancialType();
+    if (!array_key_exists($financialTypeId, $deferredFinancialType)) {
+      return TRUE;
+    }
+    return FALSE;
+  }
+
 }
index aa92d7d72bfd1f1aea318f4a3acfc9a78eaf333c..0eba0fdcaa028cc8e7565d11328194fa0d2d2236 100644 (file)
@@ -314,4 +314,23 @@ class CRM_Financial_BAO_FinancialAccountTest extends CiviUnitTestCase {
     $this->assertEquals($valid, $message, "The messages do not match");
   }
 
+  /**
+   * Test if financial type has Deferred Revenue Account is relationship with Financial Account.
+   *
+   */
+  public function testValidateFinancialType() {
+    Civi::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
+    $deferred = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType();
+    $financialTypes = CRM_Contribute_PseudoConstant::financialType();
+    foreach ($financialTypes as $key => $value) {
+      $validate = CRM_Financial_BAO_FinancialAccount::validateFinancialType($key);
+      if (array_key_exists($key, $deferred)) {
+        $this->assertFalse($validate, "This should not have thrown an error.");
+      }
+      else {
+        $this->assertEquals($validate, TRUE);
+      }
+    }
+  }
+
 }