From 6fab606ad84a28ef153e6fa329565007ffcbe6a7 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Fri, 1 Jul 2016 22:11:26 +0530 Subject: [PATCH] =?utf8?q?CRM-16189,=20added=20function=20to=20validate=20?= =?utf8?q?toggling=20of=20deferred=20revenue=20ch=E2=80=A6=20(#8572)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * CRM-16189, added function to validate toggling of deferred revenue check box ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, added ts tag for status message ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189 Added unit test for validateTogglingDeferredRevenue ---------------------------------------- * 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, removed white space * --CRM-16189, fixed CRM test --- CRM/Financial/BAO/FinancialAccount.php | 60 +++++++++++++++++++ .../Financial/BAO/FinancialAccountTest.php | 43 +++++++++++++ 2 files changed, 103 insertions(+) diff --git a/CRM/Financial/BAO/FinancialAccount.php b/CRM/Financial/BAO/FinancialAccount.php index fb551938f8..b650fa8d5f 100644 --- a/CRM/Financial/BAO/FinancialAccount.php +++ b/CRM/Financial/BAO/FinancialAccount.php @@ -437,4 +437,64 @@ LIMIT 1"; return FALSE; } + /** + * Validate if Deferred Account is set for Financial Type + * when Deferred Revenue is enabled + * + * @return string + * + */ + public static function validateTogglingDeferredRevenue() { + $deferredFinancialType = self::getDeferredFinancialType(); + $message = ts('Before Deferred Revenue can be enabled, a Deferred Revenue Account relationship must be defined for all financial types currently used for Memberships and Events, including + +In other words, please create deferred revenue accounts at Administer > CiviContribute > Financial Accounts, then configure them for the following financial types at Administer > CiviContribute > Financial Types, accounts:'); + $tables = array( + 'civicrm_membership_type', + 'civicrm_event', + 'civicrm_price_set', + 'civicrm_price_field_value', + ); + $params[2] = array('', 'Text'); + if (!empty($deferredFinancialType)) { + $params[2] = array(' AND financial_type_id NOT IN (' . implode(',', array_keys($deferredFinancialType)) . ') ', 'Text'); + } + $query_1 = 'SELECT %5.id FROM %4 WHERE %5.is_active = 1'; + $query_2 = $query_1 . ' %2'; + foreach ($tables as $table) { + $params[4] = array($table, 'Text'); + $params[5] = array($table, 'Text'); + $dao = CRM_Core_DAO::executeQuery($query_1, $params); + if ($dao->N) { + if (in_array($table, array('civicrm_price_set', 'civicrm_price_field_value'))) { + $query_2 .= " AND civicrm_price_set.name NOT IN ('default_contribution_amount', 'default_membership_type_amount') AND (civicrm_price_set.extends LIKE '%1%' OR civicrm_price_set.extends like '3')"; + if ($table == 'civicrm_price_field_value') { + $string = $table . ' INNER JOIN civicrm_price_field ON civicrm_price_field.id = civicrm_price_field_value.price_field_id INNER JOIN civicrm_price_set ON civicrm_price_set.id = civicrm_price_field.price_set_id '; + $params[4] = array($string, 'Text'); + $params[2][0] = str_replace('financial_type_id', "{$table}.financial_type_id", $params[2][0]); + } + } + $dao = CRM_Core_DAO::executeQuery($query_2, $params); + if ($dao->N) { + $message .= ''; + return $message; + } + } + } + return NULL; + } + } diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php index 0eba0fdcaa..47fa9a5280 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php @@ -333,4 +333,47 @@ class CRM_Financial_BAO_FinancialAccountTest extends CiviUnitTestCase { } } + /** + * Test Validate if Deferred Account is set for Financial Type. + */ + public function testValidateTogglingDeferredRevenue() { + $orgContactID = $this->organizationCreate(); + + //create relationship + $params = array( + 'name_a_b' => 'Relation 1', + 'name_b_a' => 'Relation 2', + 'contact_type_a' => 'Individual', + 'contact_type_b' => 'Organization', + 'is_reserved' => 1, + 'is_active' => 1, + ); + $relationshipTypeId = $this->relationshipTypeCreate($params); + $ids = array(); + $params = array( + 'name' => 'test type', + 'domain_id' => 1, + 'description' => NULL, + 'minimum_fee' => 10, + 'duration_unit' => 'year', + 'member_of_contact_id' => $orgContactID, + 'relationship_type_id' => $relationshipTypeId, + 'period_type' => 'fixed', + 'duration_interval' => 1, + 'financial_type_id' => 1, + 'visibility' => 'Public', + 'is_active' => 1, + ); + + $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids); + + $membership = $this->assertDBNotNull('CRM_Member_BAO_MembershipType', $orgContactID, + 'name', 'member_of_contact_id', + 'Database check on updated membership record.' + ); + $error = CRM_Financial_BAO_FinancialAccount::validateTogglingDeferredRevenue(); + $this->assertTrue(!empty($error), "Error message did not appear"); + $this->membershipTypeDelete(array('id' => $membershipType->id)); + } + } -- 2.25.1