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