Format cleanup, small display fix in taxes/tax_type.php
[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         } 
42         else 
43         {
44                 // make sure any entered rates are valid
45         for ($i = 0; $i < 5; $i++) 
46         {
47                 if (isset($_POST['tax_type_id' . $i]) && 
48                         $_POST['tax_type_id' . $i] != reserved_words::get_all_numeric() && 
49                         (!is_numeric($_POST['rate' . $i]) || $_POST['rate' . $i] < 0))
50                 {
51                                 display_error( _("An entered tax rate is invalid or less than zero."));
52                         $input_error = 1;
53                                 break;
54                 }
55         }
56         }
57
58         if ($input_error != 1) 
59         {
60
61                 // create an array of the taxes and array of rates
62         $taxes = array();
63         $rates = array();
64         $included = array();
65
66         for ($i = 0; $i < 5; $i++) 
67         {
68                 if (isset($_POST['tax_type_id' . $i]) &&
69                                 $_POST['tax_type_id' . $i] != reserved_words::get_any_numeric()) 
70                         {
71                         $taxes[] = $_POST['tax_type_id' . $i];
72                         $rates[] = $_POST['rate' . $i];
73                         if (isset($_POST['included' . $i]))
74                                 $included[] = 1;
75                         else    
76                                 $included[] = 0;
77                 }
78         }
79
80         if ($selected_id != -1) 
81         {
82
83                 update_tax_group($selected_id, $_POST['name'], $_POST['tax_shipping'], $taxes, 
84                         $rates, $included);
85
86         } 
87         else 
88         {
89
90                 add_tax_group($_POST['name'], $_POST['tax_shipping'], $taxes, $rates, $included);
91         }
92
93                 meta_forward($_SERVER['PHP_SELF']);
94         }
95 }
96
97 //-----------------------------------------------------------------------------------
98
99 function can_delete($selected_id)
100 {
101         if ($selected_id == -1)
102                 return false;
103         $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE tax_group_id=$selected_id";
104         $result = db_query($sql, "could not query customers");
105         $myrow = db_fetch_row($result);
106         if ($myrow[0] > 0) 
107         {
108                 display_note(_("Cannot delete this tax group because customer branches been created referring to it."));
109                 return false;
110         }
111
112         $sql = "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE tax_group_id=$selected_id";
113         $result = db_query($sql, "could not query suppliers");
114         $myrow = db_fetch_row($result);
115         if ($myrow[0] > 0) 
116         {
117                 display_note(_("Cannot delete this tax group because suppliers been created referring to it."));
118                 return false;
119         }
120
121
122         return true;
123 }
124
125
126 //-----------------------------------------------------------------------------------
127
128 if (isset($_GET['delete'])) 
129 {
130
131         if (can_delete($selected_id))
132         {
133                 delete_tax_group($selected_id);
134                 meta_forward($_SERVER['PHP_SELF']);
135         }
136 }
137
138 //-----------------------------------------------------------------------------------
139
140 $result = get_all_tax_groups();
141
142 start_table($table_style);
143 $th = array(_("Description"), _("Tax Shipping"), "", "");
144 table_header($th);
145
146 $k = 0;
147 while ($myrow = db_fetch($result)) 
148 {
149
150         alt_table_row_color($k);
151
152         label_cell($myrow["name"]);
153         if ($myrow["tax_shipping"])
154                 label_cell(_("Yes"));
155         else
156                 label_cell(_("No"));
157
158         /*for ($i=0; $i< 5; $i++)
159                 if ($myrow["type" . $i] != reserved_words::get_all_numeric())
160                         echo "<td>" . $myrow["type" . $i] . "</td>";*/
161
162         edit_link_cell("selected_id=" . $myrow["id"]);
163         delete_link_cell("selected_id=" . $myrow["id"]. "&delete=1");
164         end_row();;
165 }
166
167 end_table();
168
169 //-----------------------------------------------------------------------------------
170
171 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Tax Group"));
172
173 start_form();
174
175 start_table($table_style2);
176
177 if ($selected_id != -1) 
178 {
179         //editing an existing status code
180
181         if (!isset($_POST['name']))
182         {
183         $group = get_tax_group($selected_id);
184
185         $_POST['name']  = $group["name"];
186         $_POST['tax_shipping'] = $group["tax_shipping"];
187
188         $items = get_tax_group_items($selected_id);
189
190         $i = 0;
191         while ($tax_item = db_fetch($items)) 
192         {
193                 $_POST['tax_type_id' . $i]  = $tax_item["tax_type_id"];
194                 $_POST['rate' . $i]  = $tax_item["rate"];
195                 $_POST['included' . $i]  = $tax_item["included_in_price"];
196                 $i ++;
197         }
198         }
199
200         hidden('selected_id', $selected_id);
201 }
202 text_row_ex(_("Description:"), 'name', 40);
203 yesno_list_row(_("Tax Shipping:"), 'tax_shipping', null, "", "", true);
204
205 end_table();
206
207 display_note(_("Select the taxes that are included in this group."), 1);
208
209 start_table($table_style2);
210 $th = array(_("Tax"), _("Default Rate (%)"), _("Rate (%)"), _("Include in Price"));
211 table_header($th);
212 for ($i = 0; $i < 5; $i++) 
213 {
214         start_row();
215         if (!isset($_POST['tax_type_id' . $i]))
216                 $_POST['tax_type_id' . $i] = 0;
217         if (!isset($_POST['included' . $i]))
218                 $_POST['included' . $i] = 0;
219         tax_types_list_cells(null, 'tax_type_id' . $i, $_POST['tax_type_id' . $i], true, _("None"), true);
220
221         if ($_POST['tax_type_id' . $i] != 0 && $_POST['tax_type_id' . $i] != reserved_words::get_all_numeric()) 
222         {
223
224                 $default_rate = get_tax_type_default_rate($_POST['tax_type_id' . $i]);
225                 label_cell(number_format2($default_rate, user_percent_dec()), "nowrap align=right");
226
227                 if (!isset($_POST['rate' . $i]) || $_POST['rate' . $i] == "")
228                         $_POST['rate' . $i] = $default_rate;
229                 text_cells(null, 'rate' . $i, $_POST['rate' . $i], 10, 10);
230                 check_cells(null, 'included' . $i, $_POST['included' . $i]);
231         }
232         end_row();
233 }
234
235 end_table(1);
236
237 submit_add_or_update_center(!isset($selected_id));
238
239 end_form();
240
241 //------------------------------------------------------------------------------------
242
243 end_page();
244
245 ?>