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