GL Account in Company Defaults missing for test. Fixed
[fa-stable.git] / gl / includes / db / gl_db_accounts.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 function add_gl_account($account_code, $account_name, $account_type, $account_code2)
13 {
14         $sql = "INSERT INTO ".TB_PREF."chart_master (account_code, account_code2, account_name, account_type)
15                 VALUES (".db_escape($account_code).", ".db_escape($account_code2).", "
16                         .db_escape($account_name).", ".db_escape($account_type).")";
17
18         return db_query($sql);
19 }
20
21 function update_gl_account($account_code, $account_name, $account_type, $account_code2)
22 {
23     $sql = "UPDATE ".TB_PREF."chart_master SET account_name=".db_escape($account_name)
24     .",account_type=".db_escape($account_type).", account_code2=".db_escape($account_code2)
25                 ." WHERE account_code = ".db_escape($account_code);
26
27         return db_query($sql);
28 }
29
30 function delete_gl_account($code)
31 {
32         $sql = "DELETE FROM ".TB_PREF."chart_master WHERE account_code=".db_escape($code);
33
34         db_query($sql, "could not delete gl account");
35 }
36
37 function get_gl_accounts($from=null, $to=null, $type=null)
38 {
39         $sql = "SELECT coa.*, act_type.name AS AccountTypeName
40                 FROM "
41                         .TB_PREF."chart_master coa,"
42                         .TB_PREF."chart_types act_type
43                 WHERE coa.account_type=act_type.id";
44         if ($from != null)
45                 $sql .= " AND coa.account_code >= ".db_escape($from);
46         if ($to != null)
47                 $sql .= " AND coa.account_code <= ".db_escape($to);
48         if ($type != null)
49                 $sql .= " AND account_type=".db_escape($type);
50         $sql .= " ORDER BY account_code";
51
52         return db_query($sql, "could not get gl accounts");
53 }
54
55 function get_gl_account($code)
56 {
57         $sql = "SELECT * FROM ".TB_PREF."chart_master WHERE account_code=".db_escape($code);
58
59         $result = db_query($sql, "could not get gl account");
60         return db_fetch($result);
61 }
62
63 function is_account_balancesheet($code)
64 {
65         $sql = "SELECT act_class.ctype
66                 FROM "
67                         .TB_PREF."chart_class act_class, "
68                         .TB_PREF."chart_types act_type, "
69                         .TB_PREF."chart_master coa
70                 WHERE coa.account_type=act_type.id
71                         AND     act_type.class_id=act_class.cid
72                         AND coa.account_code=".db_escape($code);
73
74         $result = db_query($sql,"could not retreive the account class for $code");
75         $row = db_fetch_row($result);
76         return $row[0] > 0 && $row[0] < CL_INCOME;
77 }
78
79 function get_gl_account_name($code)
80 {
81         $sql = "SELECT account_name from ".TB_PREF."chart_master WHERE account_code=".db_escape($code);
82
83         $result = db_query($sql,"could not retreive the account name for $code");
84
85         if (db_num_rows($result) == 1)
86         {
87                 $row = db_fetch_row($result);
88                 return $row[0];
89         }
90
91         display_db_error("could not retreive the account name for $code", $sql, true);
92 }
93
94 function gl_account_in_company_defaults($acc)
95 {
96         $sql= "SELECT COUNT(*) 
97         FROM ".TB_PREF."sys_prefs
98         WHERE (name='debtors_act' 
99                 OR name='pyt_discount_act'
100                 OR name='creditors_act' 
101                 OR name='bank_charge_act' 
102                 OR name='exchange_diff_act'
103                 OR name='profit_loss_year_act'
104                 OR name='retained_earnings_act'
105                 OR name='freight_act'
106                 OR name='deferred_income_act'
107                 OR name='grn_clearing_act'
108                 OR name='default_sales_act' 
109                 OR name='default_sales_discount_act'
110                 OR name='default_prompt_payment_act'
111                 OR name='default_inventory_act'
112                 OR name='default_cogs_act'
113                 OR name='default_adj_act'
114                 OR name='default_inv_sales_act'
115                 OR name='default_wip_act') AND value=".db_escape($acc);
116         $result = db_query($sql,"Couldn't test for default company GL codes");
117
118         $myrow = db_fetch_row($result);
119         return ($myrow[0] > 0); 
120 }
121
122 function gl_account_in_stock_category($acc)
123 {
124         $acc = db_escape($acc);
125
126         $sql= "SELECT COUNT(*) 
127                 FROM ".TB_PREF."stock_category
128                 WHERE dflt_inventory_act=$acc 
129                         OR dflt_cogs_act=$acc
130                         OR dflt_adjustment_act=$acc 
131                         OR dflt_sales_act=$acc";
132         $result = db_query($sql,"Couldn't test for existing stock category GL codes");
133
134         $myrow = db_fetch_row($result);
135         return ($myrow[0] > 0); 
136 }
137
138 function gl_account_in_stock_master($acc)
139 {
140         $acc = db_escape($acc);
141
142         $sql= "SELECT COUNT(*)
143                 FROM ".TB_PREF."stock_master
144                 WHERE inventory_account=$acc 
145                         OR cogs_account=$acc
146                         OR adjustment_account=$acc 
147                         OR sales_account=$acc";
148         $result = db_query($sql,"Couldn't test for existing stock GL codes");
149
150         $myrow = db_fetch_row($result);
151         return ($myrow[0] > 0); 
152 }
153
154 function gl_account_in_tax_types($acc)
155 {
156         $acc = db_escape($acc);
157
158         $sql= "SELECT COUNT(*)
159                 FROM ".TB_PREF."tax_types
160                 WHERE sales_gl_code=$acc 
161                         OR purchasing_gl_code=$acc";
162         $result = db_query($sql,"Couldn't test for existing tax GL codes");
163
164         $myrow = db_fetch_row($result);
165         return ($myrow[0] > 0); 
166 }
167
168 function gl_account_in_cust_branch($acc)
169 {
170         $acc = db_escape($acc);
171
172         $sql= "SELECT COUNT(*)
173                 FROM ".TB_PREF."cust_branch
174                 WHERE sales_account=$acc 
175                         OR sales_discount_account=$acc
176                         OR receivables_account=$acc
177                         OR payment_discount_account=$acc";
178         $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
179
180         $myrow = db_fetch_row($result);
181         return ($myrow[0] > 0); 
182 }
183
184 function gl_account_in_suppliers($acc)
185 {
186         $acc = db_escape($acc);
187
188         $sql= "SELECT COUNT(*)
189                 FROM ".TB_PREF."suppliers
190                 WHERE purchase_account=$acc
191                         OR payment_discount_account=$acc
192                         OR payable_account=$acc";
193         $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
194
195         $myrow = db_fetch_row($result);
196         return ($myrow[0] > 0); 
197 }
198
199 function gl_account_in_quick_entry_lines($acc)
200 {
201         $acc = db_escape($acc);
202
203         $sql=
204         "SELECT COUNT(*)
205                 FROM ".TB_PREF."quick_entry_lines
206                 WHERE dest_id=$acc AND UPPER(LEFT(action, 1)) <> 'T'";
207         $result = db_query($sql,"Couldn't test for existing Quick Entry Line GL codes");
208
209         $myrow = db_fetch_row($result);
210         return ($myrow[0] > 0); 
211 }
212 //
213 //      Returns n>0 when account is AR, n<0 when account is AP
214 //  (priority for AR accounts)
215 //
216 function is_subledger_account($account)
217 {
218         $sql = "SELECT 1 
219                 FROM ".TB_PREF."cust_branch
220                 WHERE receivables_account=".db_escape($account)
221         ." UNION
222                 SELECT -1 
223                 FROM ".TB_PREF."suppliers 
224                 WHERE payable_account=".db_escape($account);
225
226         $result = db_query($sql,"Couldn't test AR/AP account");
227         $myrow = db_fetch_row($result);
228         return $myrow == false ? 0 : $myrow[0];
229 }
230
231 function get_subaccount_data($code_id, $person_id)
232 {
233         $sql = "SELECT debtor_ref as name, branch_code as id 
234                 FROM ".TB_PREF."cust_branch branch 
235                         LEFT JOIN ".TB_PREF."debtors_master d ON branch.debtor_no = d.debtor_no
236                 WHERE branch.receivables_account=".db_escape($code_id)
237                         ." AND d.debtor_no=".db_escape($person_id)
238                 ." UNION SELECT supp_ref as name, '' as id 
239                         FROM ".TB_PREF."suppliers supp
240                         WHERE supplier_id=".db_escape($person_id)." 
241                                 AND payable_account=".db_escape($code_id);
242         $result = db_query($sql, 'cannot retrieve counterparty name');
243
244         return  db_fetch($result);
245 }
246
247 function get_subaccount_name($code_id, $person_id)
248 {
249         $sql = "SELECT debtor_ref as ref
250                 FROM ".TB_PREF."cust_branch branch
251                         LEFT JOIN ".TB_PREF."debtors_master d ON branch.debtor_no = d.debtor_no
252                 WHERE branch.receivables_account=".db_escape($code_id)
253                         ." AND d.debtor_no=".db_escape($person_id)
254                 ." UNION SELECT supp_ref as ref
255                         FROM ".TB_PREF."suppliers supp
256                         WHERE payable_account=".db_escape($code_id)." 
257                                 AND supplier_id=".db_escape($person_id);
258         $result = db_query($sql, 'cannot retrieve counterparty name');
259         $row = db_fetch($result);
260
261         return  $row ? $row['ref'] : '';
262 }
263
264 function gl_account_in_bank_accounts($acc)
265 {
266         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE 
267                 account_code=".db_escape($acc);
268         $result = db_query($sql,"Couldn't test bank account GL codes");
269
270         $myrow = db_fetch_row($result);
271         return ($myrow[0] > 0); 
272 }
273
274 //----------------------------------------------------------------------------------
275 //      Check if given account is used by any bank_account. 
276 //      Returns id of first bank_account using account_code, null otherwise.
277 //
278 //      Keep in mind that direct posting to bank account is depreciated
279 //      because we have no way to select right bank account if 
280 //      there is more than one using given gl account.
281 //
282 function is_bank_account($account_code)
283 {
284         $sql= "SELECT id FROM ".TB_PREF."bank_accounts WHERE account_code=".db_escape($account_code);
285         $result = db_query($sql, "checking account is bank account");
286         if (db_num_rows($result) > 0) {
287                 $acct = db_fetch($result);
288                 return $acct['id'];
289         } else
290                 return false;
291 }
292
293 function get_chart_accounts_search($like, $skip=false)
294 {
295         global $SysPrefs;
296
297         if (isset($SysPrefs->max_rows_in_search))
298                 $limit = $SysPrefs->max_rows_in_search;
299         else
300                 $limit = 10;
301
302         if ($skip)
303                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
304                         FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
305                         ."LEFT JOIN ".TB_PREF."bank_accounts acc "
306                         ."ON chart.account_code=acc.account_code
307                                 WHERE acc.account_code  IS NULL
308                         AND chart.account_type=type.id ";
309         else            
310                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
311                         FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type WHERE chart.account_type=type.id ";
312         $sql .= "AND (
313                 chart.account_name LIKE " . db_escape("%" . $like. "%") . " OR
314                 chart.account_code LIKE " . db_escape("%" . $like. "%") . "
315                 ) 
316                 ORDER BY chart.account_code LIMIT 0,".(int)($limit); // We only display 10 items.
317         return db_query($sql, "Failed in retreiving GL account list.");
318 }