96ba6529d046f326cbb7f832b1fdd4be885a43d2
[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($_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($_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 && !is_numeric($_POST['account_code'])) 
63         {
64             $input_error = 1;
65             display_error( _("The account code must be numeric."));
66                 set_focus('account_code');
67         }
68
69         if ($input_error != 1)
70         {
71                 if ($accounts_alpha == 2)
72                         $_POST['account_code'] = strtoupper($_POST['account_code']);
73
74                 if (!isset($_POST['account_tags']))
75                         $_POST['account_tags'] = array();
76
77         if ($selected_account) 
78                 {
79                 if (update_gl_account($_POST['account_code'], $_POST['account_name'], 
80                                 $_POST['account_type'], $_POST['account_code2'])) {
81                                 update_record_status($_POST['account_code'], $_POST['inactive'],
82                                         'chart_master', 'account_code');
83                                 update_tag_associations(TAG_ACCOUNT, $_POST['account_code'], 
84                                         $_POST['account_tags']);
85                                 $Ajax->activate('account_code'); // in case of status change
86                                 display_notification(_("Account data has been updated."));
87                         }
88                 }
89         else 
90                 {
91                 if (add_gl_account($_POST['account_code'], $_POST['account_name'], 
92                                 $_POST['account_type'], $_POST['account_code2']))
93                                 {
94                                         add_tag_associations($_POST['account_code'], $_POST['account_tags']);
95                                         display_notification(_("New account has been added."));
96                                         $selected_account = $_POST['AccountList'] = $_POST['account_code'];
97                                 }
98                 }
99                 $Ajax->activate('_page_body');
100         }
101
102
103 //-------------------------------------------------------------------------------------
104
105 function can_delete($selected_account)
106 {
107         if ($selected_account == "")
108                 return false;
109         $acc = db_escape($selected_account);
110
111         if (key_in_foreign_table($acc, 'gl_trans', 'account', true))
112         {
113                 display_error(_("Cannot delete this account because transactions have been created using this account."));
114                 return false;
115         }
116
117         if (gl_account_in_company_defaults($acc))
118         {
119                 display_error(_("Cannot delete this account because it is used as one of the company default GL accounts."));
120                 return false;
121         }
122
123         if (key_in_foreign_table($acc, 'bank_accounts', 'account_code', true))  
124         {
125                 display_error(_("Cannot delete this account because it is used by a bank account."));
126                 return false;
127         }       
128
129         if (gl_account_in_stock_category($acc))
130         {
131                 display_error(_("Cannot delete this account because it is used by one or more Item Categories."));
132                 return false;
133         }       
134         
135         if (gl_account_in_stock_master($acc))
136         {
137                 display_error(_("Cannot delete this account because it is used by one or more Items."));
138                 return false;
139         }       
140         
141         if (gl_account_in_tax_types($acc))
142         {
143                 display_error(_("Cannot delete this account because it is used by one or more Taxes."));
144                 return false;
145         }       
146         
147         if (gl_account_in_cust_branch($acc))
148         {
149                 display_error(_("Cannot delete this account because it is used by one or more Customer Branches."));
150                 return false;
151         }               
152         
153         if (gl_account_in_suppliers($acc))
154         {
155                 display_error(_("Cannot delete this account because it is used by one or more suppliers."));
156                 return false;
157         }                                                                       
158         
159         if (gl_account_in_quick_entry_lines($acc))
160         {
161                 display_error(_("Cannot delete this account because it is used by one or more Quick Entry Lines."));
162                 return false;
163         }                                                                       
164
165         return true;
166 }
167
168 //--------------------------------------------------------------------------------------
169
170 if (isset($_POST['delete'])) 
171 {
172
173         if (can_delete($selected_account))
174         {
175                 delete_gl_account($selected_account);
176                 $selected_account = $_POST['AccountList'] = '';
177                 delete_tag_associations(TAG_ACCOUNT,$selected_account, true);
178                 $selected_account = $_POST['AccountList'] = '';
179                 display_notification(_("Selected account has been deleted"));
180                 unset($_POST['account_code']);
181                 $Ajax->activate('_page_body');
182         }
183
184
185 //-------------------------------------------------------------------------------------
186
187 start_form();
188
189 if (db_has_gl_accounts()) 
190 {
191         start_table("class = 'tablestyle_noborder'");
192         start_row();
193     gl_all_accounts_list_cells(null, 'AccountList', null, false, false,
194                 _('New account'), true, check_value('show_inactive'));
195         check_cells(_("Show inactive:"), 'show_inactive', null, true);
196         end_row();
197         end_table();
198         if (get_post('_show_inactive_update')) {
199                 $Ajax->activate('AccountList');
200                 set_focus('AccountList');
201         }
202 }
203         
204 br(1);
205 start_table($table_style2);
206
207 if ($selected_account != "") 
208 {
209         //editing an existing account
210         $myrow = get_gl_account($selected_account);
211
212         $_POST['account_code'] = $myrow["account_code"];
213         $_POST['account_code2'] = $myrow["account_code2"];
214         $_POST['account_name']  = $myrow["account_name"];
215         $_POST['account_type'] = $myrow["account_type"];
216         $_POST['inactive'] = $myrow["inactive"];
217         
218         $tags_result = get_tags_associated_with_record(TAG_ACCOUNT, $selected_account);
219         $tagids = array();
220         while ($tag = db_fetch($tags_result)) 
221                 $tagids[] = $tag['id'];
222         $_POST['account_tags'] = $tagids;
223
224         hidden('account_code', $_POST['account_code']);
225         hidden('selected_account', $selected_account);
226                 
227         label_row(_("Account Code:"), $_POST['account_code']);
228
229 else
230 {
231         if (!isset($_POST['account_code'])) {
232                 $_POST['account_tags'] = array();
233                 $_POST['account_code'] = $_POST['account_code2'] = '';
234                 $_POST['account_name']  = $_POST['account_type'] = '';
235                 $_POST['inactive'] = 0;
236         }
237         text_row_ex(_("Account Code:"), 'account_code', 15);
238 }
239
240 text_row_ex(_("Account Code 2:"), 'account_code2', 15);
241
242 text_row_ex(_("Account Name:"), 'account_name', 60);
243
244 gl_account_types_list_row(_("Account Group:"), 'account_type', null);
245
246 tag_list_row(_("Account Tags:"), 'account_tags', 5, TAG_ACCOUNT, true);
247
248 record_status_list_row(_("Account status:"), 'inactive');
249 end_table(1);
250
251 if ($selected_account == "") 
252 {
253         submit_center('add', _("Add Account"), true, '', 'default');
254
255 else 
256 {
257     submit_center_first('update', _("Update Account"), '', 'default');
258     submit_center_last('delete', _("Delete account"), '',true);
259 }
260 end_form();
261
262 end_page();
263
264 ?>