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 ***********************************************************************/
12 $page_security = 'SA_WORKORDERENTRY';
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/data_checks.inc");
20 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
21 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
24 if ($SysPrefs->use_popup_windows)
25 $js .= get_js_open_window(900, 500);
26 if (user_use_date_picker())
27 $js .= get_js_date_picker();
28 page(_($help_context = "Work Order Entry"), false, false, "", $js);
31 check_db_has_manufacturable_items(_("There are no manufacturable items defined in the system."));
33 check_db_has_locations(("There are no inventory locations defined in the system."));
35 //---------------------------------------------------------------------------------------
37 if (isset($_GET['trans_no']))
39 $selected_id = $_GET['trans_no'];
41 elseif(isset($_POST['selected_id']))
43 $selected_id = $_POST['selected_id'];
46 //---------------------------------------------------------------------------------------
48 if (isset($_GET['AddedID']))
50 $id = $_GET['AddedID'];
51 $stype = ST_WORKORDER;
53 display_notification_centered(_("The work order been added."));
55 display_note(get_trans_view_str($stype, $id, _("View this Work Order")), 0, 1);
57 if ($_GET['type'] != WO_ADVANCED)
59 include_once($path_to_root . "/reporting/includes/reporting.inc");
61 submenu_print(_("&Print This Work Order"), ST_WORKORDER, $id, 'prtopt');
62 submenu_print(_("&Email This Work Order"), ST_WORKORDER, $id, null, 1);
63 display_note(get_gl_view_str($stype, $id, _("View the GL Journal Entries for this Work Order")), 1);
64 $ar = array('PARAM_0' => $_GET['date'], 'PARAM_1' => $_GET['date'], 'PARAM_2' => $stype, 'PARAM_3' => '',
65 'PARAM_4' => (user_def_print_orientation() == 1 ? 1 : 0));
66 display_note(print_link(_("Print the GL Journal Entries for this Work Order"), 702, $ar), 1);
67 hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$stype&trans_no=$id");
73 //---------------------------------------------------------------------------------------
75 if (isset($_GET['UpdatedID']))
77 $id = $_GET['UpdatedID'];
79 display_notification_centered(_("The work order been updated."));
83 //---------------------------------------------------------------------------------------
85 if (isset($_GET['DeletedID']))
87 $id = $_GET['DeletedID'];
89 display_notification_centered(_("Work order has been deleted."));
93 //---------------------------------------------------------------------------------------
95 if (isset($_GET['ClosedID']))
97 $id = $_GET['ClosedID'];
99 display_notification_centered(_("This work order has been closed. There can be no more issues against it.") . " #$id");
103 //---------------------------------------------------------------------------------------
107 global $path_to_root;
109 hyperlink_no_params("", _("Enter a new work order"));
110 hyperlink_no_params("search_work_orders.php", _("Select an existing work order"));
112 display_footer_exit();
115 //-------------------------------------------------------------------------------------
116 if (!isset($_POST['date_']))
118 $_POST['date_'] = new_doc_date();
119 if (!is_date_in_fiscalyear($_POST['date_']))
120 $_POST['date_'] = end_fiscalyear();
123 function can_process()
125 global $selected_id, $SysPrefs, $Refs;
127 if (!isset($selected_id))
129 if (!$Refs->is_valid($_POST['wo_ref']))
131 display_error(_("You must enter a reference."));
136 if (!is_new_reference($_POST['wo_ref'], ST_WORKORDER))
138 display_error(_("The entered reference is already in use."));
144 if (!check_num('quantity', 0))
146 display_error( _("The quantity entered is invalid or less than zero."));
147 set_focus('quantity');
151 if (!is_date($_POST['date_']))
153 display_error( _("The date entered is in an invalid format."));
157 elseif (!is_date_in_fiscalyear($_POST['date_']))
159 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
163 // only check bom and quantites if quick assembly
164 if (!($_POST['type'] == WO_ADVANCED))
166 if (!has_bom($_POST['stock_id']))
168 display_error(_("The selected item to manufacture does not have a bom."));
169 set_focus('stock_id');
173 if ($_POST['Labour'] == "")
174 $_POST['Labour'] = price_format(0);
175 if (!check_num('Labour', 0))
177 display_error( _("The labour cost entered is invalid or less than zero."));
181 if ($_POST['Costs'] == "")
182 $_POST['Costs'] = price_format(0);
183 if (!check_num('Costs', 0))
185 display_error( _("The cost entered is invalid or less than zero."));
190 if (!$SysPrefs->allow_negative_stock())
192 if ($_POST['type'] == WO_ASSEMBLY)
194 // check bom if assembling
195 $result = get_bom($_POST['stock_id']);
197 while ($bom_item = db_fetch($result))
200 if (has_stock_holding($bom_item["ResourceType"]))
203 $quantity = $bom_item["quantity"] * input_num('quantity');
205 if (check_negative_stock($bom_item["component"], -$quantity, $bom_item["loc_code"], $_POST['date_']))
207 display_error(_("The work order cannot be processed because there is an insufficient quantity for component:") .
208 " " . $bom_item["component"] . " - " . $bom_item["description"] . ". " . _("Location:") . " " . $bom_item["location_name"]);
209 set_focus('quantity');
215 elseif ($_POST['type'] == WO_UNASSEMBLY)
217 // if unassembling, check item to unassemble
218 if (check_negative_stock($_POST['stock_id'], -input_num('quantity'), $_POST['StockLocation'], $_POST['date_']))
220 display_error(_("The selected item cannot be unassembled because there is insufficient stock."));
228 if (!is_date($_POST['RequDate']))
230 set_focus('RequDate');
231 display_error( _("The date entered is in an invalid format."));
234 //elseif (!is_date_in_fiscalyear($_POST['RequDate']))
236 // display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
239 if (isset($selected_id))
241 $myrow = get_work_order($selected_id, true);
243 if ($_POST['units_issued'] > input_num('quantity'))
245 set_focus('quantity');
246 display_error(_("The quantity cannot be changed to be less than the quantity already manufactured for this order."));
255 //-------------------------------------------------------------------------------------
257 if (isset($_POST['ADD_ITEM']) && can_process())
259 if (!isset($_POST['cr_acc']))
260 $_POST['cr_acc'] = "";
261 if (!isset($_POST['cr_lab_acc']))
262 $_POST['cr_lab_acc'] = "";
263 $id = add_work_order($_POST['wo_ref'], $_POST['StockLocation'], input_num('quantity'),
264 $_POST['stock_id'], $_POST['type'], $_POST['date_'],
265 $_POST['RequDate'], $_POST['memo_'], input_num('Costs'), $_POST['cr_acc'], input_num('Labour'), $_POST['cr_lab_acc']);
267 new_doc_date($_POST['date_']);
268 meta_forward($_SERVER['PHP_SELF'], "AddedID=$id&type=".$_POST['type']."&date=".$_POST['date_']);
271 //-------------------------------------------------------------------------------------
273 if (isset($_POST['UPDATE_ITEM']) && can_process())
276 update_work_order($selected_id, $_POST['StockLocation'], input_num('quantity'),
277 $_POST['stock_id'], $_POST['date_'], $_POST['RequDate'], $_POST['memo_'],
278 $_POST['old_stk_id'], $_POST['old_qty']);
279 new_doc_date($_POST['date_']);
280 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
283 //--------------------------------------------------------------------------------------
285 if (isset($_POST['delete']))
287 //the link to delete a selected record was clicked instead of the submit button
289 $cancel_delete = false;
291 // can't delete it there are productions or issues
292 if (work_order_has_productions($selected_id) ||
293 work_order_has_issues($selected_id) ||
294 work_order_has_payments($selected_id))
296 display_error(_("This work order cannot be deleted because it has already been processed."));
297 $cancel_delete = true;
300 if ($cancel_delete == false)
301 { //ie not cancelled the delete as a result of above tests
303 // delete the actual work order
304 delete_work_order($selected_id, $_POST['stock_id'], $_POST['quantity'], $_POST['date_']);
305 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
309 //-------------------------------------------------------------------------------------
311 if (isset($_POST['close']))
314 // update the closed flag in the work order
315 close_work_order($selected_id);
316 meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
319 //-------------------------------------------------------------------------------------
320 if (get_post('_type_update'))
322 $Ajax->activate('_page_body');
324 //-------------------------------------------------------------------------------------
328 start_table(TABLESTYLE2);
330 $existing_comments = "";
333 if (isset($selected_id))
335 $myrow = get_work_order($selected_id);
337 if (strlen($myrow[0]) == 0)
339 echo _("The order number sent is not valid.");
343 // if it's a closed work order can't edit it
344 if ($myrow["closed"] == 1)
347 display_error(_("This work order is closed and cannot be edited."));
351 $_POST['wo_ref'] = $myrow["wo_ref"];
352 $_POST['stock_id'] = $myrow["stock_id"];
353 //$_POST['quantity'] = qty_format($myrow["units_reqd"], $_POST['stock_id'], $dec);
354 $_POST['quantity'] = $myrow["units_reqd"];
355 $_POST['StockLocation'] = $myrow["loc_code"];
356 $_POST['released'] = $myrow["released"];
357 $_POST['closed'] = $myrow["closed"];
358 $_POST['type'] = $myrow["type"];
359 $_POST['date_'] = sql2date($myrow["date_"]);
360 $_POST['RequDate'] = sql2date($myrow["required_by"]);
361 $_POST['released_date'] = sql2date($myrow["released_date"]);
362 $_POST['memo_'] = "";
363 $_POST['units_issued'] = $myrow["units_issued"];
364 $_POST['Costs'] = price_format($myrow["additional_costs"]);
366 $_POST['memo_'] = get_comments_string(ST_WORKORDER, $selected_id);
368 hidden('wo_ref', $_POST['wo_ref']);
369 hidden('units_issued', $_POST['units_issued']);
370 hidden('released', $_POST['released']);
371 hidden('released_date', $_POST['released_date']);
372 hidden('selected_id', $selected_id);
373 hidden('old_qty', $myrow["units_reqd"]);
374 hidden('old_stk_id', $myrow["stock_id"]);
376 label_row(_("Reference:"), $_POST['wo_ref']);
377 label_row(_("Type:"), $wo_types_array[$_POST['type']]);
378 hidden('type', $myrow["type"]);
382 $_POST['units_issued'] = $_POST['released'] = 0;
383 ref_row(_("Reference:"), 'wo_ref', '', $Refs->get_next(ST_WORKORDER));
385 wo_types_list_row(_("Type:"), 'type', null);
388 if (get_post('released'))
390 hidden('stock_id', $_POST['stock_id']);
391 hidden('StockLocation', $_POST['StockLocation']);
392 hidden('type', $_POST['type']);
394 label_row(_("Item:"), $myrow["StockItemName"]);
395 label_row(_("Destination Location:"), $myrow["location_name"]);
399 stock_manufactured_items_list_row(_("Item:"), 'stock_id', null, false, true);
400 if (list_updated('stock_id'))
401 $Ajax->activate('quantity');
403 locations_list_row(_("Destination Location:"), 'StockLocation', null);
406 if (!isset($_POST['quantity']))
407 $_POST['quantity'] = qty_format(1, $_POST['stock_id'], $dec);
409 $_POST['quantity'] = qty_format($_POST['quantity'], $_POST['stock_id'], $dec);
412 if (get_post('type') == WO_ADVANCED)
414 qty_row(_("Quantity Required:"), 'quantity', null, null, null, $dec);
415 if ($_POST['released'])
416 label_row(_("Quantity Manufactured:"), number_format($_POST['units_issued'], get_qty_dec($_POST['stock_id'])));
417 date_row(_("Date") . ":", 'date_', '', true);
418 date_row(_("Date Required By") . ":", 'RequDate', '', null, $SysPrefs->default_wo_required_by());
422 qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
423 date_row(_("Date") . ":", 'date_', '', true);
424 hidden('RequDate', '');
426 $bank_act = get_default_bank_account();
427 if (!isset($_POST['Labour']))
429 $_POST['Labour'] = price_format(0);
430 $_POST['cr_lab_acc'] = $bank_act['account_code'];
433 amount_row($wo_cost_types[WO_LABOUR], 'Labour');
434 gl_all_accounts_list_row(_("Credit Labour Account"), 'cr_lab_acc', null);
435 if (!isset($_POST['Costs']))
437 $_POST['Costs'] = price_format(0);
438 $_POST['cr_acc'] = $bank_act['account_code'];
440 amount_row($wo_cost_types[WO_OVERHEAD], 'Costs');
441 gl_all_accounts_list_row(_("Credit Overhead Account"), 'cr_acc', null);
445 if (get_post('released'))
446 label_row(_("Released On:"),$_POST['released_date']);
448 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
452 if (isset($selected_id))
454 echo "<table align=center><tr>";
456 submit_cells('UPDATE_ITEM', _("Update"), '', _('Save changes to work order'), 'default');
457 if (get_post('released'))
459 submit_cells('close', _("Close This Work Order"),'','',true);
461 submit_cells('delete', _("Delete This Work Order"),'','',true);
463 echo "</tr></table>";
467 submit_center('ADD_ITEM', _("Add Workorder"), true, '', 'default');