Moved all SQL statements from PHP files into relevant *_db.inc files.
[fa-stable.git] / taxes / tax_groups.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_TAXGROUPS';
13 $path_to_root = "..";
14
15 include($path_to_root . "/includes/session.inc");
16
17 page(_($help_context = "Tax Groups"));
18
19 include_once($path_to_root . "/includes/data_checks.inc");
20 include_once($path_to_root . "/includes/ui.inc");
21
22 include_once($path_to_root . "/taxes/db/tax_groups_db.inc");
23 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
24
25 simple_page_mode(true);
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 ($Mode=='ADD_ITEM' || $Mode=='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         /* Editable rate has been removed 090920 Joe Hunt
44         else 
45         {
46                 // make sure any entered rates are valid
47         for ($i = 0; $i < 5; $i++) 
48         {
49                 if (isset($_POST['tax_type_id' . $i]) && 
50                         $_POST['tax_type_id' . $i] != ALL_NUMERIC       && 
51                         !check_num('rate' . $i, 0))
52                 {
53                         display_error( _("An entered tax rate is invalid or less than zero."));
54                         $input_error = 1;
55                         set_focus('rate');
56                         break;
57                 }
58         }
59         }
60         */
61         if ($input_error != 1) 
62         {
63
64                 // create an array of the taxes and array of rates
65         $taxes = array();
66         $rates = array();
67
68         for ($i = 0; $i < 5; $i++) 
69         {
70                 if (isset($_POST['tax_type_id' . $i]) &&
71                                 $_POST['tax_type_id' . $i] != ANY_NUMERIC) 
72                         {
73                         $taxes[] = $_POST['tax_type_id' . $i];
74                                 $rates[] = get_tax_type_default_rate($_POST['tax_type_id' . $i]);
75                                 //Editable rate has been removed 090920 Joe Hunt
76                         //$rates[] = input_num('rate' . $i);
77                 }
78         }
79
80         if ($selected_id != -1) 
81         {
82                         update_tax_group($selected_id, $_POST['name'], $_POST['tax_shipping'], $taxes, 
83                         $rates);
84                         display_notification(_('Selected tax group has been updated'));
85         } 
86         else 
87         {
88                         add_tax_group($_POST['name'], $_POST['tax_shipping'], $taxes, $rates);
89                         display_notification(_('New tax group has been added'));
90         }
91
92                 $Mode = 'RESET';
93         }
94 }
95
96 //-----------------------------------------------------------------------------------
97
98 function can_delete($selected_id)
99 {
100         if ($selected_id == -1)
101                 return false;
102         if (key_in_foreign_table($selected_id, 'cust_branch', 'tax_group_id'))  
103         {
104                 display_error(_("Cannot delete this tax group because customer branches been created referring to it."));
105                 return false;
106         }
107
108         if (key_in_foreign_table($selected_id, 'suppliers', 'tax_group_id'))
109         {
110                 display_error(_("Cannot delete this tax group because suppliers been created referring to it."));
111                 return false;
112         }
113
114
115         return true;
116 }
117
118
119 //-----------------------------------------------------------------------------------
120
121 if ($Mode == 'Delete')
122 {
123
124         if (can_delete($selected_id))
125         {
126                 delete_tax_group($selected_id);
127                 display_notification(_('Selected tax group has been deleted'));
128         }
129         $Mode = 'RESET';
130 }
131
132 if ($Mode == 'RESET')
133 {
134         $selected_id = -1;
135         $sav = get_post('show_inactive');
136         unset($_POST);
137         $_POST['show_inactive'] = $sav;
138 }
139 //-----------------------------------------------------------------------------------
140
141 $result = get_all_tax_groups(check_value('show_inactive'));
142
143 start_form();
144 start_table($table_style);
145 $th = array(_("Description"), _("Tax Shipping"), "", "");
146 inactive_control_column($th);
147 table_header($th);
148
149 $k = 0;
150 while ($myrow = db_fetch($result)) 
151 {
152
153         alt_table_row_color($k);
154
155         label_cell($myrow["name"]);
156         if ($myrow["tax_shipping"])
157                 label_cell(_("Yes"));
158         else
159                 label_cell(_("No"));
160
161         /*for ($i=0; $i< 5; $i++)
162                 if ($myrow["type" . $i] != ALL_NUMERIC)
163                         echo "<td>" . $myrow["type" . $i] . "</td>";*/
164
165         inactive_control_cell($myrow["id"], $myrow["inactive"], 'tax_groups', 'id');
166         edit_button_cell("Edit".$myrow["id"], _("Edit"));
167         delete_button_cell("Delete".$myrow["id"], _("Delete"));
168         end_row();;
169 }
170
171 inactive_control_row($th);
172 end_table(1);
173
174 //-----------------------------------------------------------------------------------
175
176 start_table($table_style2);
177
178 if ($selected_id != -1) 
179 {
180         //editing an existing status code
181
182         if ($Mode == 'Edit') {
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]  = percent_format($tax_item["rate"]);
195                 $i ++;
196         }
197         while($i<5) unset($_POST['tax_type_id'.$i++]);
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 (%)"));
211 //Editable rate has been removed 090920 Joe Hunt
212 $th = array(_("Tax"), _("Rate (%)"));
213 table_header($th);
214 for ($i = 0; $i < 5; $i++) 
215 {
216         start_row();
217         if (!isset($_POST['tax_type_id' . $i]))
218                 $_POST['tax_type_id' . $i] = 0;
219         tax_types_list_cells(null, 'tax_type_id' . $i, $_POST['tax_type_id' . $i], _("None"), true);
220
221         if ($_POST['tax_type_id' . $i] != 0 && $_POST['tax_type_id' . $i] != ALL_NUMERIC) 
222         {
223                 $default_rate = get_tax_type_default_rate($_POST['tax_type_id' . $i]);
224                 label_cell(percent_format($default_rate), "nowrap align=right");
225
226                 //Editable rate has been removed 090920 Joe Hunt
227                 //if (!isset($_POST['rate' . $i]) || $_POST['rate' . $i] == "")
228                 //      $_POST['rate' . $i] = percent_format($default_rate);
229                 //small_amount_cells(null, 'rate' . $i, $_POST['rate' . $i], null, null, 
230                 //  user_percent_dec()); 
231         }
232         end_row();
233 }
234
235 end_table(1);
236
237 submit_add_or_update_center($selected_id == -1, '', 'both');
238
239 end_form();
240
241 //------------------------------------------------------------------------------------
242
243 end_page();
244
245 ?>