[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[fa-stable.git] / manufacturing / work_order_entry.php
1 <?php
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';
13 $path_to_root = "..";
14
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/data_checks.inc");
19
20 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
21 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
22
23 $js = "";
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);
29
30
31 check_db_has_manufacturable_items(_("There are no manufacturable items defined in the system."));
32
33 check_db_has_locations(("There are no inventory locations defined in the system."));
34
35 //---------------------------------------------------------------------------------------
36
37 if (isset($_GET['trans_no']))
38 {
39         $selected_id = $_GET['trans_no'];
40 }
41 elseif(isset($_POST['selected_id']))
42 {
43         $selected_id = $_POST['selected_id'];
44 }
45
46 //---------------------------------------------------------------------------------------
47
48 if (isset($_GET['AddedID']))
49 {
50         $id = $_GET['AddedID'];
51         $stype = ST_WORKORDER;
52
53         display_notification_centered(_("The work order been added."));
54
55     display_note(get_trans_view_str($stype, $id, _("View this Work Order")), 0, 1);
56
57         if ($_GET['type'] != WO_ADVANCED)
58         {
59                 include_once($path_to_root . "/reporting/includes/reporting.inc");
60
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");
68         }
69         
70         safe_exit();
71 }
72
73 //---------------------------------------------------------------------------------------
74
75 if (isset($_GET['UpdatedID']))
76 {
77         $id = $_GET['UpdatedID'];
78
79         display_notification_centered(_("The work order been updated."));
80         safe_exit();
81 }
82
83 //---------------------------------------------------------------------------------------
84
85 if (isset($_GET['DeletedID']))
86 {
87         $id = $_GET['DeletedID'];
88
89         display_notification_centered(_("Work order has been deleted."));
90         safe_exit();
91 }
92
93 //---------------------------------------------------------------------------------------
94
95 if (isset($_GET['ClosedID']))
96 {
97         $id = $_GET['ClosedID'];
98
99         display_notification_centered(_("This work order has been closed. There can be no more issues against it.") . " #$id");
100         safe_exit();
101 }
102
103 //---------------------------------------------------------------------------------------
104
105 function safe_exit()
106 {
107         global $path_to_root;
108
109         hyperlink_no_params("", _("Enter a new work order"));
110         hyperlink_no_params("search_work_orders.php", _("Select an existing work order"));
111         
112         display_footer_exit();
113 }
114
115 //-------------------------------------------------------------------------------------
116 if (!isset($_POST['date_']))
117 {
118         $_POST['date_'] = new_doc_date();
119         if (!is_date_in_fiscalyear($_POST['date_']))
120                 $_POST['date_'] = end_fiscalyear();
121 }
122
123 function can_process()
124 {
125         global $selected_id, $SysPrefs;
126
127         if (!isset($selected_id))
128         {
129         if (!check_reference($_POST['wo_ref'], ST_WORKORDER))
130         {
131                         set_focus('wo_ref');
132                 return false;
133         }
134         }
135
136         if (!check_num('quantity', 1))
137         {
138                 display_error( _("The quantity entered is invalid or less than zero."));
139                 set_focus('quantity');
140                 return false;
141         }
142
143         if (!is_date($_POST['date_']))
144         {
145                 display_error( _("The date entered is in an invalid format."));
146                 set_focus('date_');
147                 return false;
148         }
149         elseif (!is_date_in_fiscalyear($_POST['date_']))
150         {
151                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
152                 set_focus('date_');
153                 return false;
154         }
155         // only check bom and quantites if quick assembly
156         if (!($_POST['type'] == WO_ADVANCED))
157         {
158         if (!has_bom($_POST['stock_id']))
159         {
160                 display_error(_("The selected item to manufacture does not have a bom."));
161                         set_focus('stock_id');
162                 return false;
163         }
164
165                 if ($_POST['Labour'] == "")
166                         $_POST['Labour'] = price_format(0);
167         if (!check_num('Labour', 0))
168         {
169                 display_error( _("The labour cost entered is invalid or less than zero."));
170                         set_focus('Labour');
171                 return false;
172         }
173                 if ($_POST['Costs'] == "")
174                         $_POST['Costs'] = price_format(0);
175         if (!check_num('Costs', 0))
176         {
177                 display_error( _("The cost entered is invalid or less than zero."));
178                         set_focus('Costs');
179                 return false;
180         }
181
182         if (!$SysPrefs->allow_negative_stock())
183         {
184                 if ($_POST['type'] == WO_ASSEMBLY)
185                 {
186                         // check bom if assembling
187                 $result = get_bom($_POST['stock_id']);
188
189                 while ($bom_item = db_fetch($result))
190                 {
191
192                         if (has_stock_holding($bom_item["ResourceType"]))
193                         {
194
195                                 $quantity = $bom_item["quantity"] * input_num('quantity');
196
197                         if (check_negative_stock($bom_item["component"], -$quantity, $bom_item["loc_code"], $_POST['date_']))
198                                 {
199                                         display_error(_("The work order cannot be processed because there is an insufficient quantity for component:") .
200                                                 " " . $bom_item["component"] . " - " .  $bom_item["description"] . ".  " . _("Location:") . " " . $bom_item["location_name"]);
201                                                         set_focus('quantity');
202                                                 return false;
203                                 }
204                         }
205                 }
206                 }
207                 elseif ($_POST['type'] == WO_UNASSEMBLY)
208                 {
209                         // if unassembling, check item to unassemble
210                 if (check_negative_stock($_POST['stock_id'], -input_num('quantity'), $_POST['StockLocation'], $_POST['date_']))
211                         {
212                                 display_error(_("The selected item cannot be unassembled because there is insufficient stock."));
213                                         return false;
214                         }
215                 }
216         }
217      }
218      else
219      {
220         if (!is_date($_POST['RequDate']))
221         {
222                         set_focus('RequDate');
223                 display_error( _("The date entered is in an invalid format."));
224                 return false;
225                 }
226         if (isset($selected_id))
227         {
228                 if ($_POST['units_issued'] > input_num('quantity'))
229                 {
230                                 set_focus('quantity');
231                         display_error(_("The quantity cannot be changed to be less than the quantity already manufactured for this order."));
232                         return false;
233                 }
234         }
235         }
236
237         return true;
238 }
239
240 //-------------------------------------------------------------------------------------
241
242 if (isset($_POST['ADD_ITEM']) && can_process())
243 {
244         if (!isset($_POST['cr_acc']))
245                 $_POST['cr_acc'] = "";
246         if (!isset($_POST['cr_lab_acc']))
247                 $_POST['cr_lab_acc'] = "";
248         $id = add_work_order($_POST['wo_ref'], $_POST['StockLocation'], input_num('quantity'),
249                 $_POST['stock_id'],  $_POST['type'], $_POST['date_'],
250                 $_POST['RequDate'], $_POST['memo_'], input_num('Costs'), $_POST['cr_acc'], input_num('Labour'), $_POST['cr_lab_acc']);
251
252         new_doc_date($_POST['date_']);
253         meta_forward($_SERVER['PHP_SELF'], "AddedID=$id&type=".$_POST['type']."&date=".$_POST['date_']);
254 }
255
256 //-------------------------------------------------------------------------------------
257
258 if (isset($_POST['UPDATE_ITEM']) && can_process())
259 {
260
261         update_work_order($selected_id, $_POST['StockLocation'], input_num('quantity'),
262                 $_POST['stock_id'],  $_POST['date_'], $_POST['RequDate'], $_POST['memo_']);
263         new_doc_date($_POST['date_']);
264         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
265 }
266
267 //--------------------------------------------------------------------------------------
268
269 if (isset($_POST['delete']))
270 {
271         //the link to delete a selected record was clicked instead of the submit button
272
273         $cancel_delete = false;
274
275         // can't delete it there are productions or issues
276         if (work_order_has_productions($selected_id) ||
277                 work_order_has_issues($selected_id)     ||
278                 work_order_has_payments($selected_id))
279         {
280                 display_error(_("This work order cannot be deleted because it has already been processed."));
281                 $cancel_delete = true;
282         }
283
284         if ($cancel_delete == false)
285         { //ie not cancelled the delete as a result of above tests
286
287                 // delete the actual work order
288                 delete_work_order($selected_id, $_POST['stock_id'], $_POST['quantity'], $_POST['date_']);
289                 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
290         }
291 }
292
293 //-------------------------------------------------------------------------------------
294
295 if (isset($_POST['close']))
296 {
297
298         // update the closed flag in the work order
299         close_work_order($selected_id);
300         meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
301 }
302
303 //-------------------------------------------------------------------------------------
304 if (get_post('_type_update')) 
305 {
306   $Ajax->activate('_page_body');
307 }
308 //-------------------------------------------------------------------------------------
309
310 start_form();
311
312 start_table(TABLESTYLE2);
313
314 $existing_comments = "";
315
316 $dec = 0;
317 if (isset($selected_id))
318 {
319         $myrow = get_work_order($selected_id);
320
321         if (strlen($myrow[0]) == 0)
322         {
323                 echo _("The order number sent is not valid.");
324                 safe_exit();
325         }
326
327         // if it's a closed work order can't edit it
328         if ($myrow["closed"] == 1)
329         {
330                 echo "<center>";
331                 display_error(_("This work order is closed and cannot be edited."));
332                 safe_exit();
333         }
334
335         $_POST['wo_ref'] = $myrow["wo_ref"];
336         $_POST['stock_id'] = $myrow["stock_id"];
337         //$_POST['quantity'] = qty_format($myrow["units_reqd"], $_POST['stock_id'], $dec);
338         $_POST['quantity'] = $myrow["units_reqd"];
339         $_POST['StockLocation'] = $myrow["loc_code"];
340         $_POST['released'] = $myrow["released"];
341         $_POST['closed'] = $myrow["closed"];
342         $_POST['type'] = $myrow["type"];
343         $_POST['date_'] = sql2date($myrow["date_"]);
344         $_POST['RequDate'] = sql2date($myrow["required_by"]);
345         $_POST['released_date'] = sql2date($myrow["released_date"]);
346         $_POST['units_issued'] = $myrow["units_issued"];
347         $_POST['Costs'] = price_format($myrow["additional_costs"]);
348
349         $_POST['memo_'] = get_comments_string(ST_WORKORDER, $selected_id);
350
351         hidden('wo_ref', $_POST['wo_ref']);
352         hidden('units_issued', $_POST['units_issued']);
353         hidden('released', $_POST['released']);
354         hidden('released_date', $_POST['released_date']);
355         hidden('selected_id',  $selected_id);
356
357         label_row(_("Reference:"), $_POST['wo_ref']);
358         label_row(_("Type:"), $wo_types_array[$_POST['type']]);
359         hidden('type', $myrow["type"]);
360 }
361 else
362 {
363         $_POST['units_issued'] = $_POST['released'] = 0;
364
365         ref_row(_("Reference:"), 'wo_ref', '', $Refs->get_next(ST_WORKORDER, null, get_post('date_')), false, ST_WORKORDER);
366
367         wo_types_list_row(_("Type:"), 'type', null);
368 }
369
370 if (get_post('released'))
371 {
372         hidden('stock_id', $_POST['stock_id']);
373         hidden('StockLocation', $_POST['StockLocation']);
374         hidden('type', $_POST['type']);
375
376         label_row(_("Item:"), $myrow["StockItemName"]);
377         label_row(_("Destination Location:"), $myrow["location_name"]);
378 }
379 else
380 {
381         stock_manufactured_items_list_row(_("Item:"), 'stock_id', null, false, true);
382         if (list_updated('stock_id'))
383                 $Ajax->activate('quantity');
384
385         locations_list_row(_("Destination Location:"), 'StockLocation', null);
386 }
387
388 if (!isset($_POST['quantity']))
389         $_POST['quantity'] = qty_format(1, $_POST['stock_id'], $dec);
390 else
391         $_POST['quantity'] = qty_format($_POST['quantity'], $_POST['stock_id'], $dec);
392         
393
394 if (get_post('type') == WO_ADVANCED)
395 {
396     qty_row(_("Quantity Required:"), 'quantity', null, null, null, $dec);
397     if ($_POST['released'])
398         label_row(_("Quantity Manufactured:"), number_format($_POST['units_issued'], get_qty_dec($_POST['stock_id'])));
399     date_row(_("Date") . ":", 'date_', '', true);
400         date_row(_("Date Required By") . ":", 'RequDate', '', null, $SysPrefs->default_wo_required_by());
401 }
402 else
403 {
404     qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
405     date_row(_("Date") . ":", 'date_', '', true);
406         hidden('RequDate', '');
407
408         if (!isset($_POST['Labour']) || list_updated('stock_id') || list_updated('type'))
409         {
410                 $bank_act = get_default_bank_account();
411                 $item = get_item(get_post('stock_id'));
412                 $_POST['Labour'] = price_format(get_post('type') == WO_ASSEMBLY ? $item['labour_cost'] : 0);
413                 $_POST['cr_lab_acc'] = $bank_act['account_code'];
414                 $_POST['Costs'] = price_format(get_post('type') == WO_ASSEMBLY ? $item['overhead_cost'] : 0);
415                 $_POST['cr_acc'] = $bank_act['account_code'];
416                 $Ajax->activate('_page_body');
417         }
418
419         amount_row($wo_cost_types[WO_LABOUR], 'Labour');
420         gl_all_accounts_list_row(_("Credit Labour Account"), 'cr_lab_acc', null);
421         amount_row($wo_cost_types[WO_OVERHEAD], 'Costs');
422         gl_all_accounts_list_row(_("Credit Overhead Account"), 'cr_acc', null);
423
424 }
425
426 if (get_post('released'))
427         label_row(_("Released On:"),$_POST['released_date']);
428
429 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
430
431 end_table(1);
432
433 if (isset($selected_id))
434 {
435         echo "<table align=center><tr>";
436
437         submit_cells('UPDATE_ITEM', _("Update"), '', _('Save changes to work order'), 'default');
438         if (get_post('released'))
439         {
440                 submit_cells('close', _("Close This Work Order"),'','',true);
441         }
442         submit_cells('delete', _("Delete This Work Order"),'','',true);
443
444         echo "</tr></table>";
445 }
446 else
447 {
448         submit_center('ADD_ITEM', _("Add Workorder"), true, '', 'default');
449 }
450
451 end_form();
452 end_page();
453