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");
16 page(_($help_context = "Chart of Accounts"));
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");
23 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
25 //-------------------------------------------------------------------------------------
27 if (isset($_POST['_AccountList_update']))
29 $_POST['selected_account'] = $_POST['AccountList'];
30 unset($_POST['account_code']);
33 if (isset($_POST['selected_account']))
35 $selected_account = $_POST['selected_account'];
37 elseif (isset($_GET['selected_account']))
39 $selected_account = $_GET['selected_account'];
42 $selected_account = "";
43 //-------------------------------------------------------------------------------------
45 if (isset($_POST['add']) || isset($_POST['update']))
50 if (strlen(trim($_POST['account_code'])) == 0)
53 display_error( _("The account code must be entered."));
54 set_focus('account_code');
56 elseif (strlen(trim($_POST['account_name'])) == 0)
59 display_error( _("The account name cannot be empty."));
60 set_focus('account_name');
62 elseif (!$accounts_alpha && !is_numeric($_POST['account_code']))
65 display_error( _("The account code must be numeric."));
66 set_focus('account_code');
69 if ($input_error != 1)
71 if ($accounts_alpha == 2)
72 $_POST['account_code'] = strtoupper($_POST['account_code']);
74 if (!isset($_POST['account_tags']))
75 $_POST['account_tags'] = array();
77 if ($selected_account)
79 if (get_post('inactive') == 1 && is_bank_account($_POST['account_code']))
81 display_error(_("The account belongs to a bank account and cannot be inactivated."));
83 elseif (update_gl_account($_POST['account_code'], $_POST['account_name'],
84 $_POST['account_type'], $_POST['account_code2'])) {
85 update_record_status($_POST['account_code'], $_POST['inactive'],
86 'chart_master', 'account_code');
87 update_tag_associations(TAG_ACCOUNT, $_POST['account_code'],
88 $_POST['account_tags']);
89 $Ajax->activate('account_code'); // in case of status change
90 display_notification(_("Account data has been updated."));
95 if (add_gl_account($_POST['account_code'], $_POST['account_name'],
96 $_POST['account_type'], $_POST['account_code2']))
98 add_tag_associations($_POST['account_code'], $_POST['account_tags']);
99 display_notification(_("New account has been added."));
100 $selected_account = $_POST['AccountList'] = $_POST['account_code'];
103 $Ajax->activate('_page_body');
107 //-------------------------------------------------------------------------------------
109 function can_delete($selected_account)
111 if ($selected_account == "")
114 if (key_in_foreign_table($selected_account, 'gl_trans', 'account'))
116 display_error(_("Cannot delete this account because transactions have been created using this account."));
120 if (gl_account_in_company_defaults($selected_account))
122 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
126 if (key_in_foreign_table($selected_account, 'bank_accounts', 'account_code'))
128 display_error(_("Cannot delete this account because it is used by a bank account."));
132 if (gl_account_in_stock_category($selected_account))
134 display_error(_("Cannot delete this account because it is used by one or more Item Categories."));
138 if (gl_account_in_stock_master($selected_account))
140 display_error(_("Cannot delete this account because it is used by one or more Items."));
144 if (gl_account_in_tax_types($selected_account))
146 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
150 if (gl_account_in_cust_branch($selected_account))
152 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
156 if (gl_account_in_suppliers($selected_account))
158 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
162 if (gl_account_in_quick_entry_lines($selected_account))
164 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
171 //--------------------------------------------------------------------------------------
173 if (isset($_POST['delete']))
176 if (can_delete($selected_account))
178 delete_gl_account($selected_account);
179 $selected_account = $_POST['AccountList'] = '';
180 delete_tag_associations(TAG_ACCOUNT,$selected_account, true);
181 $selected_account = $_POST['AccountList'] = '';
182 display_notification(_("Selected account has been deleted"));
183 unset($_POST['account_code']);
184 $Ajax->activate('_page_body');
188 //-------------------------------------------------------------------------------------
192 if (db_has_gl_accounts())
194 start_table(TABLESTYLE_NOBORDER);
196 gl_all_accounts_list_cells(null, 'AccountList', null, false, false,
197 _('New account'), true, check_value('show_inactive'));
198 check_cells(_("Show inactive:"), 'show_inactive', null, true);
201 if (get_post('_show_inactive_update')) {
202 $Ajax->activate('AccountList');
203 set_focus('AccountList');
208 start_table(TABLESTYLE2);
210 if ($selected_account != "")
212 //editing an existing account
213 $myrow = get_gl_account($selected_account);
215 $_POST['account_code'] = $myrow["account_code"];
216 $_POST['account_code2'] = $myrow["account_code2"];
217 $_POST['account_name'] = $myrow["account_name"];
218 $_POST['account_type'] = $myrow["account_type"];
219 $_POST['inactive'] = $myrow["inactive"];
221 $tags_result = get_tags_associated_with_record(TAG_ACCOUNT, $selected_account);
223 while ($tag = db_fetch($tags_result))
224 $tagids[] = $tag['id'];
225 $_POST['account_tags'] = $tagids;
227 hidden('account_code', $_POST['account_code']);
228 hidden('selected_account', $selected_account);
230 label_row(_("Account Code:"), $_POST['account_code']);
234 if (!isset($_POST['account_code'])) {
235 $_POST['account_tags'] = array();
236 $_POST['account_code'] = $_POST['account_code2'] = '';
237 $_POST['account_name'] = $_POST['account_type'] = '';
238 $_POST['inactive'] = 0;
240 text_row_ex(_("Account Code:"), 'account_code', 15);
243 text_row_ex(_("Account Code 2:"), 'account_code2', 15);
245 text_row_ex(_("Account Name:"), 'account_name', 60);
247 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
249 tag_list_row(_("Account Tags:"), 'account_tags', 5, TAG_ACCOUNT, true);
251 record_status_list_row(_("Account status:"), 'inactive');
254 if ($selected_account == "")
256 submit_center('add', _("Add Account"), true, '', 'default');
260 submit_center_first('update', _("Update Account"), '', 'default');
261 submit_center_last('delete', _("Delete account"), '',true);