545b694b97794256cfcfb877ceed0b70e82948c9
[fa-stable.git] / manufacturing / work_order_add_finished.php
1 <?php
2
3 $page_security = 10;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/session.inc");
6
7 include_once($path_to_root . "/includes/date_functions.inc");
8 include_once($path_to_root . "/includes/db/inventory_db.inc");
9 include_once($path_to_root . "/includes/manufacturing.inc");
10
11 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
12 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
13
14 $js = "";
15 if ($use_popup_windows)
16         $js .= get_js_open_window(900, 500);
17 if ($use_date_picker)
18         $js .= get_js_date_picker();
19 page(_("Produce or Unassemble Finished Items From Work Order"), false, false, "", $js);
20
21 if (isset($_GET['trans_no']) && $_GET['trans_no'] != "")
22 {
23         $_POST['selected_id'] = $_GET['trans_no'];
24 }
25
26 //--------------------------------------------------------------------------------------------------
27
28 if (isset($_GET['AddedID']))
29 {
30
31         display_note(_("The manufacturing process has been entered."));
32
33         hyperlink_no_params("search_work_orders.php", _("Select another Work Order to Process"));
34
35         end_page();
36         exit;
37 }
38
39 //--------------------------------------------------------------------------------------------------
40
41 $wo_details = get_work_order($_POST['selected_id']);
42
43 if (strlen($wo_details[0]) == 0)
44 {
45         display_error(_("The order number sent is not valid."));
46         exit;
47 }
48
49 //--------------------------------------------------------------------------------------------------
50
51 function can_process()
52 {
53         global $wo_details;
54
55         if (!references::is_valid($_POST['ref']))
56         {
57                 display_error(_("You must enter a reference."));
58                 set_focus('ref');
59                 return false;
60         }
61
62         if (!is_new_reference($_POST['ref'], 29))
63         {
64                 display_error(_("The entered reference is already in use."));
65                 set_focus('ref');
66                 return false;
67         }
68
69         if (!check_num('quantity', 0))
70         {
71                 display_error(_("The quantity entered is not a valid number or less then zero."));
72                 set_focus('quantity');
73                 return false;
74         }
75
76         if (!is_date($_POST['date_']))
77         {
78                 display_error(_("The entered date is invalid."));
79                 set_focus('date_');
80                 return false;
81         }
82         elseif (!is_date_in_fiscalyear($_POST['date_']))
83         {
84                 display_error(_("The entered date is not in fiscal year."));
85                 set_focus('date_');
86                 return false;
87         }
88         if (date_diff(sql2date($wo_details["released_date"]), $_POST['date_'], "d") > 0)
89         {
90                 display_error(_("The production date cannot be before the release date of the work order."));
91                 set_focus('date_');
92                 return false;
93         }
94
95         // if unassembling we need to check the qoh
96         if (($_POST['ProductionType'] == 0) && !sys_prefs::allow_negative_stock())
97         {
98                 $wo_details = get_work_order($_POST['selected_id']);
99
100                 $qoh = get_qoh_on_date($wo_details["stock_id"], $wo_details["loc_code"], $date_);
101                 if (-$_POST['quantity'] + $qoh < 0)
102                 {
103                         display_error(_("The unassembling cannot be processed because there is insufficient stock."));
104                         set_focus('quantity');
105                         return false;
106                 }
107         }
108
109         return true;
110 }
111
112 //--------------------------------------------------------------------------------------------------
113
114 if (isset($_POST['Process']) || (isset($_POST['ProcessAndClose']) && can_process() == true))
115 {
116
117         $close_wo = 0;
118         if (isset($_POST['ProcessAndClose']) && ($_POST['ProcessAndClose']!=""))
119                 $close_wo = 1;
120
121         // if unassembling, negate quantity
122         if ($_POST['ProductionType'] == 0)
123                 $_POST['quantity'] = -$_POST['quantity'];
124
125          $id = work_order_produce($_POST['selected_id'], $_POST['ref'], $_POST['quantity'],
126                         $_POST['date_'], $_POST['memo_'], $close_wo);
127
128         meta_forward($_SERVER['PHP_SELF'], "AddedID=$id");
129 }
130
131 //-------------------------------------------------------------------------------------
132
133 display_wo_details($_POST['selected_id']);
134
135 //-------------------------------------------------------------------------------------
136
137 start_form();
138
139 hidden('selected_id', $_POST['selected_id']);
140 //hidden('WOReqQuantity', $_POST['WOReqQuantity']);
141
142 if (!isset($_POST['quantity']) || $_POST['quantity'] == '')
143 {
144         $_POST['quantity'] = max($wo_details["units_reqd"] - $wo_details["units_issued"], 0);
145 }
146
147 start_table();
148
149 ref_row(_("Reference:"), 'ref', references::get_next(29));
150
151 if (!isset($_POST['ProductionType']))
152         $_POST['ProductionType'] = 1;
153
154 yesno_list_row(_("Type:"), 'ProductionType', $_POST['ProductionType'],
155         _("Produce Finished Items"), _("Return Items to Work Order"));
156
157 text_row(_("Quantity:"), 'quantity', $_POST['quantity'], 13, 15);
158
159 date_row(_("Date:"), 'date_');
160
161 textarea_row(_("Memo:"), 'memo_', null, 40, 3);
162
163 end_table(1);
164
165 submit_center_first('Process', _("Process"));
166 submit_center_last('ProcessAndClose', _("Process And Close Order"));
167
168 end_form();
169
170 end_page();
171
172 ?>