From ac7514c2af00808d4229a90595ba36caa2f3016f Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Tue, 7 May 2013 00:29:49 +0530 Subject: [PATCH] -- fixed for CRM-12478 and added condition with financial_account_type = Asset for assigning to_financial_account_id for default ---------------------------------------- * CRM-12478: Default for Financial Account should be per Financial Account Type http://issues.civicrm.org/jira/browse/CRM-12478 --- CRM/Contribute/BAO/Contribution.php | 4 +- CRM/Financial/BAO/FinancialAccount.php | 70 +++++++++----------------- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 74c83e1d99..3a143bf5b8 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -2673,7 +2673,9 @@ WHERE contribution_id = %1 "; $params['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($params['payment_instrument_id']); } else { - $params['to_financial_account_id'] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1"); + $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' ")); + $queryParams = array(1 => array($relationTypeId, 'Integer')); + $params['to_financial_account_id'] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1", $queryParams); } $totalAmount = CRM_Utils_Array::value('total_amount', $params); diff --git a/CRM/Financial/BAO/FinancialAccount.php b/CRM/Financial/BAO/FinancialAccount.php index b5953b7f17..ff847eb632 100644 --- a/CRM/Financial/BAO/FinancialAccount.php +++ b/CRM/Financial/BAO/FinancialAccount.php @@ -39,13 +39,13 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco /** * static holder for the default LT */ - static $_defaultContributionType = null; + static $_defaultContributionType = NULL; /** * class constructor */ - function __construct( ) { - parent::__construct( ); + function __construct() { + parent::__construct(); } /** @@ -63,13 +63,13 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco * @static */ static function retrieve(&$params, &$defaults) { - $financialAccount = new CRM_Financial_DAO_FinancialAccount( ); - $financialAccount->copyValues( $params ); - if ($financialAccount->find(true)) { + $financialAccount = new CRM_Financial_DAO_FinancialAccount(); + $financialAccount->copyValues($params); + if ($financialAccount->find(TRUE)) { CRM_Core_DAO::storeValues($financialAccount, $defaults); return $financialAccount; } - return null; + return NULL; } /** @@ -96,45 +96,25 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco * @return object */ static function add(&$params, &$ids) { - $params['is_active'] = CRM_Utils_Array::value( 'is_active', $params, false ); - $params['is_deductible'] = CRM_Utils_Array::value( 'is_deductible', $params, false ); - $params['is_tax'] = CRM_Utils_Array::value( 'is_tax', $params, false ); - $params['is_header_account'] = CRM_Utils_Array::value( 'is_header_account', $params, false ); - $params['is_default'] = CRM_Utils_Array::value( 'is_default', $params, false ); + $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); + $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE); + $params['is_tax'] = CRM_Utils_Array::value('is_tax', $params, FALSE); + $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE); + $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); if (CRM_Utils_Array::value('is_default', $params)) { - $query = 'UPDATE civicrm_financial_account SET is_default = 0'; - CRM_Core_DAO::executeQuery( $query ); + $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1'; + $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer')); + CRM_Core_DAO::executeQuery($query, $queryParams); } // action is taken depending upon the mode - $financialAccount = new CRM_Financial_DAO_FinancialAccount( ); + $financialAccount = new CRM_Financial_DAO_FinancialAccount(); $financialAccount->copyValues($params); - $financialAccount->id = CRM_Utils_Array::value( 'contributionType', $ids ); - $financialAccount->save( ); + $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids); + $financialAccount->save(); return $financialAccount; } - /** - * Function to delete financial Types - * - * @param int $financialAccountId - * @static - */ - static function financialAccountValidation($fields,&$errors) { - $financialAccount = array( ); - if (CRM_Utils_Array::value('financial_type_id', $fields)) { - CRM_Core_PseudoConstant::populate( $financialAccount, - 'CRM_Financial_DAO_EntityFinancialAccount', - $all = True, - $retrieve = 'financial_account_id', - $filter = null, - " account_relationship = 6 AND entity_id = {$fields['financial_type_id']} " ); - if ( !current( $financialAccount ) ) { - $errors['financial_type_id'] = "Financial Account of account relationship of 'Asset Account is' is not configured for this Financial Type"; - } - } - } - /** * Function to delete financial Types * @@ -143,29 +123,29 @@ class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAcco */ static function del($financialAccountId) { //checking if financial type is present - $check = false; + $check = FALSE; //check dependencies $dependancy = array( array('Core', 'FinancialTrxn', 'to_financial_account_id'), - array('Financial', 'FinancialTypeAccount', 'financial_account_id' ), + array('Financial', 'FinancialTypeAccount', 'financial_account_id'), ); foreach ($dependancy as $name) { require_once (str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php"); eval('$bao = new CRM_' . $name[0] . '_BAO_' . $name[1] . '();'); $bao->$name[2] = $financialAccountId; - if ($bao->find(true)) { - $check = true; + if ($bao->find(TRUE)) { + $check = TRUE; } } if ($check) { - CRM_Core_Session::setStatus( ts('This financial account cannot be deleted since it is being used as a header account. Please remove it from being a header account before trying to delete it again.') ); - return CRM_Utils_System::redirect( CRM_Utils_System::url( 'civicrm/admin/financial/financialAccount', "reset=1&action=browse" )); + CRM_Core_Session::setStatus(ts('This financial account cannot be deleted since it is being used as a header account. Please remove it from being a header account before trying to delete it again.')); + return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse")); } //delete from financial Type table - $financialAccount = new CRM_Financial_DAO_FinancialAccount( ); + $financialAccount = new CRM_Financial_DAO_FinancialAccount(); $financialAccount->id = $financialAccountId; $financialAccount->delete(); } -- 2.25.1