Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 | 32 | */ |
6a488035 TO |
33 | class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAccount { |
34 | ||
35 | /** | |
fe482240 | 36 | * Static holder for the default LT. |
6a488035 | 37 | */ |
ac7514c2 | 38 | static $_defaultContributionType = NULL; |
6a488035 TO |
39 | |
40 | /** | |
fe482240 | 41 | * Class constructor. |
6a488035 | 42 | */ |
00be9182 | 43 | public function __construct() { |
ac7514c2 | 44 | parent::__construct(); |
6a488035 | 45 | } |
0e6e8724 | 46 | |
6a488035 | 47 | /** |
fe482240 | 48 | * Fetch object based on array of properties. |
6a488035 | 49 | * |
ed5dd492 TO |
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. | |
6a488035 | 54 | * |
72b3a70c | 55 | * @return CRM_Financial_BAO_FinancialAccount |
6a488035 | 56 | */ |
00be9182 | 57 | public static function retrieve(&$params, &$defaults) { |
ac7514c2 PN |
58 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
59 | $financialAccount->copyValues($params); | |
60 | if ($financialAccount->find(TRUE)) { | |
6a488035 TO |
61 | CRM_Core_DAO::storeValues($financialAccount, $defaults); |
62 | return $financialAccount; | |
63 | } | |
ac7514c2 | 64 | return NULL; |
6a488035 TO |
65 | } |
66 | ||
67 | /** | |
fe482240 | 68 | * Update the is_active flag in the db. |
6a488035 | 69 | * |
ed5dd492 TO |
70 | * @param int $id |
71 | * Id of the database record. | |
72 | * @param bool $is_active | |
73 | * Value we want to set the is_active field. | |
6a488035 | 74 | * |
72b3a70c CW |
75 | * @return CRM_Core_DAO|null |
76 | * DAO object on success, null otherwise | |
6a488035 | 77 | */ |
00be9182 | 78 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
79 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active); |
80 | } | |
81 | ||
82 | /** | |
fe482240 | 83 | * Add the financial types. |
6a488035 | 84 | * |
ed5dd492 TO |
85 | * @param array $params |
86 | * Reference array contains the values submitted by the form. | |
87 | * @param array $ids | |
88 | * Reference array contains the id. | |
0e6e8724 | 89 | * |
72b3a70c | 90 | * @return CRM_Financial_DAO_FinancialAccount |
6a488035 | 91 | */ |
00be9182 | 92 | public static function add(&$params, &$ids = array()) { |
22e263ad | 93 | if (empty($params['id'])) { |
bc2bc079 | 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); | |
99 | } | |
a7488080 | 100 | if (!empty($params['is_default'])) { |
ac7514c2 PN |
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); | |
0e6e8724 DL |
104 | } |
105 | ||
6a488035 | 106 | // action is taken depending upon the mode |
ac7514c2 | 107 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
6a488035 | 108 | $financialAccount->copyValues($params); |
e5439581 | 109 | if (!empty($ids['contributionType'])) { |
110 | $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids); | |
111 | } | |
ac7514c2 | 112 | $financialAccount->save(); |
6a488035 TO |
113 | return $financialAccount; |
114 | } | |
0e6e8724 | 115 | |
6a488035 | 116 | /** |
fe482240 | 117 | * Delete financial Types. |
0e6e8724 | 118 | * |
6a488035 | 119 | * @param int $financialAccountId |
6a488035 | 120 | */ |
00be9182 | 121 | public static function del($financialAccountId) { |
cded2ebf | 122 | // checking if financial type is present |
ac7514c2 | 123 | $check = FALSE; |
0e6e8724 | 124 | |
6a488035 | 125 | //check dependencies |
b44e3f84 | 126 | $dependency = array( |
0e6e8724 DL |
127 | array('Core', 'FinancialTrxn', 'to_financial_account_id'), |
128 | array('Financial', 'FinancialTypeAccount', 'financial_account_id'), | |
353ffa53 | 129 | ); |
b44e3f84 | 130 | foreach ($dependency as $name) { |
045f52a3 | 131 | require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php"; |
0e6e8724 DL |
132 | $className = "CRM_{$name[0]}_BAO_{$name[1]}"; |
133 | $bao = new $className(); | |
6a488035 | 134 | $bao->$name[2] = $financialAccountId; |
ac7514c2 PN |
135 | if ($bao->find(TRUE)) { |
136 | $check = TRUE; | |
6a488035 TO |
137 | } |
138 | } | |
0e6e8724 | 139 | |
6a488035 | 140 | if ($check) { |
ac7514c2 PN |
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")); | |
6a488035 | 143 | } |
0e6e8724 | 144 | |
cded2ebf | 145 | // delete from financial Type table |
ac7514c2 | 146 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
6a488035 TO |
147 | $financialAccount->id = $financialAccountId; |
148 | $financialAccount->delete(); | |
149 | } | |
0e6e8724 | 150 | |
6a488035 | 151 | /** |
fe482240 | 152 | * Get accounting code for a financial type with account relation Income Account is. |
6a488035 | 153 | * |
100fef9d | 154 | * @param int $financialTypeId |
da6b46f4 | 155 | * |
72b3a70c CW |
156 | * @return int |
157 | * accounting code | |
6a488035 | 158 | */ |
00be9182 | 159 | public static function getAccountingCode($financialTypeId) { |
f743a6eb | 160 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); |
6a488035 TO |
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 | |
165 | WHERE cft.id = %1 | |
166 | AND account_relationship = %2"; | |
167 | $params = array( | |
168 | 1 => array($financialTypeId, 'Integer'), | |
169 | 2 => array($relationTypeId, 'Integer'), | |
170 | ); | |
171 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
172 | } | |
0e6e8724 | 173 | |
7d289724 | 174 | /** |
fe482240 | 175 | * Get AR account. |
7d289724 | 176 | * |
ed5dd492 TO |
177 | * @param $financialAccountId |
178 | * Financial account id. | |
7d289724 | 179 | * |
ed5dd492 TO |
180 | * @param $financialAccountTypeId |
181 | * Financial account type id. | |
7d289724 | 182 | * |
72b3a70c CW |
183 | * @param string $accountTypeCode |
184 | * account type code | |
7d289724 | 185 | * |
df8d3074 | 186 | * @return int |
a6c01b45 | 187 | * count |
7d289724 | 188 | */ |
00be9182 | 189 | public static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') { |
ddaa8ef1 | 190 | if (!$financialAccountTypeId) { |
7d45c236 | 191 | $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' ")); |
ddaa8ef1 | 192 | } |
7d289724 | 193 | $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2 |
ddaa8ef1 | 194 | AND id != %3 AND is_active = 1;"; |
7d289724 PN |
195 | $params = array( |
196 | 1 => array($financialAccountTypeId, 'Integer'), | |
197 | 2 => array(strtolower($accountTypeCode), 'String'), | |
198 | 3 => array($financialAccountId, 'Integer'), | |
199 | ); | |
045f52a3 | 200 | return CRM_Core_DAO::singleValueQuery($query, $params); |
7d289724 | 201 | } |
96025800 | 202 | |
bf2cf926 | 203 | /** |
204 | * Get the Financial Account for a Financial Type Relationship Combo. | |
205 | * | |
206 | * Note that some relationships are optionally configured - so far | |
207 | * Chargeback and Credit / Contra. Since these are the only 2 currently Income | |
208 | * is an appropriate fallback. In future it might make sense to extend the logic. | |
209 | * | |
210 | * Note that we avoid the CRM_Core_PseudoConstant function as it stores one | |
211 | * account per financial type and is unreliable. | |
212 | * | |
213 | * @param int $financialTypeID | |
214 | * | |
215 | * @param string $relationshipType | |
216 | * | |
217 | * @return int | |
218 | */ | |
219 | public static function getFinancialAccountForFinancialTypeByRelationship($financialTypeID, $relationshipType) { | |
220 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE '{$relationshipType}' ")); | |
221 | ||
222 | if (!isset(Civi::$statics[__CLASS__]['entity_financial_account'][$financialTypeID][$relationTypeId])) { | |
223 | $accounts = civicrm_api3('EntityFinancialAccount', 'get', array( | |
224 | 'entity_id' => $financialTypeID, | |
225 | 'entity_table' => 'civicrm_financial_type', | |
226 | )); | |
227 | ||
228 | foreach ($accounts['values'] as $account) { | |
229 | Civi::$statics[__CLASS__]['entity_financial_account'][$financialTypeID][$account['account_relationship']] = $account['financial_account_id']; | |
230 | } | |
231 | ||
232 | $accountRelationships = CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL); | |
233 | ||
234 | $incomeAccountRelationshipID = array_search('Income Account is', $accountRelationships); | |
235 | $incomeAccountFinancialAccountID = Civi::$statics[__CLASS__]['entity_financial_account'][$financialTypeID][$incomeAccountRelationshipID]; | |
236 | ||
237 | foreach (array('Chargeback Account is', 'Credit/Contra Revenue Account is') as $optionalAccountRelationship) { | |
238 | ||
239 | $accountRelationshipID = array_search($optionalAccountRelationship, $accountRelationships); | |
240 | if (empty(Civi::$statics[__CLASS__]['entity_financial_account'][$financialTypeID][$accountRelationshipID])) { | |
52da5b1e | 241 | Civi::$statics[__CLASS__]['entity_financial_account'][$financialTypeID][$accountRelationshipID] = $incomeAccountFinancialAccountID; |
bf2cf926 | 242 | } |
243 | } | |
244 | ||
245 | } | |
246 | return Civi::$statics[__CLASS__]['entity_financial_account'][$financialTypeID][$relationTypeId]; | |
247 | } | |
248 | ||
e66512af | 249 | /** |
871de167 | 250 | * Get Financial Account type relations. |
e66512af PN |
251 | * |
252 | * @param $flip bool | |
253 | * | |
254 | * @return array | |
255 | * | |
256 | */ | |
eac838e9 | 257 | public static function getfinancialAccountRelations($flip = FALSE) { |
e66512af PN |
258 | $params = array('labelColumn' => 'name'); |
259 | $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params); | |
260 | $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params); | |
261 | $Links = array( | |
262 | 'Expense Account is' => 'Expenses', | |
263 | 'Accounts Receivable Account is' => 'Asset', | |
264 | 'Income Account is' => 'Revenue', | |
265 | 'Asset Account is' => 'Asset', | |
266 | 'Cost of Sales Account is' => 'Cost of Sales', | |
267 | 'Premiums Inventory Account is' => 'Asset', | |
268 | 'Discounts Account is' => 'Revenue', | |
269 | 'Sales Tax Account is' => 'Liability', | |
270 | 'Deferred Revenue Account is' => 'Liability', | |
271 | ); | |
272 | if (!$flip) { | |
273 | foreach ($Links as $accountRelation => $accountType) { | |
274 | $financialAccountLinks[array_search($accountRelation, $accountRelationships)] = array_search($accountType, $financialAccountType); | |
275 | } | |
276 | } | |
277 | else { | |
278 | foreach ($Links as $accountRelation => $accountType) { | |
279 | $financialAccountLinks[array_search($accountType, $financialAccountType)][] = array_search($accountRelation, $accountRelationships); | |
280 | } | |
281 | } | |
e66512af PN |
282 | return $financialAccountLinks; |
283 | } | |
284 | ||
d9eeb0f4 | 285 | /** |
871de167 | 286 | * Get Deferred Financial type. |
d9eeb0f4 PN |
287 | * |
288 | * @return array | |
289 | * | |
290 | */ | |
291 | public static function getDeferredFinancialType() { | |
292 | $deferredFinancialType = array(); | |
293 | $query = "SELECT ce.entity_id, cft.name FROM civicrm_entity_financial_account ce | |
d9eeb0f4 | 294 | INNER JOIN civicrm_financial_type cft ON ce.entity_id = cft.id |
c5d6e9b5 PN |
295 | WHERE ce.entity_table = 'civicrm_financial_type' AND ce.account_relationship = %1 AND cft.is_active = 1"; |
296 | $deferredAccountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' ")); | |
297 | $queryParams = array(1 => array($deferredAccountRel, 'Integer')); | |
298 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
d9eeb0f4 PN |
299 | while ($dao->fetch()) { |
300 | $deferredFinancialType[$dao->entity_id] = $dao->name; | |
301 | } | |
302 | return $deferredFinancialType; | |
303 | } | |
304 | ||
e805833b | 305 | /** |
b1b5303b | 306 | * Check if financial account is referenced by financial item. |
e805833b | 307 | * |
b1b5303b | 308 | * @param int $financialAccountId |
e805833b | 309 | * |
b1b5303b | 310 | * @param int $financialAccountTypeID |
ea996660 | 311 | * |
e805833b | 312 | * @return bool |
ea996660 PN |
313 | * |
314 | */ | |
315 | public static function validateFinancialAccount($financialAccountId, $financialAccountTypeID = NULL) { | |
e805833b | 316 | $sql = "SELECT f.financial_account_type_id FROM civicrm_financial_account f |
fed4d93d | 317 | INNER JOIN civicrm_financial_item fi ON fi.financial_account_id = f.id |
b1b5303b | 318 | WHERE f.id = %1 AND f.financial_account_type_id IN (%2) |
e805833b | 319 | LIMIT 1"; |
b1b5303b PN |
320 | $params = array('labelColumn' => 'name'); |
321 | $financialAccountType = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params); | |
322 | $params = array( | |
323 | 1 => array($financialAccountId, 'Integer'), | |
324 | 2 => array( | |
325 | implode(',', | |
326 | array( | |
327 | array_search('Revenue', $financialAccountType), | |
328 | array_search('Liability', $financialAccountType), | |
329 | ) | |
330 | ), | |
331 | 'Text' | |
332 | ), | |
333 | ); | |
e805833b | 334 | $result = CRM_Core_DAO::singleValueQuery($sql, $params); |
fed4d93d | 335 | if ($result && $result != $financialAccountTypeID) { |
e805833b PN |
336 | return TRUE; |
337 | } | |
338 | return FALSE; | |
339 | } | |
340 | ||
6a488035 | 341 | } |