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