4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
37 class CRM_Financial_BAO_FinancialAccount
extends CRM_Financial_DAO_FinancialAccount
{
40 * Static holder for the default LT
42 static $_defaultContributionType = NULL;
47 public function __construct() {
48 parent
::__construct();
52 * Fetch object based on array of properties
54 * @param array $params (reference ) an assoc array of name/value pairs
55 * @param array $defaults (reference ) an assoc array to hold the flattened values
57 * @return CRM_Contribute_BAO_FinancialAccount object
60 public static function retrieve(&$params, &$defaults) {
61 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
62 $financialAccount->copyValues($params);
63 if ($financialAccount->find(TRUE)) {
64 CRM_Core_DAO
::storeValues($financialAccount, $defaults);
65 return $financialAccount;
71 * Update the is_active flag in the db
73 * @param int $id id of the database record
74 * @param boolean $is_active value we want to set the is_active field
76 * @return Object DAO object on sucess, null otherwise
79 public static function setIsActive($id, $is_active) {
80 return CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active);
84 * Add the financial types
86 * @param array $params reference array contains the values submitted by the form
87 * @param array $ids reference array contains the id
92 public static function add(&$params, &$ids = array()) {
93 if(empty($params['id'])) {
94 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
95 $params['is_deductible'] = CRM_Utils_Array
::value('is_deductible', $params, FALSE);
96 $params['is_tax'] = CRM_Utils_Array
::value('is_tax', $params, FALSE);
97 $params['is_header_account'] = CRM_Utils_Array
::value('is_header_account', $params, FALSE);
98 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
100 if (!empty($params['is_default'])) {
101 $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1';
102 $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer'));
103 CRM_Core_DAO
::executeQuery($query, $queryParams);
106 // action is taken depending upon the mode
107 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
108 $financialAccount->copyValues($params);
109 if (!empty($ids['contributionType'])) {
110 $financialAccount->id
= CRM_Utils_Array
::value('contributionType', $ids);
112 $financialAccount->save();
113 return $financialAccount;
117 * Delete financial Types
119 * @param int $financialAccountId
122 public static function del($financialAccountId) {
123 //checking if financial type is present
128 array('Core', 'FinancialTrxn', 'to_financial_account_id'),
129 array('Financial', 'FinancialTypeAccount', 'financial_account_id'),
131 foreach ($dependancy as $name) {
132 require_once (str_replace('_', DIRECTORY_SEPARATOR
, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php");
133 $className = "CRM_{$name[0]}_BAO_{$name[1]}";
134 $bao = new $className();
135 $bao->$name[2] = $financialAccountId;
136 if ($bao->find(TRUE)) {
142 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.'));
143 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
146 //delete from financial Type table
147 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
148 $financialAccount->id
= $financialAccountId;
149 $financialAccount->delete();
153 * Get accounting code for a financial type with account relation Income Account is
155 * @financialTypeId int Financial Type Id
157 * @param int $financialTypeId
159 * @return accounting code
162 public static function getAccountingCode($financialTypeId) {
163 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
164 $query = "SELECT cfa.accounting_code
165 FROM civicrm_financial_type cft
166 LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type'
167 LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id
169 AND account_relationship = %2";
171 1 => array($financialTypeId, 'Integer'),
172 2 => array($relationTypeId, 'Integer'),
174 return CRM_Core_DAO
::singleValueQuery($query, $params);
180 * @param $financialAccountId financial account id
182 * @param $financialAccountTypeId financial account type id
184 * @param \account|string $accountTypeCode account type code
186 * @return integer count
189 public static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') {
190 if (!$financialAccountTypeId) {
191 $financialAccountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type');
192 $financialAccountTypeId = array_search('Asset', $financialAccountType);
194 $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2
195 AND id != %3 AND is_active = 1;";
197 1 => array($financialAccountTypeId, 'Integer'),
198 2 => array(strtolower($accountTypeCode), 'String'),
199 3 => array($financialAccountId, 'Integer'),
201 return CRM_Core_DAO
::singleValueQuery($query, $params);