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