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