Fixed bug no 1703888 Apostrophes and other unescaped characters
[fa-stable.git] / gl / manage / gl_accounts.php
1 <?php
2
3 $page_security = 10;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Chart of Accounts"));
8
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");
12
13 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
14
15 //-------------------------------------------------------------------------------------
16
17 if (isset($_POST['Select'])) 
18 {
19         $_POST['selected_account'] = $_POST['AccountList'];
20 }
21
22 if (isset($_POST['selected_account']))
23 {
24         $selected_account = $_POST['selected_account'];
25
26 elseif (isset($_GET['selected_account']))
27 {
28         $selected_account = $_GET['selected_account'];
29 }
30 else
31         $selected_account = "";
32
33 //-------------------------------------------------------------------------------------
34
35 if (isset($_POST['add']) || isset($_POST['update'])) 
36 {
37
38         $input_error = 0;
39
40         if (strlen($_POST['account_code']) == 0) 
41         {
42                 $input_error = 1;
43                 display_error( _("The account code must be entered."));
44         } 
45         elseif (strlen($_POST['account_name']) == 0) 
46         {
47                 $input_error = 1;
48                 display_error( _("The account name cannot be empty."));
49         } 
50         elseif (!is_numeric($_POST['account_code'])) 
51         {
52                 $input_error = 1;
53                 display_error( _("The account code must be numeric."));
54         }
55
56         if ($input_error != 1)
57         {
58         if ($selected_account)
59                 update_gl_account($_POST['account_code'], $_POST['account_name'], $_POST['account_type'], $_POST['account_code2'], $_POST['tax_code']);                 
60         else
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']);     
63         }
64
65
66 //-------------------------------------------------------------------------------------
67
68 function can_delete($selected_account)
69 {
70         if ($selected_account == "")
71                 return false;
72         $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account=$selected_account";
73         $result = db_query($sql,"Couldn't test for existing transactions");
74
75         $myrow = db_fetch_row($result);
76         if ($myrow[0] > 0) 
77         {
78                 display_error(_("Cannot delete this account because transactions have been created using this account."));
79                 return false;
80         }
81
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");
100
101         $myrow = db_fetch_row($result);
102         if ($myrow[0] > 0) 
103         {
104                 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
105                 return false;
106         }
107         
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");
110
111         $myrow = db_fetch_row($result);
112         if ($myrow[0] > 0) 
113         {
114                 display_error(_("Cannot delete this account because it is used by a bank account."));
115                 return false;
116         }       
117
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");
124
125         $myrow = db_fetch_row($result);
126         if ($myrow[0] > 0) 
127         {
128                 display_error(_("Cannot delete this account because it is used by one or more Items."));
129                 return false;
130         }       
131         
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");
134
135         $myrow = db_fetch_row($result);
136         if ($myrow[0] > 0) 
137         {
138                 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
139                 return false;
140         }       
141         
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");
148
149         $myrow = db_fetch_row($result);
150         if ($myrow[0] > 0) 
151         {
152                 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
153                 return false;
154         }               
155         
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");
161
162         $myrow = db_fetch_row($result);
163         if ($myrow[0] > 0) 
164         {
165                 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
166                 return false;
167         }                                                                       
168         
169         return true;
170 }
171
172 //--------------------------------------------------------------------------------------
173
174 if (isset($_POST['delete'])) 
175 {
176
177         if (can_delete($selected_account))
178         {
179                 delete_gl_account($selected_account);
180                 meta_forward($_SERVER['PHP_SELF']);             
181         }
182
183
184 //-------------------------------------------------------------------------------------
185
186 start_form();
187
188 if (db_has_gl_accounts()) 
189 {
190         echo "<center>";
191     echo _("Select an Account:") . "&nbsp;";
192     gl_all_accounts_list('AccountList', null);
193     echo "&nbsp;";
194     submit('Select', _("Edit Account"));
195     echo "</center>";
196
197         
198 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Account"));
199 br(1);
200
201 start_table($table_style2);
202
203 if ($selected_account != "") 
204 {
205         //editing an existing account
206         $myrow = get_gl_account($selected_account);
207
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"];
213
214         hidden('account_code', $_POST['account_code']);
215         hidden('selected_account', $_POST['selected_account']);
216                 
217         label_row(_("Account Code:"), $_POST['account_code']);
218
219 else 
220 {
221         text_row_ex(_("Account Code:"), 'account_code', 11);
222 }
223
224 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
225
226 text_row_ex(_("Account Name:"), 'account_name', 60);
227
228 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
229
230 tax_types_list_row(_("Tax Type:"), 'tax_code', null, true, _('No Tax'));
231
232 end_table(1);
233
234 if ($selected_account == "") 
235 {
236         submit_center('add', _("Add Account"));
237
238 else 
239 {
240     submit_center_first('update', _("Update Account"));
241     submit_center_last('delete', _("Delete account"));
242 }
243
244 end_form();
245
246 end_page();
247
248 ?>