Fixed control buttons reset after record deletion.
[fa-stable.git] / gl / manage / gl_account_types.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("GL Account Groups"));
8
9 include($path_to_root . "/gl/includes/gl_db.inc");
10
11 include($path_to_root . "/includes/ui.inc");
12
13 simple_page_mode(false);
14 //-----------------------------------------------------------------------------------
15
16 function can_process() 
17 {
18         global $selected_id;
19
20         if (strlen($_POST['name']) == 0) 
21         {
22                 display_error( _("The account group name cannot be empty."));
23                 set_focus('name');
24                 return false;
25         }
26
27         if (isset($selected_id) && ($selected_id == $_POST['parent'])) 
28         {
29                 display_error(_("You cannot set an account group to be a subgroup of itself."));
30                 return false;
31         }
32
33         return true;
34 }
35
36 //-----------------------------------------------------------------------------------
37
38 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
39 {
40
41         if (can_process()) 
42         {
43
44         if ($selected_id != "") 
45         {
46                 update_account_type($selected_id, $_POST['name'], $_POST['class_id'], $_POST['parent']);
47                         display_notification(_('Selected account type has been updated'));
48         } 
49         else 
50         {
51                 add_account_type($_POST['name'], $_POST['class_id'], $_POST['parent']);
52                         display_notification(_('New account type has been added'));
53         }
54                 $Mode = 'RESET';
55         }
56 }
57
58 //-----------------------------------------------------------------------------------
59
60 function can_delete($selected_id)
61 {
62         if ($selected_id == "")
63                 return false;
64         $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_master
65                 WHERE account_type=$selected_id";
66         $result = db_query($sql, "could not query chart master");
67         $myrow = db_fetch_row($result);
68         if ($myrow[0] > 0) 
69         {
70                 display_error(_("Cannot delete this account group because GL accounts have been created referring to it."));
71                 return false;
72         }
73
74         $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_types
75                 WHERE parent=$selected_id";
76         $result = db_query($sql, "could not query chart types");
77         $myrow = db_fetch_row($result);
78         if ($myrow[0] > 0) 
79         {
80                 display_error(_("Cannot delete this account group because GL account groups have been created referring to it."));
81                 return false;
82         }
83
84         return true;
85 }
86
87
88 //-----------------------------------------------------------------------------------
89
90 if ($Mode == 'Delete')
91 {
92
93         if (can_delete($selected_id))
94         {
95                 delete_account_type($selected_id);
96                 display_notification(_('Selected currency has been deleted'));
97         }
98         $Mode = 'RESET';
99 }
100 if ($Mode == 'RESET')
101 {
102         $selected_id = '';
103         $_POST['name']  = '';
104         unset($_POST['parent']);
105         unset($_POST['class_id']);
106 }
107 //-----------------------------------------------------------------------------------
108
109 $result = get_account_types();
110 start_form();
111 start_table($table_style);
112 $th = array(_("Name"), _("Subgroup Of"), _("Class Type"), "", "");
113 table_header($th);
114
115 $k = 0;
116 while ($myrow = db_fetch($result)) 
117 {
118
119         alt_table_row_color($k);
120
121         $bs_text = get_account_class_name($myrow["class_id"]);
122
123         if ($myrow["parent"] == reserved_words::get_any_numeric()) 
124         {
125                 $parent_text = "";
126         } 
127         else 
128         {
129                 $parent_text = get_account_type_name($myrow["parent"]);
130         }
131
132         label_cell($myrow["name"]);
133         label_cell($parent_text);
134         label_cell($bs_text);
135         edit_button_cell("Edit".$myrow["id"], _("Edit"));
136         edit_button_cell("Delete".$myrow["id"], _("Delete"));
137         end_row();
138 }
139
140 end_table();
141 end_form();
142 echo '<br>';
143 //-----------------------------------------------------------------------------------
144
145 start_form();
146
147 start_table($table_style2);
148
149 if ($Mode == 'Edit') 
150 {
151         //editing an existing status code
152         $myrow = get_account_type($selected_id);
153
154         $_POST['name']  = $myrow["name"];
155         $_POST['parent']  = $myrow["parent"];
156         $_POST['class_id']  = $myrow["class_id"];
157  }
158         hidden('selected_id', $selected_id);
159
160 text_row_ex(_("Name:"), 'name', 50);
161
162 gl_account_types_list_row(_("Subgroup Of:"), 'parent', null, _("None"), true);
163
164 class_list_row(_("Class Type:"), 'class_id', null);
165
166 end_table(1);
167
168 submit_add_or_update_center($selected_id == '', '', true);
169
170 end_form();
171
172 //------------------------------------------------------------------------------------
173
174 end_page();
175
176 ?>