More restrictions on deleting gl_accounts
[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(_($help_context = "Chart of Accounts"));
17
18 include($path_to_root . "/includes/ui.inc");
19 include($path_to_root . "/gl/includes/gl_db.inc");
20 include($path_to_root . "/admin/db/tags_db.inc");
21 include_once($path_to_root . "/includes/data_checks.inc");
22
23 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
24
25 //-------------------------------------------------------------------------------------
26
27 if (isset($_POST['_AccountList_update'])) 
28 {
29         $_POST['selected_account'] = $_POST['AccountList'];
30         unset($_POST['account_code']);
31 }
32
33 if (isset($_POST['selected_account']))
34 {
35         $selected_account = $_POST['selected_account'];
36
37 elseif (isset($_GET['selected_account']))
38 {
39         $selected_account = $_GET['selected_account'];
40 }
41 else
42         $selected_account = "";
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
74                 if (!isset($_POST['account_tags']))
75                         $_POST['account_tags'] = array();
76
77         if ($selected_account) 
78                 {
79                 if (update_gl_account($_POST['account_code'], $_POST['account_name'], 
80                                 $_POST['account_type'], $_POST['account_code2'])) {
81                                 update_record_status($_POST['account_code'], $_POST['inactive'],
82                                         'chart_master', 'account_code');
83                                 update_tag_associations(TAG_ACCOUNT, $_POST['account_code'], 
84                                         $_POST['account_tags']);
85                                 $Ajax->activate('account_code'); // in case of status change
86                                 display_notification(_("Account data has been updated."));
87                         }
88                 }
89         else 
90                 {
91                 if (add_gl_account($_POST['account_code'], $_POST['account_name'], 
92                                 $_POST['account_type'], $_POST['account_code2']))
93                                 {
94                                         add_tag_associations($_POST['account_code'], $_POST['account_tags']);
95                                         display_notification(_("New account has been added."));
96                                         $selected_account = $_POST['AccountList'] = $_POST['account_code'];
97                                 }
98                 }
99                 $Ajax->activate('_page_body');
100         }
101
102
103 //-------------------------------------------------------------------------------------
104
105 function can_delete($selected_account)
106 {
107         if ($selected_account == "")
108                 return false;
109         $acc = db_escape($selected_account);
110
111         $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account=$acc";
112         $result = db_query($sql,"Couldn't test for existing transactions");
113
114         $myrow = db_fetch_row($result);
115         if ($myrow[0] > 0) 
116         {
117                 display_error(_("Cannot delete this account because transactions have been created using this account."));
118                 return false;
119         }
120
121         $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act=$acc 
122                 OR pyt_discount_act=$acc
123                 OR creditors_act=$acc 
124                 OR bank_charge_act=$acc 
125                 OR exchange_diff_act=$acc
126                 OR profit_loss_year_act=$acc
127                 OR retained_earnings_act=$acc
128                 OR freight_act=$acc
129                 OR default_sales_act=$acc 
130                 OR default_sales_discount_act=$acc
131                 OR default_prompt_payment_act=$acc
132                 OR default_inventory_act=$acc
133                 OR default_cogs_act=$acc
134                 OR default_adj_act=$acc
135                 OR default_inv_sales_act=$acc
136                 OR default_assembly_act=$acc";
137         $result = db_query($sql,"Couldn't test for default company GL codes");
138
139         $myrow = db_fetch_row($result);
140         if ($myrow[0] > 0) 
141         {
142                 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
143                 return false;
144         }
145         
146         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code=$acc";
147         $result = db_query($sql,"Couldn't test for bank accounts");
148
149         $myrow = db_fetch_row($result);
150         if ($myrow[0] > 0) 
151         {
152                 display_error(_("Cannot delete this account because it is used by a bank account."));
153                 return false;
154         }       
155
156         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE 
157                 inventory_account=$acc 
158                 OR cogs_account=$acc
159                 OR adjustment_account=$acc 
160                 OR sales_account=$acc";
161         $result = db_query($sql,"Couldn't test for existing stock GL codes");
162
163         $myrow = db_fetch_row($result);
164         if ($myrow[0] > 0) 
165         {
166                 display_error(_("Cannot delete this account because it is used by one or more Items."));
167                 return false;
168         }       
169         
170         $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code=$acc OR purchasing_gl_code=$acc";
171         $result = db_query($sql,"Couldn't test for existing tax GL codes");
172
173         $myrow = db_fetch_row($result);
174         if ($myrow[0] > 0) 
175         {
176                 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
177                 return false;
178         }       
179         
180         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE 
181                 sales_account=$acc 
182                 OR sales_discount_account=$acc
183                 OR receivables_account=$acc
184                 OR payment_discount_account=$acc";
185         $result = db_query($sql,"Couldn't test for existing cust branch 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 Customer Branches."));
191                 return false;
192         }               
193         
194         $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE 
195                 purchase_account=$acc
196                 OR payment_discount_account=$acc
197                 OR payable_account=$acc";
198         $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
199
200         $myrow = db_fetch_row($result);
201         if ($myrow[0] > 0) 
202         {
203                 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
204                 return false;
205         }                                                                       
206         
207         $sql= "SELECT COUNT(*) FROM ".TB_PREF."quick_entry_lines WHERE 
208                 dest_id=$acc AND UPPER(LEFT(action, 1)) <> 'T'";
209         $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
210
211         $myrow = db_fetch_row($result);
212         if ($myrow[0] > 0) 
213         {
214                 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
215                 return false;
216         }                                                                       
217
218         return true;
219 }
220
221 //--------------------------------------------------------------------------------------
222
223 if (isset($_POST['delete'])) 
224 {
225
226         if (can_delete($selected_account))
227         {
228                 delete_gl_account($selected_account);
229                 $selected_account = $_POST['AccountList'] = '';
230                 delete_tag_associations(TAG_ACCOUNT,$selected_account, true);
231                 $selected_account = $_POST['AccountList'] = '';
232                 display_notification(_("Selected account has been deleted"));
233                 unset($_POST['account_code']);
234                 $Ajax->activate('_page_body');
235         }
236
237
238 //-------------------------------------------------------------------------------------
239
240 start_form();
241
242 if (db_has_gl_accounts()) 
243 {
244         start_table("class = 'tablestyle_noborder'");
245         start_row();
246     gl_all_accounts_list_cells(null, 'AccountList', null, false, false,
247                 _('New account'), true, check_value('show_inactive'));
248         check_cells(_("Show inactive:"), 'show_inactive', null, true);
249         end_row();
250         end_table();
251         if (get_post('_show_inactive_update')) {
252                 $Ajax->activate('AccountList');
253                 set_focus('AccountList');
254         }
255 }
256         
257 br(1);
258 start_table($table_style2);
259
260 if ($selected_account != "") 
261 {
262         //editing an existing account
263         $myrow = get_gl_account($selected_account);
264
265         $_POST['account_code'] = $myrow["account_code"];
266         $_POST['account_code2'] = $myrow["account_code2"];
267         $_POST['account_name']  = $myrow["account_name"];
268         $_POST['account_type'] = $myrow["account_type"];
269         $_POST['inactive'] = $myrow["inactive"];
270         
271         $tags_result = get_tags_associated_with_record(TAG_ACCOUNT, $selected_account);
272         $tagids = array();
273         while ($tag = db_fetch($tags_result)) 
274                 $tagids[] = $tag['id'];
275         $_POST['account_tags'] = $tagids;
276
277         hidden('account_code', $_POST['account_code']);
278         hidden('selected_account', $selected_account);
279                 
280         label_row(_("Account Code:"), $_POST['account_code']);
281
282 else
283 {
284         if (!isset($_POST['account_code'])) {
285                 $_POST['account_tags'] = array();
286                 $_POST['account_code'] = $_POST['account_code2'] = '';
287                 $_POST['account_name']  = $_POST['account_type'] = '';
288                 $_POST['inactive'] = 0;
289         }
290         text_row_ex(_("Account Code:"), 'account_code', 11);
291 }
292
293 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
294
295 text_row_ex(_("Account Name:"), 'account_name', 60);
296
297 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
298
299 tag_list_row(_("Account Tags:"), 'account_tags', 5, TAG_ACCOUNT, true);
300
301 record_status_list_row(_("Account status:"), 'inactive');
302 end_table(1);
303
304 if ($selected_account == "") 
305 {
306         submit_center('add', _("Add Account"), true, '', 'default');
307
308 else 
309 {
310     submit_center_first('update', _("Update Account"), '', 'default');
311     submit_center_last('delete', _("Delete account"), '',true);
312 }
313 end_form();
314
315 end_page();
316
317 ?>