Fixed duplicated groups in list selectors.
[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 Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 3;
13 $path_to_root="../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("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 (strlen($_POST['name']) == 0) 
30         {
31                 display_error( _("The account group name cannot be empty."));
32                 set_focus('name');
33                 return false;
34         }
35
36         if (isset($selected_id) && ($selected_id == $_POST['parent'])) 
37         {
38                 display_error(_("You cannot set an account group to be a subgroup of itself."));
39                 return false;
40         }
41
42         return true;
43 }
44
45 //-----------------------------------------------------------------------------------
46
47 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
48 {
49
50         if (can_process()) 
51         {
52
53         if ($selected_id != -1) 
54         {
55                 update_account_type($selected_id, $_POST['name'], $_POST['class_id'], $_POST['parent']);
56                         display_notification(_('Selected account type has been updated'));
57         } 
58         else 
59         {
60                 add_account_type($_POST['id'], $_POST['name'], $_POST['class_id'], $_POST['parent']);
61                         display_notification(_('New account type has been added'));
62         }
63                 $Mode = 'RESET';
64         }
65 }
66
67 //-----------------------------------------------------------------------------------
68
69 function can_delete($selected_id)
70 {
71         if ($selected_id == -1)
72                 return false;
73         $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_master
74                 WHERE account_type=$selected_id";
75         $result = db_query($sql, "could not query chart master");
76         $myrow = db_fetch_row($result);
77         if ($myrow[0] > 0) 
78         {
79                 display_error(_("Cannot delete this account group because GL accounts have been created referring to it."));
80                 return false;
81         }
82
83         $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_types
84                 WHERE parent=$selected_id";
85         $result = db_query($sql, "could not query chart types");
86         $myrow = db_fetch_row($result);
87         if ($myrow[0] > 0) 
88         {
89                 display_error(_("Cannot delete this account group because GL account groups have been created referring to it."));
90                 return false;
91         }
92
93         return true;
94 }
95
96
97 //-----------------------------------------------------------------------------------
98
99 if ($Mode == 'Delete')
100 {
101
102         if (can_delete($selected_id))
103         {
104                 delete_account_type($selected_id);
105                 display_notification(_('Selected currency has been deleted'));
106         }
107         $Mode = 'RESET';
108 }
109 if ($Mode == 'RESET')
110 {
111         $selected_id = -1;
112         $_POST['id']  = $_POST['name']  = '';
113         unset($_POST['parent']);
114         unset($_POST['class_id']);
115 }
116 //-----------------------------------------------------------------------------------
117
118 $result = get_account_types();
119 start_form();
120 start_table($table_style);
121 $th = array(_("ID"), _("Name"), _("Subgroup Of"), _("Class Type"), "", "");
122 table_header($th);
123
124 $k = 0;
125 while ($myrow = db_fetch($result)) 
126 {
127
128         alt_table_row_color($k);
129
130         $bs_text = get_account_class_name($myrow["class_id"]);
131
132         if ($myrow["parent"] == reserved_words::get_any_numeric()) 
133         {
134                 $parent_text = "";
135         } 
136         else 
137         {
138                 $parent_text = get_account_type_name($myrow["parent"]);
139         }
140
141         label_cell($myrow["id"]);
142         label_cell($myrow["name"]);
143         label_cell($parent_text);
144         label_cell($bs_text);
145         edit_button_cell("Edit".$myrow["id"], _("Edit"));
146         delete_button_cell("Delete".$myrow["id"], _("Delete"));
147         end_row();
148 }
149
150 end_table();
151 end_form();
152 echo '<br>';
153 //-----------------------------------------------------------------------------------
154
155 start_form();
156
157 start_table($table_style2);
158
159 if ($selected_id != -1)
160 {
161         if ($Mode == 'Edit') 
162         {
163                 //editing an existing status code
164                 $myrow = get_account_type($selected_id);
165         
166                 $_POST['id']  = $myrow["id"];
167                 $_POST['name']  = $myrow["name"];
168                 $_POST['parent']  = $myrow["parent"];
169                 $_POST['class_id']  = $myrow["class_id"];
170                 hidden('selected_id', $selected_id);
171         }
172         hidden('id');
173         label_row(_("ID:"), $_POST['id']);
174 }
175 else
176         text_row_ex(_("ID:"), 'id', 4);
177 text_row_ex(_("Name:"), 'name', 50);
178
179 gl_account_types_list_row(_("Subgroup Of:"), 'parent', null, _("None"), true);
180
181 class_list_row(_("Class Type:"), 'class_id', null);
182
183 end_table(1);
184
185 submit_add_or_update_center($selected_id == -1, '', true);
186
187 end_form();
188
189 //------------------------------------------------------------------------------------
190
191 end_page();
192
193 ?>