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