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");
17 if ($SysPrefs->use_popup_windows && $SysPrefs->use_popup_search)
18 $js .= get_js_open_window(900, 500);
20 page(_($help_context = "Chart of Accounts"), false, false, "", $js);
22 include($path_to_root . "/includes/ui.inc");
23 include($path_to_root . "/gl/includes/gl_db.inc");
24 include_once($path_to_root . "/admin/db/tags_db.inc");
25 include_once($path_to_root . "/includes/data_checks.inc");
27 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
29 if (isset($_GET["id"]))
30 $_POST["id"] = $_GET["id"];
32 //-------------------------------------------------------------------------------------
34 if (isset($_POST['_AccountList_update']))
36 $_POST['selected_account'] = $_POST['AccountList'];
37 unset($_POST['account_code']);
40 if (isset($_POST['selected_account']))
42 $selected_account = $_POST['selected_account'];
44 elseif (isset($_GET['selected_account']))
46 $selected_account = $_GET['selected_account'];
49 $selected_account = "";
50 //-------------------------------------------------------------------------------------
52 if (isset($_POST['add']) || isset($_POST['update']))
57 if (strlen(trim($_POST['account_code'])) == 0)
60 display_error( _("The account code must be entered."));
61 set_focus('account_code');
63 elseif (strlen(trim($_POST['account_name'])) == 0)
66 display_error( _("The account name cannot be empty."));
67 set_focus('account_name');
69 elseif (!$SysPrefs->accounts_alpha() && !preg_match("/^[0-9.]+$/",$_POST['account_code'])) // we only allow 0-9 and a dot
72 display_error( _("The account code must be numeric."));
73 set_focus('account_code');
75 if ($input_error != 1)
77 if ($SysPrefs->accounts_alpha() == 2)
78 $_POST['account_code'] = strtoupper($_POST['account_code']);
80 if (!isset($_POST['account_tags']))
81 $_POST['account_tags'] = array();
83 if ($selected_account)
85 if (get_post('inactive') == 1 && is_bank_account($_POST['account_code']))
87 display_error(_("The account belongs to a bank account and cannot be inactivated."));
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."));
101 if (add_gl_account($_POST['account_code'], $_POST['account_name'],
102 $_POST['account_type'], $_POST['account_code2']))
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'];
109 display_error(_("Account not added, possible duplicate Account Code."));
111 $Ajax->activate('_page_body');
115 //-------------------------------------------------------------------------------------
117 function can_delete($selected_account)
119 if ($selected_account == "")
122 if (key_in_foreign_table($selected_account, 'gl_trans', 'account'))
124 display_error(_("Cannot delete this account because transactions have been created using this account."));
128 if (gl_account_in_company_defaults($selected_account))
130 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
134 if (key_in_foreign_table($selected_account, 'bank_accounts', 'account_code'))
136 display_error(_("Cannot delete this account because it is used by a bank account."));
140 if (gl_account_in_stock_category($selected_account))
142 display_error(_("Cannot delete this account because it is used by one or more Item Categories."));
146 if (gl_account_in_stock_master($selected_account))
148 display_error(_("Cannot delete this account because it is used by one or more Items."));
152 if (gl_account_in_tax_types($selected_account))
154 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
158 if (gl_account_in_cust_branch($selected_account))
160 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
163 if (gl_account_in_suppliers($selected_account))
165 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
169 if (gl_account_in_quick_entry_lines($selected_account))
171 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
178 //--------------------------------------------------------------------------------------
180 if (isset($_POST['delete']))
183 if (can_delete($selected_account))
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');
195 //-------------------------------------------------------------------------------------
196 $filter_id = (isset($_POST["id"]));
200 if (db_has_gl_accounts())
202 start_table(TABLESTYLE_NOBORDER);
205 gl_all_accounts_list_cells(null, 'AccountList', null, false, false, _('New account'), true, check_value('show_inactive'), $_POST['id']);
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);
211 if (get_post('_show_inactive_update')) {
212 $Ajax->activate('AccountList');
213 set_focus('AccountList');
218 start_table(TABLESTYLE2);
220 if ($selected_account != "")
222 //editing an existing account
223 $myrow = get_gl_account($selected_account);
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"];
231 $tags_result = get_tags_associated_with_record(TAG_ACCOUNT, $selected_account);
233 while ($tag = db_fetch($tags_result))
234 $tagids[] = $tag['id'];
235 $_POST['account_tags'] = $tagids;
237 hidden('account_code', $_POST['account_code']);
238 hidden('selected_account', $selected_account);
240 label_row(_("Account Code:"), $_POST['account_code']);
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'];
251 text_row_ex(_("Account Code:"), 'account_code', 15);
254 text_row_ex(_("Account Code 2:"), 'account_code2', 15);
256 text_row_ex(_("Account Name:"), 'account_name', 60);
258 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
260 tag_list_row(_("Account Tags:"), 'account_tags', 5, TAG_ACCOUNT, true);
262 record_status_list_row(_("Account status:"), 'inactive');
265 if ($selected_account == "")
267 submit_center('add', _("Add Account"), true, '', 'default');
271 submit_center_first('update', _("Update Account"), '', 'default');
272 submit_center_last('delete', _("Delete account"), '',true);