Some more fixes related to ajax combos behaviour and usage
[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 if (isset($_POST['_stock_id_update']))
110         $Ajax->activate('price_table');
111 //--------------------------------------------------------------------------------------------------
112
113 start_form(false, true);
114
115 if (!isset($_POST['stock_id']))
116         $_POST['stock_id'] = get_global_stock_item();
117
118 echo "<center>" . _("Item:"). "&nbsp;";
119 stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
120
121 echo "<hr></center>";
122
123 set_global_stock_item($_POST['stock_id']);
124
125 $mb_flag = get_mb_flag($_POST['stock_id']);
126
127 if ($mb_flag == -1)
128 {
129         display_error(_("Entered item is not defined. Please re-enter."));
130         set_focus('stock_id');
131 }
132 else
133 {
134
135     $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name,".TB_PREF."suppliers.curr_code
136                 FROM ".TB_PREF."purch_data INNER JOIN ".TB_PREF."suppliers
137                 ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
138                 WHERE stock_id = '" . $_POST['stock_id'] . "'";
139
140     $result = db_query($sql, "The supplier purchasing details for the selected part could not be retrieved");
141   div_start('price_table');
142     if (db_num_rows($result) == 0)
143     {
144         display_note(_("There is no purchasing data set up for the part selected"));
145     }
146     else
147     {
148         start_table("$table_style width=60%");
149
150                 $th = array(_("Supplier"), _("Price"), _("Currency"),
151                         _("Supplier's Unit"), _("Supplier's Description"), "", "");
152
153         table_header($th);
154
155         $k = $j = 0; //row colour counter
156
157         while ($myrow = db_fetch($result))
158         {
159                         alt_table_row_color($k);
160
161             label_cell($myrow["supp_name"]);
162             amount_cell($myrow["price"]);
163             label_cell($myrow["curr_code"]);
164             label_cell($myrow["suppliers_uom"]);
165             label_cell($myrow["supplier_description"]);
166             edit_link_cell("stock_id=" . $_POST['stock_id']. "&supplier_id=" . $myrow["supplier_id"] . "&Edit=1");
167             delete_link_cell("stock_id=" . $_POST['stock_id']. "&supplier_id=" . $myrow["supplier_id"] . "&Delete=1");
168             end_row();
169
170             $j++;
171             If ($j == 12)
172             {
173                 $j = 1;
174                         table_header($th);
175             } //end of page full new headings
176         } //end of while loop
177
178         end_table();
179     }
180  div_end();
181 }
182
183 //------------------------------------------------------------------------------------------------
184
185 if (isset($_GET['Edit']))
186 {
187
188         $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name FROM ".TB_PREF."purch_data
189                 INNER JOIN ".TB_PREF."suppliers ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
190                 WHERE ".TB_PREF."purch_data.supplier_id='$supplier_id'
191                 AND ".TB_PREF."purch_data.stock_id='" . $_POST['stock_id'] . "'";
192
193         $result = db_query($sql, "The supplier purchasing details for the selected supplier and item could not be retrieved");
194
195         $myrow = db_fetch($result);
196
197     $supp_name = $myrow["supp_name"];
198     $_POST['price'] = price_format($myrow["price"]);
199     $_POST['suppliers_uom'] = $myrow["suppliers_uom"];
200     $_POST['supplier_description'] = $myrow["supplier_description"];
201     $_POST['conversion_factor'] = exrate_format($myrow["conversion_factor"]);
202 }
203
204 echo "<br>";
205 start_table($table_style2);
206
207 if (isset($_GET['Edit']))
208 {
209         hidden('supplier_id', $supplier_id);
210         label_row(_("Supplier:"), $supp_name);
211 }
212 else
213 {
214         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true);
215         $supplier_id = $_POST['supplier_id'];
216 }
217 amount_row(_("Price:"), 'price', null,'', get_supplier_currency($supplier_id));
218 text_row(_("Suppliers Unit of Measure:"), 'suppliers_uom', null, 50, 51);
219
220 if (!isset($_POST['conversion_factor']) || $_POST['conversion_factor'] == "")
221 {
222         $_POST['conversion_factor'] = exrate_format(1);
223 }
224 amount_row(_("Conversion Factor (to our UOM):"), 'conversion_factor',
225   exrate_format($_POST['conversion_factor']), null, null, user_exrate_dec() );
226 text_row(_("Supplier's Code or Description:"), 'supplier_description', null, 50, 51);
227
228 end_table(1);
229
230 if (isset($_GET['Edit']))
231 {
232         submit_center('UpdateRecord', _("Update Purchasing Data"));
233 }
234 else
235 {
236         submit_center('AddRecord', _("Add Purchasing Data"));
237 }
238
239 end_form();
240 end_page();
241
242 ?>