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