Changed so that average item material price is automatic updated whenever a PO Delive...
[fa-stable.git] / manufacturing / work_order_issue.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/ui/items_cart.inc");
6
7 include_once($path_to_root . "/includes/session.inc");
8
9 include_once($path_to_root . "/includes/date_functions.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 include_once($path_to_root . "/manufacturing/includes/work_order_issue_ui.inc");
15 $js = "";
16 if ($use_date_picker)
17         $js .= get_js_date_picker();
18 page(_("Issue Items to Work Order"), false, false, "", $js);
19
20 //-----------------------------------------------------------------------------------------------
21
22 if (isset($_GET['AddedID'])) 
23 {
24         echo "<center>" . _("The work order issue has been entered.");
25         echo "<br>";
26         hyperlink_no_params("search_work_orders.php", _("Select another Work Order to Process"));
27         echo "<br><br>";
28         end_page();
29         exit;
30 }
31
32 //--------------------------------------------------------------------------------------------------
33
34 function handle_new_order()
35 {
36         if (isset($_SESSION['issue_items']))
37         {
38                 $_SESSION['issue_items']->clear_items();
39                 unset ($_SESSION['issue_items']);
40         }
41
42      Session_register("issue_items");
43
44      $_SESSION['issue_items'] = new items_cart;
45      $_SESSION['issue_items']->order_id = $_GET['trans_no'];
46 }
47
48 //-----------------------------------------------------------------------------------------------
49
50 function can_process()
51 {
52         if (!is_date($_POST['date_'])) 
53         {
54                 display_error(_("The entered date for the issue is invalid."));
55                 return false;
56         } 
57         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
58         {
59                 display_error(_("The entered date is not in fiscal year."));
60                 return false;
61         }
62         if (!references::is_valid($_POST['ref'])) 
63         {
64                 display_error(_("You must enter a reference."));
65                 return false;
66         }
67
68         if (!is_new_reference($_POST['ref'], 28)) 
69         {
70                 display_error(_("The entered reference is already in use."));
71                 return false;
72         }
73
74         $failed_item = $_SESSION['issue_items']->check_qoh($_POST['Location'], $_POST['date_'], !$_POST['IssueType']);
75         if ($failed_item != null) 
76         {
77         display_error( _("The issue cannot be processed because an entered item would cause a negative inventory balance :") .
78                 " " . $failed_item->stock_id . " - " .  $failed_item->item_description);
79                 return false;
80         }
81
82         return true;
83 }
84
85 if (isset($_POST['Process']))
86 {
87
88         // if failed, returns a stockID
89         $failed_data = add_work_order_issue($_SESSION['issue_items']->order_id,
90                 $_POST['ref'], $_POST['IssueType'], $_SESSION['issue_items']->line_items,
91                 $_POST['Location'], $_POST['WorkCentre'], $_POST['date_'], $_POST['memo_']);
92
93         if ($failed_data != null) 
94         {
95                 display_error(_("The process cannot be completed because there is an insufficient total quantity for a component.") . "<br>"
96                 . _("Component is :"). $failed_data[0] . "<br>"
97                 . _("From location :"). $failed_data[1] . "<br>");
98         } 
99         else 
100         {
101                 meta_forward($_SERVER['PHP_SELF'], "AddedID=1");
102         }
103
104 } /*end of process credit note */
105
106 //-----------------------------------------------------------------------------------------------
107
108 function check_item_data()
109 {
110         if (!is_numeric($_POST['qty']))
111         {
112                 display_error(_("The quantity entered is not a valid number."));
113                 return false;
114         }
115
116         if ($_POST['qty'] <= 0)
117         {
118                 display_error(_("The quantity entered must be greater than zero."));
119                 return false;
120         }
121
122         if (!is_numeric($_POST['std_cost']) || $_POST['std_cost'] < 0)
123         {
124                 display_error(_("The entered standard cost is negative or invalid."));
125                 return false;
126         }
127
128         return true;
129 }
130
131 //-----------------------------------------------------------------------------------------------
132
133 function handle_update_item()
134 {
135     if($_POST['UpdateItem'] != "" && check_item_data())
136     {
137         $_SESSION['issue_items']->update_cart_item($_POST['stock_id'], $_POST['qty'], $_POST['std_cost']);
138     }
139 }
140
141 //-----------------------------------------------------------------------------------------------
142
143 function handle_delete_item()
144 {
145         $_SESSION['issue_items']->remove_from_cart($_GET['Delete']);
146 }
147
148 //-----------------------------------------------------------------------------------------------
149
150 function handle_new_item()
151 {
152         if (!check_item_data())
153                 return;
154
155         add_to_order($_SESSION['issue_items'], $_POST['stock_id'], $_POST['qty'], $_POST['std_cost']);
156 }
157
158 //-----------------------------------------------------------------------------------------------
159
160 if ($_GET['Delete']!="")
161         handle_delete_item();
162
163 if ($_POST['AddItem']!="")
164         handle_new_item();
165
166 if ($_POST['UpdateItem']!="")
167         handle_update_item();
168
169 //-----------------------------------------------------------------------------------------------
170
171 if (isset($_GET['trans_no']))
172 {
173         handle_new_order();
174 }
175
176 //-----------------------------------------------------------------------------------------------
177
178 display_order_header($_SESSION['issue_items']);
179
180 start_form(false, true);
181
182 start_table("$table_style width=90%", '10');
183 echo "<tr><td>";
184 display_adjustment_items(_("Items to Issue"), $_SESSION['issue_items']);
185 adjustment_options_controls();
186 echo "</td></tr>";
187
188 end_table();
189
190 if (!isset($_POST['Process']))
191 {
192         start_table();
193     start_row();
194     submit_cells('Update', _("Update"));
195         if ($_SESSION['issue_items']->count_items() >= 1)
196         {
197             submit_cells('Process', _("Process Issue"));
198         }
199         end_row();
200         end_table();
201 }
202
203 end_form();
204
205 //------------------------------------------------------------------------------------------------
206
207 end_page();
208
209 ?>