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