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