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