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