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