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