INFRA-132 - Standardize integer to int
[civicrm-core.git] / CRM / Financial / BAO / FinancialAccount.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36 class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAccount {
37
38 /**
39 * Static holder for the default LT
40 */
41 static $_defaultContributionType = NULL;
42
43 /**
44 * Class constructor
45 */
46 public function __construct() {
47 parent::__construct();
48 }
49
50 /**
51 * Fetch object based on array of properties
52 *
53 * @param array $params
54 * (reference ) an assoc array of name/value pairs.
55 * @param array $defaults
56 * (reference ) an assoc array to hold the flattened values.
57 *
58 * @return CRM_Financial_BAO_FinancialAccount
59 * @static
60 */
61 public static function retrieve(&$params, &$defaults) {
62 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
63 $financialAccount->copyValues($params);
64 if ($financialAccount->find(TRUE)) {
65 CRM_Core_DAO::storeValues($financialAccount, $defaults);
66 return $financialAccount;
67 }
68 return NULL;
69 }
70
71 /**
72 * Update the is_active flag in the db
73 *
74 * @param int $id
75 * Id of the database record.
76 * @param bool $is_active
77 * Value we want to set the is_active field.
78 *
79 * @return CRM_Core_DAO|null
80 * DAO object on success, null otherwise
81 * @static
82 */
83 public static function setIsActive($id, $is_active) {
84 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active);
85 }
86
87 /**
88 * Add the financial types
89 *
90 * @param array $params
91 * Reference array contains the values submitted by the form.
92 * @param array $ids
93 * Reference array contains the id.
94 *
95 * @static
96 * @return CRM_Financial_DAO_FinancialAccount
97 */
98 public static function add(&$params, &$ids = array()) {
99 if (empty($params['id'])) {
100 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
101 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
102 $params['is_tax'] = CRM_Utils_Array::value('is_tax', $params, FALSE);
103 $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE);
104 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
105 }
106 if (!empty($params['is_default'])) {
107 $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1';
108 $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer'));
109 CRM_Core_DAO::executeQuery($query, $queryParams);
110 }
111
112 // action is taken depending upon the mode
113 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
114 $financialAccount->copyValues($params);
115 if (!empty($ids['contributionType'])) {
116 $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids);
117 }
118 $financialAccount->save();
119 return $financialAccount;
120 }
121
122 /**
123 * Delete financial Types
124 *
125 * @param int $financialAccountId
126 * @static
127 */
128 public static function del($financialAccountId) {
129 //checking if financial type is present
130 $check = FALSE;
131
132 //check dependencies
133 $dependancy = array(
134 array('Core', 'FinancialTrxn', 'to_financial_account_id'),
135 array('Financial', 'FinancialTypeAccount', 'financial_account_id'),
136 );
137 foreach ($dependancy as $name) {
138 require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php";
139 $className = "CRM_{$name[0]}_BAO_{$name[1]}";
140 $bao = new $className();
141 $bao->$name[2] = $financialAccountId;
142 if ($bao->find(TRUE)) {
143 $check = TRUE;
144 }
145 }
146
147 if ($check) {
148 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.'));
149 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
150 }
151
152 //delete from financial Type table
153 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
154 $financialAccount->id = $financialAccountId;
155 $financialAccount->delete();
156 }
157
158 /**
159 * Get accounting code for a financial type with account relation Income Account is
160 *
161 * @param int $financialTypeId
162 *
163 * @return int
164 * accounting code
165 * @static
166 */
167 public static function getAccountingCode($financialTypeId) {
168 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
169 $query = "SELECT cfa.accounting_code
170 FROM civicrm_financial_type cft
171 LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type'
172 LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id
173 WHERE cft.id = %1
174 AND account_relationship = %2";
175 $params = array(
176 1 => array($financialTypeId, 'Integer'),
177 2 => array($relationTypeId, 'Integer'),
178 );
179 return CRM_Core_DAO::singleValueQuery($query, $params);
180 }
181
182 /**
183 * Get AR account
184 *
185 * @param $financialAccountId
186 * Financial account id.
187 *
188 * @param $financialAccountTypeId
189 * Financial account type id.
190 *
191 * @param string $accountTypeCode
192 * account type code
193 *
194 * @return int
195 * count
196 * @static
197 */
198 public static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') {
199 if (!$financialAccountTypeId) {
200 $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
201 $financialAccountTypeId = array_search('Asset', $financialAccountType);
202 }
203 $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2
204 AND id != %3 AND is_active = 1;";
205 $params = array(
206 1 => array($financialAccountTypeId, 'Integer'),
207 2 => array(strtolower($accountTypeCode), 'String'),
208 3 => array($financialAccountId, 'Integer'),
209 );
210 return CRM_Core_DAO::singleValueQuery($query, $params);
211 }
212 }