From: Pradeep Nayak Date: Mon, 14 Jul 2014 07:25:32 +0000 (+0530) Subject: -- CRM-14362, added form rule to restrict duplicate Financial Type and added code... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=77292b55159c1f291e2041ed8ac30ad55c51c7d6;p=civicrm-core.git -- CRM-14362, added form rule to restrict duplicate Financial Type and added code for https://issues.civicrm.org/jira/browse/CRM-14362?focusedCommentId=62629&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-62629 --- diff --git a/CRM/Financial/BAO/FinancialTypeAccount.php b/CRM/Financial/BAO/FinancialTypeAccount.php index b749bd9d4c..3b18de1dad 100644 --- a/CRM/Financial/BAO/FinancialTypeAccount.php +++ b/CRM/Financial/BAO/FinancialTypeAccount.php @@ -244,48 +244,68 @@ WHERE cog.name = 'payment_instrument' "; array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID), array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID), ); - $params = array( - 'name' => $financialType->name, - 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'), - 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID), - 'description' => $financialType->description, - 'account_type_code' => 'INC', - 'is_active' => 1, + + $dao = CRM_Core_DAO::executeQuery('SELECT id, financial_account_type_id FROM civicrm_financial_account WHERE name LIKE %1', + array(1 => array($financialType->name, 'String')) ); - $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray); + $dao->fetch(); + $existingFinancialAccount = array(); + if (!$dao->N) { + $params = array( + 'name' => $financialType->name, + 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'), + 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID), + 'description' => $financialType->description, + 'account_type_code' => 'INC', + 'is_active' => 1, + ); + $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray); + } + else { + $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id; + } $params = array ( 'entity_table' => 'civicrm_financial_type', 'entity_id' => $financialType->id, ); foreach ($relationships as $key => $value) { - if ($accountRelationship[$key] == 'Accounts Receivable Account is') { - $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name'); - if (!empty($params['financial_account_id'])) { - $titles[] = 'Accounts Receivable'; - } - else { - $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account + if (!array_key_exists($value, $existingFinancialAccount)) { + if ($accountRelationship[$key] == 'Accounts Receivable Account is') { + $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name'); + if (!empty($params['financial_account_id'])) { + $titles[] = 'Accounts Receivable'; + } + else { + $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1"; + $dao = CRM_Core_DAO::executeQuery($query); + $dao->fetch(); + $params['financial_account_id'] = $dao->financial_account_id; + $titles[] = $dao->name; + } + } + elseif ($accountRelationship[$key] == 'Income Account is' && empty($existingFinancialAccount)) { + $params['financial_account_id'] = $financialAccount->id; + } + else { + $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}"; $dao = CRM_Core_DAO::executeQuery($query); $dao->fetch(); - $params['financial_account_id'] = $dao->financial_account_id; + $params['financial_account_id'] = $dao->id; $titles[] = $dao->name; } } - elseif ($accountRelationship[$key] == 'Income Account is') { - $params['financial_account_id'] = $financialAccount->id; - } else { - $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}"; - $dao = CRM_Core_DAO::executeQuery($query); - $dao->fetch(); - $params['financial_account_id'] = $dao->id; - $titles[] = $dao->name; + $params['financial_account_id'] = $existingFinancialAccount[$value]; + $titles[] = $financialType->name; } $params['account_relationship'] = $key; self::add($params); } + if (!empty($existingFinancialAccount)) { + $titles = array(); + } return $titles; } } diff --git a/CRM/Financial/Form/FinancialType.php b/CRM/Financial/Form/FinancialType.php index ddd78a913e..1a780db3d6 100644 --- a/CRM/Financial/Form/FinancialType.php +++ b/CRM/Financial/Form/FinancialType.php @@ -71,8 +71,10 @@ class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form { if ($this->_action == CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $this->_id, 'is_reserved','vid')) { $this->freeze(array('is_active')); } - - //$this->addFormRule( array( 'CRM_Financial_Form_FinancialType', 'formRule'), $this ); + + $this->addRule('name', ts('A financial type with this name already exists. Please select another name.'),'objectExists', + array('CRM_Financial_DAO_FinancialType', $this->_id) + ); } /** @@ -110,11 +112,17 @@ class CRM_Financial_Form_FinancialType extends CRM_Contribute_Form { $statusArray = array( 1 => $financialType->name, 2 => $financialType->name, - 3 => $financialType->titles[0], - 4 => $financialType->titles[1], - 5 => $financialType->titles[2], + 3 => CRM_Utils_Array::value(0, $financialType->titles), + 4 => CRM_Utils_Array::value(1, $financialType->titles), + 5 => CRM_Utils_Array::value(2, $financialType->titles), ); - CRM_Core_Session::setStatus(ts('Your Financial \'%1\' Type has been created, along with a corresponding income account \'%2\'. That income account, along with standard financial accounts \'%3\', \'%4\' and \'%5\' have been linked to the financial type. You may edit or replace those relationships here.', $statusArray)); + if (empty($financialType->titles)) { + $text = 'Your Financial \'%1\' Type has been created and assigned to an existing financial account with the same title. You should review the assigned account and determine whether additional account relationships are needed.'; + } + else { + $text = 'Your Financial \'%1\' Type has been created, along with a corresponding income account \'%2\'. That income account, along with standard financial accounts \'%3\', \'%4\' and \'%5\' have been linked to the financial type. You may edit or replace those relationships here.'; + } + CRM_Core_Session::setStatus(ts($text, $statusArray)); } $session = CRM_Core_Session::singleton();