2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
15 include_once($path_to_root . "/includes/session.inc");
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/manufacturing.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
21 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
22 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
26 $js .= get_js_date_picker();
27 page(_("Work Order Entry"), false, false, "", $js);
30 check_db_has_manufacturable_items(_("There are no manufacturable items defined in the system."));
32 check_db_has_locations(("There are no inventory locations defined in the system."));
34 //---------------------------------------------------------------------------------------
36 if (isset($_GET['trans_no']))
38 $selected_id = $_GET['trans_no'];
40 elseif(isset($_POST['selected_id']))
42 $selected_id = $_POST['selected_id'];
45 //---------------------------------------------------------------------------------------
47 if (isset($_GET['AddedID']))
49 $id = $_GET['AddedID'];
51 display_notification_centered(_("The work order been added."));
56 //---------------------------------------------------------------------------------------
58 if (isset($_GET['UpdatedID']))
60 $id = $_GET['UpdatedID'];
62 display_notification_centered(_("The work order been updated."));
66 //---------------------------------------------------------------------------------------
68 if (isset($_GET['DeletedID']))
70 $id = $_GET['DeletedID'];
72 display_notification_centered(_("Work order has been deleted."));
76 //---------------------------------------------------------------------------------------
78 if (isset($_GET['ClosedID']))
80 $id = $_GET['ClosedID'];
82 display_notification_centered(_("This work order has been closed. There can be no more issues against it.") . " #$id");
86 //---------------------------------------------------------------------------------------
92 hyperlink_no_params("", _("Enter a new work order"));
93 hyperlink_no_params("search_work_orders.php", _("Select an existing work order"));
100 //-------------------------------------------------------------------------------------
101 if (!isset($_POST['date_']))
103 $_POST['date_'] = new_doc_date();
104 if (!is_date_in_fiscalyear($_POST['date_']))
105 $_POST['date_'] = end_fiscalyear();
108 function can_process()
112 if (!isset($selected_id))
114 if (!references::is_valid($_POST['wo_ref']))
116 display_error(_("You must enter a reference."));
121 if (!is_new_reference($_POST['wo_ref'], systypes::work_order()))
123 display_error(_("The entered reference is already in use."));
129 if (!check_num('quantity', 0))
131 display_error( _("The quantity entered is invalid or less than zero."));
132 set_focus('quantity');
136 if (!is_date($_POST['date_']))
138 display_error( _("The date entered is in an invalid format."));
142 elseif (!is_date_in_fiscalyear($_POST['date_']))
144 display_error(_("The entered date is not in fiscal year."));
148 // only check bom and quantites if quick assembly
149 if (!($_POST['type'] == wo_types::advanced()))
151 if (!has_bom($_POST['stock_id']))
153 display_error(_("The selected item to manufacture does not have a bom."));
154 set_focus('stock_id');
158 if ($_POST['Costs'] == "")
159 $_POST['Costs'] = price_format(0);
160 if (!check_num('Costs', 0))
162 display_error( _("The cost entered is invalid or less than zero."));
167 if (!sys_prefs::allow_negative_stock())
169 if ($_POST['type'] == wo_types::assemble())
171 // check bom if assembling
172 $result = get_bom($_POST['stock_id']);
174 while ($bom_item = db_fetch($result))
177 if (has_stock_holding($bom_item["ResourceType"]))
180 $quantity = $bom_item["quantity"] * input_num('quantity');
182 $qoh = get_qoh_on_date($bom_item["component"], $bom_item["loc_code"], $_POST['date_']);
183 if (-$quantity + $qoh < 0)
185 display_error(_("The work order cannot be processed because there is an insufficient quantity for component:") .
186 " " . $bom_item["component"] . " - " . $bom_item["description"] . ". " . _("Location:") . " " . $bom_item["location_name"]);
187 set_focus('quantity');
193 elseif ($_POST['type'] == wo_types::unassemble())
195 // if unassembling, check item to unassemble
196 $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['StockLocation'], $_POST['date_']);
197 if (-input_num('quantity') + $qoh < 0)
199 display_error(_("The selected item cannot be unassembled because there is insufficient stock."));
207 if (!is_date($_POST['RequDate']))
209 set_focus('RequDate');
210 display_error( _("The date entered is in an invalid format."));
213 //elseif (!is_date_in_fiscalyear($_POST['RequDate']))
215 // display_error(_("The entered date is not in fiscal year."));
218 if (isset($selected_id))
220 $myrow = get_work_order($selected_id, true);
222 if ($_POST['units_issued'] > input_num('quantity'))
224 set_focus('quantity');
225 display_error(_("The quantity cannot be changed to be less than the quantity already manufactured for this order."));
234 //-------------------------------------------------------------------------------------
236 if (isset($_POST['ADD_ITEM']) && can_process())
239 $id = add_work_order($_POST['wo_ref'], $_POST['StockLocation'], input_num('quantity'),
240 $_POST['stock_id'], $_POST['type'], $_POST['date_'],
241 $_POST['RequDate'], input_num('Costs'), $_POST['memo_']);
242 new_doc_date($_POST['date_']);
243 meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
246 //-------------------------------------------------------------------------------------
248 if (isset($_POST['UPDATE_ITEM']) && can_process())
251 update_work_order($selected_id, $_POST['StockLocation'], input_num('quantity'),
252 $_POST['stock_id'], $_POST['date_'], $_POST['RequDate'], $_POST['memo_']);
253 new_doc_date($_POST['date_']);
254 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
257 //--------------------------------------------------------------------------------------
259 if (isset($_POST['delete']))
261 //the link to delete a selected record was clicked instead of the submit button
263 $cancel_delete = false;
265 // can't delete it there are productions or issues
266 if (work_order_has_productions($selected_id) ||
267 work_order_has_issues($selected_id) ||
268 work_order_has_payments($selected_id))
270 display_error(_("This work order cannot be deleted because it has already been processed."));
271 $cancel_delete = true;
274 if ($cancel_delete == false)
275 { //ie not cancelled the delete as a result of above tests
277 // delete the actual work order
278 delete_work_order($selected_id);
279 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
283 //-------------------------------------------------------------------------------------
285 if (isset($_POST['close']))
288 // update the closed flag in the work order
289 close_work_order($selected_id);
290 meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
293 //-------------------------------------------------------------------------------------
294 if (get_post('_type_update'))
296 $Ajax->activate('_page_body');
298 //-------------------------------------------------------------------------------------
302 start_table($table_style2);
304 $existing_comments = "";
307 if (isset($selected_id))
309 $myrow = get_work_order($selected_id);
311 if (strlen($myrow[0]) == 0)
313 echo _("The order number sent is not valid.");
317 // if it's a closed work order can't edit it
318 if ($myrow["closed"] == 1)
321 display_error(_("This work order is closed and cannot be edited."));
325 $_POST['wo_ref'] = $myrow["wo_ref"];
326 $_POST['stock_id'] = $myrow["stock_id"];
327 $_POST['quantity'] = qty_format($myrow["units_reqd"], $_POST['stock_id'], $dec);
328 $_POST['StockLocation'] = $myrow["loc_code"];
329 $_POST['released'] = $myrow["released"];
330 $_POST['closed'] = $myrow["closed"];
331 $_POST['type'] = $myrow["type"];
332 $_POST['date_'] = sql2date($myrow["date_"]);
333 $_POST['RequDate'] = sql2date($myrow["required_by"]);
334 $_POST['released_date'] = sql2date($myrow["released_date"]);
335 $_POST['memo_'] = "";
336 $_POST['units_issued'] = $myrow["units_issued"];
337 $_POST['Costs'] = price_format($myrow["additional_costs"]);
339 $_POST['memo_'] = get_comments_string(systypes::work_order(), $selected_id);
341 hidden('wo_ref', $_POST['wo_ref']);
342 hidden('units_issued', $_POST['units_issued']);
343 hidden('released', $_POST['released']);
344 hidden('released_date', $_POST['released_date']);
345 hidden('selected_id', $selected_id);
346 hidden('old_qty', $myrow["units_reqd"]);
347 hidden('old_stk_id', $myrow["stock_id"]);
349 label_row(_("Reference:"), $_POST['wo_ref']);
350 label_row(_("Type:"), wo_types::name($_POST['type']));
351 hidden('type', $myrow["type"]);
355 $_POST['units_issued'] = $_POST['released'] = 0;
356 ref_row(_("Reference:"), 'wo_ref', '', references::get_next(systypes::work_order()));
358 wo_types_list_row(_("Type:"), 'type', null);
361 if ($_POST['released'] == true)
363 hidden('stock_id', $_POST['stock_id']);
364 hidden('StockLocation', $_POST['StockLocation']);
365 hidden('type', $_POST['type']);
367 label_row(_("Item:"), $myrow["StockItemName"]);
368 label_row(_("Destination Location:"), $myrow["location_name"]);
372 stock_manufactured_items_list_row(_("Item:"), 'stock_id', null);
374 locations_list_row(_("Destination Location:"), 'StockLocation', null);
377 if (!isset($_POST['quantity']))
378 $_POST['quantity'] = qty_format(1, $_POST['stock_id'], $dec);
380 if (get_post('type') == wo_types::advanced())
382 qty_row(_("Quantity Required:"), 'quantity', null, null, null, $dec);
383 if ($_POST['released'])
384 label_row(_("Quantity Manufactured:"), number_format($_POST['units_issued'], get_qty_dec($_POST['stock_id'])));
385 date_row(_("Date") . ":", 'date_', '', true);
386 date_row(_("Date Required By") . ":", 'RequDate', '', null, sys_prefs::default_wo_required_by());
390 qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
391 date_row(_("Date") . ":", 'date_', '', true);
392 hidden('RequDate', '');
394 if (!isset($_POST['Costs']))
395 $_POST['Costs'] = price_format(0);
397 amount_row(_("Total Additional Costs:"), 'Costs');
400 if ($_POST['released'])
401 label_row(_("Released On:"),$_POST['released_date']);
403 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
407 if (isset($selected_id))
409 echo "<table align=center><tr>";
411 submit_cells('UPDATE_ITEM', _("Update"), '', _('Save changes to work order'), 'default');
412 if (isset($_POST['released']))
414 submit_cells('close', _("Close This Work Order"),'','',true);
416 submit_cells('delete', _("Delete This Work Order"),'','',true);
418 echo "</tr></table>";
422 submit_center('ADD_ITEM', _("Add Workorder"), true, '', 'default');