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