From b2ffc3e1bc4655125c9d8dfd8bd39000e79b028f Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Sat, 25 Jun 2016 08:18:26 +0530 Subject: [PATCH] =?utf8?q?CRM-16189,=20added=20function=20to=20check=20if?= =?utf8?q?=20financial=20type=20has=20Deferred=20Inc=E2=80=A6=20(#8565)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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 | 58 +++++++++++++++++++ .../Financial/BAO/FinancialAccountTest.php | 48 +++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/CRM/Financial/BAO/FinancialAccount.php b/CRM/Financial/BAO/FinancialAccount.php index e96c1ea619..19147b11f7 100644 --- a/CRM/Financial/BAO/FinancialAccount.php +++ b/CRM/Financial/BAO/FinancialAccount.php @@ -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; + } + } diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php index 105d357d69..aa92d7d72b 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php @@ -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"); + } + } -- 2.25.1