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