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