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