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