Changed balance_sheet field in account class to ctype and fixed class editing.
[fa-stable.git] / gl / manage / gl_account_classes.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 = 3;
13 $path_to_root="../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("GL Account Classes"));
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         if (!is_numeric($_POST['id'])) 
28         {
29                 display_error( _("The account class ID must be numeric."));
30                 set_focus('id');
31                 return false;
32         }
33         if (strlen($_POST['name']) == 0) 
34         {
35                 display_error( _("The account class name cannot be empty."));
36                 set_focus('name');
37                 return false;
38         }
39
40         return true;
41 }
42
43 //-----------------------------------------------------------------------------------
44
45 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
46 {
47
48         if (can_process()) 
49         {
50
51         if ($selected_id != -1) 
52         {
53                 update_account_class($selected_id, $_POST['name'], $_POST['ctype']);
54                         display_notification(_('Selected account class settings has been updated'));
55         } 
56         else 
57         {
58                 add_account_class($_POST['id'], $_POST['name'], $_POST['ctype']);
59                         display_notification(_('New account class has been added'));
60         }
61                 $Mode = 'RESET';
62         }
63 }
64
65 //-----------------------------------------------------------------------------------
66
67 function can_delete($selected_id)
68 {
69         if ($selected_id == -1)
70                 return false;
71         $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_types
72                 WHERE class_id=$selected_id";
73         $result = db_query($sql, "could not query chart master");
74         $myrow = db_fetch_row($result);
75         if ($myrow[0] > 0) 
76         {
77                 display_error(_("Cannot delete this account class because GL account types have been created referring to it."));
78                 return false;
79         }
80
81         return true;
82 }
83
84
85 //-----------------------------------------------------------------------------------
86
87 if ($Mode == 'Delete')
88 {
89
90         if (can_delete($selected_id))
91         {
92                 delete_account_class($selected_id);
93                 display_notification(_('Selected account class has been deleted'));
94         }
95         $Mode = 'RESET';
96 }
97
98 //-----------------------------------------------------------------------------------
99 if ($Mode == 'RESET')
100 {
101         $selected_id = -1;
102         $_POST['id']  = $_POST['name']  = $_POST['ctype'] =  '';
103 }
104 //-----------------------------------------------------------------------------------
105
106 $result = get_account_classes(check_value('show_inactive'));
107
108 start_form();
109 start_table($table_style);
110 $th = array(_("Class ID"), _("Class Name"), _("Class Type"), "", "");
111 inactive_control_column($th);
112 table_header($th);
113
114 $k = 0;
115 while ($myrow = db_fetch($result)) 
116 {
117
118         alt_table_row_color($k);
119
120         label_cell($myrow["cid"]);
121         label_cell($myrow['class_name']);
122         label_cell($class_types[$myrow["ctype"]]);
123         inactive_control_cell($myrow["cid"], $myrow["inactive"], 'chart_class', 'cid');
124         edit_button_cell("Edit".$myrow["cid"], _("Edit"));
125         delete_button_cell("Delete".$myrow["cid"], _("Delete"));
126         end_row();
127 }
128 inactive_control_row($th);
129 end_table(1);
130 //-----------------------------------------------------------------------------------
131
132 start_table($table_style2);
133
134 if ($selected_id != -1) 
135 {
136  if ($Mode == 'Edit') {
137         //editing an existing status code
138         $myrow = get_account_class($selected_id);
139
140         $_POST['id']  = $myrow["cid"];
141         $_POST['name']  = $myrow["class_name"];
142         $_POST['ctype']  = $myrow["ctype"];
143         hidden('selected_id', $selected_id);
144  }
145         hidden('id');
146         label_row(_("Class ID:"), $_POST['id']);
147
148
149 else 
150 {
151
152         text_row_ex(_("Class ID:"), 'id', 3);
153 }
154
155 text_row_ex(_("Class Name:"), 'name', 50, 60);
156
157 class_types_list_row(_("Class Type:"), 'ctype', null);
158
159 end_table(1);
160
161 submit_add_or_update_center($selected_id == -1, '', 'both');
162
163 end_form();
164
165 //------------------------------------------------------------------------------------
166
167 end_page();
168
169 ?>