Rerun of new tax group and shipping files
[fa-stable.git] / taxes / item_tax_types.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_ITEMTAXTYPE';
13 $path_to_root = "..";
14
15 include($path_to_root . "/includes/session.inc");
16
17 page(_($help_context = "Item Tax Types")); 
18
19 include_once($path_to_root . "/taxes/db/item_tax_types_db.inc");
20 include_once($path_to_root . "/taxes/db/tax_types_db.inc");
21
22 include($path_to_root . "/includes/ui.inc");
23
24 simple_page_mode(true);
25 //-----------------------------------------------------------------------------------
26
27 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
28 {
29
30         $input_error = 0;
31
32         if (strlen($_POST['name']) == 0) 
33         {
34                 $input_error = 1;
35                 display_error(_("The item tax type description cannot be empty."));
36                 set_focus('name');
37         }
38
39         if ($input_error != 1) 
40         {
41                 
42                 // create an array of the exemptions
43         $exempt_from = array();
44         
45         $tax_types = get_all_tax_types_simple();
46         $i = 0;         
47         
48         while ($myrow = db_fetch($tax_types)) 
49         {
50                 if (check_value('ExemptTax' . $myrow["id"]))
51                 {
52                         $exempt_from[$i] = $myrow["id"];
53                         $i++;
54                 }
55         }  
56         
57         if ($selected_id != -1) 
58         {               
59                 update_item_tax_type($selected_id, $_POST['name'], $_POST['exempt'], $exempt_from);
60                         display_notification(_('Selected item tax type has been updated'));
61         } 
62         else 
63         {
64                 add_item_tax_type($_POST['name'], $_POST['exempt'], $exempt_from);
65                         display_notification(_('New item tax type has been added'));
66         }
67                 $Mode = 'RESET';
68         }
69
70
71 //-----------------------------------------------------------------------------------
72
73 function can_delete($selected_id)
74 {
75         if (key_in_foreign_table($selected_id, 'stock_master', 'tax_type_id'))
76         {
77                 display_error(_("Cannot delete this item tax type because items have been created referring to it."));
78                 return false;
79         }
80         
81         return true;
82 }
83
84
85 //-----------------------------------------------------------------------------------
86
87 if ($Mode == 'Delete')
88 {
89
90         if (can_delete($selected_id))
91         {
92                 delete_item_tax_type($selected_id);
93                 display_notification(_('Selected item tax type has been deleted'));
94         }
95         $Mode = 'RESET';
96 }
97
98 if ($Mode == 'RESET')
99 {
100         $selected_id = -1;
101         $sav = get_post('show_inactive');
102         unset($_POST);
103         $_POST['show_inactive'] = $sav;
104 }
105 //-----------------------------------------------------------------------------------
106
107
108 $result2 = $result = get_all_item_tax_types(check_value('show_inactive'));
109
110 start_form();
111 start_table(TABLESTYLE, "width=30%");
112 $th = array(_("Name"), _("Tax exempt"),'','');
113 inactive_control_column($th);
114 table_header($th);
115
116 $k = 0;
117 while ($myrow = db_fetch($result2)) 
118 {
119         
120         alt_table_row_color($k);        
121
122         if ($myrow["exempt"] == 0) 
123         {
124                 $disallow_text = _("No");
125         } 
126         else 
127         {
128                 $disallow_text = _("Yes");
129         }
130         
131         label_cell($myrow["name"]);
132         label_cell($disallow_text);
133         inactive_control_cell($myrow["id"], $myrow["inactive"], 'item_tax_types', 'id');
134         edit_button_cell("Edit".$myrow["id"], _("Edit"));
135         delete_button_cell("Delete".$myrow["id"], _("Delete"));
136         end_row();
137 }
138
139 inactive_control_row($th);
140 end_table(1);
141 //-----------------------------------------------------------------------------------
142
143 start_table(TABLESTYLE2);
144
145 if ($selected_id != -1) 
146 {
147         if ($Mode == 'Edit') {
148                 $myrow = get_item_tax_type($selected_id);
149                 unset($_POST); // clear exemption checkboxes
150                 $_POST['name']  = $myrow["name"];
151                 $_POST['exempt']  = $myrow["exempt"];
152         
153                 // read the exemptions and check the ones that are on
154                 $exemptions = get_item_tax_type_exemptions($selected_id);
155         
156                 if (db_num_rows($exemptions) > 0)
157                 {
158                         while ($exmp = db_fetch($exemptions)) 
159                         {
160                                 $_POST['ExemptTax' . $exmp["tax_type_id"]] = 1;
161                         }
162                 }       
163         }
164
165         hidden('selected_id', $selected_id);
166
167
168 text_row_ex(_("Description:"), 'name', 50);
169
170 yesno_list_row(_("Is Fully Tax-exempt:"), 'exempt', null, "", "", true);
171
172 end_table(1);
173
174 if (!isset($_POST['exempt']) || $_POST['exempt'] == 0) 
175 {
176
177     display_note(_("Select which taxes this item tax type is exempt from."), 0, 1);
178     
179     start_table(TABLESTYLE2);
180     $th = array(_("Tax Name"), _("Rate"), _("Is exempt"));
181     table_header($th);
182         
183     $tax_types = get_all_tax_types_simple();            
184     
185     while ($myrow = db_fetch($tax_types)) 
186     {
187         
188         alt_table_row_color($k);        
189     
190         label_cell($myrow["name"]);
191                 label_cell(percent_format($myrow["rate"])." %", "nowrap align=right");
192         check_cells("", 'ExemptTax' . $myrow["id"], null);
193         end_row();
194     }
195     
196     end_table(1);
197 }
198
199 submit_add_or_update_center($selected_id == -1, '', 'both');
200
201 end_form();
202
203 //------------------------------------------------------------------------------------
204
205 end_page();
206
207 ?>