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 (!$accounts_alpha && !is_numeric($_POST['account_code']))
53 display_error( _("The account code must be numeric."));
56 if ($input_error != 1)
58 if ($accounts_alpha == 2)
59 $_POST['account_code'] = strtoupper($_POST['account_code']);
60 if ($selected_account)
61 update_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
63 add_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
64 meta_forward($_SERVER['PHP_SELF']);
68 //-------------------------------------------------------------------------------------
70 function can_delete($selected_account)
72 if ($selected_account == "")
74 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
75 $result = db_query($sql,"Couldn't test for existing transactions");
77 $myrow = db_fetch_row($result);
80 display_error(_("Cannot delete this account because transactions have been created using this account."));
84 $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act='$selected_account'
85 OR pyt_discount_act='$selected_account'
86 OR creditors_act='$selected_account'
87 OR grn_act='$selected_account'
88 OR exchange_diff_act='$selected_account'
89 OR purch_exchange_diff_act='$selected_account'
90 OR retained_earnings_act='$selected_account'
91 OR freight_act='$selected_account'
92 OR default_sales_act='$selected_account'
93 OR default_sales_discount_act='$selected_account'
94 OR default_prompt_payment_act='$selected_account'
95 OR default_inventory_act='$selected_account'
96 OR default_cogs_act='$selected_account'
97 OR default_adj_act='$selected_account'
98 OR default_inv_sales_act='$selected_account'
99 OR default_assembly_act='$selected_account'
100 OR payroll_act='$selected_account'";
101 $result = db_query($sql,"Couldn't test for default company GL codes");
103 $myrow = db_fetch_row($result);
106 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
110 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code='$selected_account'";
111 $result = db_query($sql,"Couldn't test for bank accounts");
113 $myrow = db_fetch_row($result);
116 display_error(_("Cannot delete this account because it is used by a bank account."));
120 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
121 inventory_account='$selected_account'
122 OR cogs_account='$selected_account'
123 OR adjustment_account='$selected_account'
124 OR sales_account='$selected_account'";
125 $result = db_query($sql,"Couldn't test for existing stock GL codes");
127 $myrow = db_fetch_row($result);
130 display_error(_("Cannot delete this account because it is used by one or more Items."));
134 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code='$selected_account' OR purchasing_gl_code='$selected_account'";
135 $result = db_query($sql,"Couldn't test for existing tax GL codes");
137 $myrow = db_fetch_row($result);
140 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
144 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
145 sales_account='$selected_account'
146 OR sales_discount_account='$selected_account'
147 OR receivables_account='$selected_account'
148 OR payment_discount_account='$selected_account'";
149 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
151 $myrow = db_fetch_row($result);
154 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
158 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
159 purchase_account='$selected_account'
160 OR payment_discount_account='$selected_account'
161 OR payable_account='$selected_account'";
162 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
164 $myrow = db_fetch_row($result);
167 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
174 //--------------------------------------------------------------------------------------
176 if (isset($_POST['delete']))
179 if (can_delete($selected_account))
181 delete_gl_account($selected_account);
182 meta_forward($_SERVER['PHP_SELF']);
186 //-------------------------------------------------------------------------------------
190 if (db_has_gl_accounts())
193 echo _("Select an Account:") . " ";
194 gl_all_accounts_list('AccountList', null);
196 submit('Select', _("Edit Account"));
200 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Account"));
203 start_table($table_style2);
205 if ($selected_account != "")
207 //editing an existing account
208 $myrow = get_gl_account($selected_account);
210 $_POST['account_code'] = $myrow["account_code"];
211 $_POST['account_code2'] = $myrow["account_code2"];
212 $_POST['account_name'] = $myrow["account_name"];
213 $_POST['account_type'] = $myrow["account_type"];
214 $_POST['tax_code'] = $myrow["tax_code"];
216 hidden('account_code', $_POST['account_code']);
217 hidden('selected_account', $_POST['selected_account']);
219 label_row(_("Account Code:"), $_POST['account_code']);
223 text_row_ex(_("Account Code:"), 'account_code', 11);
226 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
228 text_row_ex(_("Account Name:"), 'account_name', 60);
230 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
232 tax_types_list_row(_("Tax Type:"), 'tax_code', null, true, _('No Tax'));
236 if ($selected_account == "")
238 submit_center('add', _("Add Account"));
242 submit_center_first('update', _("Update Account"));
243 submit_center_last('delete', _("Delete account"));