628cbc2f8ed8a5a5fb9d3c5863e3a6fd8c9af367
[fa-stable.git] / gl / manage / gl_account_types.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_GLACCOUNTGROUP';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "GL Account Groups"));
17
18 include($path_to_root . "/gl/includes/gl_db.inc");
19
20 include($path_to_root . "/includes/ui.inc");
21
22 simple_page_mode(false);
23 //-----------------------------------------------------------------------------------
24
25 function can_process() 
26 {
27         global $selected_id;
28
29         if ($_POST['id'] == "")
30         {
31             display_error( _("The account group id cannot be empty."));
32             set_focus('id');
33             return false;
34         }
35         if (strlen($_POST['name']) == 0) 
36         {
37                 display_error( _("The account group name cannot be empty."));
38                 set_focus('name');
39                 return false;
40         }
41
42         if (strcmp($_POST['id'], $_POST['parent']) == 0) 
43         {
44                 display_error("id = {$_POST['id']}, parent = {$_POST['parent']}");
45                 //display_error(_("You cannot set an account group to be a subgroup of itself."));
46                 return false;
47         }
48
49         return true;
50 }
51
52 //-----------------------------------------------------------------------------------
53
54 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
55 {
56
57         if (can_process()) 
58         {
59
60         if ($selected_id != "") 
61         {
62                 if (update_account_type($_POST['id'], $_POST['name'], $_POST['class_id'], $_POST['parent'], $_POST['old_id']))
63                                 display_notification(_('Selected account type has been updated'));
64         } 
65         else 
66         {
67                 if (add_account_type($_POST['id'], $_POST['name'], $_POST['class_id'], $_POST['parent'])) {
68                                 display_notification(_('New account type has been added'));
69                                 $Mode = 'RESET';
70                         }
71         }
72         }
73 }
74
75 //-----------------------------------------------------------------------------------
76
77 function can_delete($selected_id)
78 {
79         if ($selected_id == "")
80                 return false;
81         $type = db_escape($selected_id);
82
83         if (key_in_foreign_table($type, 'chart_master', 'account_type', true))
84         {
85                 display_error(_("Cannot delete this account group because GL accounts have been created referring to it."));
86                 return false;
87         }
88
89         if (key_in_foreign_table($type, 'chart_types', 'parent', true))
90         {
91                 display_error(_("Cannot delete this account group because GL account groups have been created referring to it."));
92                 return false;
93         }
94
95         return true;
96 }
97
98
99 //-----------------------------------------------------------------------------------
100
101 if ($Mode == 'Delete')
102 {
103
104         if (can_delete($selected_id))
105         {
106                 delete_account_type($selected_id);
107                 display_notification(_('Selected account group has been deleted'));
108         }
109         $Mode = 'RESET';
110 }
111 if ($Mode == 'RESET')
112 {
113         $selected_id = "";
114         $_POST['id']  = $_POST['name']  = '';
115         unset($_POST['parent']);
116         unset($_POST['class_id']);
117 }
118 //-----------------------------------------------------------------------------------
119
120 $result = get_account_types(check_value('show_inactive'));
121
122 start_form();
123 start_table($table_style);
124 $th = array(_("Group ID"), _("Group Name"), _("Subgroup Of"), _("Class Type"), "", "");
125 inactive_control_column($th);
126 table_header($th);
127
128 $k = 0;
129 while ($myrow = db_fetch($result)) 
130 {
131
132         alt_table_row_color($k);
133
134         $bs_text = get_account_class_name($myrow["class_id"]);
135
136         if ($myrow["parent"] == '-1') 
137         {
138                 $parent_text = "";
139         } 
140         else 
141         {
142                 $parent_text = get_account_type_name($myrow["parent"]);
143         }
144
145         label_cell($myrow["id"]);
146         label_cell($myrow["name"]);
147         label_cell($parent_text);
148         label_cell($bs_text);
149         inactive_control_cell($myrow["id"], $myrow["inactive"], 'chart_types', 'id');
150         edit_button_cell("Edit".$myrow["id"], _("Edit"));
151         delete_button_cell("Delete".$myrow["id"], _("Delete"));
152         end_row();
153 }
154
155 inactive_control_row($th);
156 end_table(1);
157 //-----------------------------------------------------------------------------------
158
159 start_table($table_style2);
160
161 if ($selected_id != "")
162 {
163         if ($Mode == 'Edit') 
164         {
165                 //editing an existing status code
166                 $myrow = get_account_type($selected_id);
167         
168                 $_POST['id']  = $myrow["id"];
169                 $_POST['name']  = $myrow["name"];
170                 $_POST['parent']  = $myrow["parent"];
171                 if ($_POST['parent'] == '-1')
172                         $_POST['parent'] == "";
173                 $_POST['class_id']  = $myrow["class_id"];
174                 hidden('selected_id', $selected_id);
175                 hidden('old_id', $myrow["id"]);
176         }
177         hidden('id');
178 }
179 text_row_ex(_("ID:"), 'id', 10);
180 text_row_ex(_("Name:"), 'name', 50);
181
182 gl_account_types_list_row(_("Subgroup Of:"), 'parent', null, _("None"), true);
183
184 class_list_row(_("Class Type:"), 'class_id', null);
185
186 end_table(1);
187
188 submit_add_or_update_center($selected_id == "", '', 'both');
189
190 end_form();
191
192 //------------------------------------------------------------------------------------
193
194 end_page();
195
196 ?>