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