Fixed numeric fields to accept user native number format.
[fa-stable.git] / purchasing / po_entry_items.php
1 <?php
2
3 $page_security = 4;
4
5 $path_to_root="..";
6 include($path_to_root . "/purchasing/includes/po_class.inc");
7
8 include($path_to_root . "/includes/session.inc");
9
10 include($path_to_root . "/includes/data_checks.inc");
11 include($path_to_root . "/includes/manufacturing.inc");
12
13 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
14
15 $js = get_js_form_entry("StockID2", "stock_id", "qty");
16 if ($use_popup_windows)
17         $js .= get_js_open_window(900, 500);
18 if ($use_date_picker)
19         $js .= get_js_date_picker();
20
21 if (isset($_GET['ModifyOrderNumber'])) 
22 {
23         page(_("Modify Purchase Order #") . $_GET['ModifyOrderNumber'], false, false, "", $js);
24
25 else 
26 {
27         page(_("Purchase Order Entry"), false, false, "", $js);
28 }
29
30 //---------------------------------------------------------------------------------------------------
31
32 check_db_has_suppliers(_("There are no suppliers defined in the system."));
33
34 check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
35
36 //---------------------------------------------------------------------------------------------------------------
37
38 if (isset($_GET['AddedID'])) 
39 {
40         $order_no = $_GET['AddedID'];
41         $trans_type = systypes::po();   
42
43         if (!isset($_GET['Updated']))
44                 display_notification_centered(_("Purchase Order has been entered"));
45         else
46                 display_notification_centered(_("Purchase Order has been updated") . " #$order_no");
47         display_note(get_trans_view_str($trans_type, $order_no, _("View this order")));
48
49         hyperlink_params($path_to_root . "/purchasing/po_receive_items.php", _("Receive Items on this Purchase Order"), "PONumber=$order_no");
50
51         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Purchase Order"), "NewOrder=yes");
52         
53         hyperlink_no_params($path_to_root."/purchasing/inquiry/po_search.php", _("Select An Outstanding Purchase Order"));
54         
55         display_footer_exit();  
56 }
57
58 //--------------------------------------------------------------------------------------------------
59
60 function copy_to_po()
61 {
62         $_SESSION['PO']->supplier_id = $_POST['supplier_id'];   
63         $_SESSION['PO']->orig_order_date = $_POST['OrderDate'];
64         $_SESSION['PO']->reference = $_POST['ref'];
65         $_SESSION['PO']->requisition_no = $_POST['Requisition'];
66         $_SESSION['PO']->Comments = $_POST['Comments']; 
67         $_SESSION['PO']->Location = $_POST['StkLocation'];
68         $_SESSION['PO']->delivery_address = $_POST['delivery_address'];
69 }
70
71 //--------------------------------------------------------------------------------------------------
72
73 function copy_from_po()
74 {
75         $_POST['supplier_id'] = $_SESSION['PO']->supplier_id;   
76         $_POST['OrderDate'] = $_SESSION['PO']->orig_order_date; 
77     $_POST['Requisition'] = $_SESSION['PO']->requisition_no;
78     $_POST['ref'] = $_SESSION['PO']->reference;
79         $_POST['Comments'] = $_SESSION['PO']->Comments;
80     $_POST['StkLocation'] = $_SESSION['PO']->Location;
81     $_POST['delivery_address'] = $_SESSION['PO']->delivery_address;     
82 }
83
84 //--------------------------------------------------------------------------------------------------
85
86 function unset_form_variables() {
87         unset($_POST['stock_id']);
88     unset($_POST['qty']);
89     unset($_POST['price']);
90     unset($_POST['req_del_date']);
91 }
92
93 //---------------------------------------------------------------------------------------------------
94
95 function handle_delete_item()
96 {
97         $line_no = $_GET['Delete'];
98         if($_SESSION['PO']->some_already_received($line_no) == 0)
99         {
100                 $_SESSION['PO']->remove_from_order($line_no);
101                 unset_form_variables();
102         } 
103         else 
104         {
105                 display_error(_("This item cannot be deleted because some of it has already been received."));
106         }       
107 }
108
109 //---------------------------------------------------------------------------------------------------
110
111 function handle_cancel_po()
112 {
113         global $path_to_root;
114         
115         //need to check that not already dispatched or invoiced by the supplier
116         if(($_SESSION['PO']->order_no != 0) && 
117                 $_SESSION['PO']->any_already_received() == 1)
118         {
119                 display_error(_("This order cannot be cancelled because some of it has already been received.") 
120                         . "<br>" . _("The line item quantities may be modified to quantities more than already received. prices cannot be altered for lines that have already been received and quantities cannot be reduced below the quantity already received."));
121                 return;
122         }
123         
124         if($_SESSION['PO']->order_no != 0)
125         {
126                 delete_po($_SESSION['PO']->order_no);
127         }       
128
129         $_SESSION['PO']->clear_items();
130         $_SESSION['PO'] = new purch_order;
131
132         display_note(_("This purchase order has been cancelled."), 0, 1);
133
134         hyperlink_params($path_to_root . "/purchasing/po_entry_items.php", _("Enter a new purchase order"), "NewOrder=Yes");
135         echo "<br>";
136
137         end_page();
138         exit;
139 }
140
141 //---------------------------------------------------------------------------------------------------
142 if (isset($_GET['Delete']) || isset($_GET['Edit']))
143 {
144         copy_from_po();
145 }
146         
147 if (isset($_GET['Delete']))
148 {
149         handle_delete_item();
150 }
151
152 //---------------------------------------------------------------------------------------------------
153
154 function check_data()
155 {
156     if (!check_num('qty',0))
157     {
158                 display_error(_("The quantity of the order item must be numeric and not less than zero."));
159                 return false;
160     }
161
162     if (!check_num('price', 0))
163     {
164                 display_error(_("The price entered must be numeric and not less than zero."));
165                 return false;      
166     }
167     if (!is_date($_POST['req_del_date'])){
168         display_error(_("The date entered is in an invalid format."));
169                 return false;            
170     }
171      
172     return true;        
173 }
174
175 //---------------------------------------------------------------------------------------------------
176
177 function handle_update_item()
178 {
179         $allow_update = check_data(); 
180
181         if ($allow_update && 
182                 ($_SESSION['PO']->line_items[$_POST['line_no']]->qty_inv > input_num('qty') ||
183                 $_SESSION['PO']->line_items[$_POST['line_no']]->qty_received > input_num('qty')))
184         {
185                 display_error(_("You are attempting to make the quantity ordered a quantity less than has already been invoiced or received.  This is prohibited.") .
186                         "<br>" . _("The quantity received can only be modified by entering a negative receipt and the quantity invoiced can only be reduced by entering a credit note against this item."));
187                 return;
188         }
189         
190         $_SESSION['PO']->update_order_item($_POST['line_no'], input_num('qty'), input_num('price'),
191                 $_POST['req_del_date']);
192         unset_form_variables();
193 }
194
195 //---------------------------------------------------------------------------------------------------
196
197 function handle_add_new_item()
198 {
199         $allow_update = check_data();
200         
201         if ($allow_update == true)
202         { 
203                 if (count($_SESSION['PO']->line_items) > 0)
204                 {
205                     foreach ($_SESSION['PO']->line_items AS $order_item) 
206                     {
207
208                         /* do a loop round the items on the order to see that the item
209                         is not already on this order */
210                             if (($order_item->stock_id == $_POST['stock_id']) && 
211                                 ($order_item->Deleted==False)) 
212                             {
213                                         $allow_update = False;
214                                         display_error(_("The selected item is already on this order."));
215                             }
216                     } /* end of the foreach loop to look for pre-existing items of the same code */
217                 }
218
219                 if ($allow_update == true)
220                 {
221                         $sql = "SELECT description, units, mb_flag
222                                 FROM ".TB_PREF."stock_master WHERE stock_id = '". $_POST['stock_id'] . "'";
223
224                     $result = db_query($sql,"The stock details for " . $_POST['stock_id'] . " could not be retrieved");
225
226                     if (db_num_rows($result) == 0)
227                     {
228                                 $allow_update = False;
229                     }               
230
231                         if ($allow_update)
232                         {
233                                 $myrow = db_fetch($result);
234                                 $_SESSION['PO']->add_to_order ($_POST['line_no'], $_POST['stock_id'], input_num('qty'), 
235                                         $myrow["description"], input_num('price'), $myrow["units"],
236                                         $_POST['req_del_date'], 0, 0);
237
238                                 unset_form_variables();
239                                 $_POST['StockID2'] = $_POST['stock_id'] = "";
240                         } 
241                         else 
242                         {
243                              display_error(_("The selected item does not exist or it is a kit part and therefore cannot be purchased."));
244                         }
245
246                 } /* end of if not already on the order and allow input was true*/
247     }
248 }
249
250 //---------------------------------------------------------------------------------------------------
251
252 function can_commit()
253 {
254         if (!is_date($_POST['OrderDate'])) 
255         {
256                 display_error(_("The entered order date is invalid."));
257                 return false;
258         } 
259         
260         if (!$_SESSION['PO']->order_no) 
261         {
262         if (!references::is_valid($_SESSION['PO']->reference)) 
263         {
264                 display_error(_("There is no reference entered for this purchase order."));
265                 return false;
266         } 
267         
268         if (!is_new_reference($_SESSION['PO']->reference, systypes::po())) 
269         {
270                 display_error(_("The entered reference is already in use."));
271                 return false;
272         }
273         }
274         
275         if ($_SESSION['PO']->delivery_address == "")
276         {
277                 display_error(_("There is no delivery address specified."));
278                 return false;
279         } 
280         
281         if (!isset($_SESSION['PO']->Location) || $_SESSION['PO']->Location == "")
282         {
283                 display_error(_("There is no location specified to move any items into."));
284                 return false;
285         } 
286         
287         if ($_SESSION['PO']->order_has_items() == false)
288         {
289         display_error (_("The order cannot be placed because there are no lines entered on this order."));
290         return false;
291         }
292                 
293         return true;
294 }
295
296 //---------------------------------------------------------------------------------------------------
297
298 function handle_commit_order()
299 {
300         copy_to_po();
301
302         if (can_commit())
303         {
304
305                 if ($_SESSION['PO']->order_no == 0)
306                 { 
307                         
308                         /*its a new order to be inserted */
309                         $order_no = add_po($_SESSION['PO']);
310                          
311                         unset($_SESSION['PO']);
312                          
313                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");        
314
315                 } 
316                 else 
317                 { 
318
319                         /*its an existing order need to update the old order info */
320                         $order_no = update_po($_SESSION['PO']);
321                         
322                         unset($_SESSION['PO']);
323                         
324                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no&Updated=1");      
325                 }
326         }       
327 }
328
329 //---------------------------------------------------------------------------------------------------
330
331 if (isset($_POST['Commit']))
332 {
333         handle_commit_order();
334 }
335
336 //--------------------------------------------------------------------------------------------------- 
337
338 if (isset($_POST['UpdateLine']))
339 {
340         copy_to_po();
341         handle_update_item();
342 }
343
344 //---------------------------------------------------------------------------------------------------
345
346 If (isset($_POST['EnterLine']))
347 {
348         copy_to_po();
349         handle_add_new_item();
350
351
352 //---------------------------------------------------------------------------------------------------
353
354 if (isset($_POST['CancelOrder'])) 
355 {
356         handle_cancel_po();
357 }
358
359 //---------------------------------------------------------------------------------------------------
360
361 if (isset($_POST['CancelUpdate']))
362 {
363         copy_to_po();
364         unset_form_variables();
365 }
366
367 //---------------------------------------------------------------------------------------------------
368
369 if (isset($_GET['ModifyOrderNumber']) && $_GET['ModifyOrderNumber'] != "")
370 {
371         create_new_po();
372         
373         $_SESSION['PO']->order_no = $_GET['ModifyOrderNumber']; 
374
375         /*read in all the selected order into the Items cart  */
376         read_po($_SESSION['PO']->order_no, $_SESSION['PO']);
377         copy_from_po();
378 }
379
380 //--------------------------------------------------------------------------------
381
382 if (isset($_GET['NewOrder']))
383 {
384         create_new_po();
385
386 else 
387 {
388         if (!isset($_POST['supplier_id']))
389                 $_POST['supplier_id'] = $_SESSION['PO']->supplier_id;
390         if (!isset($_POST['OrderDate']))                
391                 $_POST['OrderDate'] = $_SESSION['PO']->orig_order_date;
392         if (!isset($_POST['Requisition']))              
393                 $_POST['Requisition'] = $_SESSION['PO']->requisition_no;
394         if (!isset($_POST['Comments']))         
395                 $_POST['Comments'] = $_SESSION['PO']->Comments;
396 }
397
398 //---------------------------------------------------------------------------------------------------
399
400 start_form(false, true);
401
402 display_po_header($_SESSION['PO']);
403 echo "<br>";
404
405 display_po_items($_SESSION['PO']);
406
407 start_table($table_style2);
408 textarea_row(_("Memo:"), 'Comments', null, 70, 4);
409
410 end_table(1);
411 if ($_SESSION['PO']->order_has_items()) 
412 {
413         if ($_SESSION['PO']->order_no)
414                 submit_center_first('Commit', _("Update Order"));
415         else
416                 submit_center_first('Commit', _("Place Order"));
417         submit_center_last('CancelOrder', _("Cancel Order"));   
418 }
419 else
420         submit_center('CancelOrder', _("Cancel Order"));        
421
422 //---------------------------------------------------------------------------------------------------
423
424 end_form();
425 end_page();
426 ?>