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