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