From 707f6952d2d24f1b5badcfeb090505af1708dbfa Mon Sep 17 00:00:00 2001 From: Rohan Katkar Date: Thu, 22 May 2014 12:24:45 +0530 Subject: [PATCH] VAT-86-94-224 Added validation when 'Is Tax?' is checked. Checked for financial account to have 'Is Tax?' enabled. Add validation on edit for 'Is Tax?'. --- CRM/Financial/Form/FinancialAccount.php | 26 +++++++++++++++++++-- CRM/Financial/Form/FinancialTypeAccount.php | 7 ++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CRM/Financial/Form/FinancialAccount.php b/CRM/Financial/Form/FinancialAccount.php index a04b33276f..dd6318ba5e 100644 --- a/CRM/Financial/Form/FinancialAccount.php +++ b/CRM/Financial/Form/FinancialAccount.php @@ -146,11 +146,33 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { */ static function formRule( $values, $files, $self ) { $errorMsg = array( ); - if (!empty( $values['tax_rate'])) { - if ($values['tax_rate'] <= 0 || $values['tax_rate'] > 100) { + $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' ")); + if (isset($values['is_tax'])) { + if ($values['financial_account_type_id'] != $financialAccountTypeId) { + $errorMsg['financial_account_type_id'] = ts('Taxable accounts should have Financial Account Type set to Liability.'); + } + if (CRM_Utils_Array::value('tax_rate', $values) == NULL) { + $errorMsg['tax_rate'] = ts('Please enter value for tax rate'); + } + } + if ((CRM_Utils_Array::value('tax_rate', $values) != NULL)) { + if ($values['tax_rate'] < 0 || $values['tax_rate'] >= 100) { $errorMsg['tax_rate'] = ts('Tax Rate Should be between 0 - 100'); } } + if ($self->_action & CRM_Core_Action::UPDATE) { + if (!(isset($values['is_tax']))) { + $relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")); + $params = array( + 'financial_account_id' => $self->_id, + 'account_relationship'=> $relationshipId + ); + $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); + if ($result) { + $errorMsg['is_tax'] = ts('Is Tax? must be set for this financial account'); + } + } + } return CRM_Utils_Array::crmIsEmptyArray( $errorMsg ) ? true : $errorMsg; } diff --git a/CRM/Financial/Form/FinancialTypeAccount.php b/CRM/Financial/Form/FinancialTypeAccount.php index 00fcecc471..90859f444f 100644 --- a/CRM/Financial/Form/FinancialTypeAccount.php +++ b/CRM/Financial/Form/FinancialTypeAccount.php @@ -283,6 +283,13 @@ class CRM_Financial_Form_FinancialTypeAccount extends CRM_Contribute_Form { ); $defaults = array(); if ($self->_action == CRM_Core_Action::ADD) { + $relationshipId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")); + $isTax = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $values['financial_account_id'], 'is_tax'); + if ($values['account_relationship'] == $relationshipId) { + if (!($isTax)) { + $errorMsg['financial_account_id'] = ts('Is Tax? must be set for respective financial account'); + } + } $result = CRM_Financial_BAO_FinancialTypeAccount::retrieve($params, $defaults); if ($result) { $errorFlag = TRUE; -- 2.25.1