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