Merged changes from main CVS up to 2.1.5
[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         global $use_oldstyle_convert;
28         if (!is_numeric($_POST['id'])) 
29         {
30                 display_error( _("The account class ID must be numeric."));
31                 set_focus('id');
32                 return false;
33         }
34         if (strlen($_POST['name']) == 0) 
35         {
36                 display_error( _("The account class name cannot be empty."));
37                 set_focus('name');
38                 return false;
39         }
40         if (isset($use_oldstyle_convert) && $use_oldstyle_convert == 1)
41                 $_POST['Balance'] = check_value('Balance');
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_class($selected_id, $_POST['name'], $_POST['ctype']);
56                         display_notification(_('Selected account class settings has been updated'));
57         } 
58         else 
59         {
60                 add_account_class($_POST['id'], $_POST['name'], $_POST['ctype']);
61                         display_notification(_('New account class 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_types
74                 WHERE class_id=$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 class because GL account types have been created referring to it."));
80                 return false;
81         }
82
83         return true;
84 }
85
86
87 //-----------------------------------------------------------------------------------
88
89 if ($Mode == 'Delete')
90 {
91
92         if (can_delete($selected_id))
93         {
94                 delete_account_class($selected_id);
95                 display_notification(_('Selected account class has been deleted'));
96         }
97         $Mode = 'RESET';
98 }
99
100 //-----------------------------------------------------------------------------------
101 if ($Mode == 'RESET')
102 {
103         $selected_id = -1;
104         $_POST['id']  = $_POST['name']  = $_POST['ctype'] =  '';
105 }
106 //-----------------------------------------------------------------------------------
107
108 $result = get_account_classes(check_value('show_inactive'));
109
110 start_form();
111 start_table($table_style);
112 $th = array(_("Class ID"), _("Class Name"), _("Class Type"), "", "");
113 if (isset($use_oldstyle_convert) && $use_oldstyle_convert == 1)
114         $th[2] = _("Balance Sheet");
115 inactive_control_column($th);
116 table_header($th);
117
118 $k = 0;
119 while ($myrow = db_fetch($result)) 
120 {
121
122         alt_table_row_color($k);
123
124         label_cell($myrow["cid"]);
125         label_cell($myrow['class_name']);
126         if (isset($use_oldstyle_convert) && $use_oldstyle_convert == 1)
127         {
128                 $myrow['ctype'] = ($myrow["ctype"] >= CL_ASSETS && $myrow["ctype"] < CL_INCOME ? 1 : 0);
129                 label_cell(($myrow['ctype'] == 1 ? _("Yes") : _("No")));
130         }       
131         else    
132                 label_cell($class_types[$myrow["ctype"]]);
133         inactive_control_cell($myrow["cid"], $myrow["inactive"], 'chart_class', 'cid');
134         edit_button_cell("Edit".$myrow["cid"], _("Edit"));
135         delete_button_cell("Delete".$myrow["cid"], _("Delete"));
136         end_row();
137 }
138 inactive_control_row($th);
139 end_table(1);
140 //-----------------------------------------------------------------------------------
141
142 start_table($table_style2);
143
144 if ($selected_id != -1) 
145 {
146  if ($Mode == 'Edit') {
147         //editing an existing status code
148         $myrow = get_account_class($selected_id);
149
150         $_POST['id']  = $myrow["cid"];
151         $_POST['name']  = $myrow["class_name"];
152         if (isset($use_oldstyle_convert) && $use_oldstyle_convert == 1)
153                 $_POST['ctype'] = ($myrow["ctype"] >= CL_ASSETS && $myrow["ctype"] < CL_INCOME ? 1 : 0);
154         else
155                 $_POST['ctype']  = $myrow["ctype"];
156         hidden('selected_id', $selected_id);
157  }
158         hidden('id');
159         label_row(_("Class ID:"), $_POST['id']);
160
161
162 else 
163 {
164
165         text_row_ex(_("Class ID:"), 'id', 3);
166 }
167
168 text_row_ex(_("Class Name:"), 'name', 50, 60);
169
170 if (isset($use_oldstyle_convert) && $use_oldstyle_convert == 1)
171         check_row(_("Balance Sheet"), 'ctype', null);
172 else
173         class_types_list_row(_("Class Type:"), 'ctype', null);
174
175 end_table(1);
176
177 submit_add_or_update_center($selected_id == -1, '', 'both');
178
179 end_form();
180
181 //------------------------------------------------------------------------------------
182
183 end_page();
184
185 ?>