5 include($path_to_root . "/includes/session.inc");
7 page(_("Chart of Accounts"));
9 include($path_to_root . "/includes/ui.inc");
10 include($path_to_root . "/gl/includes/gl_db.inc");
11 include_once($path_to_root . "/includes/data_checks.inc");
13 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
15 //-------------------------------------------------------------------------------------
17 if (isset($_POST['Select']))
19 $_POST['selected_account'] = $_POST['AccountList'];
22 if (isset($_POST['selected_account']))
24 $selected_account = $_POST['selected_account'];
26 elseif (isset($_GET['selected_account']))
28 $selected_account = $_GET['selected_account'];
31 $selected_account = "";
33 //-------------------------------------------------------------------------------------
35 if (isset($_POST['add']) || isset($_POST['update']))
40 if (strlen($_POST['account_code']) == 0)
43 display_error( _("The account code must be entered."));
45 elseif (strlen($_POST['account_name']) == 0)
48 display_error( _("The account name cannot be empty."));
50 elseif (!is_numeric($_POST['account_code']))
53 display_error( _("The account code must be numeric."));
56 if ($input_error != 1)
58 if ($selected_account)
59 update_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
61 add_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
62 meta_forward($_SERVER['PHP_SELF']);
66 //-------------------------------------------------------------------------------------
68 function can_delete($selected_account)
70 if ($selected_account == "")
72 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account=$selected_account";
73 $result = db_query($sql,"Couldn't test for existing transactions");
75 $myrow = db_fetch_row($result);
78 display_error(_("Cannot delete this account because transactions have been created using this account."));
82 $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act=$selected_account
83 OR pyt_discount_act=$selected_account
84 OR creditors_act=$selected_account
85 OR grn_act=$selected_account
86 OR exchange_diff_act=$selected_account
87 OR purch_exchange_diff_act=$selected_account
88 OR retained_earnings_act=$selected_account
89 OR freight_act=$selected_account
90 OR default_sales_act=$selected_account
91 OR default_sales_discount_act=$selected_account
92 OR default_prompt_payment_act=$selected_account
93 OR default_inventory_act=$selected_account
94 OR default_cogs_act=$selected_account
95 OR default_adj_act=$selected_account
96 OR default_inv_sales_act=$selected_account
97 OR default_assembly_act=$selected_account
98 OR payroll_act=$selected_account";
99 $result = db_query($sql,"Couldn't test for default company GL codes");
101 $myrow = db_fetch_row($result);
104 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
108 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code=$selected_account";
109 $result = db_query($sql,"Couldn't test for bank accounts");
111 $myrow = db_fetch_row($result);
114 display_error(_("Cannot delete this account because it is used by a bank account."));
118 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
119 inventory_account=$selected_account
120 OR cogs_account=$selected_account
121 OR adjustment_account=$selected_account
122 OR sales_account=$selected_account";
123 $result = db_query($sql,"Couldn't test for existing stock GL codes");
125 $myrow = db_fetch_row($result);
128 display_error(_("Cannot delete this account because it is used by one or more Items."));
132 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code=$selected_account OR purchasing_gl_code=$selected_account";
133 $result = db_query($sql,"Couldn't test for existing tax GL codes");
135 $myrow = db_fetch_row($result);
138 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
142 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
143 sales_account=$selected_account
144 OR sales_discount_account=$selected_account
145 OR receivables_account=$selected_account
146 OR payment_discount_account=$selected_account";
147 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
149 $myrow = db_fetch_row($result);
152 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
156 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
157 purchase_account=$selected_account
158 OR payment_discount_account=$selected_account
159 OR payable_account=$selected_account";
160 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
162 $myrow = db_fetch_row($result);
165 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
172 //--------------------------------------------------------------------------------------
174 if (isset($_POST['delete']))
177 if (can_delete($selected_account))
179 delete_gl_account($selected_account);
180 meta_forward($_SERVER['PHP_SELF']);
184 //-------------------------------------------------------------------------------------
188 if (db_has_gl_accounts())
191 echo _("Select an Account:") . " ";
192 gl_all_accounts_list('AccountList', null);
194 submit('Select', _("Edit Account"));
198 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Account"));
201 start_table($table_style2);
203 if ($selected_account != "")
205 //editing an existing account
206 $myrow = get_gl_account($selected_account);
208 $_POST['account_code'] = $myrow["account_code"];
209 $_POST['account_code2'] = $myrow["account_code2"];
210 $_POST['account_name'] = $myrow["account_name"];
211 $_POST['account_type'] = $myrow["account_type"];
212 $_POST['tax_code'] = $myrow["tax_code"];
214 hidden('account_code', $_POST['account_code']);
215 hidden('selected_account', $_POST['selected_account']);
217 label_row(_("Account Code:"), $_POST['account_code']);
221 text_row_ex(_("Account Code:"), 'account_code', 11);
224 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
226 text_row_ex(_("Account Name:"), 'account_name', 60);
228 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
230 tax_types_list_row(_("Tax Type:"), 'tax_code', null, true, _('No Tax'));
234 if ($selected_account == "")
236 submit_center('add', _("Add Account"));
240 submit_center_first('update', _("Update Account"));
241 submit_center_last('delete', _("Delete account"));