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['_AccountList_update']))
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)
65 update_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
66 display_notification(_("Account data has been updated."));
70 add_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);
71 $selected_account = $_POST['AccountList'] = $_POST['account_code'];
72 display_notification(_("New account has been added."));
73 $Ajax->activate('_page_body');
79 //-------------------------------------------------------------------------------------
81 function can_delete($selected_account)
83 if ($selected_account == "")
85 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
86 $result = db_query($sql,"Couldn't test for existing transactions");
88 $myrow = db_fetch_row($result);
91 display_error(_("Cannot delete this account because transactions have been created using this account."));
95 $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act='$selected_account'
96 OR pyt_discount_act='$selected_account'
97 OR creditors_act='$selected_account'
98 OR freight_act='$selected_account'
99 OR default_sales_act='$selected_account'
100 OR default_sales_discount_act='$selected_account'
101 OR default_prompt_payment_act='$selected_account'
102 OR default_inventory_act='$selected_account'
103 OR default_cogs_act='$selected_account'
104 OR default_adj_act='$selected_account'
105 OR default_inv_sales_act='$selected_account'
106 OR default_assembly_act='$selected_account'";
107 $result = db_query($sql,"Couldn't test for default company GL codes");
109 $myrow = db_fetch_row($result);
112 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
116 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code='$selected_account'";
117 $result = db_query($sql,"Couldn't test for bank accounts");
119 $myrow = db_fetch_row($result);
122 display_error(_("Cannot delete this account because it is used by a bank account."));
126 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
127 inventory_account='$selected_account'
128 OR cogs_account='$selected_account'
129 OR adjustment_account='$selected_account'
130 OR sales_account='$selected_account'";
131 $result = db_query($sql,"Couldn't test for existing stock GL codes");
133 $myrow = db_fetch_row($result);
136 display_error(_("Cannot delete this account because it is used by one or more Items."));
140 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code='$selected_account' OR purchasing_gl_code='$selected_account'";
141 $result = db_query($sql,"Couldn't test for existing tax GL codes");
143 $myrow = db_fetch_row($result);
146 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
150 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
151 sales_account='$selected_account'
152 OR sales_discount_account='$selected_account'
153 OR receivables_account='$selected_account'
154 OR payment_discount_account='$selected_account'";
155 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
157 $myrow = db_fetch_row($result);
160 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
164 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
165 purchase_account='$selected_account'
166 OR payment_discount_account='$selected_account'
167 OR payable_account='$selected_account'";
168 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
170 $myrow = db_fetch_row($result);
173 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
180 //--------------------------------------------------------------------------------------
182 if (isset($_POST['delete']))
185 if (can_delete($selected_account))
187 delete_gl_account($selected_account);
188 $selected_account = $_POST['account_code'] = $_POST['AccountList'] = '';
189 display_notification(_("Selected account has been deleted"));
190 $Ajax->activate('_page_body');
194 //-------------------------------------------------------------------------------------
198 if (db_has_gl_accounts())
201 echo _("Select an Account:") . " ";
202 gl_all_accounts_list('AccountList', null, false, false, false,
203 _('New account'), true);
208 start_table($table_style2);
210 if ($selected_account != "")
212 //editing an existing account
213 $myrow = get_gl_account($selected_account);
215 $_POST['account_code'] = $myrow["account_code"];
216 $_POST['account_code2'] = $myrow["account_code2"];
217 $_POST['account_name'] = $myrow["account_name"];
218 $_POST['account_type'] = $myrow["account_type"];
219 $_POST['tax_code'] = $myrow["tax_code"];
221 hidden('account_code', $_POST['account_code']);
222 hidden('selected_account', $selected_account);
224 label_row(_("Account Code:"), $_POST['account_code']);
228 $_POST['account_code'] = $_POST['account_code2'] = '';
229 $_POST['account_name'] = $_POST['account_type'] = $_POST['tax_code'] = '';
230 text_row_ex(_("Account Code:"), 'account_code', 11);
233 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
235 text_row_ex(_("Account Name:"), 'account_name', 60);
237 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
239 tax_types_list_row(_("Tax Type:"), 'tax_code', null, _('No Tax'));
243 if ($selected_account == "")
245 submit_center('add', _("Add Account"), true, '', true);
249 submit_center_first('update', _("Update Account"), '', true);
250 submit_center_last('delete', _("Delete account"), '',true);