Security update merged from 2.1.
[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(_("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         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_master WHERE tax_type_id=".db_escape($selected_id);
76         $result = db_query($sql, "could not query stock master");
77         $myrow = db_fetch_row($result);
78         if ($myrow[0] > 0) 
79         {
80                 display_error(_("Cannot delete this item tax type because items have been created referring to it."));
81                 return false;
82         }
83         
84         return true;
85 }
86
87
88 //-----------------------------------------------------------------------------------
89
90 if ($Mode == 'Delete')
91 {
92
93         if (can_delete($selected_id))
94         {
95                 delete_item_tax_type($selected_id);
96                 display_notification(_('Selected item tax type has been deleted'));
97         }
98         $Mode = 'RESET';
99 }
100
101 if ($Mode == 'RESET')
102 {
103         $selected_id = -1;
104         $sav = get_post('show_inactive');
105         unset($_POST);
106         $_POST['show_inactive'] = $sav;
107 }
108 //-----------------------------------------------------------------------------------
109
110
111 $result2 = $result = get_all_item_tax_types(check_value('show_inactive'));
112
113 start_form();
114 start_table("$table_style width=30%");
115 $th = array(_("Name"), _("Tax exempt"),'','');
116 inactive_control_column($th);
117 table_header($th);
118
119 $k = 0;
120 while ($myrow = db_fetch($result2)) 
121 {
122         
123         alt_table_row_color($k);        
124
125         if ($myrow["exempt"] == 0) 
126         {
127                 $disallow_text = _("No");
128         } 
129         else 
130         {
131                 $disallow_text = _("Yes");
132         }
133         
134         label_cell($myrow["name"]);
135         label_cell($disallow_text);
136         inactive_control_cell($myrow["id"], $myrow["inactive"], 'item_tax_types', 'id');
137         edit_button_cell("Edit".$myrow["id"], _("Edit"));
138         delete_button_cell("Delete".$myrow["id"], _("Delete"));
139         end_row();
140 }
141
142 inactive_control_row($th);
143 end_table(1);
144 //-----------------------------------------------------------------------------------
145
146 start_table($table_style2);
147
148 if ($selected_id != -1) 
149 {
150         if ($Mode == 'Edit') {
151                 $myrow = get_item_tax_type($selected_id);
152     
153                 $_POST['name']  = $myrow["name"];
154                 $_POST['exempt']  = $myrow["exempt"];
155         
156                 // read the exemptions and check the ones that are on
157                 $exemptions = get_item_tax_type_exemptions($selected_id);
158         
159                 if (db_num_rows($exemptions) > 0)
160                 {
161                         while ($exmp = db_fetch($exemptions)) 
162                         {
163                                 $_POST['ExemptTax' . $exmp["tax_type_id"]] = 1;
164                         }
165                 }       
166         }
167
168         hidden('selected_id', $selected_id);
169
170
171 text_row_ex(_("Description:"), 'name', 50);
172
173 yesno_list_row(_("Is Fully Tax-exempt:"), 'exempt', null, "", "", true);
174
175 end_table(1);
176
177 if (!isset($_POST['exempt']) || $_POST['exempt'] == 0) 
178 {
179
180     display_note(_("Select which taxes this item tax type is exempt from."), 0, 1);
181     
182     start_table($table_style2);
183     $th = array(_("Tax Name"), _("Rate"), _("Is exempt"));
184     table_header($th);
185         
186     $tax_types = get_all_tax_types_simple();            
187     
188     while ($myrow = db_fetch($tax_types)) 
189     {
190         
191         alt_table_row_color($k);        
192     
193         label_cell($myrow["name"]);
194                 label_cell(percent_format($myrow["rate"])." %", "nowrap align=right");
195         check_cells("", 'ExemptTax' . $myrow["id"], null);
196         end_row();
197     }
198     
199     end_table(1);
200 }
201
202 submit_add_or_update_center($selected_id == -1, '', 'both');
203
204 end_form();
205
206 //------------------------------------------------------------------------------------
207
208 end_page();
209
210 ?>