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