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