Eliminated non-static method calls and other bulk fixes to fix php5 warnings
[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(_("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         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] != 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] != 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                         update_tax_group($selected_id, $_POST['name'], $_POST['tax_shipping'], $taxes, 
80                         $rates);
81                         display_notification(_('Selected tax group has been updated'));
82         } 
83         else 
84         {
85                         add_tax_group($_POST['name'], $_POST['tax_shipping'], $taxes, $rates);
86                         display_notification(_('New tax group has been added'));
87         }
88
89                 $Mode = 'RESET';
90         }
91 }
92
93 //-----------------------------------------------------------------------------------
94
95 function can_delete($selected_id)
96 {
97         if ($selected_id == -1)
98                 return false;
99         $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE tax_group_id=$selected_id";
100         $result = db_query($sql, "could not query customers");
101         $myrow = db_fetch_row($result);
102         if ($myrow[0] > 0) 
103         {
104                 display_note(_("Cannot delete this tax group because customer branches been created referring to it."));
105                 return false;
106         }
107
108         $sql = "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE tax_group_id=$selected_id";
109         $result = db_query($sql, "could not query suppliers");
110         $myrow = db_fetch_row($result);
111         if ($myrow[0] > 0) 
112         {
113                 display_note(_("Cannot delete this tax group because suppliers been created referring to it."));
114                 return false;
115         }
116
117
118         return true;
119 }
120
121
122 //-----------------------------------------------------------------------------------
123
124 if ($Mode == 'Delete')
125 {
126
127         if (can_delete($selected_id))
128         {
129                 delete_tax_group($selected_id);
130                 display_notification(_('Selected tax group has been deleted'));
131         }
132         $Mode = 'RESET';
133 }
134
135 if ($Mode == 'RESET')
136 {
137         $selected_id = -1;
138         $sav = get_post('show_inactive');
139         unset($_POST);
140         $_POST['show_inactive'] = $sav;
141 }
142 //-----------------------------------------------------------------------------------
143
144 $result = get_all_tax_groups(check_value('show_inactive'));
145
146 start_form();
147 start_table($table_style);
148 $th = array(_("Description"), _("Tax Shipping"), "", "");
149 inactive_control_column($th);
150 table_header($th);
151
152 $k = 0;
153 while ($myrow = db_fetch($result)) 
154 {
155
156         alt_table_row_color($k);
157
158         label_cell($myrow["name"]);
159         if ($myrow["tax_shipping"])
160                 label_cell(_("Yes"));
161         else
162                 label_cell(_("No"));
163
164         /*for ($i=0; $i< 5; $i++)
165                 if ($myrow["type" . $i] != ALL_NUMERIC)
166                         echo "<td>" . $myrow["type" . $i] . "</td>";*/
167
168         inactive_control_cell($myrow["id"], $myrow["inactive"], 'tax_groups', 'id');
169         edit_button_cell("Edit".$myrow["id"], _("Edit"));
170         delete_button_cell("Delete".$myrow["id"], _("Delete"));
171         end_row();;
172 }
173
174 inactive_control_row($th);
175 end_table(1);
176 //-----------------------------------------------------------------------------------
177
178 start_table($table_style2);
179
180 if ($selected_id != -1) 
181 {
182         //editing an existing status code
183
184         if ($Mode == 'Edit') {
185         $group = get_tax_group($selected_id);
186
187         $_POST['name']  = $group["name"];
188         $_POST['tax_shipping'] = $group["tax_shipping"];
189
190         $items = get_tax_group_items($selected_id);
191
192         $i = 0;
193         while ($tax_item = db_fetch($items)) 
194         {
195                 $_POST['tax_type_id' . $i]  = $tax_item["tax_type_id"];
196                 $_POST['rate' . $i]  = percent_format($tax_item["rate"]);
197                 $i ++;
198         }
199         }
200
201         hidden('selected_id', $selected_id);
202 }
203 text_row_ex(_("Description:"), 'name', 40);
204 yesno_list_row(_("Tax Shipping:"), 'tax_shipping', null, "", "", true);
205
206 end_table();
207
208 display_note(_("Select the taxes that are included in this group."), 1);
209
210 start_table($table_style2);
211 $th = array(_("Tax"), _("Default Rate (%)"), _("Rate (%)"));
212 table_header($th);
213 for ($i = 0; $i < 5; $i++) 
214 {
215         start_row();
216         if (!isset($_POST['tax_type_id' . $i]))
217                 $_POST['tax_type_id' . $i] = 0;
218         tax_types_list_cells(null, 'tax_type_id' . $i, $_POST['tax_type_id' . $i], _("None"), true);
219
220         if ($_POST['tax_type_id' . $i] != 0 && $_POST['tax_type_id' . $i] != ALL_NUMERIC) 
221         {
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                 if (!isset($_POST['rate' . $i]) || $_POST['rate' . $i] == "")
227                         $_POST['rate' . $i] = percent_format($default_rate);
228                 small_amount_cells(null, 'rate' . $i, $_POST['rate' . $i], null, null, 
229                   user_percent_dec());
230         }
231         end_row();
232 }
233
234 end_table(1);
235
236 submit_add_or_update_center($selected_id == -1, '', 'both');
237
238 end_form();
239
240 //------------------------------------------------------------------------------------
241
242 end_page();
243
244 ?>