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