178d98b78175ad9762432b746187cddd576459a4
[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='default_sales_act' 
108                 OR name='default_sales_discount_act'
109                 OR name='default_prompt_payment_act'
110                 OR name='default_inventory_act'
111                 OR name='default_cogs_act'
112                 OR name='default_adj_act'
113                 OR name='default_inv_sales_act'
114                 OR name='default_wip_act') AND value=".db_escape($acc);
115         $result = db_query($sql,"Couldn't test for default company GL codes");
116
117         $myrow = db_fetch_row($result);
118         return ($myrow[0] > 0); 
119 }
120
121 function gl_account_in_stock_category($acc)
122 {
123         $acc = db_escape($acc);
124
125         $sql= "SELECT COUNT(*) 
126                 FROM ".TB_PREF."stock_category
127                 WHERE dflt_inventory_act=$acc 
128                         OR dflt_cogs_act=$acc
129                         OR dflt_adjustment_act=$acc 
130                         OR dflt_sales_act=$acc";
131         $result = db_query($sql,"Couldn't test for existing stock category GL codes");
132
133         $myrow = db_fetch_row($result);
134         return ($myrow[0] > 0); 
135 }
136
137 function gl_account_in_stock_master($acc)
138 {
139         $acc = db_escape($acc);
140
141         $sql= "SELECT COUNT(*)
142                 FROM ".TB_PREF."stock_master
143                 WHERE inventory_account=$acc 
144                         OR cogs_account=$acc
145                         OR adjustment_account=$acc 
146                         OR sales_account=$acc";
147         $result = db_query($sql,"Couldn't test for existing stock GL codes");
148
149         $myrow = db_fetch_row($result);
150         return ($myrow[0] > 0); 
151 }
152
153 function gl_account_in_tax_types($acc)
154 {
155         $acc = db_escape($acc);
156
157         $sql= "SELECT COUNT(*)
158                 FROM ".TB_PREF."tax_types
159                 WHERE sales_gl_code=$acc 
160                         OR purchasing_gl_code=$acc";
161         $result = db_query($sql,"Couldn't test for existing tax GL codes");
162
163         $myrow = db_fetch_row($result);
164         return ($myrow[0] > 0); 
165 }
166
167 function gl_account_in_cust_branch($acc)
168 {
169         $acc = db_escape($acc);
170
171         $sql= "SELECT COUNT(*)
172                 FROM ".TB_PREF."cust_branch
173                 WHERE sales_account=$acc 
174                         OR sales_discount_account=$acc
175                         OR receivables_account=$acc
176                         OR payment_discount_account=$acc";
177         $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
178
179         $myrow = db_fetch_row($result);
180         return ($myrow[0] > 0); 
181 }
182
183 function gl_account_in_suppliers($acc)
184 {
185         $acc = db_escape($acc);
186
187         $sql= "SELECT COUNT(*)
188                 FROM ".TB_PREF."suppliers
189                 WHERE purchase_account=$acc
190                         OR payment_discount_account=$acc
191                         OR payable_account=$acc";
192         $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
193
194         $myrow = db_fetch_row($result);
195         return ($myrow[0] > 0); 
196 }
197
198 function gl_account_in_quick_entry_lines($acc)
199 {
200         $acc = db_escape($acc);
201
202         $sql=
203         "SELECT COUNT(*)
204                 FROM ".TB_PREF."quick_entry_lines
205                 WHERE dest_id=$acc AND UPPER(LEFT(action, 1)) <> 'T'";
206         $result = db_query($sql,"Couldn't test for existing Quick Entry Line GL codes");
207
208         $myrow = db_fetch_row($result);
209         return ($myrow[0] > 0); 
210 }
211 //
212 //      Returns n>0 when account is AR, n<0 when account is AP
213 //  (priority for AR accounts)
214 //
215 function is_subledger_account($account)
216 {
217         $sql = "SELECT 1 
218                 FROM ".TB_PREF."cust_branch
219                 WHERE receivables_account=".db_escape($account)
220         ." UNION
221                 SELECT -1 
222                 FROM ".TB_PREF."suppliers 
223                 WHERE payable_account=".db_escape($account);
224
225         $result = db_query($sql,"Couldn't test AR/AP account");
226         $myrow = db_fetch_row($result);
227         return $myrow == false ? 0 : $myrow[0];
228 }
229
230 function get_subaccount_data($code_id, $person_id)
231 {
232         $sql = "SELECT debtor_ref as name, branch_code as id 
233                 FROM ".TB_PREF."cust_branch branch 
234                         LEFT JOIN ".TB_PREF."debtors_master d ON branch.debtor_no = d.debtor_no
235                 WHERE branch.receivables_account=".db_escape($code_id)
236                         ." AND d.debtor_no=".db_escape($person_id)
237                 ." UNION SELECT supp_ref as name, '' as id 
238                         FROM ".TB_PREF."suppliers supp
239                         WHERE supplier_id=".db_escape($person_id)." 
240                                 AND payable_account=".db_escape($code_id);
241         $result = db_query($sql, 'cannot retrieve counterparty name');
242
243         return  db_fetch($result);
244 }
245
246 function get_subaccount_name($code_id, $person_id)
247 {
248         $sql = "SELECT debtor_ref as ref
249                 FROM ".TB_PREF."cust_branch branch
250                         LEFT JOIN ".TB_PREF."debtors_master d ON branch.debtor_no = d.debtor_no
251                 WHERE branch.receivables_account=".db_escape($code_id)
252                         ." AND d.debtor_no=".db_escape($person_id)
253                 ." UNION SELECT supp_ref as ref
254                         FROM ".TB_PREF."suppliers supp
255                         WHERE payable_account=".db_escape($code_id)." 
256                                 AND supplier_id=".db_escape($person_id);
257         $result = db_query($sql, 'cannot retrieve counterparty name');
258         $row = db_fetch($result);
259
260         return  $row ? $row['ref'] : '';
261 }
262
263 function gl_account_in_bank_accounts($acc)
264 {
265         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE 
266                 account_code=".db_escape($acc);
267         $result = db_query($sql,"Couldn't test bank account GL codes");
268
269         $myrow = db_fetch_row($result);
270         return ($myrow[0] > 0); 
271 }
272
273 //----------------------------------------------------------------------------------
274 //      Check if given account is used by any bank_account. 
275 //      Returns id of first bank_account using account_code, null otherwise.
276 //
277 //      Keep in mind that direct posting to bank account is depreciated
278 //      because we have no way to select right bank account if 
279 //      there is more than one using given gl account.
280 //
281 function is_bank_account($account_code)
282 {
283         $sql= "SELECT id FROM ".TB_PREF."bank_accounts WHERE account_code=".db_escape($account_code);
284         $result = db_query($sql, "checking account is bank account");
285         if (db_num_rows($result) > 0) {
286                 $acct = db_fetch($result);
287                 return $acct['id'];
288         } else
289                 return false;
290 }
291
292 function get_chart_accounts_search($like, $skip=false)
293 {
294         global $SysPrefs;
295
296         if (isset($SysPrefs->max_rows_in_search))
297                 $limit = $SysPrefs->max_rows_in_search;
298         else
299                 $limit = 10;
300
301         if ($skip)
302                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
303                         FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
304                         ."LEFT JOIN ".TB_PREF."bank_accounts acc "
305                         ."ON chart.account_code=acc.account_code
306                                 WHERE acc.account_code  IS NULL
307                         AND chart.account_type=type.id ";
308         else            
309                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
310                         FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type WHERE chart.account_type=type.id ";
311         $sql .= "AND (
312                 chart.account_name LIKE " . db_escape("%" . $like. "%") . " OR
313                 chart.account_code LIKE " . db_escape("%" . $like. "%") . "
314                 ) 
315                 ORDER BY chart.account_code LIMIT 0,".(int)($limit); // We only display 10 items.
316         return db_query($sql, "Failed in retreiving GL account list.");
317 }