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'], $_POST['account_type'], $_POST['account_code2']);
75 display_notification(_("Account data has been updated."));
79 add_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2']);
80 $selected_account = $_POST['AccountList'] = $_POST['account_code'];
81 display_notification(_("New account has been added."));
83 $Ajax->activate('_page_body');
87 //-------------------------------------------------------------------------------------
89 function can_delete($selected_account)
91 if ($selected_account == "")
93 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
94 $result = db_query($sql,"Couldn't test for existing transactions");
96 $myrow = db_fetch_row($result);
99 display_error(_("Cannot delete this account because transactions have been created using this account."));
103 $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act='$selected_account'
104 OR pyt_discount_act='$selected_account'
105 OR creditors_act='$selected_account'
106 OR freight_act='$selected_account'
107 OR default_sales_act='$selected_account'
108 OR default_sales_discount_act='$selected_account'
109 OR default_prompt_payment_act='$selected_account'
110 OR default_inventory_act='$selected_account'
111 OR default_cogs_act='$selected_account'
112 OR default_adj_act='$selected_account'
113 OR default_inv_sales_act='$selected_account'
114 OR default_assembly_act='$selected_account'";
115 $result = db_query($sql,"Couldn't test for default company GL codes");
117 $myrow = db_fetch_row($result);
120 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
124 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code='$selected_account'";
125 $result = db_query($sql,"Couldn't test for bank accounts");
127 $myrow = db_fetch_row($result);
130 display_error(_("Cannot delete this account because it is used by a bank account."));
134 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
135 inventory_account='$selected_account'
136 OR cogs_account='$selected_account'
137 OR adjustment_account='$selected_account'
138 OR sales_account='$selected_account'";
139 $result = db_query($sql,"Couldn't test for existing stock GL codes");
141 $myrow = db_fetch_row($result);
144 display_error(_("Cannot delete this account because it is used by one or more Items."));
148 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code='$selected_account' OR purchasing_gl_code='$selected_account'";
149 $result = db_query($sql,"Couldn't test for existing tax GL codes");
151 $myrow = db_fetch_row($result);
154 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
158 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
159 sales_account='$selected_account'
160 OR sales_discount_account='$selected_account'
161 OR receivables_account='$selected_account'
162 OR payment_discount_account='$selected_account'";
163 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
165 $myrow = db_fetch_row($result);
168 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
172 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
173 purchase_account='$selected_account'
174 OR payment_discount_account='$selected_account'
175 OR payable_account='$selected_account'";
176 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
178 $myrow = db_fetch_row($result);
181 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
185 $sql= "SELECT COUNT(*) FROM ".TB_PREF."quick_entry_lines WHERE
186 dest_id='$selected_account' AND UPPER(LEFT(action, 1)) <> 'T'";
187 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
189 $myrow = db_fetch_row($result);
192 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
199 //--------------------------------------------------------------------------------------
201 if (isset($_POST['delete']))
204 if (can_delete($selected_account))
206 delete_gl_account($selected_account);
207 $selected_account = $_POST['account_code'] = $_POST['AccountList'] = '';
208 display_notification(_("Selected account has been deleted"));
209 $Ajax->activate('_page_body');
213 //-------------------------------------------------------------------------------------
217 if (db_has_gl_accounts())
220 echo _("Select an Account:") . " ";
221 gl_all_accounts_list('AccountList', null, false, false,
222 _('New account'), true);
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"];
239 hidden('account_code', $_POST['account_code']);
240 hidden('selected_account', $selected_account);
242 label_row(_("Account Code:"), $_POST['account_code']);
246 $_POST['account_code'] = $_POST['account_code2'] = '';
247 $_POST['account_name'] = $_POST['account_type'] = '';
248 text_row_ex(_("Account Code:"), 'account_code', 11);
251 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
253 text_row_ex(_("Account Name:"), 'account_name', 60);
255 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
259 if ($selected_account == "")
261 submit_center('add', _("Add Account"), true, '', true);
265 submit_center_first('update', _("Update Account"), '', true);
266 submit_center_last('delete', _("Delete account"), '',true);