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