6 include_once($path_to_root . "/includes/session.inc");
8 page(_("Work Order Entry"));
10 include_once($path_to_root . "/includes/date_functions.inc");
11 include_once($path_to_root . "/includes/manufacturing.inc");
12 include_once($path_to_root . "/includes/data_checks.inc");
14 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
15 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
17 check_db_has_manufacturable_items(_("There are no manufacturable items defined in the system."));
19 check_db_has_locations(("There are no inventory locations defined in the system."));
21 //---------------------------------------------------------------------------------------
23 if (isset($_GET['trans_no']))
25 $selected_id = $_GET['trans_no'];
27 elseif(isset($_POST['selected_id']))
29 $selected_id = $_POST['selected_id'];
32 //---------------------------------------------------------------------------------------
34 if (isset($_GET['AddedID']))
36 $id = $_GET['AddedID'];
38 display_notification_centered(_("The work order been added."));
43 //---------------------------------------------------------------------------------------
45 if (isset($_GET['UpdatedID']))
47 $id = $_GET['UpdatedID'];
49 display_notification_centered(_("The work order been updated."));
53 //---------------------------------------------------------------------------------------
55 if (isset($_GET['DeletedID']))
57 $id = $_GET['DeletedID'];
59 display_notification_centered(_("Work order has been deleted."));
63 //---------------------------------------------------------------------------------------
65 if (isset($_GET['ClosedID']))
67 $id = $_GET['ClosedID'];
69 display_notification_centered(_("This work order has been closed. There can be no more issues against it.") . " #$id");
73 //---------------------------------------------------------------------------------------
79 hyperlink_no_params("", _("Enter a new work order"));
80 hyperlink_no_params("search_work_orders.php", _("Select an existing work order"));
87 //-------------------------------------------------------------------------------------
88 if (!isset($_POST['date_']))
90 $_POST['date_'] = Today();
91 if (!is_date_in_fiscalyear($_POST['date_']))
92 $_POST['date_'] = end_fiscalyear();
95 function can_process()
99 if (!isset($selected_id))
101 if (!references::is_valid($_POST['wo_ref']))
103 display_error(_("You must enter a reference."));
107 if (!is_new_reference($_POST['wo_ref'], systypes::work_order()))
109 display_error(_("The entered reference is already in use."));
114 if (!is_numeric($_POST['quantity']))
116 display_error( _("The quantity entered must be numeric."));
120 if ($_POST['quantity'] <= 0)
122 display_error( _("The quantity entered must be a positive number greater than zero."));
126 if (!is_date($_POST['date_']))
128 display_error( _("The date entered is in an invalid format."));
131 elseif (!is_date_in_fiscalyear($_POST['date_']))
133 display_error(_("The entered date is not in fiscal year."));
136 // only check bom and quantites if quick assembly
137 if (!($_POST['type'] == wo_types::advanced()))
139 if (!has_bom($_POST['stock_id']))
141 display_error(_("The selected item to manufacture does not have a bom."));
145 if (!is_numeric($_POST['Costs']))
147 display_error( _("The cost entered must be numeric."));
151 if ($_POST['Costs'] < 0)
153 display_error( _("The cost entered cannot be negative."));
157 if (!sys_prefs::allow_negative_stock())
159 if ($_POST['type'] == wo_types::assemble())
161 // check bom if assembling
162 $result = get_bom($_POST['stock_id']);
164 while ($bom_item = db_fetch($result))
167 if (has_stock_holding($bom_item["ResourceType"]))
170 $quantity = $bom_item["quantity"] * $_POST['quantity'];
172 $qoh = get_qoh_on_date($bom_item["component"], $bom_item["loc_code"], $_POST['date_']);
173 if (-$quantity + $qoh < 0)
175 display_error(_("The work order cannot be processed because there is an insufficient quantity for component:") .
176 " " . $bom_item["component"] . " - " . $bom_item["description"] . ". " . _("Location:") . " " . $bom_item["location_name"]);
182 elseif ($_POST['type'] == wo_types::unassemble())
184 // if unassembling, check item to unassemble
185 $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['StockLocation'], $_POST['date_']);
186 if (-$_POST['quantity'] + $qoh < 0)
188 display_error(_("The selected item cannot be unassembled because there is insufficient stock."));
196 if (!is_date($_POST['RequDate']))
198 display_error( _("The date entered is in an invalid format."));
201 //elseif (!is_date_in_fiscalyear($_POST['RequDate']))
203 // display_error(_("The entered date is not in fiscal year."));
206 if (isset($selected_id))
208 $myrow = get_work_order($selected_id, true);
210 if ($_POST['units_issued'] > $_POST['quantity'])
212 display_error(_("The quantity cannot be changed to be less than the quantity already manufactured for this order."));
221 //-------------------------------------------------------------------------------------
223 if (isset($_POST['ADD_ITEM']) && can_process())
226 $id = add_work_order($_POST['wo_ref'], $_POST['StockLocation'], $_POST['quantity'],
227 $_POST['stock_id'], $_POST['type'], $_POST['date_'],
228 $_POST['RequDate'], $_POST['Costs'], $_POST['memo_']);
230 meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
233 //-------------------------------------------------------------------------------------
235 if (isset($_POST['UPDATE_ITEM']) && can_process())
238 update_work_order($selected_id, $_POST['StockLocation'], $_POST['quantity'],
239 $_POST['stock_id'], $_POST['date_'], $_POST['RequDate'], $_POST['memo_']);
241 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
244 //--------------------------------------------------------------------------------------
246 if (isset($_POST['delete']))
248 //the link to delete a selected record was clicked instead of the submit button
250 $cancel_delete = false;
252 // can't delete it there are productions or issues
253 if (work_order_has_productions($selected_id) ||
254 work_order_has_issues($selected_id) ||
255 work_order_has_payments($selected_id))
257 display_error(_("This work order cannot be deleted because it has already been processed."));
258 $cancel_delete = true;
261 if ($cancel_delete == false)
262 { //ie not cancelled the delete as a result of above tests
264 // delete the actual work order
265 delete_work_order($selected_id);
266 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
270 //-------------------------------------------------------------------------------------
272 if (isset($_POST['close']))
275 // update the closed flag in the work order
276 close_work_order($selected_id);
277 meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
280 //-------------------------------------------------------------------------------------
284 start_table($table_style2);
286 $existing_comments = "";
288 if (isset($selected_id))
290 $myrow = get_work_order($selected_id);
292 if (strlen($myrow[0]) == 0)
294 echo _("The order number sent is not valid.");
298 // if it's a closed work order can't edit it
299 if ($myrow["closed"] == 1)
302 display_error(_("This work order is closed and cannot be edited."));
306 $_POST['wo_ref'] = $myrow["wo_ref"];
307 $_POST['quantity'] = $myrow["units_reqd"];
308 $_POST['StockLocation'] = $myrow["loc_code"];
309 $_POST['released'] = $myrow["released"];
310 $_POST['closed'] = $myrow["closed"];
311 $_POST['stock_id'] = $myrow["stock_id"];
312 $_POST['type'] = $myrow["type"];
313 $_POST['date_'] = sql2date($myrow["date_"]);
314 $_POST['RequDate'] = sql2date($myrow["required_by"]);
315 $_POST['released_date'] = sql2date($myrow["released_date"]);
316 $_POST['memo_'] = "";
317 $_POST['units_issued'] = $myrow["units_issued"];
318 $_POST['Costs'] = $myrow["Costs"];
320 $_POST['memo_'] = get_comments_string(systypes::work_order(), $selected_id);
322 hidden('wo_ref', $_POST['wo_ref']);
323 hidden('units_issued', $_POST['units_issued']);
324 hidden('released', $_POST['released']);
325 hidden('released_date', $_POST['released_date']);
326 hidden('selected_id', $selected_id);
328 label_row(_("Reference:"), $_POST['wo_ref']);
329 label_row(_("Type:"), wo_types::name($_POST['type']));
333 $_POST['units_issued'] = $_POST['released'] = 0;
334 ref_row(_("Reference:"), 'wo_ref', references::get_next(systypes::work_order()));
336 wo_types_list_row(_("Type:"), 'type', $_POST['type']);
339 if ($_POST['released'] == true)
341 hidden('stock_id', $_POST['stock_id']);
342 hidden('StockLocation', $_POST['StockLocation']);
343 hidden('type', $_POST['type']);
345 label_row(_("Item:"), $myrow["StockItemName"]);
346 label_row(_("Destination Location:"), $myrow["location_name"]);
350 stock_manufactured_items_list_row(_("Item:"), 'stock_id', null);
352 locations_list_row(_("Destination Location:"), 'StockLocation', null);
355 if (!isset($_POST['quantity']))
356 $_POST['quantity'] = 1;
358 if ($_POST['type'] == wo_types::advanced())
360 text_row_ex(_("Quantity Required:"), 'quantity', 12);
361 if ($_POST['released'])
362 label_row(_("Quantity Manufactured:"), $_POST['units_issued']);
363 date_row(_("Date") . ":", 'date_');
364 date_row(_("Date Required By") . ":", 'RequDate', null, sys_prefs::default_wo_required_by());
368 text_row_ex(_("Quantity:"), 'quantity', 12);
369 date_row(_("Date") . ":", 'date_');
370 hidden('RequDate', '');
372 if (!isset($_POST['Costs']))
375 amount_row(_("Total Additional Costs:"), 'Costs');
378 if ($_POST['released'])
379 label_row(_("Released On:"),$_POST['released_date']);
381 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
385 submit_add_or_update_center(!isset($selected_id));
387 if (isset($selected_id))
389 echo "<br><br><table align=center><tr>";
391 if (isset($_POST['released']))
393 submit_cells('close', _("Close This Work Order"));
395 submit_cells('delete', _("Delete This Work Order"));
397 echo "</tr></table>";