Gettext fixes
[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['_AccountList_update'])) 
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                 set_focus('account_code');
45         } 
46         elseif (strlen($_POST['account_name']) == 0) 
47         {
48                 $input_error = 1;
49                 display_error( _("The account name cannot be empty."));
50                 set_focus('account_name');
51         } 
52         elseif (!$accounts_alpha && !is_numeric($_POST['account_code'])) 
53         {
54             $input_error = 1;
55             display_error( _("The account code must be numeric."));
56                 set_focus('account_code');
57         }
58
59         if ($input_error != 1)
60         {
61                 if ($accounts_alpha == 2)
62                         $_POST['account_code'] = strtoupper($_POST['account_code']);
63         if ($selected_account) 
64                 {
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."));
67                 }
68         else 
69                 {
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');
74                 }
75
76         }
77
78
79 //-------------------------------------------------------------------------------------
80
81 function can_delete($selected_account)
82 {
83         if ($selected_account == "")
84                 return false;
85         $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE account='$selected_account'";
86         $result = db_query($sql,"Couldn't test for existing transactions");
87
88         $myrow = db_fetch_row($result);
89         if ($myrow[0] > 0) 
90         {
91                 display_error(_("Cannot delete this account because transactions have been created using this account."));
92                 return false;
93         }
94
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");
108
109         $myrow = db_fetch_row($result);
110         if ($myrow[0] > 0) 
111         {
112                 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
113                 return false;
114         }
115         
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");
118
119         $myrow = db_fetch_row($result);
120         if ($myrow[0] > 0) 
121         {
122                 display_error(_("Cannot delete this account because it is used by a bank account."));
123                 return false;
124         }       
125
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");
132
133         $myrow = db_fetch_row($result);
134         if ($myrow[0] > 0) 
135         {
136                 display_error(_("Cannot delete this account because it is used by one or more Items."));
137                 return false;
138         }       
139         
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");
142
143         $myrow = db_fetch_row($result);
144         if ($myrow[0] > 0) 
145         {
146                 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
147                 return false;
148         }       
149         
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");
156
157         $myrow = db_fetch_row($result);
158         if ($myrow[0] > 0) 
159         {
160                 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
161                 return false;
162         }               
163         
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");
169
170         $myrow = db_fetch_row($result);
171         if ($myrow[0] > 0) 
172         {
173                 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
174                 return false;
175         }                                                                       
176         
177         return true;
178 }
179
180 //--------------------------------------------------------------------------------------
181
182 if (isset($_POST['delete'])) 
183 {
184
185         if (can_delete($selected_account))
186         {
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');
191         }
192
193
194 //-------------------------------------------------------------------------------------
195
196 start_form();
197
198 if (db_has_gl_accounts()) 
199 {
200         echo "<center>";
201     echo _("Select an Account:") . "&nbsp;";
202     gl_all_accounts_list('AccountList', null, false, false, false,
203                 _('New account'), true);
204     echo "</center>";
205 }
206         
207 br(1);
208 start_table($table_style2);
209
210 if ($selected_account != "") 
211 {
212         //editing an existing account
213         $myrow = get_gl_account($selected_account);
214
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"];
220
221         hidden('account_code', $_POST['account_code']);
222         hidden('selected_account', $selected_account);
223                 
224         label_row(_("Account Code:"), $_POST['account_code']);
225
226 else 
227 {
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);
231 }
232
233 text_row_ex(_("Account Code 2:"), 'account_code2', 11);
234
235 text_row_ex(_("Account Name:"), 'account_name', 60);
236
237 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
238
239 tax_types_list_row(_("Tax Type:"), 'tax_code', null, _('No Tax'));
240
241 end_table(1);
242
243 if ($selected_account == "") 
244 {
245         submit_center('add', _("Add Account"), true, '', true);
246
247 else 
248 {
249     submit_center_first('update', _("Update Account"), '', true);
250     submit_center_last('delete', _("Delete account"), '',true);
251 }
252 end_form();
253
254 end_page();
255
256 ?>