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