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