df1f3df88849555ecfe8f6affdbff136ced009a4
[fa-stable.git] / gl / manage / gl_accounts.php
1 <?php
2
3 $page_security = 10;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Chart of Accounts"));
8
9 include($path_to_root . "/includes/ui.inc");
10 include($path_to_root . "/gl/includes/gl_db.inc");
11 include_once($path_to_root . "/includes/data_checks.inc");
12
13 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
14
15 //-------------------------------------------------------------------------------------
16
17 if (isset($_POST['Select'])) 
18 {
19         $_POST['selected_account'] = $_POST['AccountList'];
20 }
21
22 if (isset($_POST['selected_account']))
23 {
24         $selected_account = $_POST['selected_account'];
25
26 elseif (isset($_GET['selected_account']))
27 {
28         $selected_account = $_GET['selected_account'];
29 }
30 else
31         $selected_account = "";
32
33 //-------------------------------------------------------------------------------------
34
35 if (isset($_POST['add']) || isset($_POST['update'])) 
36 {
37
38         $input_error = 0;
39
40         if (strlen($_POST['account_code']) == 0) 
41         {
42                 $input_error = 1;
43                 display_error( _("The account code must be entered."));
44                 set_focus('account_code');
45         } 
46         elseif (strlen($_POST['account_name']) == 0) 
47         {
48                 $input_error = 1;
49                 display_error( _("The account name cannot be empty."));
50                 set_focus('account_name');
51         } 
52         elseif (!$accounts_alpha && !is_numeric($_POST['account_code'])) 
53         {
54             $input_error = 1;
55             display_error( _("The account code must be numeric."));
56                 set_focus('account_code');
57         }
58
59         if ($input_error != 1)
60         {
61                 if ($accounts_alpha == 2)
62                         $_POST['account_code'] = strtoupper($_POST['account_code']);
63         if ($selected_account)
64                 update_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);                 
65         else
66                 add_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
67                 meta_forward($_SERVER['PHP_SELF']);     
68         }
69
70
71 //-------------------------------------------------------------------------------------
72
73 function can_delete($selected_account)
74 {
75         if ($selected_account == "")
76                 return false;
77         $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
78         $result = db_query($sql,"Couldn't test for existing transactions");
79
80         $myrow = db_fetch_row($result);
81         if ($myrow[0] > 0) 
82         {
83                 display_error(_("Cannot delete this account because transactions have been created using this account."));
84                 return false;
85         }
86
87         $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act='$selected_account' 
88                 OR pyt_discount_act='$selected_account' 
89                 OR creditors_act='$selected_account' 
90                 OR grn_act='$selected_account' 
91                 OR exchange_diff_act='$selected_account' 
92                 OR purch_exchange_diff_act='$selected_account' 
93                 OR retained_earnings_act='$selected_account'
94                 OR freight_act='$selected_account'
95                 OR default_sales_act='$selected_account' 
96                 OR default_sales_discount_act='$selected_account'
97                 OR default_prompt_payment_act='$selected_account'
98                 OR default_inventory_act='$selected_account'
99                 OR default_cogs_act='$selected_account'
100                 OR default_adj_act='$selected_account'
101                 OR default_inv_sales_act='$selected_account'
102                 OR default_assembly_act='$selected_account'
103                 OR payroll_act='$selected_account'";
104         $result = db_query($sql,"Couldn't test for default company GL codes");
105
106         $myrow = db_fetch_row($result);
107         if ($myrow[0] > 0) 
108         {
109                 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
110                 return false;
111         }
112         
113         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code='$selected_account'";
114         $result = db_query($sql,"Couldn't test for bank accounts");
115
116         $myrow = db_fetch_row($result);
117         if ($myrow[0] > 0) 
118         {
119                 display_error(_("Cannot delete this account because it is used by a bank account."));
120                 return false;
121         }       
122
123         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE 
124                 inventory_account='$selected_account' 
125                 OR cogs_account='$selected_account'
126                 OR adjustment_account='$selected_account' 
127                 OR sales_account='$selected_account'";
128         $result = db_query($sql,"Couldn't test for existing stock GL codes");
129
130         $myrow = db_fetch_row($result);
131         if ($myrow[0] > 0) 
132         {
133                 display_error(_("Cannot delete this account because it is used by one or more Items."));
134                 return false;
135         }       
136         
137         $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code='$selected_account' OR purchasing_gl_code='$selected_account'";
138         $result = db_query($sql,"Couldn't test for existing tax GL codes");
139
140         $myrow = db_fetch_row($result);
141         if ($myrow[0] > 0) 
142         {
143                 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
144                 return false;
145         }       
146         
147         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE 
148                 sales_account='$selected_account' 
149                 OR sales_discount_account='$selected_account'
150                 OR receivables_account='$selected_account'
151                 OR payment_discount_account='$selected_account'";
152         $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
153
154         $myrow = db_fetch_row($result);
155         if ($myrow[0] > 0) 
156         {
157                 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
158                 return false;
159         }               
160         
161         $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE 
162                 purchase_account='$selected_account' 
163                 OR payment_discount_account='$selected_account' 
164                 OR payable_account='$selected_account'";
165         $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
166
167         $myrow = db_fetch_row($result);
168         if ($myrow[0] > 0) 
169         {
170                 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
171                 return false;
172         }                                                                       
173         
174         return true;
175 }
176
177 //--------------------------------------------------------------------------------------
178
179 if (isset($_POST['delete'])) 
180 {
181
182         if (can_delete($selected_account))
183         {
184                 delete_gl_account($selected_account);
185                 meta_forward($_SERVER['PHP_SELF']);             
186         }
187
188
189 //-------------------------------------------------------------------------------------
190
191 start_form();
192
193 if (db_has_gl_accounts()) 
194 {
195         echo "<center>";
196     echo _("Select an Account:") . "&nbsp;";
197     gl_all_accounts_list('AccountList', null);
198     echo "&nbsp;";
199     submit('Select', _("Edit Account"));
200     echo "</center>";
201
202         
203 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Account"));
204 br(1);
205
206 start_table($table_style2);
207
208 if ($selected_account != "") 
209 {
210         //editing an existing account
211         $myrow = get_gl_account($selected_account);
212
213         $_POST['account_code'] = $myrow["account_code"];
214         $_POST['account_code2'] = $myrow["account_code2"];
215         $_POST['account_name']  = $myrow["account_name"];
216         $_POST['account_type'] = $myrow["account_type"];
217         $_POST['tax_code'] = $myrow["tax_code"];
218
219         hidden('account_code', $_POST['account_code']);
220         hidden('selected_account', $_POST['selected_account']);
221                 
222         label_row(_("Account Code:"), $_POST['account_code']);
223
224 else 
225 {
226         text_row_ex(_("Account Code:"), 'account_code', 11);
227 }
228
229 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
230
231 text_row_ex(_("Account Name:"), 'account_name', 60);
232
233 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
234
235 tax_types_list_row(_("Tax Type:"), 'tax_code', null, _('No Tax'));
236
237 end_table(1);
238
239 if ($selected_account == "") 
240 {
241         submit_center('add', _("Add Account"));
242
243 else 
244 {
245     submit_center_first('update', _("Update Account"));
246     submit_center_last('delete', _("Delete account"));
247 }
248
249 end_form();
250
251 end_page();
252
253 ?>