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