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