8b9c10f79e816fda97bac8a85e6de2fa0cac3c8f
[fa-stable.git] / manufacturing / work_order_entry.php
1 <?php
2
3 $page_security = 10;
4 $path_to_root="..";
5
6 include_once($path_to_root . "/includes/session.inc");
7
8 page(_("Work Order Entry"));
9
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");
13
14 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
15 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
16
17 check_db_has_manufacturable_items(_("There are no manufacturable items defined in the system."));
18
19 check_db_has_locations(("There are no inventory locations defined in the system."));
20
21 //---------------------------------------------------------------------------------------
22
23 if (isset($_GET['trans_no']))
24 {
25         $selected_id = $_GET['trans_no'];
26
27 elseif(isset($_POST['selected_id']))
28 {
29         $selected_id = $_POST['selected_id'];
30 }
31
32 //---------------------------------------------------------------------------------------
33
34 if (isset($_GET['AddedID'])) 
35 {
36         $id = $_GET['AddedID'];
37
38         display_notification_centered(_("The work order been added."));
39
40         safe_exit();
41 }
42
43 //---------------------------------------------------------------------------------------
44
45 if (isset($_GET['UpdatedID'])) 
46 {
47         $id = $_GET['UpdatedID'];
48
49         display_notification_centered(_("The work order been updated."));
50         safe_exit();
51 }
52
53 //---------------------------------------------------------------------------------------
54
55 if (isset($_GET['DeletedID'])) 
56 {
57         $id = $_GET['DeletedID'];
58
59         display_notification_centered(_("Work order has been deleted."));
60         safe_exit();
61 }
62
63 //---------------------------------------------------------------------------------------
64
65 if (isset($_GET['ClosedID'])) 
66 {
67         $id = $_GET['ClosedID'];
68
69         display_notification_centered(_("This work order has been closed. There can be no more issues against it.") . " #$id");
70         safe_exit();
71 }
72
73 //---------------------------------------------------------------------------------------
74
75 function safe_exit()
76 {
77         global $path_to_root;
78
79         hyperlink_no_params("", _("Enter a new work order"));
80         hyperlink_no_params("search_work_orders.php", _("Select an existing work order"));
81         echo "<br>";
82         end_form();
83         end_page();
84         exit;
85 }
86
87 //-------------------------------------------------------------------------------------
88 if (!isset($_POST['date_']))
89 {
90         $_POST['date_'] = Today();
91         if (!is_date_in_fiscalyear($_POST['date_']))
92                 $_POST['date_'] = end_fiscalyear();
93 }               
94
95 function can_process()
96 {
97         global $selected_id;
98
99         if (!isset($selected_id)) 
100         {
101         if (!references::is_valid($_POST['wo_ref'])) 
102         {
103                 display_error(_("You must enter a reference."));
104                 return false;
105         }
106
107         if (!is_new_reference($_POST['wo_ref'], systypes::work_order())) 
108         {
109                 display_error(_("The entered reference is already in use."));
110                 return false;
111         }
112         }
113
114         if (!is_numeric($_POST['quantity']))
115         {
116                 display_error( _("The quantity entered must be numeric."));
117                 return false;
118         }
119
120         if ($_POST['quantity'] <= 0)
121         {
122                 display_error( _("The quantity entered must be a positive number greater than zero."));
123                 return false;
124         }
125
126         if (!is_date($_POST['date_']))
127         {
128                 display_error( _("The date entered is in an invalid format."));
129                 return false;
130         } 
131         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
132         {
133                 display_error(_("The entered date is not in fiscal year."));
134                 return false;
135         }
136         // only check bom and quantites if quick assembly
137         if (!($_POST['type'] == wo_types::advanced())) 
138         {
139         if (!has_bom($_POST['stock_id'])) 
140         {
141                 display_error(_("The selected item to manufacture does not have a bom."));
142                 return false;
143         }
144
145         if (!is_numeric($_POST['Costs']))
146         {
147                 display_error( _("The cost entered must be numeric."));
148                 return false;
149         }
150
151         if ($_POST['Costs'] < 0)
152         {
153                 display_error( _("The cost entered cannot be negative."));
154                 return false;
155         }
156
157         if (!sys_prefs::allow_negative_stock()) 
158         {
159                 if ($_POST['type'] == wo_types::assemble()) 
160                 {
161                         // check bom if assembling
162                 $result = get_bom($_POST['stock_id']);
163
164                 while ($bom_item = db_fetch($result)) 
165                 {
166
167                         if (has_stock_holding($bom_item["ResourceType"])) 
168                         {
169
170                                 $quantity = $bom_item["quantity"] * $_POST['quantity'];
171
172                         $qoh = get_qoh_on_date($bom_item["component"], $bom_item["loc_code"], $_POST['date_']);
173                                 if (-$quantity + $qoh < 0) 
174                                 {
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"]);
177                                                 return false;
178                                 }
179                         }
180                 }
181                 } 
182                 elseif ($_POST['type'] == wo_types::unassemble()) 
183                 {
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) 
187                         {
188                                 display_error(_("The selected item cannot be unassembled because there is insufficient stock."));
189                                         return false;
190                         }
191                 }
192         }
193      } 
194      else 
195      {
196         if (!is_date($_POST['RequDate']))
197         {
198                 display_error( _("The date entered is in an invalid format."));
199                 return false;
200                 } 
201                 //elseif (!is_date_in_fiscalyear($_POST['RequDate'])) 
202                 //{
203                 //      display_error(_("The entered date is not in fiscal year."));
204                 //      return false;
205                 //}
206         if (isset($selected_id)) 
207         {
208                 $myrow = get_work_order($selected_id, true);
209
210                 if ($_POST['units_issued'] > $_POST['quantity']) 
211                 {
212                         display_error(_("The quantity cannot be changed to be less than the quantity already manufactured for this order."));
213                         return false;
214                 }
215         }
216         }
217
218         return true;
219 }
220
221 //-------------------------------------------------------------------------------------
222
223 if (isset($_POST['ADD_ITEM']) && can_process()) 
224 {
225
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_']);
229
230         meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
231 }
232
233 //-------------------------------------------------------------------------------------
234
235 if (isset($_POST['UPDATE_ITEM']) && can_process()) 
236 {
237
238         update_work_order($selected_id, $_POST['StockLocation'], $_POST['quantity'],
239                 $_POST['stock_id'],  $_POST['date_'], $_POST['RequDate'], $_POST['memo_']);
240
241         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
242 }
243
244 //--------------------------------------------------------------------------------------
245
246 if (isset($_POST['delete'])) 
247 {
248         //the link to delete a selected record was clicked instead of the submit button
249
250         $cancel_delete = false;
251
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))
256         {
257                 display_error(_("This work order cannot be deleted because it has already been processed."));
258                 $cancel_delete = true;
259         }
260
261         if ($cancel_delete == false) 
262         { //ie not cancelled the delete as a result of above tests
263
264                 // delete the actual work order
265                 delete_work_order($selected_id);
266                 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
267         }
268 }
269
270 //-------------------------------------------------------------------------------------
271
272 if (isset($_POST['close'])) 
273 {
274
275         // update the closed flag in the work order
276         close_work_order($selected_id);
277         meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
278 }
279
280 //-------------------------------------------------------------------------------------
281
282 start_form();
283
284 start_table($table_style2);
285
286 $existing_comments = "";
287
288 if (isset($selected_id))
289 {
290         $myrow = get_work_order($selected_id);
291
292         if (strlen($myrow[0]) == 0) 
293         {
294                 echo _("The order number sent is not valid.");
295                 safe_exit();
296         }
297
298         // if it's a closed work order can't edit it
299         if ($myrow["closed"] == 1) 
300         {
301                 echo "<center>";
302                 display_error(_("This work order is closed and cannot be edited."));
303                 safe_exit();
304         }
305
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"];
319
320         $_POST['memo_'] = get_comments_string(systypes::work_order(), $selected_id);
321
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);
327
328         label_row(_("Reference:"), $_POST['wo_ref']);
329         label_row(_("Type:"), wo_types::name($_POST['type']));
330
331 else 
332 {
333         $_POST['units_issued'] = $_POST['released'] = 0;
334         ref_row(_("Reference:"), 'wo_ref', references::get_next(systypes::work_order()));
335
336         wo_types_list_row(_("Type:"), 'type', $_POST['type']);
337 }
338
339 if ($_POST['released'] == true) 
340 {
341         hidden('stock_id', $_POST['stock_id']);
342         hidden('StockLocation', $_POST['StockLocation']);
343         hidden('type', $_POST['type']);
344
345         label_row(_("Item:"), $myrow["StockItemName"]);
346         label_row(_("Destination Location:"), $myrow["location_name"]);
347
348 else 
349 {
350         stock_manufactured_items_list_row(_("Item:"), 'stock_id', null);
351
352         locations_list_row(_("Destination Location:"), 'StockLocation', null);
353 }
354
355 if (!isset($_POST['quantity']))
356         $_POST['quantity'] = 1;
357
358 if ($_POST['type'] == wo_types::advanced()) 
359 {
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());
365 }
366 else 
367 {
368     text_row_ex(_("Quantity:"), 'quantity', 12);
369     date_row(_("Date") . ":", 'date_');
370         hidden('RequDate', '');
371
372         if (!isset($_POST['Costs']))
373                 $_POST['Costs'] = 0;
374
375         amount_row(_("Total Additional Costs:"), 'Costs');
376 }
377
378 if ($_POST['released'])
379         label_row(_("Released On:"),$_POST['released_date']);
380
381 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
382
383 end_table(1);
384
385 submit_add_or_update_center(!isset($selected_id));
386
387 if (isset($selected_id)) 
388 {
389         echo "<br><br><table align=center><tr>";
390
391         if (isset($_POST['released'])) 
392         {
393                 submit_cells('close', _("Close This Work Order"));
394         }
395         submit_cells('delete', _("Delete This Work Order"));
396         
397         echo "</tr></table>";
398 }
399
400 end_form();
401 end_page();
402
403 ?>