Accounts entry. We only accept 0-9 and a dot if $accounts_alpha = 0 (default)
[fa-stable.git] / gl / manage / gl_accounts.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_GLACCOUNT';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15  
16 page(_($help_context = "Chart of Accounts"));
17
18 include($path_to_root . "/includes/ui.inc");
19 include($path_to_root . "/gl/includes/gl_db.inc");
20 include($path_to_root . "/admin/db/tags_db.inc");
21 include_once($path_to_root . "/includes/data_checks.inc");
22
23 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
24
25 //-------------------------------------------------------------------------------------
26
27 if (isset($_POST['_AccountList_update'])) 
28 {
29         $_POST['selected_account'] = $_POST['AccountList'];
30         unset($_POST['account_code']);
31 }
32
33 if (isset($_POST['selected_account']))
34 {
35         $selected_account = $_POST['selected_account'];
36
37 elseif (isset($_GET['selected_account']))
38 {
39         $selected_account = $_GET['selected_account'];
40 }
41 else
42         $selected_account = "";
43 //-------------------------------------------------------------------------------------
44
45 if (isset($_POST['add']) || isset($_POST['update'])) 
46 {
47
48         $input_error = 0;
49
50         if (strlen(trim($_POST['account_code'])) == 0) 
51         {
52                 $input_error = 1;
53                 display_error( _("The account code must be entered."));
54                 set_focus('account_code');
55         } 
56         elseif (strlen(trim($_POST['account_name'])) == 0) 
57         {
58                 $input_error = 1;
59                 display_error( _("The account name cannot be empty."));
60                 set_focus('account_name');
61         }  
62         elseif (!$accounts_alpha && !preg_match("/^[0-9.]+$/",$_POST['account_code'])) // we only allow 0-9 and a dot
63         {
64             $input_error = 1;
65             display_error( _("The account code must be numeric."));
66                 set_focus('account_code');
67         }
68         if ($input_error != 1)
69         {
70                 if ($accounts_alpha == 2)
71                         $_POST['account_code'] = strtoupper($_POST['account_code']);
72
73                 if (!isset($_POST['account_tags']))
74                         $_POST['account_tags'] = array();
75
76         if ($selected_account) 
77                 {
78                         if (get_post('inactive') == 1 && is_bank_account($_POST['account_code']))
79                         {
80                                 display_error(_("The account belongs to a bank account and cannot be inactivated."));
81                         }       
82                 elseif (update_gl_account($_POST['account_code'], $_POST['account_name'], 
83                                 $_POST['account_type'], $_POST['account_code2'])) {
84                                 update_record_status($_POST['account_code'], $_POST['inactive'],
85                                         'chart_master', 'account_code');
86                                 update_tag_associations(TAG_ACCOUNT, $_POST['account_code'], 
87                                         $_POST['account_tags']);
88                                 $Ajax->activate('account_code'); // in case of status change
89                                 display_notification(_("Account data has been updated."));
90                         }
91                 }
92         else 
93                 {
94                 if (add_gl_account($_POST['account_code'], $_POST['account_name'], 
95                                 $_POST['account_type'], $_POST['account_code2']))
96                                 {
97                                         add_tag_associations($_POST['account_code'], $_POST['account_tags']);
98                                         display_notification(_("New account has been added."));
99                                         $selected_account = $_POST['AccountList'] = $_POST['account_code'];
100                                 }
101                         else
102                  display_error(_("Account not added, possible duplicate Account Code."));
103                 }
104                 $Ajax->activate('_page_body');
105         }
106
107
108 //-------------------------------------------------------------------------------------
109
110 function can_delete($selected_account)
111 {
112         if ($selected_account == "")
113                 return false;
114
115         if (key_in_foreign_table($selected_account, 'gl_trans', 'account'))
116         {
117                 display_error(_("Cannot delete this account because transactions have been created using this account."));
118                 return false;
119         }
120
121         if (gl_account_in_company_defaults($selected_account))
122         {
123                 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
124                 return false;
125         }
126
127         if (key_in_foreign_table($selected_account, 'bank_accounts', 'account_code'))
128         {
129                 display_error(_("Cannot delete this account because it is used by a bank account."));
130                 return false;
131         }       
132
133         if (gl_account_in_stock_category($selected_account))
134         {
135                 display_error(_("Cannot delete this account because it is used by one or more Item Categories."));
136                 return false;
137         }       
138         
139         if (gl_account_in_stock_master($selected_account))
140         {
141                 display_error(_("Cannot delete this account because it is used by one or more Items."));
142                 return false;
143         }       
144         
145         if (gl_account_in_tax_types($selected_account))
146         {
147                 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
148                 return false;
149         }       
150         
151         if (gl_account_in_cust_branch($selected_account))
152         {
153                 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
154                 return false;
155         }               
156         
157         if (gl_account_in_suppliers($selected_account))
158         {
159                 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
160                 return false;
161         }
162
163         if (gl_account_in_quick_entry_lines($selected_account))
164         {
165                 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
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                 $selected_account = $_POST['AccountList'] = '';
181                 delete_tag_associations(TAG_ACCOUNT,$selected_account, true);
182                 $selected_account = $_POST['AccountList'] = '';
183                 display_notification(_("Selected account has been deleted"));
184                 unset($_POST['account_code']);
185                 $Ajax->activate('_page_body');
186         }
187
188
189 //-------------------------------------------------------------------------------------
190
191 start_form();
192
193 if (db_has_gl_accounts()) 
194 {
195         start_table(TABLESTYLE_NOBORDER);
196         start_row();
197     gl_all_accounts_list_cells(null, 'AccountList', null, false, false,
198                 _('New account'), true, check_value('show_inactive'));
199         check_cells(_("Show inactive:"), 'show_inactive', null, true);
200         end_row();
201         end_table();
202         if (get_post('_show_inactive_update')) {
203                 $Ajax->activate('AccountList');
204                 set_focus('AccountList');
205         }
206 }
207         
208 br(1);
209 start_table(TABLESTYLE2);
210
211 if ($selected_account != "") 
212 {
213         //editing an existing account
214         $myrow = get_gl_account($selected_account);
215
216         $_POST['account_code'] = $myrow["account_code"];
217         $_POST['account_code2'] = $myrow["account_code2"];
218         $_POST['account_name']  = $myrow["account_name"];
219         $_POST['account_type'] = $myrow["account_type"];
220         $_POST['inactive'] = $myrow["inactive"];
221         
222         $tags_result = get_tags_associated_with_record(TAG_ACCOUNT, $selected_account);
223         $tagids = array();
224         while ($tag = db_fetch($tags_result)) 
225                 $tagids[] = $tag['id'];
226         $_POST['account_tags'] = $tagids;
227
228         hidden('account_code', $_POST['account_code']);
229         hidden('selected_account', $selected_account);
230                 
231         label_row(_("Account Code:"), $_POST['account_code']);
232
233 else
234 {
235         if (!isset($_POST['account_code'])) {
236                 $_POST['account_tags'] = array();
237                 $_POST['account_code'] = $_POST['account_code2'] = '';
238                 $_POST['account_name']  = $_POST['account_type'] = '';
239                 $_POST['inactive'] = 0;
240         }
241         text_row_ex(_("Account Code:"), 'account_code', 15);
242 }
243
244 text_row_ex(_("Account Code 2:"), 'account_code2', 15);
245
246 text_row_ex(_("Account Name:"), 'account_name', 60);
247
248 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
249
250 tag_list_row(_("Account Tags:"), 'account_tags', 5, TAG_ACCOUNT, true);
251
252 record_status_list_row(_("Account status:"), 'inactive');
253 end_table(1);
254
255 if ($selected_account == "") 
256 {
257         submit_center('add', _("Add Account"), true, '', 'default');
258
259 else 
260 {
261     submit_center_first('update', _("Update Account"), '', 'default');
262     submit_center_last('delete', _("Delete account"), '',true);
263 }
264 end_form();
265
266 end_page();
267
268 ?>