Focus set to invalid field after submit check fail
[fa-stable.git] / taxes / tax_groups.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="..";
5
6 include($path_to_root . "/includes/session.inc");
7
8 page(_("Tax Groups"));
9
10 include_once($path_to_root . "/includes/data_checks.inc");
11 include_once($path_to_root . "/includes/ui.inc");
12
13 include_once($path_to_root . "/taxes/db/tax_groups_db.inc");
14 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
15
16 if (isset($_GET['selected_id']))
17 {
18         $selected_id = $_GET['selected_id'];
19
20 elseif(isset($_POST['selected_id']))
21 {
22         $selected_id = $_POST['selected_id'];
23 }
24 else
25         $selected_id = -1;
26         
27 check_db_has_tax_types(_("There are no tax types defined. Define tax types before defining tax groups."));
28
29 //-----------------------------------------------------------------------------------
30
31 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
32 {
33
34         //initialise no input errors assumed initially before we test
35         $input_error = 0;
36
37         if (strlen($_POST['name']) == 0) 
38         {
39                 $input_error = 1;
40                 display_error(_("The tax group name cannot be empty."));
41                 set_focus('name');
42         } 
43         else 
44         {
45                 // make sure any entered rates are valid
46         for ($i = 0; $i < 5; $i++) 
47         {
48                 if (isset($_POST['tax_type_id' . $i]) && 
49                         $_POST['tax_type_id' . $i] != reserved_words::get_all_numeric() && 
50                         !check_num('rate' . $i, 0))
51                 {
52                         display_error( _("An entered tax rate is invalid or less than zero."));
53                         $input_error = 1;
54                         set_focus('rate');
55                         break;
56                 }
57         }
58         }
59
60         if ($input_error != 1) 
61         {
62
63                 // create an array of the taxes and array of rates
64         $taxes = array();
65         $rates = array();
66
67         for ($i = 0; $i < 5; $i++) 
68         {
69                 if (isset($_POST['tax_type_id' . $i]) &&
70                                 $_POST['tax_type_id' . $i] != reserved_words::get_any_numeric()) 
71                         {
72                         $taxes[] = $_POST['tax_type_id' . $i];
73                         $rates[] = input_num('rate' . $i);
74                 }
75         }
76
77         if ($selected_id != -1) 
78         {
79
80                 update_tax_group($selected_id, $_POST['name'], $_POST['tax_shipping'], $taxes, 
81                         $rates);
82
83         } 
84         else 
85         {
86
87                 add_tax_group($_POST['name'], $_POST['tax_shipping'], $taxes, $rates);
88         }
89
90                 meta_forward($_SERVER['PHP_SELF']);
91         }
92 }
93
94 //-----------------------------------------------------------------------------------
95
96 function can_delete($selected_id)
97 {
98         if ($selected_id == -1)
99                 return false;
100         $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE tax_group_id=$selected_id";
101         $result = db_query($sql, "could not query customers");
102         $myrow = db_fetch_row($result);
103         if ($myrow[0] > 0) 
104         {
105                 display_note(_("Cannot delete this tax group because customer branches been created referring to it."));
106                 return false;
107         }
108
109         $sql = "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE tax_group_id=$selected_id";
110         $result = db_query($sql, "could not query suppliers");
111         $myrow = db_fetch_row($result);
112         if ($myrow[0] > 0) 
113         {
114                 display_note(_("Cannot delete this tax group because suppliers been created referring to it."));
115                 return false;
116         }
117
118
119         return true;
120 }
121
122
123 //-----------------------------------------------------------------------------------
124
125 if (isset($_GET['delete'])) 
126 {
127
128         if (can_delete($selected_id))
129         {
130                 delete_tax_group($selected_id);
131                 meta_forward($_SERVER['PHP_SELF']);
132         }
133 }
134
135 //-----------------------------------------------------------------------------------
136
137 $result = get_all_tax_groups();
138
139 start_table($table_style);
140 $th = array(_("Description"), _("Tax Shipping"), "", "");
141 table_header($th);
142
143 $k = 0;
144 while ($myrow = db_fetch($result)) 
145 {
146
147         alt_table_row_color($k);
148
149         label_cell($myrow["name"]);
150         if ($myrow["tax_shipping"])
151                 label_cell(_("Yes"));
152         else
153                 label_cell(_("No"));
154
155         /*for ($i=0; $i< 5; $i++)
156                 if ($myrow["type" . $i] != reserved_words::get_all_numeric())
157                         echo "<td>" . $myrow["type" . $i] . "</td>";*/
158
159         edit_link_cell("selected_id=" . $myrow["id"]);
160         delete_link_cell("selected_id=" . $myrow["id"]. "&delete=1");
161         end_row();;
162 }
163
164 end_table();
165
166 //-----------------------------------------------------------------------------------
167
168 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Tax Group"));
169
170 start_form();
171
172 start_table($table_style2);
173
174 if ($selected_id != -1) 
175 {
176         //editing an existing status code
177
178         if (!isset($_POST['name']))
179         {
180         $group = get_tax_group($selected_id);
181
182         $_POST['name']  = $group["name"];
183         $_POST['tax_shipping'] = $group["tax_shipping"];
184
185         $items = get_tax_group_items($selected_id);
186
187         $i = 0;
188         while ($tax_item = db_fetch($items)) 
189         {
190                 $_POST['tax_type_id' . $i]  = $tax_item["tax_type_id"];
191                 $_POST['rate' . $i]  = percent_format($tax_item["rate"]);
192                 $i ++;
193         }
194         }
195
196         hidden('selected_id', $selected_id);
197 }
198 text_row_ex(_("Description:"), 'name', 40);
199 yesno_list_row(_("Tax Shipping:"), 'tax_shipping', null, "", "", true);
200
201 end_table();
202
203 display_note(_("Select the taxes that are included in this group."), 1);
204
205 start_table($table_style2);
206 $th = array(_("Tax"), _("Default Rate (%)"), _("Rate (%)"));
207 table_header($th);
208 for ($i = 0; $i < 5; $i++) 
209 {
210         start_row();
211         if (!isset($_POST['tax_type_id' . $i]))
212                 $_POST['tax_type_id' . $i] = 0;
213         tax_types_list_cells(null, 'tax_type_id' . $i, $_POST['tax_type_id' . $i], true, _("None"), true);
214
215         if ($_POST['tax_type_id' . $i] != 0 && $_POST['tax_type_id' . $i] != reserved_words::get_all_numeric()) 
216         {
217
218                 $default_rate = get_tax_type_default_rate($_POST['tax_type_id' . $i]);
219                 label_cell(percent_format($default_rate), "nowrap align=right");
220
221                 if (!isset($_POST['rate' . $i]) || $_POST['rate' . $i] == "")
222                         $_POST['rate' . $i] = percent_format($default_rate);
223                 small_amount_cells(null, 'rate' . $i, $_POST['rate' . $i], null, null, 
224                   user_percent_dec());
225         }
226         end_row();
227 }
228
229 end_table(1);
230
231 submit_add_or_update_center(!isset($selected_id));
232
233 end_form();
234
235 //------------------------------------------------------------------------------------
236
237 end_page();
238
239 ?>