Added ajax extensions
[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                 set_focus('wo_ref');
109                 return false;
110         }
111
112         if (!is_new_reference($_POST['wo_ref'], systypes::work_order()))
113         {
114                 display_error(_("The entered reference is already in use."));
115                 set_focus('wo_ref');
116                 return false;
117         }
118         }
119
120         if (!check_num('quantity', 0))
121         {
122                 display_error( _("The quantity entered is invalid or less than zero."));
123                 set_focus('quantity');
124                 return false;
125         }
126
127         if (!is_date($_POST['date_']))
128         {
129                 display_error( _("The date entered is in an invalid format."));
130                 set_focus('date_');
131                 return false;
132         }
133         elseif (!is_date_in_fiscalyear($_POST['date_']))
134         {
135                 display_error(_("The entered date is not in fiscal year."));
136                 set_focus('date_');
137                 return false;
138         }
139         // only check bom and quantites if quick assembly
140         if (!($_POST['type'] == wo_types::advanced()))
141         {
142         if (!has_bom($_POST['stock_id']))
143         {
144                 display_error(_("The selected item to manufacture does not have a bom."));
145                 set_focus('stock_id');
146                 return false;
147         }
148
149                 if ($_POST['Costs'] == "")
150                         $_POST['Costs'] = price_format(0);
151         if (!check_num('Costs', 0))
152         {
153                 display_error( _("The cost entered is invalid or less than zero."));
154                 set_focus('Costs');
155                 return false;
156         }
157
158         if (!sys_prefs::allow_negative_stock())
159         {
160                 if ($_POST['type'] == wo_types::assemble())
161                 {
162                         // check bom if assembling
163                 $result = get_bom($_POST['stock_id']);
164
165                 while ($bom_item = db_fetch($result))
166                 {
167
168                         if (has_stock_holding($bom_item["ResourceType"]))
169                         {
170
171                                 $quantity = $bom_item["quantity"] * input_num('quantity');
172
173                         $qoh = get_qoh_on_date($bom_item["component"], $bom_item["loc_code"], $_POST['date_']);
174                                 if (-$quantity + $qoh < 0)
175                                 {
176                                         display_error(_("The work order cannot be processed because there is an insufficient quantity for component:") .
177                                                 " " . $bom_item["component"] . " - " .  $bom_item["description"] . ".  " . _("Location:") . " " . $bom_item["location_name"]);
178                                                 set_focus('quantity');
179                                                 return false;
180                                 }
181                         }
182                 }
183                 }
184                 elseif ($_POST['type'] == wo_types::unassemble())
185                 {
186                         // if unassembling, check item to unassemble
187                                 $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['StockLocation'], $_POST['date_']);
188                         if (-input_num('quantity') + $qoh < 0)
189                         {
190                                 display_error(_("The selected item cannot be unassembled because there is insufficient stock."));
191                                         return false;
192                         }
193                 }
194         }
195      }
196      else
197      {
198         if (!is_date($_POST['RequDate']))
199         {
200                 set_focus('RequDate');
201                 display_error( _("The date entered is in an invalid format."));
202                 return false;
203                 }
204                 //elseif (!is_date_in_fiscalyear($_POST['RequDate']))
205                 //{
206                 //      display_error(_("The entered date is not in fiscal year."));
207                 //      return false;
208                 //}
209         if (isset($selected_id))
210         {
211                 $myrow = get_work_order($selected_id, true);
212
213                 if ($_POST['units_issued'] > input_num('quantity'))
214                 {
215                         set_focus('quantity');
216                         display_error(_("The quantity cannot be changed to be less than the quantity already manufactured for this order."));
217                         return false;
218                 }
219         }
220         }
221
222         return true;
223 }
224
225 //-------------------------------------------------------------------------------------
226
227 if (isset($_POST['ADD_ITEM']) && can_process())
228 {
229
230         $id = add_work_order($_POST['wo_ref'], $_POST['StockLocation'], input_num('quantity'),
231                 $_POST['stock_id'],  $_POST['type'], $_POST['date_'],
232                 $_POST['RequDate'], input_num('Costs'), $_POST['memo_']);
233
234         meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
235 }
236
237 //-------------------------------------------------------------------------------------
238
239 if (isset($_POST['UPDATE_ITEM']) && can_process())
240 {
241
242         update_work_order($selected_id, $_POST['StockLocation'], input_num('quantity'),
243                 $_POST['stock_id'],  $_POST['date_'], $_POST['RequDate'], $_POST['memo_']);
244
245         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$selected_id");
246 }
247
248 //--------------------------------------------------------------------------------------
249
250 if (isset($_POST['delete']))
251 {
252         //the link to delete a selected record was clicked instead of the submit button
253
254         $cancel_delete = false;
255
256         // can't delete it there are productions or issues
257         if (work_order_has_productions($selected_id) ||
258                 work_order_has_issues($selected_id)     ||
259                 work_order_has_payments($selected_id))
260         {
261                 display_error(_("This work order cannot be deleted because it has already been processed."));
262                 $cancel_delete = true;
263         }
264
265         if ($cancel_delete == false)
266         { //ie not cancelled the delete as a result of above tests
267
268                 // delete the actual work order
269                 delete_work_order($selected_id);
270                 meta_forward($_SERVER['PHP_SELF'], "DeletedID=$selected_id");
271         }
272 }
273
274 //-------------------------------------------------------------------------------------
275
276 if (isset($_POST['close']))
277 {
278
279         // update the closed flag in the work order
280         close_work_order($selected_id);
281         meta_forward($_SERVER['PHP_SELF'], "ClosedID=$selected_id");
282 }
283
284 //-------------------------------------------------------------------------------------
285 if (get_post('_type_update')) 
286 {
287   $Ajax->activate('_page_body');
288 }
289 //-------------------------------------------------------------------------------------
290
291 start_form();
292
293 start_table($table_style2);
294
295 $existing_comments = "";
296
297 $dec = 0;
298 if (isset($selected_id))
299 {
300         $myrow = get_work_order($selected_id);
301
302         if (strlen($myrow[0]) == 0)
303         {
304                 echo _("The order number sent is not valid.");
305                 safe_exit();
306         }
307
308         // if it's a closed work order can't edit it
309         if ($myrow["closed"] == 1)
310         {
311                 echo "<center>";
312                 display_error(_("This work order is closed and cannot be edited."));
313                 safe_exit();
314         }
315
316         $_POST['wo_ref'] = $myrow["wo_ref"];
317         $_POST['stock_id'] = $myrow["stock_id"];
318         $_POST['quantity'] = qty_format($myrow["units_reqd"], $_POST['stock_id'], $dec);
319         $_POST['StockLocation'] = $myrow["loc_code"];
320         $_POST['released'] = $myrow["released"];
321         $_POST['closed'] = $myrow["closed"];
322         $_POST['type'] = $myrow["type"];
323         $_POST['date_'] = sql2date($myrow["date_"]);
324         $_POST['RequDate'] = sql2date($myrow["required_by"]);
325         $_POST['released_date'] = sql2date($myrow["released_date"]);
326         $_POST['memo_'] = "";
327         $_POST['units_issued'] = $myrow["units_issued"];
328         $_POST['Costs'] = price_format($myrow["additional_costs"]);
329
330         $_POST['memo_'] = get_comments_string(systypes::work_order(), $selected_id);
331
332         hidden('wo_ref', $_POST['wo_ref']);
333         hidden('units_issued', $_POST['units_issued']);
334         hidden('released', $_POST['released']);
335         hidden('released_date', $_POST['released_date']);
336         hidden('selected_id',  $selected_id);
337         hidden('old_qty', $myrow["units_reqd"]);
338         hidden('old_stk_id', $myrow["stock_id"]);
339
340         label_row(_("Reference:"), $_POST['wo_ref']);
341         label_row(_("Type:"), wo_types::name($_POST['type']));
342         hidden('type', $myrow["type"]);
343 }
344 else
345 {
346         $_POST['units_issued'] = $_POST['released'] = 0;
347         ref_row(_("Reference:"), 'wo_ref', '', references::get_next(systypes::work_order()));
348
349         wo_types_list_row(_("Type:"), 'type', null);
350 }
351
352 if ($_POST['released'] == true)
353 {
354         hidden('stock_id', $_POST['stock_id']);
355         hidden('StockLocation', $_POST['StockLocation']);
356         hidden('type', $_POST['type']);
357
358         label_row(_("Item:"), $myrow["StockItemName"]);
359         label_row(_("Destination Location:"), $myrow["location_name"]);
360 }
361 else
362 {
363         stock_manufactured_items_list_row(_("Item:"), 'stock_id', null);
364
365         locations_list_row(_("Destination Location:"), 'StockLocation', null);
366 }
367
368 if (!isset($_POST['quantity']))
369         $_POST['quantity'] = qty_format(1, $_POST['stock_id'], $dec);
370
371 if (get_post('type') == wo_types::advanced())
372 {
373     qty_row(_("Quantity Required:"), 'quantity', null, null, null, $dec);
374     if ($_POST['released'])
375         label_row(_("Quantity Manufactured:"), number_format($_POST['units_issued'], get_qty_dec($_POST['stock_id'])));
376     date_row(_("Date") . ":", 'date_');
377         date_row(_("Date Required By") . ":", 'RequDate', '', null, sys_prefs::default_wo_required_by());
378 }
379 else
380 {
381     qty_row(_("Quantity:"), 'quantity', null, null, null, $dec);
382     date_row(_("Date") . ":", 'date_');
383         hidden('RequDate', '');
384
385         if (!isset($_POST['Costs']))
386                 $_POST['Costs'] = price_format(0);
387
388         amount_row(_("Total Additional Costs:"), 'Costs');
389 }
390
391 if ($_POST['released'])
392         label_row(_("Released On:"),$_POST['released_date']);
393
394 textarea_row(_("Memo:"), 'memo_', null, 40, 5);
395
396 end_table(1);
397
398 if (isset($selected_id))
399 {
400         echo "<table align=center><tr>";
401
402         submit_cells('UPDATE_ITEM', _("Update"), '', _('Save changes to work order'), true);
403         if (isset($_POST['released']))
404         {
405                 submit_cells('close', _("Close This Work Order"),'','',true);
406         }
407         submit_cells('delete', _("Delete This Work Order"),'','',true);
408
409         echo "</tr></table>";
410 }
411 else
412 {
413         submit_center('ADD_ITEM', _("Add Workorder"), true, '', true);
414 }
415
416 end_form();
417 end_page();
418
419 ?>