Layout improvements
[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 $path_to_root = "..";
13 $page_security = 'SA_PURCHASEORDER';
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 . "/purchasing/includes/db/suppliers_db.inc");
18 include_once($path_to_root . "/reporting/includes/reporting.inc");
19
20 set_page_security( @$_SESSION['PO']->trans_type,
21         array(  ST_PURCHORDER => 'SA_PURCHASEORDER',
22                         ST_SUPPRECEIVE => 'SA_GRN',
23                         ST_SUPPINVOICE => 'SA_SUPPLIERINVOICE'),
24         array(  'NewOrder' => 'SA_PURCHASEORDER',
25                         'ModifyOrderNumber' => 'SA_PURCHASEORDER',
26                         'NewGRN' => 'SA_GRN',
27                         'NewInvoice' => 'SA_SUPPLIERINVOICE')
28 );
29
30 $js = '';
31 if ($use_popup_windows)
32         $js .= get_js_open_window(900, 500);
33 if ($use_date_picker)
34         $js .= get_js_date_picker();
35
36 if (isset($_GET['ModifyOrderNumber']) && is_numeric($_GET['ModifyOrderNumber'])) {
37
38         $_SESSION['page_title'] = _($help_context = "Modify Purchase Order #") . $_GET['ModifyOrderNumber'];
39         create_new_po(ST_PURCHORDER, $_GET['ModifyOrderNumber']);
40         copy_from_cart();
41 } elseif (isset($_GET['NewOrder'])) {
42
43         $_SESSION['page_title'] = _($help_context = "Purchase Order Entry");
44         create_new_po(ST_PURCHORDER, 0);
45         copy_from_cart();
46 } elseif (isset($_GET['NewGRN'])) {
47
48         $_SESSION['page_title'] = _($help_context = "Direct GRN Entry");
49         create_new_po(ST_SUPPRECEIVE, 0);
50         copy_from_cart();
51 } elseif (isset($_GET['NewInvoice'])) {
52
53         $_SESSION['page_title'] = _($help_context = "Direct Purchase Invoice Entry");
54         create_new_po(ST_SUPPINVOICE, 0);
55         copy_from_cart();
56 }
57
58 page($_SESSION['page_title'], false, false, "", $js);
59
60 //---------------------------------------------------------------------------------------------------
61
62 check_db_has_suppliers(_("There are no suppliers defined in the system."));
63
64 check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
65
66 //---------------------------------------------------------------------------------------------------------------
67
68 if (isset($_GET['AddedID'])) 
69 {
70         $order_no = $_GET['AddedID'];
71         $trans_type = ST_PURCHORDER;    
72
73         if (!isset($_GET['Updated']))
74                 display_notification_centered(_("Purchase Order has been entered"));
75         else
76                 display_notification_centered(_("Purchase Order has been updated") . " #$order_no");
77         display_note(get_trans_view_str($trans_type, $order_no, _("&View this order")), 0, 1);
78
79         display_note(print_document_link($order_no, _("&Print This Order"), true, $trans_type), 0, 1);
80
81         display_note(print_document_link($order_no, _("&Email This Order"), true, $trans_type, false, "printlink", "", 1));
82
83         hyperlink_params($path_to_root . "/purchasing/po_receive_items.php", _("&Receive Items on this Purchase Order"), "PONumber=$order_no");
84
85         hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Purchase Order"), "NewOrder=yes");
86         
87         hyperlink_no_params($path_to_root."/purchasing/inquiry/po_search.php", _("Select An &Outstanding Purchase Order"));
88         
89         display_footer_exit();  
90
91 } elseif (isset($_GET['AddedGRN'])) {
92
93         $trans_no = $_GET['AddedGRN'];
94         $trans_type = ST_SUPPRECEIVE;
95
96         display_notification_centered(_("Direct GRN has been entered"));
97
98         display_note(get_trans_view_str($trans_type, $trans_no, _("&View this GRN")), 0);
99
100 // not yet
101 //      display_note(print_document_link($trans_no, _("&Print This GRN"), true, $trans_type), 0, 1);
102
103         hyperlink_params("$path_to_root/purchasing/supplier_invoice.php",
104                 _("Entry purchase &invoice for this receival"), "New=1");
105
106         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), 
107                 "filterType=$trans_type&trans_no=$trans_no");
108
109         hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another GRN"), "NewGRN=Yes");
110         
111         display_footer_exit();  
112
113 } elseif (isset($_GET['AddedPI'])) {
114
115         $trans_no = $_GET['AddedPI'];
116         $trans_type = ST_SUPPINVOICE;
117
118         display_notification_centered(_("Direct Purchase Invoice has been entered"));
119
120         display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Invoice")), 0);
121
122 // not yet
123 //      display_note(print_document_link($trans_no, _("&Print This Invoice"), true, $trans_type), 0, 1);
124
125         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Journal Entries for this Invoice")), 1);
126
127         hyperlink_params("$path_to_root/purchasing/supplier_payment.php", _("Entry supplier &payment for this invoice"),
128                 "PInvoice=".$trans_no);
129
130         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), 
131                 "filterType=$trans_type&trans_no=$trans_no");
132
133         hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Direct Invoice"), "NewInvoice=Yes");
134         
135         display_footer_exit();  
136 }
137 //--------------------------------------------------------------------------------------------------
138
139 function line_start_focus() {
140   global        $Ajax;
141
142   $Ajax->activate('items_table');
143   set_focus('_stock_id_edit');
144 }
145 //--------------------------------------------------------------------------------------------------
146
147 function unset_form_variables() {
148         unset($_POST['stock_id']);
149     unset($_POST['qty']);
150     unset($_POST['price']);
151     unset($_POST['req_del_date']);
152 }
153
154 //---------------------------------------------------------------------------------------------------
155
156 function handle_delete_item($line_no)
157 {
158         if($_SESSION['PO']->some_already_received($line_no) == 0)
159         {
160                 $_SESSION['PO']->remove_from_order($line_no);
161                 unset_form_variables();
162         } 
163         else 
164         {
165                 display_error(_("This item cannot be deleted because some of it has already been received."));
166         }       
167     line_start_focus();
168 }
169
170 //---------------------------------------------------------------------------------------------------
171
172 function handle_cancel_po()
173 {
174         global $path_to_root;
175         
176         //need to check that not already dispatched or invoiced by the supplier
177         if(($_SESSION['PO']->order_no != 0) && 
178                 $_SESSION['PO']->any_already_received() == 1)
179         {
180                 display_error(_("This order cannot be cancelled because some of it has already been received.") 
181                         . "<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."));
182                 return;
183         }
184         
185         if($_SESSION['PO']->order_no != 0)
186         {
187                 delete_po($_SESSION['PO']->order_no);
188         } else {
189                 unset($_SESSION['PO']);
190                 meta_forward($path_to_root.'/index.php','application=AP');
191         }
192
193         $_SESSION['PO']->clear_items();
194         $_SESSION['PO'] = new purch_order;
195
196         display_notification(_("This purchase order has been cancelled."));
197
198         hyperlink_params($path_to_root . "/purchasing/po_entry_items.php", _("Enter a new purchase order"), "NewOrder=Yes");
199         echo "<br>";
200
201         end_page();
202         exit;
203 }
204
205 //---------------------------------------------------------------------------------------------------
206
207 function check_data()
208 {
209         if(!get_post('stock_id_text', true)) {
210                 display_error( _("Item description cannot be empty."));
211                 set_focus('stock_id_edit');
212                 return false;
213         }
214
215         $dec = get_qty_dec($_POST['stock_id']);
216         $min = 1 / pow(10, $dec);
217     if (!check_num('qty',$min))
218     {
219         $min = number_format2($min, $dec);
220                 display_error(_("The quantity of the order item must be numeric and not less than ").$min);
221                 set_focus('qty');
222                 return false;
223     }
224
225     if (!check_num('price', 0))
226     {
227                 display_error(_("The price entered must be numeric and not less than zero."));
228                 set_focus('price');
229                 return false;      
230     }
231     if ($_SESSION['PO']->trans_type == ST_PURCHORDER && !is_date($_POST['req_del_date'])){
232                 display_error(_("The date entered is in an invalid format."));
233                 set_focus('req_del_date');
234                 return false;            
235     }
236      
237     return true;        
238 }
239
240 //---------------------------------------------------------------------------------------------------
241
242 function handle_update_item()
243 {
244         $allow_update = check_data(); 
245
246         if ($allow_update)
247         {
248                 if ($_SESSION['PO']->line_items[$_POST['line_no']]->qty_inv > input_num('qty') ||
249                         $_SESSION['PO']->line_items[$_POST['line_no']]->qty_received > input_num('qty'))
250                 {
251                         display_error(_("You are attempting to make the quantity ordered a quantity less than has already been invoiced or received.  This is prohibited.") .
252                                 "<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."));
253                         set_focus('qty');
254                         return;
255                 }
256         
257                 $_SESSION['PO']->update_order_item($_POST['line_no'], input_num('qty'), input_num('price'),
258                         @$_POST['req_del_date'], $_POST['item_description'] );
259                 unset_form_variables();
260         }       
261     line_start_focus();
262 }
263
264 //---------------------------------------------------------------------------------------------------
265
266 function handle_add_new_item()
267 {
268         $allow_update = check_data();
269         
270         if ($allow_update == true)
271         { 
272                 if (count($_SESSION['PO']->line_items) > 0)
273                 {
274                     foreach ($_SESSION['PO']->line_items as $order_item) 
275                     {
276                         /* do a loop round the items on the order to see that the item
277                         is not already on this order */
278                             if (($order_item->stock_id == $_POST['stock_id'])) 
279                             {
280                                         display_warning(_("The selected item is already on this order."));
281                             }
282                     } /* end of the foreach loop to look for pre-existing items of the same code */
283                 }
284
285                 if ($allow_update == true)
286                 {
287                         $result = get_short_info($_POST['stock_id']);
288
289                         if (db_num_rows($result) == 0)
290                         {
291                                 $allow_update = false;
292                         }
293
294                         if ($allow_update)
295                         {
296                                 $myrow = db_fetch($result);
297                                 $_SESSION['PO']->add_to_order (count($_SESSION['PO']->line_items), $_POST['stock_id'], input_num('qty'), 
298                                         get_post('stock_id_text'), //$myrow["description"], 
299                                         input_num('price'), '', // $myrow["units"], (retrived in cart)
300                                         $_SESSION['PO']->trans_type == ST_PURCHORDER ? $_POST['req_del_date'] : '', 0, 0);
301
302                                 unset_form_variables();
303                                 $_POST['stock_id']      = "";
304                         } 
305                         else 
306                         {
307                              display_error(_("The selected item does not exist or it is a kit part and therefore cannot be purchased."));
308                         }
309
310                 } /* end of if not already on the order and allow input was true*/
311     }
312         line_start_focus();
313 }
314
315 //---------------------------------------------------------------------------------------------------
316
317 function can_commit()
318 {
319         global $Refs;
320
321         if (!get_post('supplier_id')) 
322         {
323                 display_error(_("There is no supplier selected."));
324                 set_focus('supplier_id');
325                 return false;
326         } 
327         
328         if (!is_date($_POST['OrderDate'])) 
329         {
330                 display_error(_("The entered order date is invalid."));
331                 set_focus('OrderDate');
332                 return false;
333         } 
334         
335         if (!$_SESSION['PO']->order_no) 
336         {
337         if (!$Refs->is_valid(get_post('ref'))) 
338         {
339                 display_error(_("There is no reference entered for this purchase order."));
340                         set_focus('ref');
341                 return false;
342         } 
343         
344         if (!is_new_reference(get_post('ref'), $_SESSION['PO']->trans_type)) 
345         {
346                 display_error(_("The entered reference is already in use."));
347                         set_focus('ref');
348                 return false;
349         }
350         }
351         
352         if ($_SESSION['PO']->trans_type == ST_SUPPINVOICE && !$Refs->is_valid(get_post('supp_ref'))) 
353         {
354                 display_error(_("You must enter a supplier's invoice reference."));
355                 set_focus('supp_ref');
356                 return false;
357         }
358         if ($_SESSION['PO']->trans_type == ST_PURCHORDER && get_post('delivery_address') == '')
359         {
360                 display_error(_("There is no delivery address specified."));
361                 set_focus('delivery_address');
362                 return false;
363         } 
364         if (get_post('StkLocation') == '')
365         {
366                 display_error(_("There is no location specified to move any items into."));
367                 set_focus('StkLocation');
368                 return false;
369         } 
370         
371         if ($_SESSION['PO']->order_has_items() == false)
372         {
373         display_error (_("The order cannot be placed because there are no lines entered on this order."));
374         return false;
375         }
376                 
377         return true;
378 }
379
380 //---------------------------------------------------------------------------------------------------
381
382 function handle_commit_order()
383 {
384         $cart = &$_SESSION['PO'];
385
386         if (can_commit()) {
387
388                 copy_to_cart();
389                 if ($cart->trans_type != ST_PURCHORDER) {
390                         // for direct grn/invoice set same dates for lines as for whole document
391                         foreach ($cart->line_items as $line_no =>$line)
392                                 $cart->line_items[$line_no]->req_del_date = $cart->orig_order_date;
393                 }
394                 if ($cart->order_no == 0) { // new po/grn/invoice
395                         /*its a new order to be inserted */
396                         $ref = $cart->reference;
397                         if ($cart->trans_type != ST_PURCHORDER) {
398                                 $cart->reference = 'auto';
399                                 begin_transaction();    // all db changes as single transaction for direct document
400                         }
401                         $order_no = add_po($cart);
402                         new_doc_date($cart->orig_order_date); 
403                 $cart->order_no = $order_no;
404
405                         if ($cart->trans_type == ST_PURCHORDER) {
406                                 unset($_SESSION['PO']);
407                         meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");
408                 }
409                         //Direct GRN
410                         if ($cart->trans_type == ST_SUPPRECEIVE)
411                                 $cart->reference = $ref;
412                         $cart->Comments = $cart->reference; //grn does not hold supp_ref
413                         foreach($cart->line_items as $key => $line)
414                                 $cart->line_items[$key]->receive_qty = $line->quantity;
415                         $grn_no = add_grn($cart);
416                         if ($cart->trans_type == ST_SUPPRECEIVE) {
417                                 commit_transaction(); // save PO+GRN
418                                 unset($_SESSION['PO']);
419                         meta_forward($_SERVER['PHP_SELF'], "AddedGRN=$grn_no");
420                         }
421 //                      Direct Purchase Invoice
422                         $inv = new supp_trans(ST_SUPPINVOICE);
423                         $inv->Comments = $cart->Comments;
424                         $inv->supplier_id = $cart->supplier_id;
425                         $inv->tran_date = $cart->orig_order_date;
426                         $inv->reference = $ref;
427                         $inv->supp_reference = $cart->supp_ref;
428                         $inv->tax_included = $cart->tax_included;
429                         $supp = get_supplier($cart->supplier_id);
430                         $inv->tax_group_id = $supp['tax_group_id'];
431 //                      $inv->ov_discount 'this isn't used at all'
432 //                      $inv->terms = not used, TODO
433                         $terms =  $supp['payment_terms'];
434                         $inv->ov_amount = $inv->ov_gst = 0;
435                         $inv->due_date = get_invoice_duedate($terms, $inv->tran_date);
436                         foreach($cart->line_items as $key => $line) {
437                                 $inv->add_grn_to_trans($line->grn_item_id, $line->po_detail_rec, $line->stock_id,
438                                         $line->item_description, $line->receive_qty, 0, $line->receive_qty,
439                                         $line->price, $line->price, true, get_standard_cost($line->stock_id), '');
440                                 $inv->ov_amount += round2(($line->receive_qty * $line->price), user_price_dec());
441                         }
442                         $taxes = $inv->get_taxes($inv->tax_group_id, 0, false);
443                         foreach( $taxes as $taxitem) {
444                                 $inv->ov_gst += round2($taxitem['Value'], user_price_dec());
445                         }
446                         $inv_no = add_supp_invoice($inv);
447                         commit_transaction(); // save PO+GRN+PI
448                         // payment for cash terms...
449                         unset($_SESSION['PO']);
450                 meta_forward($_SERVER['PHP_SELF'], "AddedPI=$inv_no");
451                 }
452                 else { // order modification
453                 
454                         $order_no = update_po($cart);
455                         unset($_SESSION['PO']);
456                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no&Updated=1");      
457                 }
458         }
459 }
460 //---------------------------------------------------------------------------------------------------
461 $id = find_submit('Delete');
462 if ($id != -1)
463         handle_delete_item($id);
464
465 if (isset($_POST['Commit']))
466 {
467         handle_commit_order();
468 }
469 if (isset($_POST['UpdateLine']))
470         handle_update_item();
471
472 if (isset($_POST['EnterLine']))
473         handle_add_new_item();
474
475 if (isset($_POST['CancelOrder'])) 
476         handle_cancel_po();
477
478 if (isset($_POST['CancelUpdate']))
479         unset_form_variables();
480
481 if (isset($_POST['CancelUpdate']) || isset($_POST['UpdateLine'])) {
482         line_start_focus();
483 }
484
485 //---------------------------------------------------------------------------------------------------
486
487 start_form();
488
489 display_po_header($_SESSION['PO']);
490 echo "<br>";
491
492 display_po_items($_SESSION['PO']);
493
494 start_table(TABLESTYLE2);
495 textarea_row(_("Memo:"), 'Comments', null, 70, 4);
496
497 end_table(1);
498
499 div_start('controls', 'items_table');
500 $process_txt = _("Place Order");
501 $update_txt = _("Update Order");
502 $cancel_txt = _("Cancel Order");
503 if ($_SESSION['PO']->trans_type == ST_SUPPRECEIVE) {
504         $process_txt = _("Process GRN");
505         $update_txt = _("Update GRN");
506         $cancel_txt = _("Cancel GRN");
507 }       
508 elseif ($_SESSION['PO']->trans_type == ST_SUPPINVOICE) {
509         $process_txt = _("Process Invoice");
510         $update_txt = _("Update Invoice");
511         $cancel_txt = _("Cancel Invoice");
512 }       
513 if ($_SESSION['PO']->order_has_items()) 
514 {
515         if ($_SESSION['PO']->order_no)
516                 submit_center_first('Commit', $update_txt, '', 'default');
517         else
518                 submit_center_first('Commit', $process_txt, '', 'default');
519         submit_center_last('CancelOrder', $cancel_txt);         
520 }
521 else
522         submit_center('CancelOrder', $cancel_txt, true, false, 'cancel');
523 div_end();
524 //---------------------------------------------------------------------------------------------------
525
526 end_form();
527 end_page();
528 ?>