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($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 //-------------------------------------------------------------------------------------
31 if (isset($_POST['_AccountList_update']))
33 $_POST['selected_account'] = $_POST['AccountList'];
34 unset($_POST['account_code']);
37 if (isset($_POST['selected_account']))
39 $selected_account = $_POST['selected_account'];
41 elseif (isset($_GET['selected_account']))
43 $selected_account = $_GET['selected_account'];
46 $selected_account = "";
47 //-------------------------------------------------------------------------------------
49 if (isset($_POST['add']) || isset($_POST['update']))
54 if (strlen(trim($_POST['account_code'])) == 0)
57 display_error( _("The account code must be entered."));
58 set_focus('account_code');
60 elseif (strlen(trim($_POST['account_name'])) == 0)
63 display_error( _("The account name cannot be empty."));
64 set_focus('account_name');
66 elseif (!$SysPrefs->accounts_alpha() && !preg_match("/^[0-9.]+$/",$_POST['account_code'])) // we only allow 0-9 and a dot
69 display_error( _("The account code must be numeric."));
70 set_focus('account_code');
72 if ($input_error != 1)
74 if ($SysPrefs->accounts_alpha() == 2)
75 $_POST['account_code'] = strtoupper($_POST['account_code']);
77 if (!isset($_POST['account_tags']))
78 $_POST['account_tags'] = array();
80 if ($selected_account)
82 if (get_post('inactive') == 1 && is_bank_account($_POST['account_code']))
84 display_error(_("The account belongs to a bank account and cannot be inactivated."));
86 elseif (update_gl_account($_POST['account_code'], $_POST['account_name'],
87 $_POST['account_type'], $_POST['account_code2'])) {
88 update_record_status($_POST['account_code'], $_POST['inactive'],
89 'chart_master', 'account_code');
90 update_tag_associations(TAG_ACCOUNT, $_POST['account_code'],
91 $_POST['account_tags']);
92 $Ajax->activate('account_code'); // in case of status change
93 display_notification(_("Account data has been updated."));
98 if (add_gl_account($_POST['account_code'], $_POST['account_name'],
99 $_POST['account_type'], $_POST['account_code2']))
101 add_tag_associations($_POST['account_code'], $_POST['account_tags']);
102 display_notification(_("New account has been added."));
103 $selected_account = $_POST['AccountList'] = $_POST['account_code'];
106 display_error(_("Account not added, possible duplicate Account Code."));
108 $Ajax->activate('_page_body');
112 //-------------------------------------------------------------------------------------
114 function can_delete($selected_account)
116 if ($selected_account == "")
119 if (key_in_foreign_table($selected_account, 'gl_trans', 'account'))
121 display_error(_("Cannot delete this account because transactions have been created using this account."));
125 if (gl_account_in_company_defaults($selected_account))
127 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
131 if (key_in_foreign_table($selected_account, 'bank_accounts', 'account_code'))
133 display_error(_("Cannot delete this account because it is used by a bank account."));
137 if (gl_account_in_stock_category($selected_account))
139 display_error(_("Cannot delete this account because it is used by one or more Item Categories."));
143 if (gl_account_in_stock_master($selected_account))
145 display_error(_("Cannot delete this account because it is used by one or more Items."));
149 if (gl_account_in_tax_types($selected_account))
151 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
155 if (gl_account_in_cust_branch($selected_account))
157 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
160 if (gl_account_in_suppliers($selected_account))
162 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
166 if (gl_account_in_quick_entry_lines($selected_account))
168 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
175 //--------------------------------------------------------------------------------------
177 if (isset($_POST['delete']))
180 if (can_delete($selected_account))
182 delete_gl_account($selected_account);
183 $selected_account = $_POST['AccountList'] = '';
184 delete_tag_associations(TAG_ACCOUNT,$selected_account, true);
185 $selected_account = $_POST['AccountList'] = '';
186 display_notification(_("Selected account has been deleted"));
187 unset($_POST['account_code']);
188 $Ajax->activate('_page_body');
192 //-------------------------------------------------------------------------------------
196 if (db_has_gl_accounts())
198 start_table(TABLESTYLE_NOBORDER);
200 gl_all_accounts_list_cells(null, 'AccountList', null, false, false,
201 _('New account'), true, check_value('show_inactive'));
202 check_cells(_("Show inactive:"), 'show_inactive', null, true);
205 if (get_post('_show_inactive_update')) {
206 $Ajax->activate('AccountList');
207 set_focus('AccountList');
212 start_table(TABLESTYLE2);
214 if ($selected_account != "")
216 //editing an existing account
217 $myrow = get_gl_account($selected_account);
219 $_POST['account_code'] = $myrow["account_code"];
220 $_POST['account_code2'] = $myrow["account_code2"];
221 $_POST['account_name'] = $myrow["account_name"];
222 $_POST['account_type'] = $myrow["account_type"];
223 $_POST['inactive'] = $myrow["inactive"];
225 $tags_result = get_tags_associated_with_record(TAG_ACCOUNT, $selected_account);
227 while ($tag = db_fetch($tags_result))
228 $tagids[] = $tag['id'];
229 $_POST['account_tags'] = $tagids;
231 hidden('account_code', $_POST['account_code']);
232 hidden('selected_account', $selected_account);
234 label_row(_("Account Code:"), $_POST['account_code']);
238 if (!isset($_POST['account_code'])) {
239 $_POST['account_tags'] = array();
240 $_POST['account_code'] = $_POST['account_code2'] = '';
241 $_POST['account_name'] = $_POST['account_type'] = '';
242 $_POST['inactive'] = 0;
244 text_row_ex(_("Account Code:"), 'account_code', 15);
247 text_row_ex(_("Account Code 2:"), 'account_code2', 15);
249 text_row_ex(_("Account Name:"), 'account_name', 60);
251 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
253 tag_list_row(_("Account Tags:"), 'account_tags', 5, TAG_ACCOUNT, true);
255 record_status_list_row(_("Account status:"), 'inactive');
258 if ($selected_account == "")
260 submit_center('add', _("Add Account"), true, '', 'default');
264 submit_center_first('update', _("Update Account"), '', 'default');
265 submit_center_last('delete', _("Delete account"), '',true);