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