Fixed buggy qoh check message.
[fa-stable.git] / manufacturing / work_order_issue.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_MANUFISSUE';
13 $path_to_root = "..";
14
15 include_once($path_to_root . "/includes/ui/items_cart.inc");
16
17 include_once($path_to_root . "/includes/session.inc");
18
19 include_once($path_to_root . "/includes/date_functions.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21
22 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
23 include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
24 include_once($path_to_root . "/manufacturing/includes/work_order_issue_ui.inc");
25 $js = "";
26 if ($use_popup_windows)
27         $js .= get_js_open_window(800, 500);
28 if ($use_date_picker)
29         $js .= get_js_date_picker();
30
31 page(_($help_context = "Issue Items to Work Order"), false, false, "", $js);
32
33 //-----------------------------------------------------------------------------------------------
34
35 if (isset($_GET['AddedID'])) 
36 {
37         $id = $_GET['AddedID'];
38         display_notification(_("The work order issue has been entered."));
39
40     display_note(get_trans_view_str(ST_WORKORDER, $id, _("View this Work Order")));
41
42         display_note(get_gl_view_str(ST_WORKORDER, $id, _("View the GL Journal Entries for this Work Order")), 1);
43
44         hyperlink_no_params("search_work_orders.php", _("Select another &Work Order to Process"));
45
46         display_footer_exit();
47 }
48 //--------------------------------------------------------------------------------------------------
49
50 function line_start_focus() {
51   global        $Ajax;
52
53   $Ajax->activate('items_table');
54   set_focus('_stock_id_edit');
55 }
56
57 //--------------------------------------------------------------------------------------------------
58
59 function handle_new_order()
60 {
61         if (isset($_SESSION['issue_items']))
62         {
63                 $_SESSION['issue_items']->clear_items();
64                 unset ($_SESSION['issue_items']);
65         }
66
67      Session_register("issue_items");
68
69      $_SESSION['issue_items'] = new items_cart(28);
70      $_SESSION['issue_items']->order_id = $_GET['trans_no'];
71 }
72
73 //-----------------------------------------------------------------------------------------------
74 function can_process()
75 {
76         global $Refs;
77
78         if (!is_date($_POST['date_'])) 
79         {
80                 display_error(_("The entered date for the issue is invalid."));
81                 set_focus('date_');
82                 return false;
83         } 
84         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
85         {
86                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
87                 set_focus('date_');
88                 return false;
89         }
90         if (!$Refs->is_valid($_POST['ref'])) 
91         {
92                 display_error(_("You must enter a reference."));
93                 set_focus('ref');
94                 return false;
95         }
96
97         if (!is_new_reference($_POST['ref'], 28)) 
98         {
99                 display_error(_("The entered reference is already in use."));
100                 set_focus('ref');
101                 return false;
102         }
103
104         $failed_item = $_SESSION['issue_items']->check_qoh($_POST['Location'], $_POST['date_'], !$_POST['IssueType']);
105         if ($failed_item != -1) 
106         {
107                 $item = $_SESSION['issue_items']->line_items[$failed_item];
108         display_error( _("The issue cannot be processed because an entered item would cause a negative inventory balance :") .
109                 " " . $item->stock_id . " - " .  $item->item_description);
110                 return false;
111         }
112
113         return true;
114 }
115
116 if (isset($_POST['Process']) && can_process())
117 {
118
119         // if failed, returns a stockID
120         $failed_data = add_work_order_issue($_SESSION['issue_items']->order_id,
121                 $_POST['ref'], $_POST['IssueType'], $_SESSION['issue_items']->line_items,
122                 $_POST['Location'], $_POST['WorkCentre'], $_POST['date_'], $_POST['memo_']);
123
124         if ($failed_data != null) 
125         {
126                 display_error(_("The process cannot be completed because there is an insufficient total quantity for a component.") . "<br>"
127                 . _("Component is :"). $failed_data[0] . "<br>"
128                 . _("From location :"). $failed_data[1] . "<br>");
129         } 
130         else 
131         {
132                 meta_forward($_SERVER['PHP_SELF'], "AddedID=".$_SESSION['issue_items']->order_id);
133         }
134
135 } /*end of process credit note */
136
137 //-----------------------------------------------------------------------------------------------
138
139 function check_item_data()
140 {
141         if (!check_num('qty', 0))
142         {
143                 display_error(_("The quantity entered is negative or invalid."));
144                 set_focus('qty');
145                 return false;
146         }
147
148         if (!check_num('std_cost', 0))
149         {
150                 display_error(_("The entered standard cost is negative or invalid."));
151                 set_focus('std_cost');
152                 return false;
153         }
154
155         return true;
156 }
157
158 //-----------------------------------------------------------------------------------------------
159
160 function handle_update_item()
161 {
162     if($_POST['UpdateItem'] != "" && check_item_data())
163     {
164                 $id = $_POST['LineNo'];
165         $_SESSION['issue_items']->update_cart_item($id, input_num('qty'), input_num('std_cost'));
166     }
167         line_start_focus();
168 }
169
170 //-----------------------------------------------------------------------------------------------
171
172 function handle_delete_item($id)
173 {
174         $_SESSION['issue_items']->remove_from_cart($id);
175         line_start_focus();
176 }
177
178 //-----------------------------------------------------------------------------------------------
179
180 function handle_new_item()
181 {
182         if (!check_item_data())
183                 return;
184
185         add_to_issue($_SESSION['issue_items'], $_POST['stock_id'], input_num('qty'),
186                  input_num('std_cost'));
187         line_start_focus();
188 }
189
190 //-----------------------------------------------------------------------------------------------
191 $id = find_submit('Delete');
192 if ($id != -1)
193         handle_delete_item($id);
194
195 if (isset($_POST['AddItem']))
196         handle_new_item();
197
198 if (isset($_POST['UpdateItem']))
199         handle_update_item();
200
201 if (isset($_POST['CancelItemChanges'])) {
202         line_start_focus();
203 }
204 //-----------------------------------------------------------------------------------------------
205
206 if (isset($_GET['trans_no']))
207 {
208         handle_new_order();
209 }
210
211 //-----------------------------------------------------------------------------------------------
212
213 display_wo_details($_SESSION['issue_items']->order_id);
214 echo "<br>";
215
216 start_form();
217
218 start_table(TABLESTYLE, "width=90%", 10);
219 echo "<tr><td>";
220 display_issue_items(_("Items to Issue"), $_SESSION['issue_items']);
221 issue_options_controls();
222 echo "</td></tr>";
223
224 end_table();
225
226 submit_center('Process', _("Process Issue"), true, '', 'default');
227
228 end_form();
229
230 //------------------------------------------------------------------------------------------------
231
232 end_page();
233
234 ?>