3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Financial_BAO_FinancialAccount
extends CRM_Financial_DAO_FinancialAccount
{
36 * Static holder for the default LT.
38 static $_defaultContributionType = NULL;
43 public function __construct() {
44 parent
::__construct();
48 * Fetch object based on array of properties.
50 * @param array $params
51 * (reference ) an assoc array of name/value pairs.
52 * @param array $defaults
53 * (reference ) an assoc array to hold the flattened values.
55 * @return CRM_Financial_BAO_FinancialAccount
57 public static function retrieve(&$params, &$defaults) {
58 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
59 $financialAccount->copyValues($params);
60 if ($financialAccount->find(TRUE)) {
61 CRM_Core_DAO
::storeValues($financialAccount, $defaults);
62 return $financialAccount;
68 * Update the is_active flag in the db.
71 * Id of the database record.
72 * @param bool $is_active
73 * Value we want to set the is_active field.
75 * @return CRM_Core_DAO|null
76 * DAO object on success, null otherwise
78 public static function setIsActive($id, $is_active) {
79 return CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active);
83 * Add the financial types.
85 * @param array $params
86 * Reference array contains the values submitted by the form.
88 * Reference array contains the id.
90 * @return CRM_Financial_DAO_FinancialAccount
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
121 public static function del($financialAccountId) {
122 // checking if financial type is present
127 array('Core', 'FinancialTrxn', 'to_financial_account_id'),
128 array('Financial', 'FinancialTypeAccount', 'financial_account_id'),
130 foreach ($dependency as $name) {
131 require_once str_replace('_', DIRECTORY_SEPARATOR
, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php";
132 $className = "CRM_{$name[0]}_BAO_{$name[1]}";
133 $bao = new $className();
134 $bao->$name[2] = $financialAccountId;
135 if ($bao->find(TRUE)) {
141 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.'));
142 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
145 // delete from financial Type table
146 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
147 $financialAccount->id
= $financialAccountId;
148 $financialAccount->delete();
152 * Get accounting code for a financial type with account relation Income Account is.
154 * @param int $financialTypeId
159 public static function getAccountingCode($financialTypeId) {
160 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
161 $query = "SELECT cfa.accounting_code
162 FROM civicrm_financial_type cft
163 LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type'
164 LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id
166 AND account_relationship = %2";
168 1 => array($financialTypeId, 'Integer'),
169 2 => array($relationTypeId, 'Integer'),
171 return CRM_Core_DAO
::singleValueQuery($query, $params);
177 * @param $financialAccountId
178 * Financial account id.
180 * @param $financialAccountTypeId
181 * Financial account type id.
183 * @param string $accountTypeCode
189 public static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') {
190 if (!$financialAccountTypeId) {
191 $financialAccountTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
193 $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2
194 AND id != %3 AND is_active = 1;";
196 1 => array($financialAccountTypeId, 'Integer'),
197 2 => array(strtolower($accountTypeCode), 'String'),
198 3 => array($financialAccountId, 'Integer'),
200 return CRM_Core_DAO
::singleValueQuery($query, $params);