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 ***********************************************************************/
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_once($path_to_root . "/includes/data_checks.inc");
22 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
24 //-------------------------------------------------------------------------------------
26 if (isset($_POST['_AccountList_update']))
28 $_POST['selected_account'] = $_POST['AccountList'];
31 if (isset($_POST['selected_account']))
33 $selected_account = $_POST['selected_account'];
35 elseif (isset($_GET['selected_account']))
37 $selected_account = $_GET['selected_account'];
40 $selected_account = "";
42 //-------------------------------------------------------------------------------------
44 if (isset($_POST['add']) || isset($_POST['update']))
49 if (strlen($_POST['account_code']) == 0)
52 display_error( _("The account code must be entered."));
53 set_focus('account_code');
55 elseif (strlen($_POST['account_name']) == 0)
58 display_error( _("The account name cannot be empty."));
59 set_focus('account_name');
61 elseif (!$accounts_alpha && !is_numeric($_POST['account_code']))
64 display_error( _("The account code must be numeric."));
65 set_focus('account_code');
68 if ($input_error != 1)
70 if ($accounts_alpha == 2)
71 $_POST['account_code'] = strtoupper($_POST['account_code']);
72 if ($selected_account)
74 update_gl_account($_POST['account_code'], $_POST['account_name'],
75 $_POST['account_type'], $_POST['account_code2']);
76 update_record_status($_POST['account_code'], $_POST['inactive'],
77 'chart_master', 'account_code');
78 $Ajax->activate('account_code'); // in case of status change
79 display_notification(_("Account data has been updated."));
83 add_gl_account($_POST['account_code'], $_POST['account_name'],
84 $_POST['account_type'], $_POST['account_code2']);
85 $selected_account = $_POST['AccountList'] = $_POST['account_code'];
86 display_notification(_("New account has been added."));
88 $Ajax->activate('_page_body');
92 //-------------------------------------------------------------------------------------
94 function can_delete($selected_account)
96 if ($selected_account == "")
98 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
99 $result = db_query($sql,"Couldn't test for existing transactions");
101 $myrow = db_fetch_row($result);
104 display_error(_("Cannot delete this account because transactions have been created using this account."));
108 $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act='$selected_account'
109 OR pyt_discount_act='$selected_account'
110 OR creditors_act='$selected_account'
111 OR freight_act='$selected_account'
112 OR default_sales_act='$selected_account'
113 OR default_sales_discount_act='$selected_account'
114 OR default_prompt_payment_act='$selected_account'
115 OR default_inventory_act='$selected_account'
116 OR default_cogs_act='$selected_account'
117 OR default_adj_act='$selected_account'
118 OR default_inv_sales_act='$selected_account'
119 OR default_assembly_act='$selected_account'";
120 $result = db_query($sql,"Couldn't test for default company GL codes");
122 $myrow = db_fetch_row($result);
125 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
129 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code='$selected_account'";
130 $result = db_query($sql,"Couldn't test for bank accounts");
132 $myrow = db_fetch_row($result);
135 display_error(_("Cannot delete this account because it is used by a bank account."));
139 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
140 inventory_account='$selected_account'
141 OR cogs_account='$selected_account'
142 OR adjustment_account='$selected_account'
143 OR sales_account='$selected_account'";
144 $result = db_query($sql,"Couldn't test for existing stock GL codes");
146 $myrow = db_fetch_row($result);
149 display_error(_("Cannot delete this account because it is used by one or more Items."));
153 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code='$selected_account' OR purchasing_gl_code='$selected_account'";
154 $result = db_query($sql,"Couldn't test for existing tax GL codes");
156 $myrow = db_fetch_row($result);
159 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
163 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
164 sales_account='$selected_account'
165 OR sales_discount_account='$selected_account'
166 OR receivables_account='$selected_account'
167 OR payment_discount_account='$selected_account'";
168 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
170 $myrow = db_fetch_row($result);
173 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
177 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
178 purchase_account='$selected_account'
179 OR payment_discount_account='$selected_account'
180 OR payable_account='$selected_account'";
181 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
183 $myrow = db_fetch_row($result);
186 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
193 //--------------------------------------------------------------------------------------
195 if (isset($_POST['delete']))
198 if (can_delete($selected_account))
200 delete_gl_account($selected_account);
201 $selected_account = $_POST['account_code'] = $_POST['AccountList'] = '';
202 display_notification(_("Selected account has been deleted"));
203 $Ajax->activate('_page_body');
207 //-------------------------------------------------------------------------------------
211 if (db_has_gl_accounts())
213 start_table("class = 'tablestyle_noborder'");
215 gl_all_accounts_list_cells(null, 'AccountList', null, false, false,
216 _('New account'), true, check_value('show_inactive'));
217 check_cells(_("Show inactive:"), 'show_inactive', null, true);
220 if (get_post('_show_inactive_update')) {
221 $Ajax->activate('AccountList');
222 set_focus('AccountList');
227 start_table($table_style2);
229 if ($selected_account != "")
231 //editing an existing account
232 $myrow = get_gl_account($selected_account);
234 $_POST['account_code'] = $myrow["account_code"];
235 $_POST['account_code2'] = $myrow["account_code2"];
236 $_POST['account_name'] = $myrow["account_name"];
237 $_POST['account_type'] = $myrow["account_type"];
238 $_POST['inactive'] = $myrow["inactive"];
240 hidden('account_code', $_POST['account_code']);
241 hidden('selected_account', $selected_account);
243 label_row(_("Account Code:"), $_POST['account_code']);
247 $_POST['account_code'] = $_POST['account_code2'] = '';
248 $_POST['account_name'] = $_POST['account_type'] = '';
249 $_POST['inactive'] = 0;
250 text_row_ex(_("Account Code:"), 'account_code', 11);
253 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
255 text_row_ex(_("Account Name:"), 'account_name', 60);
257 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
259 record_status_list_row(_("Account status:"), 'inactive');
262 if ($selected_account == "")
264 submit_center('add', _("Add Account"), true, '', 'default');
268 submit_center_first('update', _("Update Account"), '', 'default');
269 submit_center_last('delete', _("Delete account"), '',true);