Merge pull request #7181 from eileenmcnaughton/CRM-17335-2
[civicrm-core.git] / CRM / Financial / BAO / FinancialAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 */
33 class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAccount {
34
35 /**
36 * Static holder for the default LT.
37 */
38 static $_defaultContributionType = NULL;
39
40 /**
41 * Class constructor.
42 */
43 public function __construct() {
44 parent::__construct();
45 }
46
47 /**
48 * Fetch object based on array of properties.
49 *
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.
54 *
55 * @return CRM_Financial_BAO_FinancialAccount
56 */
57 public static function retrieve(&$params, &$defaults) {
58 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
59 $financialAccount->copyValues($params);
60 if ($financialAccount->find(TRUE)) {
61 CRM_Core_DAO::storeValues($financialAccount, $defaults);
62 return $financialAccount;
63 }
64 return NULL;
65 }
66
67 /**
68 * Update the is_active flag in the db.
69 *
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.
74 *
75 * @return CRM_Core_DAO|null
76 * DAO object on success, null otherwise
77 */
78 public static function setIsActive($id, $is_active) {
79 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active);
80 }
81
82 /**
83 * Add the financial types.
84 *
85 * @param array $params
86 * Reference array contains the values submitted by the form.
87 * @param array $ids
88 * Reference array contains the id.
89 *
90 * @return CRM_Financial_DAO_FinancialAccount
91 */
92 public static function add(&$params, &$ids = array()) {
93 if (empty($params['id'])) {
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 }
100 if (!empty($params['is_default'])) {
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);
104 }
105
106 // action is taken depending upon the mode
107 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
108 $financialAccount->copyValues($params);
109 if (!empty($ids['contributionType'])) {
110 $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids);
111 }
112 $financialAccount->save();
113 return $financialAccount;
114 }
115
116 /**
117 * Delete financial Types.
118 *
119 * @param int $financialAccountId
120 */
121 public static function del($financialAccountId) {
122 // checking if financial type is present
123 $check = FALSE;
124
125 //check dependencies
126 $dependency = array(
127 array('Core', 'FinancialTrxn', 'to_financial_account_id'),
128 array('Financial', 'FinancialTypeAccount', 'financial_account_id'),
129 );
130 foreach ($dependency as $name) {
131 require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php";
132 $className = "CRM_{$name[0]}_BAO_{$name[1]}";
133 $bao = new $className();
134 $bao->$name[2] = $financialAccountId;
135 if ($bao->find(TRUE)) {
136 $check = TRUE;
137 }
138 }
139
140 if ($check) {
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"));
143 }
144
145 // delete from financial Type table
146 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
147 $financialAccount->id = $financialAccountId;
148 $financialAccount->delete();
149 }
150
151 /**
152 * Get accounting code for a financial type with account relation Income Account is.
153 *
154 * @param int $financialTypeId
155 *
156 * @return int
157 * accounting code
158 */
159 public static function getAccountingCode($financialTypeId) {
160 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
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 }
173
174 /**
175 * Get AR account.
176 *
177 * @param $financialAccountId
178 * Financial account id.
179 *
180 * @param $financialAccountTypeId
181 * Financial account type id.
182 *
183 * @param string $accountTypeCode
184 * account type code
185 *
186 * @return int
187 * count
188 */
189 public static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') {
190 if (!$financialAccountTypeId) {
191 $financialAccountTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
192 }
193 $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2
194 AND id != %3 AND is_active = 1;";
195 $params = array(
196 1 => array($financialAccountTypeId, 'Integer'),
197 2 => array(strtolower($accountTypeCode), 'String'),
198 3 => array($financialAccountId, 'Integer'),
199 );
200 return CRM_Core_DAO::singleValueQuery($query, $params);
201 }
202
203 }