From 7d2897244548959b0a3a3f441ebfbec1305b7137 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 15 May 2013 18:07:51 +0530 Subject: [PATCH] -- worked on CRM-12492 ---------------------------------------- * CRM-12492: Prevent deletion of last AR financial account, and of A/R relationship for Financial Type http://issues.civicrm.org/jira/browse/CRM-12492 --- CRM/Financial/BAO/FinancialAccount.php | 23 +++++++++ CRM/Financial/Form/FinancialAccount.php | 53 ++++++++++++++++++--- CRM/Financial/Form/FinancialTypeAccount.php | 38 +++++++++++---- CRM/Financial/Page/FinancialTypeAccount.php | 26 ++++++---- 4 files changed, 116 insertions(+), 24 deletions(-) diff --git a/CRM/Financial/BAO/FinancialAccount.php b/CRM/Financial/BAO/FinancialAccount.php index ff847eb632..5abb6dd079 100644 --- a/CRM/Financial/BAO/FinancialAccount.php +++ b/CRM/Financial/BAO/FinancialAccount.php @@ -172,5 +172,28 @@ WHERE cft.id = %1 ); return CRM_Core_DAO::singleValueQuery($query, $params); } + + /** + * get AR account + * + * @param $financialAccountId financial account id + * + * @param $financialAccountTypeId financial account type id + * + * @param $accountTypeCode account type code + * + * @return integer count + * @static + */ + static function getARAccounts($financialAccountId, $financialAccountTypeId, $accountTypeCode = 'ar') { + $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2 + AND id != %3;"; + $params = array( + 1 => array($financialAccountTypeId, 'Integer'), + 2 => array(strtolower($accountTypeCode), 'String'), + 3 => array($financialAccountId, 'Integer'), + ); + return CRM_Core_DAO::singleValueQuery($query, $params); + } } diff --git a/CRM/Financial/Form/FinancialAccount.php b/CRM/Financial/Form/FinancialAccount.php index 392219b47b..acfb9334eb 100644 --- a/CRM/Financial/Form/FinancialAccount.php +++ b/CRM/Financial/Form/FinancialAccount.php @@ -40,6 +40,43 @@ */ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { + /** + * Flag if its a AR account type + * + * @var boolean + */ + protected $_isARFlag = FALSE; + + + /** + * Function to set variables up before form is built + * + * @return void + * @access public + */ + public function preProcess() { + parent::preProcess(); + + if ($this->_id) { + $params = array( + 'id' => $this->_id, + ); + $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, CRM_Core_DAO::$_nullArray); + $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type'); + if ($financialAccount->financial_account_type_id == array_search('Asset', $financialAccountType) + && strtolower($financialAccount->account_type_code) == 'ar' + && !CRM_Financial_BAO_FinancialAccount::getARAccounts($this->_id, array_search('Asset', $financialAccountType))) { + $this->_isARFlag = TRUE; + if ($this->_action & CRM_Core_Action::DELETE) { + CRM_Core_Session::setStatus(ts("Financial account with 'AR' account cannot be deleted."), + '', 'error'); + CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', + "reset=1&action=browse")); + } + } + } + } + /** * Function to build the form * @@ -48,8 +85,8 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { */ public function buildQuickForm( ) { parent::buildQuickForm( ); - $dataURL = CRM_Utils_System::url( 'civicrm/ajax/rest', - 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=contact&org=1', false, null, false ); + $dataURL = CRM_Utils_System::url('civicrm/ajax/rest', + 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=contact&org=1', FALSE, NULL, FALSE); $this->assign('dataURL', $dataURL); if ($this->_action & CRM_Core_Action::DELETE) { @@ -58,13 +95,13 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { $this->applyFilter('__ALL__', 'trim'); $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialAccount'); - $this->add('text', 'name', ts('Name'), $attributes['name'],true); + $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE); $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'), 'objectExists', array('CRM_Financial_DAO_FinancialAccount', $this->_id)); $this->add('text', 'description', ts('Description'), $attributes['description']); $this->add('text', 'accounting_code', ts('Accounting Code'), $attributes['accounting_code']); - $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']); + $elementAccounting = $this->add('text', 'account_type_code', ts('Account Type Code'), $attributes['account_type_code']); $this->add('text', 'contact_name', ts('Owner'), $attributes['name']); $this->add('hidden', 'contact_id', '', array('id' => 'contact_id')); $this->add('text', 'tax_rate', ts('Tax Rate'), $attributes['tax_rate']); @@ -75,8 +112,12 @@ class CRM_Financial_Form_FinancialAccount extends CRM_Contribute_Form { $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type'); if (!empty($financialAccountType)) { - $this->add('select', 'financial_account_type_id', ts('Financial Account Type'), - array('' => '- select -') + $financialAccountType, true); + $element = $this->add('select', 'financial_account_type_id', ts('Financial Account Type'), + array('' => '- select -') + $financialAccountType, TRUE); + if ($this->_isARFlag) { + $element->freeze(); + $elementAccounting->freeze(); + } } if ($this->_action == CRM_Core_Action::UPDATE && diff --git a/CRM/Financial/Form/FinancialTypeAccount.php b/CRM/Financial/Form/FinancialTypeAccount.php index df162e8994..a703c2526c 100644 --- a/CRM/Financial/Form/FinancialTypeAccount.php +++ b/CRM/Financial/Form/FinancialTypeAccount.php @@ -63,6 +63,13 @@ class CRM_Financial_Form_FinancialTypeAccount extends CRM_Contribute_Form { */ protected $_BAOName; + /** + * Flag if its a AR account type + * + * @var boolean + */ + protected $_isARFlag = FALSE; + /** * Function to set variables up before form is built * @@ -71,28 +78,40 @@ class CRM_Financial_Form_FinancialTypeAccount extends CRM_Contribute_Form { */ public function preProcess() { $this->_aid = CRM_Utils_Request::retrieve('aid', 'Positive', $this); - $this->_id = CRM_Utils_Request::retrieve('id' , 'Positive', $this); + $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); if (!$this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { - $this->_id = CRM_Utils_Type::escape($this->_id , 'Positive'); + $this->_id = CRM_Utils_Type::escape($this->_id, 'Positive'); } + $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', + "reset=1&action=browse&aid={$this->_aid}"); + $this->_BAOName = 'CRM_Financial_BAO_FinancialTypeAccount'; if ($this->_aid && ($this->_action & CRM_Core_Action::ADD)) { $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name'); CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Financial Accounts')); - $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', - "reset=1&action=browse&aid={$this->_aid}"); - $session = CRM_Core_Session::singleton(); $session->pushUserContext($url); } + // CRM-12492 + if (!($this->_action & CRM_Core_Action::ADD)) { + $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); + $accountRelationship = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'account_relationship'); + if ($accountRelationship == $relationTypeId) { + $this->_isARFlag = TRUE; + if ($this->_action & CRM_Core_Action::DELETE) { + CRM_Core_Session::setStatus(ts("Selected financial type account with 'Accounts Receivable Account is' account relationship cannot be deleted."), + '', 'error'); + CRM_Utils_System::redirect($url); + } + } + } if ($this->_id) { $financialAccount = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $this->_id, 'financial_account_id'); $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccount, 'name'); - CRM_Utils_System::setTitle($fieldTitle .' - '.ts('Financial Type Accounts')); + CRM_Utils_System::setTitle($fieldTitle . ' - '. ts('Financial Type Accounts')); } - $url = CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$this->_aid}"); $breadCrumb = array( array('title' => ts('Financial Type Accounts'), 'url' => $url, @@ -145,13 +164,16 @@ class CRM_Financial_Form_FinancialTypeAccount extends CRM_Contribute_Form { } $AccountTypeRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship'); if (!empty($AccountTypeRelationship)) { - $this->add('select', + $element = $this->add('select', 'account_relationship', ts('Financial Account Relationship'), array('select' => '- select -') + $AccountTypeRelationship, TRUE ); } + if ($this->_isARFlag) { + $element->freeze(); + } if ($this->_action == CRM_Core_Action::ADD) { if (CRM_Utils_Array::value('account_relationship', $this->_submitValues) || CRM_Utils_Array::value('financial_account_id', $this->_submitValues)) { diff --git a/CRM/Financial/Page/FinancialTypeAccount.php b/CRM/Financial/Page/FinancialTypeAccount.php index 83ebeab520..af2060edf2 100644 --- a/CRM/Financial/Page/FinancialTypeAccount.php +++ b/CRM/Financial/Page/FinancialTypeAccount.php @@ -72,15 +72,15 @@ class CRM_Financial_Page_FinancialTypeAccount extends CRM_Core_Page { if (!(self::$_links)) { self::$_links = array( CRM_Core_Action::UPDATE => array( - 'name' => ts('Edit'), - 'url' => 'civicrm/admin/financial/financialType/accounts', - 'qs' => 'action=update&id=%%id%%&aid=%%aid%%&reset=1', + 'name' => ts('Edit'), + 'url' => 'civicrm/admin/financial/financialType/accounts', + 'qs' => 'action=update&id=%%id%%&aid=%%aid%%&reset=1', 'title' => ts('Edit Financial Type Account'), ), CRM_Core_Action::DELETE => array( - 'name' => ts('Delete'), - 'url' => 'civicrm/admin/financial/financialType/accounts', - 'qs' => 'action=delete&id=%%id%%&aid=%%aid%%', + 'name' => ts('Delete'), + 'url' => 'civicrm/admin/financial/financialType/accounts', + 'qs' => 'action=delete&id=%%id%%&aid=%%aid%%', 'title' => ts('Delete Financial Type Account'), ), ); @@ -135,6 +135,7 @@ class CRM_Financial_Page_FinancialTypeAccount extends CRM_Core_Page { $params['entity_id'] = $this->_aid; $params['entity_table'] = 'civicrm_financial_type'; if ($this->_aid) { + $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); $this->_title = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_aid, 'name'); CRM_Utils_System::setTitle($this->_title .' - '.ts( 'Assigned Financial Accounts')); $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type'); @@ -143,9 +144,9 @@ class CRM_Financial_Page_FinancialTypeAccount extends CRM_Core_Page { $dao->find(); while ($dao->fetch()) { $financialType[$dao->id] = array(); - CRM_Core_DAO::storeValues( $dao, $financialType[$dao->id] ); + CRM_Core_DAO::storeValues($dao, $financialType[$dao->id]); - $params = array( 'id' => $dao->financial_account_id ); + $params = array('id' => $dao->financial_account_id); $defaults = array(); $financialAccount = CRM_Financial_BAO_FinancialAccount::retrieve($params, $defaults); if (!empty($financialAccount)) { @@ -166,10 +167,15 @@ class CRM_Financial_Page_FinancialTypeAccount extends CRM_Core_Page { $financialType[$dao->id]['account_relationship'] = CRM_Utils_Array::value($dao->account_relationship, $accountRelationship); } } - // form all action links $action = array_sum(array_keys($this->links())); - $financialType[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(), $action, + $links = self::links(); + + //CRM-12492 + if ($dao->account_relationship == $relationTypeId) { + unset($links[CRM_Core_Action::DELETE]); + } + $financialType[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action, array( 'id' => $dao->id, 'aid'=> $dao->entity_id, -- 2.25.1