Moved all SQL statements from PHP files into relevant *_db.inc files.
[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)
38 {
39         $sql = "SELECT ".TB_PREF."chart_master.*,".TB_PREF."chart_types.name AS AccountTypeName
40                 FROM ".TB_PREF."chart_master,".TB_PREF."chart_types
41                 WHERE ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id";
42         if ($from != null)
43                 $sql .= " AND ".TB_PREF."chart_master.account_code >= ".db_escape($from);
44         if ($to != null)
45                 $sql .= " AND ".TB_PREF."chart_master.account_code <= ".db_escape($to);
46         $sql .= " ORDER BY account_code";
47
48         return db_query($sql, "could not get gl accounts");
49 }
50
51 function get_gl_accounts_all($balance=-1)
52 {
53         if ($balance == 1)
54                 $where ="WHERE ctype>0 AND ctype<".CL_INCOME;
55         elseif ($balance == 0)  
56                 $where ="WHERE ctype>".CL_EQUITY." OR ctype=0"; // backwards compatibility
57         $sql = "SELECT ".TB_PREF."chart_master.account_code, ".TB_PREF."chart_master.account_name, ".TB_PREF."chart_master.account_code2,
58                 ".TB_PREF."chart_types.name AS AccountTypeName,".TB_PREF."chart_types.id AS AccountType,
59                 ".TB_PREF."chart_types.parent, ".TB_PREF."chart_class.class_name AS AccountClassName, ".TB_PREF."chart_class.cid AS ClassID, 
60                 ".TB_PREF."chart_class.ctype AS ClassType
61                 FROM ".TB_PREF."chart_types INNER JOIN ".TB_PREF."chart_class ON ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid
62                 LEFT JOIN ".TB_PREF."chart_master ON ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id ";
63         if ($balance != -1)
64                 $sql .= $where;                                 
65          $sql .= " ORDER BY ".TB_PREF."chart_class.cid, IF(parent > 0,parent,".TB_PREF."chart_types.id), 
66                 IF(parent > 0,".TB_PREF."chart_types.id, parent), ".TB_PREF."chart_master.account_code";
67
68         return db_query($sql, "could not get gl accounts");
69 }
70
71 function get_gl_account($code)
72 {
73         $sql = "SELECT * FROM ".TB_PREF."chart_master WHERE account_code=".db_escape($code);
74
75         $result = db_query($sql, "could not get gl account");
76         return db_fetch($result);
77 }
78
79 function is_account_balancesheet($code)
80 {
81         $sql = "SELECT ".TB_PREF."chart_class.ctype FROM ".TB_PREF."chart_class, "
82                 .TB_PREF."chart_types, ".TB_PREF."chart_master
83                 WHERE ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id AND
84                 ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid
85                 AND ".TB_PREF."chart_master.account_code=".db_escape($code);
86
87         $result = db_query($sql,"could not retreive the account class for $code");
88         $row = db_fetch_row($result);
89         return $row[0] > 0 && $row[0] < CL_INCOME;
90 }
91
92 function get_gl_account_name($code)
93 {
94         $sql = "SELECT account_name from ".TB_PREF."chart_master WHERE account_code=".db_escape($code);
95
96         $result = db_query($sql,"could not retreive the account name for $code");
97
98         if (db_num_rows($result) == 1)
99         {
100                 $row = db_fetch_row($result);
101                 return $row[0];
102         }
103
104         display_db_error("could not retreive the account name for $code", $sql, true);
105 }
106
107 function gl_account_in_transactions($acc)
108 {
109         $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account=$acc";
110         $result = db_query($sql,"Couldn't test for existing transactions");
111
112         $myrow = db_fetch_row($result);
113         return ($myrow[0] > 0); 
114 }
115
116 function gl_account_in_company_defaults($acc)
117 {
118         $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act=$acc 
119                 OR pyt_discount_act=$acc
120                 OR creditors_act=$acc 
121                 OR bank_charge_act=$acc 
122                 OR exchange_diff_act=$acc
123                 OR profit_loss_year_act=$acc
124                 OR retained_earnings_act=$acc
125                 OR freight_act=$acc
126                 OR default_sales_act=$acc 
127                 OR default_sales_discount_act=$acc
128                 OR default_prompt_payment_act=$acc
129                 OR default_inventory_act=$acc
130                 OR default_cogs_act=$acc
131                 OR default_adj_act=$acc
132                 OR default_inv_sales_act=$acc
133                 OR default_assembly_act=$acc";
134         $result = db_query($sql,"Couldn't test for default company GL codes");
135
136         $myrow = db_fetch_row($result);
137         return ($myrow[0] > 0); 
138 }
139
140 function gl_account_in_bank_accounts($acc)
141 {
142         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code=$acc";
143         $result = db_query($sql,"Couldn't test for bank accounts");
144
145         $myrow = db_fetch_row($result);
146         return ($myrow[0] > 0); 
147 }
148
149 function gl_account_in_stock_category($acc)
150 {
151         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE 
152                 dflt_inventory_act=$acc 
153                 OR dflt_cogs_act=$acc
154                 OR dflt_adjustment_act=$acc 
155                 OR dflt_sales_act=$acc";
156         $result = db_query($sql,"Couldn't test for existing stock category GL codes");
157
158         $myrow = db_fetch_row($result);
159         return ($myrow[0] > 0); 
160 }
161
162 function gl_account_in_stock_master($acc)
163 {
164         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE 
165                 inventory_account=$acc 
166                 OR cogs_account=$acc
167                 OR adjustment_account=$acc 
168                 OR sales_account=$acc";
169         $result = db_query($sql,"Couldn't test for existing stock GL codes");
170
171         $myrow = db_fetch_row($result);
172         return ($myrow[0] > 0); 
173 }
174
175 function gl_account_in_tax_types($acc)
176 {
177         $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code=$acc OR purchasing_gl_code=$acc";
178         $result = db_query($sql,"Couldn't test for existing tax GL codes");
179
180         $myrow = db_fetch_row($result);
181         return ($myrow[0] > 0); 
182 }
183
184 function gl_account_in_cust_branch($acc)
185 {
186         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE 
187                 sales_account=$acc 
188                 OR sales_discount_account=$acc
189                 OR receivables_account=$acc
190                 OR payment_discount_account=$acc";
191         $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
192
193         $myrow = db_fetch_row($result);
194         return ($myrow[0] > 0); 
195 }
196
197 function gl_account_in_suppliers($acc)
198 {
199         $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE 
200                 purchase_account=$acc
201                 OR payment_discount_account=$acc
202                 OR payable_account=$acc";
203         $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
204
205         $myrow = db_fetch_row($result);
206         return ($myrow[0] > 0); 
207 }
208
209 function gl_account_in_quick_entry_lines($acc)
210 {
211         $sql= "SELECT COUNT(*) FROM ".TB_PREF."quick_entry_lines WHERE 
212                 dest_id=$acc AND UPPER(LEFT(action, 1)) <> 'T'";
213         $result = db_query($sql,"Couldn't test for existing Quick Entry Line GL codes");
214
215         $myrow = db_fetch_row($result);
216         return ($myrow[0] > 0); 
217 }
218 ?>