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."));
74 $Ajax->activate('_page_body');
78 //-------------------------------------------------------------------------------------
80 function can_delete($selected_account)
82 if ($selected_account == "")
84 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
85 $result = db_query($sql,"Couldn't test for existing transactions");
87 $myrow = db_fetch_row($result);
90 display_error(_("Cannot delete this account because transactions have been created using this account."));
94 $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE debtors_act='$selected_account'
95 OR pyt_discount_act='$selected_account'
96 OR creditors_act='$selected_account'
97 OR freight_act='$selected_account'
98 OR default_sales_act='$selected_account'
99 OR default_sales_discount_act='$selected_account'
100 OR default_prompt_payment_act='$selected_account'
101 OR default_inventory_act='$selected_account'
102 OR default_cogs_act='$selected_account'
103 OR default_adj_act='$selected_account'
104 OR default_inv_sales_act='$selected_account'
105 OR default_assembly_act='$selected_account'";
106 $result = db_query($sql,"Couldn't test for default company GL codes");
108 $myrow = db_fetch_row($result);
111 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
115 $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE account_code='$selected_account'";
116 $result = db_query($sql,"Couldn't test for bank accounts");
118 $myrow = db_fetch_row($result);
121 display_error(_("Cannot delete this account because it is used by a bank account."));
125 $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE
126 inventory_account='$selected_account'
127 OR cogs_account='$selected_account'
128 OR adjustment_account='$selected_account'
129 OR sales_account='$selected_account'";
130 $result = db_query($sql,"Couldn't test for existing stock GL codes");
132 $myrow = db_fetch_row($result);
135 display_error(_("Cannot delete this account because it is used by one or more Items."));
139 $sql= "SELECT COUNT(*) FROM ".TB_PREF."tax_types WHERE sales_gl_code='$selected_account' OR purchasing_gl_code='$selected_account'";
140 $result = db_query($sql,"Couldn't test for existing tax GL codes");
142 $myrow = db_fetch_row($result);
145 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
149 $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE
150 sales_account='$selected_account'
151 OR sales_discount_account='$selected_account'
152 OR receivables_account='$selected_account'
153 OR payment_discount_account='$selected_account'";
154 $result = db_query($sql,"Couldn't test for existing cust branch GL codes");
156 $myrow = db_fetch_row($result);
159 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
163 $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE
164 purchase_account='$selected_account'
165 OR payment_discount_account='$selected_account'
166 OR payable_account='$selected_account'";
167 $result = db_query($sql,"Couldn't test for existing suppliers GL codes");
169 $myrow = db_fetch_row($result);
172 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
179 //--------------------------------------------------------------------------------------
181 if (isset($_POST['delete']))
184 if (can_delete($selected_account))
186 delete_gl_account($selected_account);
187 $selected_account = $_POST['account_code'] = $_POST['AccountList'] = '';
188 display_notification(_("Selected account has been deleted"));
189 $Ajax->activate('_page_body');
193 //-------------------------------------------------------------------------------------
197 if (db_has_gl_accounts())
200 echo _("Select an Account:") . " ";
201 gl_all_accounts_list('AccountList', null, false, false, false,
202 _('New account'), true);
207 start_table($table_style2);
209 if ($selected_account != "")
211 //editing an existing account
212 $myrow = get_gl_account($selected_account);
214 $_POST['account_code'] = $myrow["account_code"];
215 $_POST['account_code2'] = $myrow["account_code2"];
216 $_POST['account_name'] = $myrow["account_name"];
217 $_POST['account_type'] = $myrow["account_type"];
218 $_POST['tax_code'] = $myrow["tax_code"];
220 hidden('account_code', $_POST['account_code']);
221 hidden('selected_account', $selected_account);
223 label_row(_("Account Code:"), $_POST['account_code']);
227 $_POST['account_code'] = $_POST['account_code2'] = '';
228 $_POST['account_name'] = $_POST['account_type'] = $_POST['tax_code'] = '';
229 text_row_ex(_("Account Code:"), 'account_code', 11);
232 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
234 text_row_ex(_("Account Name:"), 'account_name', 60);
236 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
238 tax_groups_list_row(_("Tax Group:"), 'tax_code', null, _('No Tax'));
242 if ($selected_account == "")
244 submit_center('add', _("Add Account"), true, '', true);
248 submit_center_first('update', _("Update Account"), '', true);
249 submit_center_last('delete', _("Delete account"), '',true);