CRM-16189, added function to check if financial type has Deferred Inc… (#8565)
authorPradeep Nayak <pradpnayak@gmail.com>
Sat, 25 Jun 2016 02:48:26 +0000 (08:18 +0530)
committercolemanw <coleman@civicrm.org>
Sat, 25 Jun 2016 02:48:26 +0000 (22:48 -0400)
* CRM-16189, added function to check if financial type has Deferred Income Account is relationship

----------------------------------------
* 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 checkForValidFinancialType function

----------------------------------------
* 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, changed the function name

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

* CRM-16189, updated class name

----------------------------------------
* 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 e96c1ea619811fdace6c9ad341db655ca80b36a6..19147b11f7726f7a80aa0add5799b67b0e199219 100644 (file)
@@ -338,4 +338,62 @@ LIMIT 1";
     return FALSE;
   }
 
+  /**
+   * Validate Financial Type has Deferred Revenue account relationship
+   * with Financial Account
+   *
+   * @param array $params
+   *
+   * @param int $contributionID
+   *
+   * @param obj $form
+   *
+   * @return string
+   *
+   */
+  public static function checkFinancialTypeHasDeferred($params, $contributionID = NULL, $form = NULL) {
+    if (!CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
+      return FALSE;
+    }
+    $recognitionDate = CRM_Utils_Array::value('revenue_recognition_date', $params);
+    if (!(!CRM_Utils_System::isNull($recognitionDate)
+      || ($contributionID && $params['prevContribution']->revenue_recognition_date))
+    ) {
+      return FALSE;
+    }
+
+    $message = ts('Revenue recognition date can only be specified if the financial type selected has a deferred revenue account configured. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts');
+    $lineItems = CRM_Utils_Array::value('line_item', $params);
+    $financialTypeID = CRM_Utils_Array::value('financial_type_id', $params);
+    if (!$financialTypeID) {
+      $financialTypeID = $params['prevContribution']->financial_type_id;
+    }
+    if (($contributionID || !empty($params['price_set_id'])) && empty($lineItems)) {
+      if (!$contributionID) {
+        CRM_Price_BAO_PriceSet::processAmount($form->_priceSet['fields'],
+        $params, $items);
+      }
+      else {
+        $items = CRM_Price_BAO_LineItem::getLineItems($contributionID, 'contribution', TRUE, TRUE, TRUE);
+      }
+      if (!empty($items)) {
+        $lineItems[] = $items;
+      }
+    }
+    $deferredFinancialType = self::getDeferredFinancialType();
+    if (!empty($lineItems)) {
+      foreach ($lineItems as $lineItem) {
+        foreach ($lineItem as $items) {
+          if (!array_key_exists($items['financial_type_id'], $deferredFinancialType)) {
+            return $message;
+          }
+        }
+      }
+    }
+    elseif (!array_key_exists($financialTypeID, $deferredFinancialType)) {
+      return $message;
+    }
+    return FALSE;
+  }
+
 }
index 105d357d69d88efb30566b7cb659d678085a9b60..aa92d7d72bfd1f1aea318f4a3acfc9a78eaf333c 100644 (file)
@@ -266,4 +266,52 @@ class CRM_Financial_BAO_FinancialAccountTest extends CiviUnitTestCase {
     $this->assertFalse($message, "The financial account can be deleted. Failed asserting this was true.");
   }
 
+  /**
+   * Test for validating financial type has deferred revenue account relationship.
+   */
+  public function testcheckFinancialTypeHasDeferred() {
+    Civi::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
+    $params = array();
+    $valid = CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($params);
+    $this->assertFalse($valid, "This should have been false");
+    $cid = $this->individualCreate();
+    $params = array(
+      'contact_id' => $cid,
+      'receive_date' => '2010-01-20',
+      'total_amount' => 100,
+      'financial_type_id' => 3,
+      'revenue_recognition_date' => date('Ymd', strtotime("+1 month")),
+      'line_items' => array(
+        array(
+          'line_item' => array(
+            array(
+              'entity_table' => 'civicrm_contribution',
+              'price_field_id' => 8,
+              'price_field_value_id' => 16,
+              'label' => 'test 1',
+              'qty' => 1,
+              'unit_price' => 100,
+              'line_total' => 100,
+            ),
+            array(
+              'entity_table' => 'civicrm_contribution',
+              'price_field_id' => 8,
+              'price_field_value_id' => 17,
+              'label' => 'Test 2',
+              'qty' => 1,
+              'unit_price' => 200,
+              'line_total' => 200,
+              'financial_type_id' => 1,
+            ),
+          ),
+          'params' => array(),
+        ),
+      ),
+    );
+    $contribution = CRM_Contribute_BAO_Contribution::create($params);
+    $valid = CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($params, $contribution->id);
+    $message = "Revenue recognition date can only be specified if the financial type selected has a deferred revenue account configured. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts";
+    $this->assertEquals($valid, $message, "The messages do not match");
+  }
+
 }