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