Focus set to invalid field after submit check fail
[fa-stable.git] / inventory / purchasing_data.php
1 <?php
2
3
4 $page_security = 4;
5 $path_to_root="..";
6 include_once($path_to_root . "/includes/session.inc");
7
8 page(_("Supplier Purchasing Data"));
9
10 include_once($path_to_root . "/includes/date_functions.inc");
11 include_once($path_to_root . "/includes/ui.inc");
12 include_once($path_to_root . "/includes/manufacturing.inc");
13 include_once($path_to_root . "/includes/data_checks.inc");
14
15 check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
16 check_db_has_suppliers(_("There are no suppliers defined in the system."));
17
18 if (isset($_GET['supplier_id']))
19 {
20         $supplier_id = strtoupper($_GET['supplier_id']);
21
22 elseif (isset($_POST['supplier_id']))
23 {
24         $supplier_id = strtoupper($_POST['supplier_id']);
25 }
26
27 if (isset($_GET['stock_id']))
28 {
29         $_POST['stock_id'] = $_GET['stock_id'];
30 }
31
32 //--------------------------------------------------------------------------------------------------
33
34 if ((isset($_POST['AddRecord']) || isset($_POST['UpdateRecord'])) && isset($supplier_id))
35 {
36
37         $input_error = 0;
38         if ($_POST['stock_id'] == "" || !isset($_POST['stock_id']))
39         {
40         $input_error = 1;
41         display_error( _("There is no item selected."));
42         set_focus('stock_id');
43         }
44         elseif (!check_num('price', 0))
45         {
46         $input_error = 1;
47         display_error( _("The price entered was not numeric."));
48         set_focus('price');
49         }
50         elseif (!check_num('conversion_factor'))
51         {
52         $input_error = 1;
53         display_error( _("The conversion factor entered was not numeric. The conversion factor is the number by which the price must be divided by to get the unit price in our unit of measure."));
54         set_focus('conversion_factor');
55         }
56
57         if ($input_error == 0)
58         {
59         if (isset($_POST['AddRecord']))
60         {
61
62                 $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
63                         conversion_factor, supplier_description) VALUES (";
64                 $sql .= "'$supplier_id', '" . $_POST['stock_id'] . "', " . 
65                     input_num('price') . ", '" . $_POST['suppliers_uom'] . "', " .
66                         input_num('conversion_factor') . ", '" . $_POST['supplier_description'] . "')";
67
68                 db_query($sql,"The supplier purchasing details could not be added");
69                 display_notification(_("This supplier purchasing data has been added."));
70         }
71
72         if (isset($_POST['UpdateRecord']))
73         {
74                 $sql = "UPDATE ".TB_PREF."purch_data SET price=" . input_num('price') . ",
75                                 suppliers_uom='" . $_POST['suppliers_uom'] . "',
76                                 conversion_factor=" . input_num('conversion_factor') . ",
77                                 supplier_description='" . $_POST['supplier_description'] . "'
78                                 WHERE stock_id='" . $_POST['stock_id'] . "' AND
79                                 supplier_id='$supplier_id'";
80                 db_query($sql,"The supplier purchasing details could not be updated");
81
82                 display_notification(_("Supplier purchasing data has been updated."));
83         }
84
85         if (isset($_POST['UpdateRecord']) || isset($_POST['AddRecord']))
86         {
87                 //update or insert took place and need to clear the form
88                 unset($supplier_id);
89                 unset($_POST['price']);
90                 unset($_POST['suppliers_uom']);
91                 unset($_POST['conversion_factor']);
92                 unset($_POST['supplier_description']);
93         }
94         }
95 }
96
97 //--------------------------------------------------------------------------------------------------
98
99 if (isset($_GET['Delete']))
100 {
101
102         $sql = "DELETE FROM ".TB_PREF."purch_data WHERE supplier_id='$supplier_id'
103                 AND stock_id='" . $_POST['stock_id'] . "'";
104         db_query($sql,"could not delete purchasing data");
105
106         display_note(_("The purchasing data item has been sucessfully deleted."));
107         unset ($supplier_id);
108 }
109
110 //--------------------------------------------------------------------------------------------------
111
112 start_form(false, true);
113
114 if (!isset($_POST['stock_id']))
115         $_POST['stock_id'] = get_global_stock_item();
116
117 echo "<center>" . _("Item:"). "&nbsp;";
118 stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
119
120 echo "<hr><center>";
121
122 set_global_stock_item($_POST['stock_id']);
123
124 $mb_flag = get_mb_flag($_POST['stock_id']);
125
126 if ($mb_flag == -1) 
127 {
128         display_error(_("Entered item is not defined. Please re-enter."));
129         set_focus('stock_id');
130
131 else 
132 {
133
134     $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name,".TB_PREF."suppliers.curr_code
135                 FROM ".TB_PREF."purch_data INNER JOIN ".TB_PREF."suppliers
136                 ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
137                 WHERE stock_id = '" . $_POST['stock_id'] . "'";
138
139     $result = db_query($sql, "The supplier purchasing details for the selected part could not be retrieved");
140
141     if (db_num_rows($result) == 0)
142     {
143         display_note(_("There is no purchasing data set up for the part selected"));
144     } 
145     else 
146     {
147         start_table("$table_style width=60%");
148
149                 $th = array(_("Supplier"), _("Price"), _("Currency"),
150                         _("Supplier's Unit"), _("Supplier's Description"), "", "");
151
152         table_header($th);
153
154         $k = $j = 0; //row colour counter
155
156         while ($myrow = db_fetch($result))
157         {
158                         alt_table_row_color($k);
159
160             label_cell($myrow["supp_name"]);
161             amount_cell($myrow["price"]);
162             label_cell($myrow["curr_code"]);
163             label_cell($myrow["suppliers_uom"]);
164             label_cell($myrow["supplier_description"]);
165             edit_link_cell("stock_id=" . $_POST['stock_id']. "&supplier_id=" . $myrow["supplier_id"] . "&Edit=1");
166             delete_link_cell("stock_id=" . $_POST['stock_id']. "&supplier_id=" . $myrow["supplier_id"] . "&Delete=1");
167             end_row();
168
169             $j++;
170             If ($j == 12)
171             {
172                 $j = 1;
173                         table_header($th);
174             } //end of page full new headings
175         } //end of while loop
176
177         end_table();
178     }
179 }
180
181 //------------------------------------------------------------------------------------------------
182
183 if (isset($_GET['Edit']))
184 {
185
186         $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name FROM ".TB_PREF."purch_data
187                 INNER JOIN ".TB_PREF."suppliers ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
188                 WHERE ".TB_PREF."purch_data.supplier_id='$supplier_id'
189                 AND ".TB_PREF."purch_data.stock_id='" . $_POST['stock_id'] . "'";
190
191         $result = db_query($sql, "The supplier purchasing details for the selected supplier and item could not be retrieved");
192
193         $myrow = db_fetch($result);
194
195     $supp_name = $myrow["supp_name"];
196     $_POST['price'] = price_format($myrow["price"]);
197     $_POST['suppliers_uom'] = $myrow["suppliers_uom"];
198     $_POST['supplier_description'] = $myrow["supplier_description"];
199     $_POST['conversion_factor'] = exrate_format($myrow["conversion_factor"]);
200 }
201
202 echo "<br>";
203 start_table($table_style2);
204
205 if (isset($_GET['Edit'])) 
206 {
207         hidden('supplier_id', $supplier_id);
208         label_row(_("Supplier:"), $supp_name);
209
210 else
211 {
212         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true);
213         $supplier_id = $_POST['supplier_id'];
214 }       
215 amount_row(_("Price:"), 'price', null,'', get_supplier_currency($supplier_id));
216 text_row(_("Suppliers Unit of Measure:"), 'suppliers_uom', null, 50, 51);
217
218 if (!isset($_POST['conversion_factor']) || $_POST['conversion_factor'] == "")
219 {
220         $_POST['conversion_factor'] = exrate_format(1);
221 }
222 amount_row(_("Conversion Factor (to our UOM):"), 'conversion_factor', 
223   exrate_format($_POST['conversion_factor']), null, null, user_exrate_dec() );
224 text_row(_("Supplier's Code or Description:"), 'supplier_description', null, 50, 51);
225
226 end_table(1);
227
228 if (isset($_GET['Edit']))
229 {
230         submit_center('UpdateRecord', _("Update Purchasing Data"));
231
232 else 
233 {
234         submit_center('AddRecord', _("Add Purchasing Data"));
235 }
236
237 end_form();
238 end_page();
239
240 ?>