95eee3701ffbcd29cbb003f89d27322737ebbcb2
[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['Balance'], $_POST['convert']);
54                         display_notification(_('Selected account class settings has been updated'));
55         } 
56         else 
57         {
58                 add_account_class($_POST['id'], $_POST['name'], $_POST['Balance'], $_POST['convert']);
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['Balance'] = $_POST['sign_convert'] = '';
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"), _("Balance Sheet"), _("Sign Convert"), "", "");
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         if ($myrow["balance_sheet"] == 0) 
121         {
122                 $bs_text = _("No");
123         } 
124         else 
125         {
126                 $bs_text = _("Yes");
127         }
128         if ($myrow["sign_convert"] == 0) 
129         {
130                 $sc_text = _("No");
131         } 
132         else 
133         {
134                 $sc_text = _("Yes");
135         }
136         label_cell($myrow["cid"]);
137         label_cell($myrow['class_name']);
138         label_cell($bs_text);
139         label_cell($sc_text);
140         inactive_control_cell($myrow["cid"], $myrow["inactive"], 'chart_class', 'cid');
141         edit_button_cell("Edit".$myrow["cid"], _("Edit"));
142         delete_button_cell("Delete".$myrow["cid"], _("Delete"));
143         end_row();
144 }
145 inactive_control_row($th);
146 end_table(1);
147 //-----------------------------------------------------------------------------------
148
149 start_table($table_style2);
150
151 if ($selected_id != -1) 
152 {
153  if ($Mode == 'Edit') {
154         //editing an existing status code
155         $myrow = get_account_class($selected_id);
156
157         $_POST['id']  = $myrow["cid"];
158         $_POST['name']  = $myrow["class_name"];
159         $_POST['Balance']  = $myrow["balance_sheet"];
160         $_POST['convert']  = $myrow["sign_convert"];
161         hidden('selected_id', $selected_id);
162  }
163         hidden('id');
164         label_row(_("Class ID:"), $_POST['id']);
165
166
167 else 
168 {
169
170         text_row_ex(_("Class ID:"), 'id', 3);
171 }
172
173 text_row_ex(_("Class Name:"), 'name', 50, 60);
174
175 yesno_list_row(_("Balance Sheet:"), 'Balance', null, "", "", false);
176
177 yesno_list_row(_("Sign Convert (Balance Sheet/PL statement):"), 'convert', null, "", "", false);
178
179 end_table(1);
180
181 submit_add_or_update_center($selected_id == -1, '', 'both');
182
183 end_form();
184
185 //------------------------------------------------------------------------------------
186
187 end_page();
188
189 ?>