13897245858c748ca00511d8c23f1137dd5796b9
[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 = '';
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                 set_focus('qty');
157                 return false;
158     }
159
160     if (!check_num('price', 0))
161     {
162                 display_error(_("The price entered must be numeric and not less than zero."));
163                 set_focus('price');
164                 return false;      
165     }
166     if (!is_date($_POST['req_del_date'])){
167                 display_error(_("The date entered is in an invalid format."));
168                 set_focus('req_del_date');
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                 set_focus('qty');
188                 return;
189         }
190         
191         $_SESSION['PO']->update_order_item($_POST['line_no'], input_num('qty'), input_num('price'),
192                 $_POST['req_del_date']);
193         unset_form_variables();
194 }
195
196 //---------------------------------------------------------------------------------------------------
197
198 function handle_add_new_item()
199 {
200         $allow_update = check_data();
201         
202         if ($allow_update == true)
203         { 
204                 if (count($_SESSION['PO']->line_items) > 0)
205                 {
206                     foreach ($_SESSION['PO']->line_items AS $order_item) 
207                     {
208
209                         /* do a loop round the items on the order to see that the item
210                         is not already on this order */
211                             if (($order_item->stock_id == $_POST['stock_id']) && 
212                                 ($order_item->Deleted==False)) 
213                             {
214                                         $allow_update = False;
215                                         display_error(_("The selected item is already on this order."));
216                             }
217                     } /* end of the foreach loop to look for pre-existing items of the same code */
218                 }
219
220                 if ($allow_update == true)
221                 {
222                         $sql = "SELECT description, units, mb_flag
223                                 FROM ".TB_PREF."stock_master WHERE stock_id = '". $_POST['stock_id'] . "'";
224
225                     $result = db_query($sql,"The stock details for " . $_POST['stock_id'] . " could not be retrieved");
226
227                     if (db_num_rows($result) == 0)
228                     {
229                                 $allow_update = False;
230                     }               
231
232                         if ($allow_update)
233                         {
234                                 $myrow = db_fetch($result);
235                                 $_SESSION['PO']->add_to_order ($_POST['line_no'], $_POST['stock_id'], input_num('qty'), 
236                                         $myrow["description"], input_num('price'), $myrow["units"],
237                                         $_POST['req_del_date'], 0, 0);
238
239                                 unset_form_variables();
240                                 $_POST['StockID2'] = $_POST['stock_id'] = "";
241                         } 
242                         else 
243                         {
244                              display_error(_("The selected item does not exist or it is a kit part and therefore cannot be purchased."));
245                         }
246
247                 } /* end of if not already on the order and allow input was true*/
248     }
249 }
250
251 //---------------------------------------------------------------------------------------------------
252
253 function can_commit()
254 {
255         if (!is_date($_POST['OrderDate'])) 
256         {
257                 display_error(_("The entered order date is invalid."));
258                 set_focus('OrderDate');
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                 set_focus('ref');
268                 return false;
269         } 
270         
271         if (!is_new_reference($_SESSION['PO']->reference, systypes::po())) 
272         {
273                 display_error(_("The entered reference is already in use."));
274                 set_focus('ref');
275                 return false;
276         }
277         }
278         
279         if ($_SESSION['PO']->delivery_address == "")
280         {
281                 display_error(_("There is no delivery address specified."));
282                 set_focus('delivery_address');
283                 return false;
284         } 
285         
286         if (!isset($_SESSION['PO']->Location) || $_SESSION['PO']->Location == "")
287         {
288                 display_error(_("There is no location specified to move any items into."));
289                 set_focus('StkLocation');
290                 return false;
291         } 
292         
293         if ($_SESSION['PO']->order_has_items() == false)
294         {
295         display_error (_("The order cannot be placed because there are no lines entered on this order."));
296         return false;
297         }
298                 
299         return true;
300 }
301
302 //---------------------------------------------------------------------------------------------------
303
304 function handle_commit_order()
305 {
306         copy_to_po();
307
308         if (can_commit())
309         {
310
311                 if ($_SESSION['PO']->order_no == 0)
312                 { 
313                         
314                         /*its a new order to be inserted */
315                         $order_no = add_po($_SESSION['PO']);
316                          
317                         unset($_SESSION['PO']);
318                          
319                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");        
320
321                 } 
322                 else 
323                 { 
324
325                         /*its an existing order need to update the old order info */
326                         $order_no = update_po($_SESSION['PO']);
327                         
328                         unset($_SESSION['PO']);
329                         
330                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no&Updated=1");      
331                 }
332         }       
333 }
334
335 //---------------------------------------------------------------------------------------------------
336
337 if (isset($_POST['Commit']))
338 {
339         handle_commit_order();
340 }
341
342 //--------------------------------------------------------------------------------------------------- 
343
344 if (isset($_POST['UpdateLine']))
345 {
346         copy_to_po();
347         handle_update_item();
348 }
349
350 //---------------------------------------------------------------------------------------------------
351
352 If (isset($_POST['EnterLine']))
353 {
354         copy_to_po();
355         handle_add_new_item();
356
357
358 //---------------------------------------------------------------------------------------------------
359
360 if (isset($_POST['CancelOrder'])) 
361 {
362         handle_cancel_po();
363 }
364
365 //---------------------------------------------------------------------------------------------------
366
367 if (isset($_POST['CancelUpdate']))
368 {
369         copy_to_po();
370         unset_form_variables();
371 }
372
373 //---------------------------------------------------------------------------------------------------
374
375 if (isset($_GET['ModifyOrderNumber']) && $_GET['ModifyOrderNumber'] != "")
376 {
377         create_new_po();
378         
379         $_SESSION['PO']->order_no = $_GET['ModifyOrderNumber']; 
380
381         /*read in all the selected order into the Items cart  */
382         read_po($_SESSION['PO']->order_no, $_SESSION['PO']);
383         copy_from_po();
384 }
385
386 //--------------------------------------------------------------------------------
387
388 if (isset($_GET['NewOrder']))
389 {
390         create_new_po();
391
392 else 
393 {
394         if (!isset($_POST['supplier_id']))
395                 $_POST['supplier_id'] = $_SESSION['PO']->supplier_id;
396         if (!isset($_POST['OrderDate']))                
397                 $_POST['OrderDate'] = $_SESSION['PO']->orig_order_date;
398         if (!isset($_POST['Requisition']))              
399                 $_POST['Requisition'] = $_SESSION['PO']->requisition_no;
400         if (!isset($_POST['Comments']))         
401                 $_POST['Comments'] = $_SESSION['PO']->Comments;
402 }
403
404 //---------------------------------------------------------------------------------------------------
405
406 start_form(false, true);
407
408 display_po_header($_SESSION['PO']);
409 echo "<br>";
410
411 display_po_items($_SESSION['PO']);
412
413 start_table($table_style2);
414 textarea_row(_("Memo:"), 'Comments', null, 70, 4);
415
416 end_table(1);
417 if ($_SESSION['PO']->order_has_items()) 
418 {
419         if ($_SESSION['PO']->order_no)
420                 submit_center_first('Commit', _("Update Order"));
421         else
422                 submit_center_first('Commit', _("Place Order"));
423         submit_center_last('CancelOrder', _("Cancel Order"));   
424 }
425 else
426         submit_center('CancelOrder', _("Cancel Order"));        
427
428 //---------------------------------------------------------------------------------------------------
429
430 end_form();
431 end_page();
432 ?>