Account Class/Group drill down added according to Apmuthu.
[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 $js = "";
17 if ($SysPrefs->use_popup_windows && $SysPrefs->use_popup_search)
18         $js .= get_js_open_window(900, 500);
19
20 page(_($help_context = "Chart of Accounts"), false, false, "", $js);
21
22 include($path_to_root . "/includes/ui.inc");
23 include($path_to_root . "/gl/includes/gl_db.inc");
24 include($path_to_root . "/admin/db/tags_db.inc");
25 include_once($path_to_root . "/includes/data_checks.inc");
26
27 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
28
29 if (isset($_GET["id"]))
30         $_POST["id"] = $_GET["id"];     
31
32 //-------------------------------------------------------------------------------------
33
34 if (isset($_POST['_AccountList_update'])) 
35 {
36         $_POST['selected_account'] = $_POST['AccountList'];
37         unset($_POST['account_code']);
38 }
39
40 if (isset($_POST['selected_account']))
41 {
42         $selected_account = $_POST['selected_account'];
43
44 elseif (isset($_GET['selected_account']))
45 {
46         $selected_account = $_GET['selected_account'];
47 }
48 else
49         $selected_account = "";
50 //-------------------------------------------------------------------------------------
51
52 if (isset($_POST['add']) || isset($_POST['update'])) 
53 {
54
55         $input_error = 0;
56
57         if (strlen(trim($_POST['account_code'])) == 0) 
58         {
59                 $input_error = 1;
60                 display_error( _("The account code must be entered."));
61                 set_focus('account_code');
62         } 
63         elseif (strlen(trim($_POST['account_name'])) == 0) 
64         {
65                 $input_error = 1;
66                 display_error( _("The account name cannot be empty."));
67                 set_focus('account_name');
68         } 
69         elseif (!$SysPrefs->accounts_alpha() && !preg_match("/^[0-9.]+$/",$_POST['account_code'])) // we only allow 0-9 and a dot
70         {
71             $input_error = 1;
72             display_error( _("The account code must be numeric."));
73                 set_focus('account_code');
74         }
75         if ($input_error != 1)
76         {
77                 if ($SysPrefs->accounts_alpha() == 2)
78                         $_POST['account_code'] = strtoupper($_POST['account_code']);
79
80                 if (!isset($_POST['account_tags']))
81                         $_POST['account_tags'] = array();
82
83         if ($selected_account) 
84                 {
85                         if (get_post('inactive') == 1 && is_bank_account($_POST['account_code']))
86                         {
87                                 display_error(_("The account belongs to a bank account and cannot be inactivated."));
88                         }
89                 elseif (update_gl_account($_POST['account_code'], $_POST['account_name'], 
90                                 $_POST['account_type'], $_POST['account_code2'])) {
91                                 update_record_status($_POST['account_code'], $_POST['inactive'],
92                                         'chart_master', 'account_code');
93                                 update_tag_associations(TAG_ACCOUNT, $_POST['account_code'], 
94                                         $_POST['account_tags']);
95                                 $Ajax->activate('account_code'); // in case of status change
96                                 display_notification(_("Account data has been updated."));
97                         }
98                 }
99         else 
100                 {
101                 if (add_gl_account($_POST['account_code'], $_POST['account_name'], 
102                                 $_POST['account_type'], $_POST['account_code2']))
103                                 {
104                                         add_tag_associations($_POST['account_code'], $_POST['account_tags']);
105                                         display_notification(_("New account has been added."));
106                                         $selected_account = $_POST['AccountList'] = $_POST['account_code'];
107                                 }
108                         else
109                  display_error(_("Account not added, possible duplicate Account Code."));
110                 }
111                 $Ajax->activate('_page_body');
112         }
113
114
115 //-------------------------------------------------------------------------------------
116
117 function can_delete($selected_account)
118 {
119         if ($selected_account == "")
120                 return false;
121
122         if (key_in_foreign_table($selected_account, 'gl_trans', 'account'))
123         {
124                 display_error(_("Cannot delete this account because transactions have been created using this account."));
125                 return false;
126         }
127
128         if (gl_account_in_company_defaults($selected_account))
129         {
130                 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
131                 return false;
132         }
133
134         if (key_in_foreign_table($selected_account, 'bank_accounts', 'account_code'))
135         {
136                 display_error(_("Cannot delete this account because it is used by a bank account."));
137                 return false;
138         }
139
140         if (gl_account_in_stock_category($selected_account))
141         {
142                 display_error(_("Cannot delete this account because it is used by one or more Item Categories."));
143                 return false;
144         }
145
146         if (gl_account_in_stock_master($selected_account))
147         {
148                 display_error(_("Cannot delete this account because it is used by one or more Items."));
149                 return false;
150         }
151
152         if (gl_account_in_tax_types($selected_account))
153         {
154                 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
155                 return false;
156         }
157
158         if (gl_account_in_cust_branch($selected_account))
159         {
160                 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
161                 return false;
162         }
163         if (gl_account_in_suppliers($selected_account))
164         {
165                 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
166                 return false;
167         }
168
169         if (gl_account_in_quick_entry_lines($selected_account))
170         {
171                 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
172                 return false;
173         }
174
175         return true;
176 }
177
178 //--------------------------------------------------------------------------------------
179
180 if (isset($_POST['delete'])) 
181 {
182
183         if (can_delete($selected_account))
184         {
185                 delete_gl_account($selected_account);
186                 $selected_account = $_POST['AccountList'] = '';
187                 delete_tag_associations(TAG_ACCOUNT,$selected_account, true);
188                 $selected_account = $_POST['AccountList'] = '';
189                 display_notification(_("Selected account has been deleted"));
190                 unset($_POST['account_code']);
191                 $Ajax->activate('_page_body');
192         }
193
194
195 //-------------------------------------------------------------------------------------
196 $filter_id = (isset($_POST["id"]));
197
198 start_form();
199
200 if (db_has_gl_accounts()) 
201 {
202         start_table(TABLESTYLE_NOBORDER);
203         start_row();
204         if ($filter_id)
205                 gl_all_accounts_list_cells(null, 'AccountList', null, false, false, _('New account'), true, check_value('show_inactive'), $_POST['id']);
206         else
207                 gl_all_accounts_list_cells(null, 'AccountList', null, false, false, _('New account'), true, check_value('show_inactive'));
208         check_cells(_("Show inactive:"), 'show_inactive', null, true);
209         end_row();
210         end_table();
211         if (get_post('_show_inactive_update')) {
212                 $Ajax->activate('AccountList');
213                 set_focus('AccountList');
214         }
215 }
216         
217 br(1);
218 start_table(TABLESTYLE2);
219
220 if ($selected_account != "") 
221 {
222         //editing an existing account
223         $myrow = get_gl_account($selected_account);
224
225         $_POST['account_code'] = $myrow["account_code"];
226         $_POST['account_code2'] = $myrow["account_code2"];
227         $_POST['account_name']  = $myrow["account_name"];
228         $_POST['account_type'] = $myrow["account_type"];
229         $_POST['inactive'] = $myrow["inactive"];
230         
231         $tags_result = get_tags_associated_with_record(TAG_ACCOUNT, $selected_account);
232         $tagids = array();
233         while ($tag = db_fetch($tags_result)) 
234                 $tagids[] = $tag['id'];
235         $_POST['account_tags'] = $tagids;
236
237         hidden('account_code', $_POST['account_code']);
238         hidden('selected_account', $selected_account);
239                 
240         label_row(_("Account Code:"), $_POST['account_code']);
241
242 else
243 {
244         if (!isset($_POST['account_code'])) {
245                 $_POST['account_tags'] = array();
246                 $_POST['account_code'] = $_POST['account_code2'] = '';
247                 $_POST['account_name']  = $_POST['account_type'] = '';
248                 $_POST['inactive'] = 0;
249                 if ($filter_id) $_POST['account_type'] = $_POST['id'];
250         }
251         text_row_ex(_("Account Code:"), 'account_code', 15);
252 }
253
254 text_row_ex(_("Account Code 2:"), 'account_code2', 15);
255
256 text_row_ex(_("Account Name:"), 'account_name', 60);
257
258 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
259
260 tag_list_row(_("Account Tags:"), 'account_tags', 5, TAG_ACCOUNT, true);
261
262 record_status_list_row(_("Account status:"), 'inactive');
263 end_table(1);
264
265 if ($selected_account == "") 
266 {
267         submit_center('add', _("Add Account"), true, '', 'default');
268
269 else 
270 {
271     submit_center_first('update', _("Update Account"), '', 'default');
272     submit_center_last('delete', _("Delete account"), '',true);
273 }
274 end_form();
275
276 end_page();
277