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)
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).")";
18 return db_query($sql);
21 function update_gl_account($account_code, $account_name, $account_type, $account_code2)
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);
27 return db_query($sql);
30 function delete_gl_account($code)
32 $sql = "DELETE FROM ".TB_PREF."chart_master WHERE account_code=".db_escape($code);
34 db_query($sql, "could not delete gl account");
37 function get_gl_accounts($from=null, $to=null, $type=null)
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";
43 $sql .= " AND ".TB_PREF."chart_master.account_code >= ".db_escape($from);
45 $sql .= " AND ".TB_PREF."chart_master.account_code <= ".db_escape($to);
47 $sql .= " AND account_type=".db_escape($type);
48 $sql .= " ORDER BY account_code";
50 return db_query($sql, "could not get gl accounts");
53 function get_gl_account($code)
55 $sql = "SELECT * FROM ".TB_PREF."chart_master WHERE account_code=".db_escape($code);
57 $result = db_query($sql, "could not get gl account");
58 return db_fetch($result);
61 function is_account_balancesheet($code)
63 $sql = "SELECT ".TB_PREF."chart_class.ctype FROM ".TB_PREF."chart_class, "
64 .TB_PREF."chart_types, ".TB_PREF."chart_master
65 WHERE ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id AND
66 ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid
67 AND ".TB_PREF."chart_master.account_code=".db_escape($code);
69 $result = db_query($sql,"could not retreive the account class for $code");
70 $row = db_fetch_row($result);
71 return $row[0] > 0 && $row[0] < CL_INCOME;
74 function get_gl_account_name($code)
76 $sql = "SELECT account_name from ".TB_PREF."chart_master WHERE account_code=".db_escape($code);
78 $result = db_query($sql,"could not retreive the account name for $code");
80 if (db_num_rows($result) == 1)
82 $row = db_fetch_row($result);
86 display_db_error("could not retreive the account name for $code", $sql, true);
89 function gl_account_in_company_defaults($acc)
91 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sys_prefs WHERE (name='debtors_act'
92 OR name='pyt_discount_act'
93 OR name='creditors_act'
94 OR name='bank_charge_act'
95 OR name='exchange_diff_act'
96 OR name='profit_loss_year_act'
97 OR name='retained_earnings_act'
99 OR name='deferred_income_act'
100 OR name='default_sales_act'
101 OR name='default_sales_discount_act'
102 OR name='default_prompt_payment_act'
103 OR name='default_inventory_act'
104 OR name='default_cogs_act'
105 OR name='default_adj_act'
106 OR name='default_inv_sales_act'
107 OR name='default_assembly_act') AND value=".db_escape($acc);
108 $result = db_query($sql,"Couldn't test for default company GL codes");
110 $myrow = db_fetch_row($result);
111 return ($myrow[0] > 0);
114 function gl_account_in_stock_category($acc)
116 $acc = db_escape($acc);
118 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_category WHERE
119 dflt_inventory_act=$acc
120 OR dflt_cogs_act=$acc
121 OR dflt_adjustment_act=$acc
122 OR dflt_sales_act=$acc";
123 $result = db_query($sql,"Couldn't test for existing stock category GL codes");
125 $myrow = db_fetch_row($result);
126 return ($myrow[0] > 0);
129 function gl_account_in_stock_master($acc)
131 $acc = db_escape($acc);
133 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
134 inventory_account=$acc
136 OR adjustment_account=$acc
137 OR sales_account=$acc";
138 $result = db_query($sql,"Couldn't test for existing stock GL codes");
140 $myrow = db_fetch_row($result);
141 return ($myrow[0] > 0);
144 function gl_account_in_tax_types($acc)
146 $acc = db_escape($acc);
148 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code=$acc OR purchasing_gl_code=$acc";
149 $result = db_query($sql,"Couldn't test for existing tax GL codes");
151 $myrow = db_fetch_row($result);
152 return ($myrow[0] > 0);
155 function gl_account_in_cust_branch($acc)
157 $acc = db_escape($acc);
159 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
161 OR sales_discount_account=$acc
162 OR receivables_account=$acc
163 OR payment_discount_account=$acc";
164 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
166 $myrow = db_fetch_row($result);
167 return ($myrow[0] > 0);
170 function gl_account_in_suppliers($acc)
172 $acc = db_escape($acc);
174 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
175 purchase_account=$acc
176 OR payment_discount_account=$acc
177 OR payable_account=$acc";
178 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
180 $myrow = db_fetch_row($result);
181 return ($myrow[0] > 0);
184 function gl_account_in_quick_entry_lines($acc)
186 $acc = db_escape($acc);
188 $sql= "SELECT COUNT(*) FROM ".TB_PREF."quick_entry_lines WHERE
189 dest_id=$acc AND UPPER(LEFT(action, 1)) <> 'T'";
190 $result = db_query($sql,"Couldn't test for existing Quick Entry Line GL codes");
192 $myrow = db_fetch_row($result);
193 return ($myrow[0] > 0);
196 // Returns n>0 when account is AR, n<0 when account is AP
197 // (priority for AR accounts)
199 function is_subledger_account($account)
201 $sql = "SELECT 1 FROM ".TB_PREF."cust_branch WHERE receivables_account=".db_escape($account)
202 ." UNION SELECT -1 FROM ".TB_PREF."suppliers WHERE payable_account=".db_escape($account);
204 $result = db_query($sql,"Couldn't test AR/AP account");
205 $myrow = db_fetch_row($result);
209 function get_subaccount_name($code_id, $person_id)
211 $sql = "SELECT debtor_ref as ref FROM ".TB_PREF."cust_branch branch LEFT JOIN ".TB_PREF."debtors_master d ON branch.debtor_no = d.debtor_no
212 WHERE branch.receivables_account=".db_escape($code_id)." AND d.debtor_no=".db_escape($person_id)
213 ." UNION SELECT supp_ref as ref FROM ".TB_PREF."suppliers supp
214 WHERE payable_account=".db_escape($code_id)." AND supplier_id=".db_escape($person_id);
215 $result = db_query($sql, 'cannot retrieve counterparty name');
216 $row = db_fetch($result);
218 return $row ? $row['ref'] : '';
221 function gl_account_in_bank_accounts($acc)
223 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE
224 account_code=".db_escape($acc);
225 $result = db_query($sql,"Couldn't test bank account GL codes");
227 $myrow = db_fetch_row($result);
228 return ($myrow[0] > 0);