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