Changed tax grup retrieval functions to return all tax types (with filtered tax rates...
[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
44         if ($input_error != 1) 
45         {
46
47                 // create an array of the taxes and array of rates
48         $taxes = array();
49         $rates = array();
50
51                 while (($id = find_submit('tax_type_id'))!=-1)
52                 {
53                 $taxes[] = $id;
54                         $rates[] = get_tax_type_default_rate($id);
55                         unset($_POST['tax_type_id' . $id]);
56                 }
57         if ($selected_id != -1) 
58         {
59                         update_tax_group($selected_id, $_POST['name'], $_POST['tax_shipping'], $taxes, 
60                         $rates);
61                         display_notification(_('Selected tax group has been updated'));
62         } 
63         else 
64         {
65                         add_tax_group($_POST['name'], $_POST['tax_shipping'], $taxes, $rates);
66                         display_notification(_('New tax group has been added'));
67         }
68
69                 $Mode = 'RESET';
70         }
71 }
72
73 //-----------------------------------------------------------------------------------
74
75 function can_delete($selected_id)
76 {
77         if ($selected_id == -1)
78                 return false;
79         if (key_in_foreign_table($selected_id, 'cust_branch', 'tax_group_id'))  
80         {
81                 display_error(_("Cannot delete this tax group because customer branches been created referring to it."));
82                 return false;
83         }
84
85         if (key_in_foreign_table($selected_id, 'suppliers', 'tax_group_id'))
86         {
87                 display_error(_("Cannot delete this tax group because suppliers been created referring to it."));
88                 return false;
89         }
90
91
92         return true;
93 }
94
95
96 //-----------------------------------------------------------------------------------
97
98 if ($Mode == 'Delete')
99 {
100
101         if (can_delete($selected_id))
102         {
103                 delete_tax_group($selected_id);
104                 display_notification(_('Selected tax group has been deleted'));
105         }
106         $Mode = 'RESET';
107 }
108
109 if ($Mode == 'RESET')
110 {
111         $selected_id = -1;
112         $sav = get_post('show_inactive');
113         unset($_POST);
114         if($sav)
115                 $_POST['show_inactive'] = $sav;
116 }
117 //-----------------------------------------------------------------------------------
118
119 $result = get_all_tax_groups(check_value('show_inactive'));
120
121 start_form();
122
123 start_table(TABLESTYLE);
124 $th = array(_("Description"), _("Shipping Tax"), "", "");
125 inactive_control_column($th);
126
127 table_header($th);
128
129 $k = 0;
130 while ($myrow = db_fetch($result)) 
131 {
132
133         alt_table_row_color($k);
134
135         label_cell($myrow["name"]);
136         if ($myrow["tax_shipping"])
137                 label_cell(_("Yes"));
138         else
139                 label_cell(_("No"));
140
141         inactive_control_cell($myrow["id"], $myrow["inactive"], 'tax_groups', 'id');
142         edit_button_cell("Edit".$myrow["id"], _("Edit"));
143         delete_button_cell("Delete".$myrow["id"], _("Delete"));
144         end_row();;
145 }
146
147 inactive_control_row($th);
148 end_table(1);
149
150 //-----------------------------------------------------------------------------------
151
152 start_table(TABLESTYLE2);
153
154 if ($selected_id != -1) 
155 {
156         //editing an existing status code
157
158         if ($Mode == 'Edit') {
159         $group = get_tax_group($selected_id);
160
161         $_POST['name']  = $group["name"];
162         $_POST['tax_shipping'] = $group["tax_shipping"];
163
164         }
165         hidden('selected_id', $selected_id);
166 }
167 text_row_ex(_("Description:"), 'name', 40);
168 yesno_list_row(_("Tax applied to Shipping:"), 'tax_shipping', null, "", "", true);
169
170 end_table();
171
172 display_note(_("Select the taxes that are included in this group."), 1, 1);
173
174 // null means transport tax group, but for new we do not use real rates
175 $items = get_tax_group_rates($selected_id!=-1 ? $selected_id : null);
176
177 $th = array(_("Tax"), "");
178
179 start_table(TABLESTYLE2);
180 table_header($th);
181
182 while($item = db_fetch_assoc($items)) 
183 {
184         check_row($item['tax_type_name'], 'tax_type_id' . $item['tax_type_id'], 
185                 $selected_id!=-1 && isset($item['rate']), "align='center'");
186 }
187
188 end_table(1);
189
190 submit_add_or_update_center($selected_id == -1, '', 'both');
191
192 end_form();
193
194 //------------------------------------------------------------------------------------
195
196 end_page();
197
198 ?>