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