6 include_once($path_to_root . "/includes/session.inc");
8 include_once($path_to_root . "/includes/date_functions.inc");
9 include_once($path_to_root . "/includes/manufacturing.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
12 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
13 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
17 $js .= get_js_date_picker();
18 page(_("Work Order Entry"), false, false, "", $js);
21 check_db_has_manufacturable_items(_("There are no manufacturable items defined in the system."));
23 check_db_has_locations(("There are no inventory locations defined in the system."));
25 //---------------------------------------------------------------------------------------
27 if (isset($_GET['trans_no']))
29 $selected_id = $_GET['trans_no'];
31 elseif(isset($_POST['selected_id']))
33 $selected_id = $_POST['selected_id'];
36 //---------------------------------------------------------------------------------------
38 if (isset($_GET['AddedID']))
40 $id = $_GET['AddedID'];
42 display_notification_centered(_("The work order been added."));
47 //---------------------------------------------------------------------------------------
49 if (isset($_GET['UpdatedID']))
51 $id = $_GET['UpdatedID'];
53 display_notification_centered(_("The work order been updated."));
57 //---------------------------------------------------------------------------------------
59 if (isset($_GET['DeletedID']))
61 $id = $_GET['DeletedID'];
63 display_notification_centered(_("Work order has been deleted."));
67 //---------------------------------------------------------------------------------------
69 if (isset($_GET['ClosedID']))
71 $id = $_GET['ClosedID'];
73 display_notification_centered(_("This work order has been closed. There can be no more issues against it.") . " #$id");
77 //---------------------------------------------------------------------------------------
83 hyperlink_no_params("", _("Enter a new work order"));
84 hyperlink_no_params("search_work_orders.php", _("Select an existing work order"));
91 //-------------------------------------------------------------------------------------
92 if (!isset($_POST['date_']))
94 $_POST['date_'] = Today();
95 if (!is_date_in_fiscalyear($_POST['date_']))
96 $_POST['date_'] = end_fiscalyear();
99 function can_process()
103 if (!isset($selected_id))
105 if (!references::is_valid($_POST['wo_ref']))
107 display_error(_("You must enter a reference."));
111 if (!is_new_reference($_POST['wo_ref'], systypes::work_order()))
113 display_error(_("The entered reference is already in use."));
118 if (!check_num('quantity', 0))
120 display_error( _("The quantity entered is invalid or less than zero."));
124 if (!is_date($_POST['date_']))
126 display_error( _("The date entered is in an invalid format."));
129 elseif (!is_date_in_fiscalyear($_POST['date_']))
131 display_error(_("The entered date is not in fiscal year."));
134 // only check bom and quantites if quick assembly
135 if (!($_POST['type'] == wo_types::advanced()))
137 if (!has_bom($_POST['stock_id']))
139 display_error(_("The selected item to manufacture does not have a bom."));
143 if ($_POST['Costs'] == "")
144 $_POST['Costs'] = price_format(0);
145 if (!check_num('Costs', 0))
147 display_error( _("The cost entered is invalid or less than zero."));
151 if (!sys_prefs::allow_negative_stock())
153 if ($_POST['type'] == wo_types::assemble())
155 // check bom if assembling
156 $result = get_bom($_POST['stock_id']);
158 while ($bom_item = db_fetch($result))
161 if (has_stock_holding($bom_item["ResourceType"]))
164 $quantity = $bom_item["quantity"] * input_num('quantity');
166 $qoh = get_qoh_on_date($bom_item["component"], $bom_item["loc_code"], $_POST['date_']);
167 if (-$quantity + $qoh < 0)
169 display_error(_("The work order cannot be processed because there is an insufficient quantity for component:") .
170 " " . $bom_item["component"] . " - " . $bom_item["description"] . ". " . _("Location:") . " " . $bom_item["location_name"]);
176 elseif ($_POST['type'] == wo_types::unassemble())
178 // if unassembling, check item to unassemble
179 $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['StockLocation'], $_POST['date_']);
180 if (-input_num('quantity') + $qoh < 0)
182 display_error(_("The selected item cannot be unassembled because there is insufficient stock."));
190 if (!is_date($_POST['RequDate']))
192 display_error( _("The date entered is in an invalid format."));
195 //elseif (!is_date_in_fiscalyear($_POST['RequDate']))
197 // display_error(_("The entered date is not in fiscal year."));
200 if (isset($selected_id))
202 $myrow = get_work_order($selected_id, true);
204 if ($_POST['units_issued'] > input_num('quantity'))
206 display_error(_("The quantity cannot be changed to be less than the quantity already manufactured for this order."));
215 //-------------------------------------------------------------------------------------
217 if (isset($_POST['ADD_ITEM']) && can_process())
220 $id = add_work_order($_POST['wo_ref'], $_POST['StockLocation'], input_num('quantity'),
221 $_POST['stock_id'], $_POST['type'], $_POST['date_'],
222 $_POST['RequDate'], input_num('Costs'), $_POST['memo_']);
224 meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
227 //-------------------------------------------------------------------------------------
229 if (isset($_POST['UPDATE_ITEM']) && can_process())
232 update_work_order($selected_id, $_POST['StockLocation'], input_num('quantity'),
233 $_POST['stock_id'], $_POST['date_'], $_POST['RequDate'], $_POST['memo_']);
235 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
238 //--------------------------------------------------------------------------------------
240 if (isset($_POST['delete']))
242 //the link to delete a selected record was clicked instead of the submit button
244 $cancel_delete = false;
246 // can't delete it there are productions or issues
247 if (work_order_has_productions($selected_id) ||
248 work_order_has_issues($selected_id) ||
249 work_order_has_payments($selected_id))
251 display_error(_("This work order cannot be deleted because it has already been processed."));
252 $cancel_delete = true;
255 if ($cancel_delete == false)
256 { //ie not cancelled the delete as a result of above tests
258 // delete the actual work order
259 delete_work_order($selected_id);
260 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
264 //-------------------------------------------------------------------------------------
266 if (isset($_POST['close']))
269 // update the closed flag in the work order
270 close_work_order($selected_id);
271 meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
274 //-------------------------------------------------------------------------------------
278 start_table($table_style2);
280 $existing_comments = "";
282 if (isset($selected_id))
284 $myrow = get_work_order($selected_id);
286 if (strlen($myrow[0]) == 0)
288 echo _("The order number sent is not valid.");
292 // if it's a closed work order can't edit it
293 if ($myrow["closed"] == 1)
296 display_error(_("This work order is closed and cannot be edited."));
300 $_POST['wo_ref'] = $myrow["wo_ref"];
301 $_POST['quantity'] = qty_format($myrow["units_reqd"]);
302 $_POST['StockLocation'] = $myrow["loc_code"];
303 $_POST['released'] = $myrow["released"];
304 $_POST['closed'] = $myrow["closed"];
305 $_POST['stock_id'] = $myrow["stock_id"];
306 $_POST['type'] = $myrow["type"];
307 $_POST['date_'] = sql2date($myrow["date_"]);
308 $_POST['RequDate'] = sql2date($myrow["required_by"]);
309 $_POST['released_date'] = sql2date($myrow["released_date"]);
310 $_POST['memo_'] = "";
311 $_POST['units_issued'] = $myrow["units_issued"];
312 $_POST['Costs'] = price_format($myrow["Costs"]);
314 $_POST['memo_'] = get_comments_string(systypes::work_order(), $selected_id);
316 hidden('wo_ref', $_POST['wo_ref']);
317 hidden('units_issued', $_POST['units_issued']);
318 hidden('released', $_POST['released']);
319 hidden('released_date', $_POST['released_date']);
320 hidden('selected_id', $selected_id);
321 hidden('old_qty', $myrow["units_reqd"]);
322 hidden('old_stk_id', $myrow["stock_id"]);
324 label_row(_("Reference:"), $_POST['wo_ref']);
325 label_row(_("Type:"), wo_types::name($_POST['type']));
329 $_POST['units_issued'] = $_POST['released'] = 0;
330 ref_row(_("Reference:"), 'wo_ref', references::get_next(systypes::work_order()));
332 wo_types_list_row(_("Type:"), 'type', $_POST['type']);
335 if ($_POST['released'] == true)
337 hidden('stock_id', $_POST['stock_id']);
338 hidden('StockLocation', $_POST['StockLocation']);
339 hidden('type', $_POST['type']);
341 label_row(_("Item:"), $myrow["StockItemName"]);
342 label_row(_("Destination Location:"), $myrow["location_name"]);
346 stock_manufactured_items_list_row(_("Item:"), 'stock_id', null);
348 locations_list_row(_("Destination Location:"), 'StockLocation', null);
351 if (!isset($_POST['quantity']))
352 $_POST['quantity'] = qty_format(1);
354 if ($_POST['type'] == wo_types::advanced())
356 qty_row(_("Quantity Required:"), 'quantity', 12);
357 if ($_POST['released'])
358 label_row(_("Quantity Manufactured:"), qty_format($_POST['units_issued']));
359 date_row(_("Date") . ":", 'date_');
360 date_row(_("Date Required By") . ":", 'RequDate', null, sys_prefs::default_wo_required_by());
364 qty_row(_("Quantity:"), 'quantity', 12);
365 date_row(_("Date") . ":", 'date_');
366 hidden('RequDate', '');
368 if (!isset($_POST['Costs']))
369 $_POST['Costs'] = price_format(0);
371 amount_row(_("Total Additional Costs:"), 'Costs');
374 if ($_POST['released'])
375 label_row(_("Released On:"),$_POST['released_date']);
377 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
381 submit_add_or_update_center(!isset($selected_id));
383 if (isset($selected_id))
385 echo "<br><br><table align=center><tr>";
387 if (isset($_POST['released']))
389 submit_cells('close', _("Close This Work Order"));
391 submit_cells('delete', _("Delete This Work Order"));
393 echo "</tr></table>";