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