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(_("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($_POST['account_code']) == 0)
53 display_error( _("The account code must be entered."));
54 set_focus('account_code');
56 elseif (strlen($_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 ($selected_account)
76 if (update_gl_account($_POST['account_code'], $_POST['account_name'],
77 $_POST['account_type'], $_POST['account_code2'])) {
78 update_record_status($_POST['account_code'], $_POST['inactive'],
79 'chart_master', 'account_code');
80 update_tag_associations(TAG_ACCOUNT, $_POST['account_code'],
81 $_POST['account_tags']);
82 $Ajax->activate('account_code'); // in case of status change
83 display_notification(_("Account data has been updated."));
88 if (add_gl_account($_POST['account_code'], $_POST['account_name'],
89 $_POST['account_type'], $_POST['account_code2']))
91 add_tag_associations($_POST['account_code'], $_POST['account_tags']);
92 display_notification(_("New account has been added."));
93 $selected_account = $_POST['AccountList'] = $_POST['account_code'];
96 $Ajax->activate('_page_body');
100 //-------------------------------------------------------------------------------------
102 function can_delete($selected_account)
104 if ($selected_account == "")
106 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
107 $result = db_query($sql,"Couldn't test for existing transactions");
109 $myrow = db_fetch_row($result);
112 display_error(_("Cannot delete this account because transactions have been created using this account."));
116 $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act='$selected_account'
117 OR pyt_discount_act='$selected_account'
118 OR creditors_act='$selected_account'
119 OR freight_act='$selected_account'
120 OR default_sales_act='$selected_account'
121 OR default_sales_discount_act='$selected_account'
122 OR default_prompt_payment_act='$selected_account'
123 OR default_inventory_act='$selected_account'
124 OR default_cogs_act='$selected_account'
125 OR default_adj_act='$selected_account'
126 OR default_inv_sales_act='$selected_account'
127 OR default_assembly_act='$selected_account'";
128 $result = db_query($sql,"Couldn't test for default company GL codes");
130 $myrow = db_fetch_row($result);
133 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
137 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code='$selected_account'";
138 $result = db_query($sql,"Couldn't test for bank accounts");
140 $myrow = db_fetch_row($result);
143 display_error(_("Cannot delete this account because it is used by a bank account."));
147 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
148 inventory_account='$selected_account'
149 OR cogs_account='$selected_account'
150 OR adjustment_account='$selected_account'
151 OR sales_account='$selected_account'";
152 $result = db_query($sql,"Couldn't test for existing stock GL codes");
154 $myrow = db_fetch_row($result);
157 display_error(_("Cannot delete this account because it is used by one or more Items."));
161 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code='$selected_account' OR purchasing_gl_code='$selected_account'";
162 $result = db_query($sql,"Couldn't test for existing tax GL codes");
164 $myrow = db_fetch_row($result);
167 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
171 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
172 sales_account='$selected_account'
173 OR sales_discount_account='$selected_account'
174 OR receivables_account='$selected_account'
175 OR payment_discount_account='$selected_account'";
176 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
178 $myrow = db_fetch_row($result);
181 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
185 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
186 purchase_account='$selected_account'
187 OR payment_discount_account='$selected_account'
188 OR payable_account='$selected_account'";
189 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
191 $myrow = db_fetch_row($result);
194 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
198 $sql= "SELECT COUNT(*) FROM ".TB_PREF."quick_entry_lines WHERE
199 dest_id='$selected_account' AND UPPER(LEFT(action, 1)) <> 'T'";
200 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
202 $myrow = db_fetch_row($result);
205 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
212 //--------------------------------------------------------------------------------------
214 if (isset($_POST['delete']))
217 if (can_delete($selected_account))
219 delete_gl_account($selected_account);
220 $selected_account = $_POST['AccountList'] = '';
221 delete_tag_associations(TAG_ACCOUNT,$selected_account, true);
222 $selected_account = $_POST['AccountList'] = '';
223 display_notification(_("Selected account has been deleted"));
224 unset($_POST['account_code']);
225 $Ajax->activate('_page_body');
229 //-------------------------------------------------------------------------------------
233 if (db_has_gl_accounts())
235 start_table("class = 'tablestyle_noborder'");
237 gl_all_accounts_list_cells(null, 'AccountList', null, false, false,
238 _('New account'), true, check_value('show_inactive'));
239 check_cells(_("Show inactive:"), 'show_inactive', null, true);
242 if (get_post('_show_inactive_update')) {
243 $Ajax->activate('AccountList');
244 set_focus('AccountList');
249 start_table($table_style2);
251 if ($selected_account != "")
253 //editing an existing account
254 $myrow = get_gl_account($selected_account);
256 $_POST['account_code'] = $myrow["account_code"];
257 $_POST['account_code2'] = $myrow["account_code2"];
258 $_POST['account_name'] = $myrow["account_name"];
259 $_POST['account_type'] = $myrow["account_type"];
260 $_POST['inactive'] = $myrow["inactive"];
262 $tags_result = get_tags_associated_with_record(TAG_ACCOUNT, $selected_account);
264 while ($tag = db_fetch($tags_result))
265 $tagids[] = $tag['id'];
266 $_POST['account_tags'] = $tagids;
268 hidden('account_code', $_POST['account_code']);
269 hidden('selected_account', $selected_account);
271 label_row(_("Account Code:"), $_POST['account_code']);
275 if (!isset($_POST['account_code'])) {
276 $_POST['account_tags'] = array();
277 $_POST['account_code'] = $_POST['account_code2'] = '';
278 $_POST['account_name'] = $_POST['account_type'] = '';
279 $_POST['inactive'] = 0;
281 text_row_ex(_("Account Code:"), 'account_code', 11);
284 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
286 text_row_ex(_("Account Name:"), 'account_name', 60);
288 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
290 tag_list_row(_("Account Tags:"), 'account_tags', 5, TAG_ACCOUNT, true);
292 record_status_list_row(_("Account status:"), 'inactive');
295 if ($selected_account == "")
297 submit_center('add', _("Add Account"), true, '', false);
301 submit_center_first('update', _("Update Account"), '', false);
302 submit_center_last('delete', _("Delete account"), '',true);