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