User side percent/qty/amount/exrate input formatting via onblur handler.
[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         }
43         elseif (!check_num('price', 0))
44         {
45         $input_error = 1;
46         display_error( _("The price entered was not numeric."));
47         }
48         elseif (!check_num('conversion_factor'))
49         {
50         $input_error = 1;
51         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."));
52         }
53
54         if ($input_error == 0)
55         {
56         if (isset($_POST['AddRecord']))
57         {
58
59                 $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
60                         conversion_factor, supplier_description) VALUES (";
61                 $sql .= "'$supplier_id', '" . $_POST['stock_id'] . "', " . 
62                     input_num('price') . ", '" . $_POST['suppliers_uom'] . "', " .
63                         input_num('conversion_factor') . ", '" . $_POST['supplier_description'] . "')";
64
65                 db_query($sql,"The supplier purchasing details could not be added");
66                 display_notification(_("This supplier purchasing data has been added."));
67         }
68
69         if (isset($_POST['UpdateRecord']))
70         {
71                 $sql = "UPDATE ".TB_PREF."purch_data SET price=" . input_num('price') . ",
72                                 suppliers_uom='" . $_POST['suppliers_uom'] . "',
73                                 conversion_factor=" . input_num('conversion_factor') . ",
74                                 supplier_description='" . $_POST['supplier_description'] . "'
75                                 WHERE stock_id='" . $_POST['stock_id'] . "' AND
76                                 supplier_id='$supplier_id'";
77                 db_query($sql,"The supplier purchasing details could not be updated");
78
79                 display_notification(_("Supplier purchasing data has been updated."));
80         }
81
82         if (isset($_POST['UpdateRecord']) || isset($_POST['AddRecord']))
83         {
84                 //update or insert took place and need to clear the form
85                 unset($supplier_id);
86                 unset($_POST['price']);
87                 unset($_POST['suppliers_uom']);
88                 unset($_POST['conversion_factor']);
89                 unset($_POST['supplier_description']);
90         }
91         }
92 }
93
94 //--------------------------------------------------------------------------------------------------
95
96 if (isset($_GET['Delete']))
97 {
98
99         $sql = "DELETE FROM ".TB_PREF."purch_data WHERE supplier_id='$supplier_id'
100                 AND stock_id='" . $_POST['stock_id'] . "'";
101         db_query($sql,"could not delete purchasing data");
102
103         display_note(_("The purchasing data item has been sucessfully deleted."));
104         unset ($supplier_id);
105 }
106
107 //--------------------------------------------------------------------------------------------------
108
109 start_form(false, true);
110
111 if (!isset($_POST['stock_id']))
112         $_POST['stock_id'] = get_global_stock_item();
113
114 echo "<center>" . _("Item:"). "&nbsp;";
115 stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
116
117 echo "<hr><center>";
118
119 set_global_stock_item($_POST['stock_id']);
120
121 $mb_flag = get_mb_flag($_POST['stock_id']);
122
123 if ($mb_flag == -1) 
124 {
125         display_error(_("Entered item is not defined. Please re-enter."));
126
127 else 
128 {
129
130     $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name,".TB_PREF."suppliers.curr_code
131                 FROM ".TB_PREF."purch_data INNER JOIN ".TB_PREF."suppliers
132                 ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
133                 WHERE stock_id = '" . $_POST['stock_id'] . "'";
134
135     $result = db_query($sql, "The supplier purchasing details for the selected part could not be retrieved");
136
137     if (db_num_rows($result) == 0)
138     {
139         display_note(_("There is no purchasing data set up for the part selected"));
140     } 
141     else 
142     {
143         start_table("$table_style width=60%");
144
145                 $th = array(_("Supplier"), _("Price"), _("Currency"),
146                         _("Supplier's Unit"), _("Supplier's Description"), "", "");
147
148         table_header($th);
149
150         $k = $j = 0; //row colour counter
151
152         while ($myrow = db_fetch($result))
153         {
154                         alt_table_row_color($k);
155
156             label_cell($myrow["supp_name"]);
157             amount_cell($myrow["price"]);
158             label_cell($myrow["curr_code"]);
159             label_cell($myrow["suppliers_uom"]);
160             label_cell($myrow["supplier_description"]);
161             edit_link_cell("stock_id=" . $_POST['stock_id']. "&supplier_id=" . $myrow["supplier_id"] . "&Edit=1");
162             delete_link_cell("stock_id=" . $_POST['stock_id']. "&supplier_id=" . $myrow["supplier_id"] . "&Delete=1");
163             end_row();
164
165             $j++;
166             If ($j == 12)
167             {
168                 $j = 1;
169                         table_header($th);
170             } //end of page full new headings
171         } //end of while loop
172
173         end_table();
174     }
175 }
176
177 //------------------------------------------------------------------------------------------------
178
179 if (isset($_GET['Edit']))
180 {
181
182         $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name FROM ".TB_PREF."purch_data
183                 INNER JOIN ".TB_PREF."suppliers ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
184                 WHERE ".TB_PREF."purch_data.supplier_id='$supplier_id'
185                 AND ".TB_PREF."purch_data.stock_id='" . $_POST['stock_id'] . "'";
186
187         $result = db_query($sql, "The supplier purchasing details for the selected supplier and item could not be retrieved");
188
189         $myrow = db_fetch($result);
190
191     $supp_name = $myrow["supp_name"];
192     $_POST['price'] = price_format($myrow["price"]);
193     $_POST['suppliers_uom'] = $myrow["suppliers_uom"];
194     $_POST['supplier_description'] = $myrow["supplier_description"];
195     $_POST['conversion_factor'] = exrate_format($myrow["conversion_factor"]);
196 }
197
198 echo "<br>";
199 start_table($table_style2);
200
201 if (isset($_GET['Edit'])) 
202 {
203         hidden('supplier_id', $supplier_id);
204         label_row(_("Supplier:"), $supp_name);
205
206 else
207 {
208         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true);
209         $supplier_id = $_POST['supplier_id'];
210 }       
211 amount_row(_("Price:"), 'price', null,'', get_supplier_currency($supplier_id));
212 text_row(_("Suppliers Unit of Measure:"), 'suppliers_uom', null, 50, 51);
213
214 if (!isset($_POST['conversion_factor']) || $_POST['conversion_factor'] == "")
215 {
216         $_POST['conversion_factor'] = exrate_format(1);
217 }
218 amount_row(_("Conversion Factor (to our UOM):"), 'conversion_factor', 
219   exrate_format($_POST['conversion_factor']), null, null, user_exrate_dec() );
220 text_row(_("Supplier's Code or Description:"), 'supplier_description', null, 50, 51);
221
222 end_table(1);
223
224 if (isset($_GET['Edit']))
225 {
226         submit_center('UpdateRecord', _("Update Purchasing Data"));
227
228 else 
229 {
230         submit_center('AddRecord', _("Add Purchasing Data"));
231 }
232
233 end_form();
234 end_page();
235
236 ?>