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