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