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