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