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."));
44 set_focus('account_code');
46 elseif (strlen($_POST['account_name']) == 0)
49 display_error( _("The account name cannot be empty."));
50 set_focus('account_name');
52 elseif (!$accounts_alpha && !is_numeric($_POST['account_code']))
55 display_error( _("The account code must be numeric."));
56 set_focus('account_code');
59 if ($input_error != 1)
61 if ($accounts_alpha == 2)
62 $_POST['account_code'] = strtoupper($_POST['account_code']);
63 if ($selected_account)
64 update_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
66 add_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
67 meta_forward($_SERVER['PHP_SELF']);
71 //-------------------------------------------------------------------------------------
73 function can_delete($selected_account)
75 if ($selected_account == "")
77 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
78 $result = db_query($sql,"Couldn't test for existing transactions");
80 $myrow = db_fetch_row($result);
83 display_error(_("Cannot delete this account because transactions have been created using this account."));
87 $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act='$selected_account'
88 OR pyt_discount_act='$selected_account'
89 OR creditors_act='$selected_account'
90 OR grn_act='$selected_account'
91 OR exchange_diff_act='$selected_account'
92 OR purch_exchange_diff_act='$selected_account'
93 OR retained_earnings_act='$selected_account'
94 OR freight_act='$selected_account'
95 OR default_sales_act='$selected_account'
96 OR default_sales_discount_act='$selected_account'
97 OR default_prompt_payment_act='$selected_account'
98 OR default_inventory_act='$selected_account'
99 OR default_cogs_act='$selected_account'
100 OR default_adj_act='$selected_account'
101 OR default_inv_sales_act='$selected_account'
102 OR default_assembly_act='$selected_account'
103 OR payroll_act='$selected_account'";
104 $result = db_query($sql,"Couldn't test for default company GL codes");
106 $myrow = db_fetch_row($result);
109 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
113 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code='$selected_account'";
114 $result = db_query($sql,"Couldn't test for bank accounts");
116 $myrow = db_fetch_row($result);
119 display_error(_("Cannot delete this account because it is used by a bank account."));
123 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
124 inventory_account='$selected_account'
125 OR cogs_account='$selected_account'
126 OR adjustment_account='$selected_account'
127 OR sales_account='$selected_account'";
128 $result = db_query($sql,"Couldn't test for existing stock GL codes");
130 $myrow = db_fetch_row($result);
133 display_error(_("Cannot delete this account because it is used by one or more Items."));
137 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code='$selected_account' OR purchasing_gl_code='$selected_account'";
138 $result = db_query($sql,"Couldn't test for existing tax GL codes");
140 $myrow = db_fetch_row($result);
143 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
147 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
148 sales_account='$selected_account'
149 OR sales_discount_account='$selected_account'
150 OR receivables_account='$selected_account'
151 OR payment_discount_account='$selected_account'";
152 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
154 $myrow = db_fetch_row($result);
157 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
161 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
162 purchase_account='$selected_account'
163 OR payment_discount_account='$selected_account'
164 OR payable_account='$selected_account'";
165 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
167 $myrow = db_fetch_row($result);
170 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
177 //--------------------------------------------------------------------------------------
179 if (isset($_POST['delete']))
182 if (can_delete($selected_account))
184 delete_gl_account($selected_account);
185 meta_forward($_SERVER['PHP_SELF']);
189 //-------------------------------------------------------------------------------------
193 if (db_has_gl_accounts())
196 echo _("Select an Account:") . " ";
197 gl_all_accounts_list('AccountList', null);
199 submit('Select', _("Edit Account"));
203 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Account"));
206 start_table($table_style2);
208 if ($selected_account != "")
210 //editing an existing account
211 $myrow = get_gl_account($selected_account);
213 $_POST['account_code'] = $myrow["account_code"];
214 $_POST['account_code2'] = $myrow["account_code2"];
215 $_POST['account_name'] = $myrow["account_name"];
216 $_POST['account_type'] = $myrow["account_type"];
217 $_POST['tax_code'] = $myrow["tax_code"];
219 hidden('account_code', $_POST['account_code']);
220 hidden('selected_account', $_POST['selected_account']);
222 label_row(_("Account Code:"), $_POST['account_code']);
226 text_row_ex(_("Account Code:"), 'account_code', 11);
229 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
231 text_row_ex(_("Account Name:"), 'account_name', 60);
233 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
235 tax_types_list_row(_("Tax Type:"), 'tax_code', null, _('No Tax'));
239 if ($selected_account == "")
241 submit_center('add', _("Add Account"));
245 submit_center_first('update', _("Update Account"));
246 submit_center_last('delete', _("Delete account"));