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