Added Purchase Delivery edition.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Wed, 3 Jul 2019 10:11:32 +0000 (12:11 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 19 Aug 2019 10:41:57 +0000 (12:41 +0200)
includes/app_entries.inc
includes/ui/ui_view.inc
purchasing/includes/db/grn_db.inc
purchasing/includes/db/po_db.inc
purchasing/includes/po_class.inc
purchasing/includes/purchasing_db.inc
purchasing/includes/ui/grn_ui.inc
purchasing/includes/ui/po_ui.inc
purchasing/po_entry_items.php
purchasing/po_receive_items.php
purchasing/view/view_grn.php

index 1fa7bb73571db0cdf3fee54256fdd240e2658707..58cace7a9f38fe28a6bf2a3dbdcf921c939f82e4 100644 (file)
@@ -41,7 +41,7 @@ $trans_editors = array(
 
        //ST_SUPPCREDIT =>  ,
        //ST_SUPPAYMENT =>  ,
-       //ST_SUPPRECEIVE => ,
+       ST_SUPPRECEIVE => "/purchasing/po_receive_items.php?ModifyGRN=%d",
 
        //ST_WORKORDER =>  ,
        //ST_MANUISSUE =>  ,
index 59fa6888b0f256c6ff63f2cc8dd59d588c643f49..88435a71777a27edb267c7c55e3bb2ffdc798dd3 100644 (file)
@@ -1566,7 +1566,7 @@ function trans_editor_link($type, $trans_no)
                ST_SUPPCREDIT =>  "/purchasing/supplier_credit.php?ModifyCredit=%d",
 //   21=> Supplier Credit Note,
 //   22=> Supplier Payment,
-//   25=> Purchase Order Delivery,
+               ST_SUPPRECEIVE => "/purchasing/po_receive_items.php?ModifyGRN=%d",
                ST_PURCHORDER => "/purchasing/po_entry_items.php?ModifyOrderNumber=%d",
 //   28=> Work Order Issue,
 //   29=> Work Order Production",
index af51c0d8125127000364a24215c135b8eab21337..740ef18ca4713c06480b085f330781324a5ae86d 100644 (file)
@@ -78,12 +78,15 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
 }
 
 //-------------------------------------------------------------------------------------------------------------
-
-function add_grn(&$po)
+//
+//     Add or update GRN transaction.
+//
+function write_grn(&$po)
 {
        global $Refs;
 
        $date_ = $po->orig_order_date;
+       $grn = $po->grn_id;
 
        begin_transaction();
        hook_db_prewrite($po, ST_SUPPRECEIVE);
@@ -97,15 +100,23 @@ function add_grn(&$po)
        } else
                $po->ex_rate = 1;
 
-       $grn = add_grn_batch($po->order_no, $po->supplier_id, $po->reference, $po->Location, $date_, $po->ex_rate);
+       if(!$grn)
+               $grn = add_grn_batch($po->order_no, $po->supplier_id, $po->reference, $po->Location, $date_, $po->ex_rate);
+       else {
 
-    $clearing_act = get_company_pref('grn_clearing_act');
-       if ($clearing_act) {    // otherwise GRN clearing account is not used
-           $total = 0;
+               update_grn_batch($grn, $po->reference, $po->Location, $date_);
+
+               void_gl_trans(ST_SUPPRECEIVE, $grn, true);
+               void_stock_move(ST_SUPPRECEIVE, $grn);
+               void_trans_tax_details(ST_SUPPRECEIVE, $grn);
+               delete_comments(ST_SUPPRECEIVE, $grn);
        }
+
+    $clearing_act = get_company_pref('grn_clearing_act');
+    $total = 0;
        foreach ($po->line_items as $line_no => $order_line)
        {
-               if ($order_line->receive_qty != 0 && $order_line->receive_qty != "" && isset($order_line->receive_qty))
+               if ($order_line->quantity != 0)
                {
                        $stock_gl_code = get_stock_gl_code($order_line->stock_id);
 
@@ -119,7 +130,7 @@ function add_grn(&$po)
                                                $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
                                            $order_line->taxfree_charge_value($po), $po->supplier_id, '', 0, $order_line->stock_id);
                                update_average_material_cost($po->supplier_id, $order_line->stock_id, $order_line->price,
-                                       $order_line->receive_qty, $date_);
+                                       $order_line->quantity, $date_);
                        }
                        //------------------------------------------------------------------------------------------------------
                        if ($order_line->qty_received == 0)
@@ -133,16 +144,16 @@ function add_grn(&$po)
                                $order_line->item_description); 
 
                        /*Need to insert a grn item */ // also need to check for over-receive.(within allowance)
-                       if ($order_line->receive_qty + $order_line->qty_received > $order_line->quantity)
-                               $order_line->quantity = $order_line->receive_qty + $order_line->qty_received;
-                       $grn_item = add_grn_detail_item($grn, $order_line->po_detail_rec,
-                               $order_line->stock_id, $order_line->item_description,
-                               $order_line->unit_cost, $order_line->receive_qty, $order_line->price, $order_line->quantity);
+                       if ($order_line->quantity + $order_line->qty_received > $order_line->qty_ordered)
+                               $order_line->qty_ordered = $order_line->quantity + $order_line->qty_received;
+
+                       $grn_item = write_grn_item($grn, $order_line->po_detail_rec,
+                               $order_line->stock_id, $order_line->item_description, $order_line->quantity, $order_line->grn_item_id);
 
                        $po->line_items[$line_no]->grn_item_id = $grn_item;
                        /* Update location stock records - NB  a po cannot be entered for a service/kit parts done automatically */
                        add_stock_move(ST_SUPPRECEIVE, $order_line->stock_id, $grn, $po->Location, $date_, "",
-                               $order_line->receive_qty, $order_line->unit_cost, $order_line->taxfree_charge_value($po)/$order_line->receive_qty);             
+                               $order_line->quantity, $order_line->unit_cost, $order_line->taxfree_charge_value($po)/$order_line->quantity);
 
                } /*quantity received is != 0 */
        } /*end of order_line loop */
@@ -178,25 +189,42 @@ function add_grn_batch($po_number, $supplier_id, $reference, $location, $date_,
        return db_insert_id();
 }
 
+//----------------------------------------------------------------------------------------
+
+function update_grn_batch($grn, $reference, $location, $date_)
+{
+       $date = date2sql($date_);
+
+       $sql = "UPDATE ".TB_PREF."grn_batch SET 
+                       delivery_date=".db_escape($date).",
+                       reference=".db_escape($reference).", 
+                       loc_code=".db_escape($location)."
+                       WHERE id=".db_escape($grn);
+
+       db_query($sql, "A grn batch record could not be updated.");
+
+       return db_insert_id();
+}
 //-------------------------------------------------------------------------------------------------------------
 
-function add_grn_detail_item($grn_batch_id, $po_detail_item, $item_code, $description, $standard_unit_cost,
-       $quantity_received, $price, $quantity)
+function write_grn_item($grn_batch_id, $po_detail_item, $item_code, $description, $quantity_received, $grn_item_id)
 {
-       $sql = "UPDATE ".TB_PREF."purch_order_details
-        SET quantity_received = quantity_received + ".db_escape($quantity_received).",
-        std_cost_unit=".db_escape($standard_unit_cost).",
-        quantity_ordered=".db_escape($quantity).",
-        act_price=".db_escape($price)."
-        WHERE po_detail_item = ".db_escape($po_detail_item);
-       db_query($sql, "a purchase order details record could not be updated. This receipt of goods has not been processed ");
+       if ($grn_item_id)
+               $sql = "UPDATE ".TB_PREF."grn_items SET qty_recd=".db_escape($quantity_received)
+                       ." WHERE id=".db_escape($grn_item_id);
+       else
+               $sql = "INSERT INTO ".TB_PREF."grn_items (grn_batch_id, po_detail_item, item_code, description, qty_recd)
+                       VALUES (".db_escape($grn_batch_id).", "
+                       .db_escape($po_detail_item).", ".db_escape($item_code).", ".db_escape($description)
+                       .", ".db_escape($quantity_received).")";
 
-       $sql = "INSERT INTO ".TB_PREF."grn_items (grn_batch_id, po_detail_item, item_code, description, qty_recd)
-               VALUES (".db_escape($grn_batch_id).", "
-               .db_escape($po_detail_item).", ".db_escape($item_code).", ".db_escape($description)
-               .", ".db_escape($quantity_received).")";
+       db_query($sql, "A GRN detail item could not be stored.");
 
-       db_query($sql, "A GRN detail item could not be inserted.");
+       $sql = "UPDATE ".TB_PREF."purch_order_details po,
+                       (SELECT SUM(qty_recd) received FROM ".TB_PREF."grn_items grn WHERE po_detail_item=".db_escape($po_detail_item).") grn
+        SET po.quantity_received = grn.received
+        WHERE po_detail_item = ".db_escape($po_detail_item);
+       db_query($sql, "a purchase order details record could not be updated. This receipt of goods has not been processed ");
 
        return db_insert_id();
 }
@@ -261,11 +289,12 @@ function get_grn_items($grn_batch_id=0, $supplier_id="", $outstanding_only=false
        po_item.unit_price,
        po_item.act_price,
        po_item.quantity_ordered,
+       po_item.quantity_received,
        po_item.std_cost_unit, units
-       FROM    ".TB_PREF."grn_batch grn"
-                       .TB_PREF."purch_order_details po_item, "
-                       .TB_PREF."stock_master stock_item,"
-                               .TB_PREF."grn_items grn_item ";
+       FROM    ".TB_PREF."grn_batch grn"
+                               ." INNER JOIN ".TB_PREF."grn_items grn_item ON grn_item.grn_batch_id=grn.id"
+                       ." INNER JOIN ".TB_PREF."purch_order_details po_item ON grn_item.po_detail_item=po_item.po_detail_item"
+                       ." INNER JOIN ".TB_PREF."stock_master stock_item ON stock_item.stock_id=grn_item.item_code";
  
     if ($invoice_no != 0) {
        $sql .= " LEFT JOIN ".TB_PREF."supp_invoice_items invoice_item ON ";
@@ -277,8 +306,8 @@ function get_grn_items($grn_batch_id=0, $supplier_id="", $outstanding_only=false
                        $inv_cond .= " = $invoice_no";
                $sql .= "($inv_cond) AND grn_item.id=invoice_item.grn_item_id";
        }
-    $sql .= " WHERE grn_item.grn_batch_id=grn.id
-               AND grn_item.po_detail_item=po_item.po_detail_item";
+
+    $sql .= " WHERE grn_item.qty_recd != 0";
 
        if ($outstanding_only)
        $sql .= " AND (".($invoice_no ? "$inv_cond OR ":'')."grn_item.qty_recd - grn_item.quantity_inv > 0)";
@@ -332,7 +361,7 @@ function read_grn_items_to_order($grn_batch, &$order)
        if (db_num_rows($result) > 0)
        {
 
-               while ($myrow = db_fetch($result))
+               while ($myrow = db_fetch_assoc($result))
                {
                        if ($myrow['qty_recd'] == 0 && $myrow['quantity_inv'] == 0)
                                continue; // We will not have empty credited rows.
@@ -348,7 +377,7 @@ function read_grn_items_to_order($grn_batch, &$order)
                        $order->add_to_order($order->lines_on_order, $myrow["item_code"],
                                $myrow["qty_recd"],$myrow["description"], $myrow["unit_price"],$units,
                                sql2date($myrow["req_del_date"]), $myrow["quantity_inv"],
-                               $myrow["qty_recd"]);
+                               $myrow["quantity_received"], $myrow["quantity_ordered"], $myrow["id"]);
 
                        $order->line_items[$order->lines_on_order-1]->po_detail_rec = $myrow["po_detail_item"];
                } /* line po from purchase order details */
@@ -378,6 +407,7 @@ function read_grn($grn_batch, &$order)
                $order->orig_order_date = sql2date($row["delivery_date"]);
                $order->Location = $row["loc_code"];
                $order->reference = $row["reference"];
+               $order->grn_id = $grn_batch;
 
                read_grn_items_to_order($grn_batch, $order);
        }
index a904c2ff4b396cf47135975c05b31b6362a7dd61..c11e7b9b903622227cb9c47bbc2299e3db9f3c5f 100644 (file)
@@ -242,7 +242,7 @@ function read_po_items($order_no, &$order, $open_items_only=false)
             if ($order->add_to_order($order->lines_on_order, $myrow["item_code"],
                $myrow["quantity_ordered"],$myrow["description"],
                $myrow["unit_price"],$units, sql2date($myrow["delivery_date"]),
-               $myrow["qty_invoiced"], $myrow["quantity_received"])) {
+               $myrow["qty_invoiced"], $myrow["quantity_received"], $myrow["quantity_ordered"])) {
                                $order->line_items[$order->lines_on_order-1]->po_detail_rec = $myrow["po_detail_item"];
                        }
         } /* line po from purchase order details */
index d6f244dad5676b5bcda57fa57c43d2e1b1a194de..b7f15ac5307b5864aa6e3ea13590e7eea723d4ad 100644 (file)
@@ -46,6 +46,7 @@ class purch_order
        var $prepayments = array();
 
        var $fixed_asset = false;
+       var $grn_id ;   // grn batch id used in edition only
 
        function __construct()
        {
@@ -64,12 +65,12 @@ class purch_order
                $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
        }
        
-       function add_to_order($line_no, $stock_id, $qty, $item_descr, $price, $uom, $req_del_date, $qty_inv, $qty_recd)
+       function add_to_order($line_no, $stock_id, $qty, $item_descr, $price, $uom, $req_del_date, $qty_inv, $qty_recd, $qty_ordered=0, $grn_item_id=0)
        {
                if (isset($qty) && $qty != 0)
                {
                        $this->line_items[$line_no] = new po_line_details($line_no, $stock_id, $item_descr, $qty, $price, $uom, 
-                               $req_del_date, $qty_inv, $qty_recd);
+                               $req_del_date, $qty_inv, $qty_recd, $qty_ordered, $grn_item_id);
                        $this->lines_on_order++;
                        return 1;
                }
@@ -149,7 +150,7 @@ class purch_order
 
                foreach ($this->line_items as $ln_itm) {
                        $items[] = $ln_itm->stock_id;
-                       $prices[] = round($ln_itm->price * ($receival ? $ln_itm->receive_qty : $ln_itm->quantity),  user_price_dec());
+                       $prices[] = round($ln_itm->price * $ln_itm->quantity,  user_price_dec());
                }
                $taxes = get_tax_for_items($items, $prices, $shipping_cost,
                  $this->tax_group_id, $this->tax_included,  $this->tax_group_array);
@@ -208,18 +209,17 @@ class po_line_details
        var $tax_type;
        var $tax_type_name;
 
-       var $quantity;          // current/entry quantity of PO line
-       var $qty_inv;   // quantity already invoiced against this line
-       var $receive_qty;       // current/entry GRN quantity
-       var $qty_received;      // quantity already received against this line
+       var $quantity;          // this document line quantity
+       var $qty_inv;   // quantity already invoiced against this line (all PIs)
+       var $qty_received;      // quantity already received against this line (all GRNs)
+       var $qty_ordered;       // quantity on order (not used on PO entry)
 
        var $unit_cost;
        var $descr_editable;
 
        function __construct($line_no, $stock_item, $item_descr, $qty, $prc, $uom, $req_del_date, 
-               $qty_inv, $qty_recd, $grn_item_id=0)
+               $qty_inv, $qty_recd, $qty_ordered=0, $grn_item_id=0)
        {
-
                /* Constructor function to add a new LineDetail object with passed params */
                $this->line_no = $line_no;
                $this->stock_id = $stock_item;
@@ -241,9 +241,9 @@ class po_line_details
                $this->units = $item_row["units"];
                $this->qty_received = $qty_recd;
                $this->qty_inv = $qty_inv;
-               $this->receive_qty = 0; /*initialise these last two only */
                $this->unit_cost =0;
                $this->grn_item_id = $grn_item_id;
+               $this->qty_ordered = $qty_ordered;
        }
        
        // Deprecated, use with care. If you need line value as it is calculated in invoice, use taxfree_charge_value below.
@@ -258,7 +258,7 @@ class po_line_details
        //
        function taxfree_charge_value($po)
        {
-               return get_tax_free_price_for_item($this->stock_id, $this->receive_qty*$this->price,
+               return get_tax_free_price_for_item($this->stock_id, $this->quantity*$this->price,
                  $po->tax_group_id, $po->tax_included, $po->tax_group_array);
        }
 }
index 89c4d94b4d5097803047f44bd5bef820f9344985..c963fa4a221c5f557e9860b501db58116d3ad436 100644 (file)
@@ -172,9 +172,8 @@ function add_direct_supp_trans($cart)
                $cart->reference = $ref;
        if ($cart->trans_type != ST_SUPPINVOICE)
                $cart->Comments = $cart->reference; //grn does not hold supp_ref
-       foreach($cart->line_items as $key => $line)
-               $cart->line_items[$key]->receive_qty = $line->quantity;
-       $grn_no = add_grn($cart);
+
+       $grn_no = write_grn($cart);
        if ($cart->trans_type == ST_SUPPRECEIVE) {
                commit_transaction(); // save PO+GRN
                return $grn_no;
@@ -196,9 +195,9 @@ function add_direct_supp_trans($cart)
        $total = 0;
                foreach($cart->line_items as $key => $line) {
                $inv->add_grn_to_trans($line->grn_item_id, $line->po_detail_rec, $line->stock_id,
-                       $line->item_description, $line->receive_qty, 0, $line->receive_qty,
+                       $line->item_description, $line->quantity, 0, $line->quantity,
                        $line->price, $line->price, true, get_unit_cost($line->stock_id), '');
-               $total += round2(($line->receive_qty * $line->price), user_price_dec());
+               $total += round2(($line->quantity * $line->price), user_price_dec());
        }
        $inv->tax_overrides = $cart->tax_overrides;
        if (!$inv->tax_included) {
index 1112ce077f1e0cadcd72a137be1826c5f30bf25f..8fb7568a523c43f2fe6b73100f56ef8b4e273d6b 100644 (file)
@@ -60,9 +60,8 @@ function edit_grn_summary(&$po)
 
        table_section(2);
        if (!isset($_POST['ref']))
-           $_POST['ref'] = $Refs->get_next(ST_SUPPRECEIVE, null, array('supplier' => $po->supplier_id, 'date' => Today()));
-       ref_row(_("Reference"), 'ref', '', null);
-
+                   $_POST['ref'] = $Refs->get_next(ST_SUPPRECEIVE, null, array('supplier' => $po->supplier_id, 'date' => Today()));
+               ref_row(_("Reference"), 'ref', '', null);
         if (!isset($_POST['Location']))
                $_POST['Location'] = $po->Location;
 
index b71359576c1f5b06759d925a69c22e7489bff8cc..88385649307122e43961df37c0f61557ed92497c 100644 (file)
@@ -64,7 +64,7 @@ function copy_to_cart()
 
 //---------------------------------------------------------------------------------------------------
 
-function create_new_po($trans_type, $trans_no)
+function create_cart($trans_type, $trans_no)
 {
        global $Refs;
 
@@ -72,20 +72,28 @@ function create_new_po($trans_type, $trans_no)
                unset ($_SESSION['PO']->line_items, $_SESSION['PO']);
 
        $cart = new purch_order;
-       $_POST['OrderDate'] = new_doc_date();
-       if (!is_date_in_fiscalyear($_POST['OrderDate']))
-               $_POST['OrderDate'] = end_fiscalyear();
-       $cart->due_date = $cart->orig_order_date = $_POST['OrderDate'];
-
        $cart->trans_type = $trans_type;
-       $cart->order_no = $trans_no;
-       /*read in all the selected order into the Items cart  */
-       if ($trans_no) {
-               read_po($trans_no, $cart);
+
+       if ($trans_type == ST_SUPPRECEIVE && $trans_no != 0) {
+               read_grn($trans_no, $cart);
+               foreach($cart->line_items as &$line) {
+                       $line->qty_old = $line->quantity; // for concurrency checks
+                       $line->qty_received -= $line->quantity;
+               }
+       } else {
                $cart->order_no = $trans_no;
-       } else
-               $cart->reference = $Refs->get_next($trans_type, null,
-                       array('supplier_id' => $cart->supplier_id, 'date' => get_post('OrderDate')));
+               $_POST['OrderDate'] = new_doc_date();
+               if (!is_date_in_fiscalyear($_POST['OrderDate']))
+                       $_POST['OrderDate'] = end_fiscalyear();
+               $cart->due_date = $cart->orig_order_date = $_POST['OrderDate'];
+               /* read in all the selected order into the Items cart  */
+               if ($trans_no) {
+                       read_po($trans_no, $cart);
+                       $cart->order_no = $trans_no;
+               } else
+                       $cart->reference = $Refs->get_next($trans_type, null,
+                               array('supplier_id' => $cart->supplier_id, 'date' => get_post('OrderDate')));
+       }
        $_SESSION['PO'] = &$cart;
 }
 
index 22f383819b0f0ab0980f6a23b11fa808df88032d..b562e7cfa9e77cbdc725804e76740bafc026cdc0 100644 (file)
@@ -39,21 +39,21 @@ if (user_use_date_picker())
 if (isset($_GET['ModifyOrderNumber']) && is_numeric($_GET['ModifyOrderNumber'])) {
 
        $_SESSION['page_title'] = _($help_context = "Modify Purchase Order #") . $_GET['ModifyOrderNumber'];
-       create_new_po(ST_PURCHORDER, $_GET['ModifyOrderNumber']);
+       create_cart(ST_PURCHORDER, $_GET['ModifyOrderNumber']);
        copy_from_cart();
 } elseif (isset($_GET['NewOrder'])) {
 
        $_SESSION['page_title'] = _($help_context = "Purchase Order Entry");
-       create_new_po(ST_PURCHORDER, 0);
+       create_cart(ST_PURCHORDER, 0);
        copy_from_cart();
 } elseif (isset($_GET['NewGRN'])) {
 
        $_SESSION['page_title'] = _($help_context = "Direct GRN Entry");
-       create_new_po(ST_SUPPRECEIVE, 0);
+       create_cart(ST_SUPPRECEIVE, 0);
        copy_from_cart();
 } elseif (isset($_GET['NewInvoice'])) {
 
-       create_new_po(ST_SUPPINVOICE, 0);
+       create_cart(ST_SUPPINVOICE, 0);
        copy_from_cart();
 
        if (isset($_GET['FixedAsset'])) {
index fcd7348b4e9b2fd72f3fa5153db533044c48892d..2850496dca12f55cadc6c2fe25ae69ed52beeca6 100644 (file)
@@ -22,13 +22,35 @@ if ($SysPrefs->use_popup_windows)
        $js .= get_js_open_window(900, 500);
 if (user_use_date_picker())
        $js .= get_js_date_picker();
-page(_($help_context = "Receive Purchase Order Items"), false, false, "", $js);
+
+//--------------------------------------------------------------------------------------------------
+
+if (isset($_GET['PONumber']) && $_GET['PONumber'] > 0 && !isset($_POST['Update']))
+{
+       $_SESSION['page_title'] = _($help_context = "Receive Purchase Order Items");
+       create_cart(ST_PURCHORDER, $_GET['PONumber']);
+       $_SESSION['PO']->trans_type = ST_SUPPRECEIVE;
+       $_SESSION['PO']->reference = $Refs->get_next(ST_SUPPRECEIVE, 
+               array('date' => Today(), 'supplier' => $_SESSION['PO']->supplier_id));
+       foreach ($_SESSION['PO']->line_items as $line)
+               $line->quantity = $line->qty_ordered - $line->qty_received;
+       copy_from_cart();
+}
+elseif (isset($_GET['ModifyGRN']) && $_GET['ModifyGRN'] > 0 && !isset($_POST['Update']))
+{
+       $_SESSION['page_title'] = _($help_context = "Modify GRN #") . $_GET['ModifyGRN'];
+       create_cart(ST_SUPPRECEIVE, $_GET['ModifyGRN']);
+       copy_from_cart();
+}
+
+page($_SESSION['page_title'], false, false, "", $js);
 
 //---------------------------------------------------------------------------------------------------------------
 
-if (isset($_GET['AddedID']))
+if (isset($_GET['AddedID']) || isset($_GET['UpdatedID']))
 {
-       $grn = $_GET['AddedID'];
+       $new = isset($_GET['AddedID']);
+       $grn = $_GET[$new ? 'AddedID' : 'UpdatedID'];
        $trans_type = ST_SUPPRECEIVE;
 
        display_notification_centered(_("Purchase Order Delivery has been processed"));
@@ -41,16 +63,19 @@ if (isset($_GET['AddedID']))
 
        hyperlink_params("$path_to_root/purchasing/supplier_invoice.php", _("Entry purchase &invoice for this receival"), "New=1");
 
-       hyperlink_no_params("$path_to_root/purchasing/inquiry/po_search.php", _("Select a different &purchase order for receiving items against"));
+       if ($new)
+               hyperlink_no_params("$path_to_root/purchasing/inquiry/po_search.php", _("Select another &purchase order for receiving items against"));
+       else
+               hyperlink_no_params("$path_to_root/purchasing/inquiry/supplier_inquiry.php", _("Select another &document for edition"));
 
        display_footer_exit();
 }
 
 //--------------------------------------------------------------------------------------------------
 
-if ((!isset($_GET['PONumber']) || $_GET['PONumber'] == 0) && !isset($_SESSION['PO']))
+if ((!isset($_GET['PONumber']) || $_GET['PONumber'] == 0) && (!isset($_GET['ModifyGRN']) || $_GET['ModifyGRN'] == 0) && !isset($_SESSION['PO']))
 {
-       die (_("This page can only be opened if a purchase order has been selected. Please select a purchase order first."));
+       die (_("This page can only be opened with valid Purchase Order or GRN number."));
 }
 
 //--------------------------------------------------------------------------------------------------
@@ -59,47 +84,55 @@ function display_po_receive_items()
 {
        div_start('grn_items');
     start_table(TABLESTYLE, "colspan=7 width='90%'");
-    $th = array(_("Item Code"), _("Description"), _("Ordered"), _("Units"), _("Received"),
-       _("Outstanding"), _("This Delivery"), _("Price"), _("Total"));
+    $mod_grn = $_SESSION['PO']->grn_id;
+
+       if ($mod_grn) // modify GRN
+           $th = array(_("Item Code"), _("Description"), _("Ordered"), _("Units"), _("Received"), _("Invoiced"), _("This Delivery"), _("Price"), _("Total"));
+    else
+           $th = array(_("Item Code"), _("Description"), _("Ordered"), _("Units"), _("Received"), _("Outstanding"), _("This Delivery"), _("Price"), _("Total"));
     table_header($th);
 
-    /*show the line items on the order with the quantity being received for modification */
+    /* show the line items on the order with the quantity being received for modification */
 
     $total = 0;
     $k = 0; //row colour counter
 
-    if (count($_SESSION['PO']->line_items)> 0 )
+    if (count($_SESSION['PO']->line_items) > 0 )
     {
                foreach ($_SESSION['PO']->line_items as $ln_itm)
                {
 
                        alt_table_row_color($k);
 
-               $qty_outstanding = $ln_itm->quantity - $ln_itm->qty_received;
+               $qty_outstanding = $ln_itm->qty_ordered - $ln_itm->qty_received;
 
-                       if (!isset($_POST['Update']) && !isset($_POST['ProcessGoodsReceived']) && $ln_itm->receive_qty == 0)
+                       if (!isset($_POST['Update']) && !isset($_POST['ProcessGoodsReceived']) && $ln_itm->quantity == 0)
                {   //If no quantites yet input default the balance to be received
-               $ln_itm->receive_qty = $qty_outstanding;
+               $ln_itm->quantity = $qty_outstanding;
                }
 
-               $line_total = ($ln_itm->receive_qty * $ln_itm->price);
+               $line_total = ($ln_itm->quantity * $ln_itm->price);
                $total += $line_total;
 
                        label_cell($ln_itm->stock_id);
-                       if ($qty_outstanding > 0)
+                       if ($mod_grn || $qty_outstanding > 0)
                                text_cells(null, $ln_itm->stock_id . "Desc", $ln_itm->item_description, 30, 50);
                        else
                                label_cell($ln_itm->item_description);
+
                        $dec = get_qty_dec($ln_itm->stock_id);
-                       qty_cell($ln_itm->quantity, false, $dec);
+                       qty_cell($ln_itm->qty_ordered, false, $dec);
                        label_cell($ln_itm->units);
                        qty_cell($ln_itm->qty_received, false, $dec);
-                       qty_cell($qty_outstanding, false, $dec);
+                       if ($mod_grn)
+                               qty_cell($ln_itm->qty_inv, false, $dec);
+                       else
+                               qty_cell($qty_outstanding, false, $dec);
 
-                       if ($qty_outstanding > 0)
-                               qty_cells(null, $ln_itm->line_no, number_format2($ln_itm->receive_qty, $dec), "align=right", null, $dec);
+                       if ($mod_grn || $qty_outstanding > 0)
+                               qty_cells(null, $ln_itm->line_no, number_format2($ln_itm->quantity, $dec), "align=right", null, $dec);
                        else
-                               label_cell(number_format2($ln_itm->receive_qty, $dec), "align=right");
+                               label_cell(number_format2($ln_itm->quantity, $dec), "align=right");
 
                        amount_decimal_cell($ln_itm->price);
                        amount_cell($line_total);
@@ -140,13 +173,13 @@ function check_po_changed()
        {
                $ln_item = $_SESSION['PO']->line_items[$line_no];
                // only compare against items that are outstanding
-               $qty_outstanding = $ln_item->quantity - $ln_item->qty_received;
+               $qty_outstanding = $ln_item->qty_ordered - $ln_item->qty_received;
                if ($qty_outstanding > 0)
                {
                if ($ln_item->qty_inv != $myrow["qty_invoiced"] ||
                        $ln_item->stock_id != $myrow["item_code"] ||
-                       $ln_item->quantity != $myrow["quantity_ordered"] ||
-                       $ln_item->qty_received != $myrow["quantity_received"])
+                       $ln_item->qty_ordered != $myrow["quantity_ordered"] ||
+                       $ln_item->qty_received + @$ln_item->qty_old != $myrow["quantity_received"])
                {
                        return true;
                }
@@ -162,7 +195,7 @@ function check_po_changed()
 function can_process()
 {
        global $SysPrefs;
-       
+
        if (count($_SESSION['PO']->line_items) <= 0)
        {
         display_error(_("There is nothing to process. Please enter valid quantities greater than zero."));
@@ -181,7 +214,7 @@ function can_process()
                return false;
        }
 
-       if (!check_reference($_POST['ref'], ST_SUPPRECEIVE))
+       if (!check_reference($_POST['ref'], ST_SUPPRECEIVE, $_SESSION['PO']->grn_id))
        {
                set_focus('ref');
                return false;
@@ -190,7 +223,7 @@ function can_process()
        $something_received = 0;
        foreach ($_SESSION['PO']->line_items as $order_line)
        {
-               if ($order_line->receive_qty > 0)
+               if ($order_line->quantity > 0)
                {
                        $something_received = 1;
                        break;
@@ -201,8 +234,8 @@ function can_process()
     $delivery_qty_too_large = 0;
        foreach ($_SESSION['PO']->line_items as $order_line)
        {
-               if ($order_line->receive_qty+$order_line->qty_received >
-                       $order_line->quantity * (1+ ($SysPrefs->over_receive_allowance() / 100)))
+               if ($order_line->quantity+$order_line->qty_received >
+                       $order_line->qty_ordered * (1+ ($SysPrefs->over_receive_allowance() / 100)))
                {
                        $delivery_qty_too_large = 1;
                        break;
@@ -241,9 +274,14 @@ function process_receive_po()
                hyperlink_no_params("$path_to_root/purchasing/inquiry/po_search.php",
                 _("Select a different purchase order for receiving goods against"));
 
-               hyperlink_params("$path_to_root/purchasing/po_receive_items.php", 
-                        _("Re-Read the updated purchase order for receiving goods against"),
-                        "PONumber=" . $_SESSION['PO']->order_no);
+               if (!$_SESSION['PO']->grn_id)
+                       hyperlink_params("$path_to_root/purchasing/po_receive_items.php", 
+                                _("Re-Read the updated purchase order for receiving goods against"),
+                                "PONumber=" . $_SESSION['PO']->order_no);
+               else
+                       hyperlink_params("$path_to_root/purchasing/po_receive_items.php", 
+                                _("Re-Read the GRN for reedition"),
+                                "ModifyGRN=" . $_SESSION['PO']->grn_id);
 
                unset($_SESSION['PO']->line_items);
                unset($_SESSION['PO']);
@@ -258,36 +296,28 @@ function process_receive_po()
        $grn->Location = $_POST['Location'];
        $grn->ex_rate = input_num('_ex_rate', null);
 
-       $grn_no = add_grn($grn);
+       $trans_no = write_grn($grn);
+       $new = $grn->grn_id == 0;
 
        new_doc_date($_POST['DefaultReceivedDate']);
        unset($_SESSION['PO']->line_items);
        unset($_SESSION['PO']);
 
-       meta_forward($_SERVER['PHP_SELF'], "AddedID=$grn_no");
-}
-
-//--------------------------------------------------------------------------------------------------
-
-if (isset($_GET['PONumber']) && $_GET['PONumber'] > 0 && !isset($_POST['Update']))
-{
-       create_new_po(ST_PURCHORDER, $_GET['PONumber']);
-       $_SESSION['PO']->trans_type = ST_SUPPRECEIVE;
-       $_SESSION['PO']->reference = $Refs->get_next(ST_SUPPRECEIVE, 
-               array('date' => Today(), 'supplier' => $_SESSION['PO']->supplier_id));
-       copy_from_cart();
+       if ($new)
+               meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
+       else
+               meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
 }
 
 //--------------------------------------------------------------------------------------------------
 
 if (isset($_POST['Update']) || isset($_POST['ProcessGoodsReceived']))
 {
-
        /* if update quantities button is hit page has been called and ${$line->line_no} would have be
        set from the post to the quantity to be received in this receival*/
        foreach ($_SESSION['PO']->line_items as $line)
        {
-        if( ($line->quantity - $line->qty_received)>0) {
+        if( ($line->qty_ordered - $line->qty_received) > 0) {
                $_POST[$line->line_no] = max($_POST[$line->line_no], 0);
                if (!check_num($line->line_no))
                        $_POST[$line->line_no] = number_format2(0, get_qty_dec($line->stock_id));
@@ -295,7 +325,7 @@ if (isset($_POST['Update']) || isset($_POST['ProcessGoodsReceived']))
                if (!isset($_POST['DefaultReceivedDate']) || $_POST['DefaultReceivedDate'] == "")
                        $_POST['DefaultReceivedDate'] = new_doc_date();
 
-               $_SESSION['PO']->line_items[$line->line_no]->receive_qty = input_num($line->line_no);
+               $_SESSION['PO']->line_items[$line->line_no]->quantity = input_num($line->line_no);
 
                if (isset($_POST[$line->stock_id . "Desc"]) && strlen($_POST[$line->stock_id . "Desc"]) > 0)
                {
index b2d95bb8e26390226249069d414976456536ef51..b5159b639ba4eaf6700d2b1eaa405785bf624a28 100644 (file)
@@ -48,8 +48,7 @@ $overdue_items = false;
 
 foreach ($purchase_order->line_items as $stock_item)
 {
-
-       $line_total = $stock_item->qty_received * $stock_item->price;
+       $line_total = $stock_item->quantity * $stock_item->price;
 
        // if overdue and outstanding quantities, then highlight as so
        if (date1_greater_date2($purchase_order->orig_order_date, $stock_item->req_del_date))
@@ -66,7 +65,7 @@ foreach ($purchase_order->line_items as $stock_item)
        label_cell($stock_item->item_description);
        label_cell($stock_item->req_del_date, "nowrap align=right");
        $dec = get_qty_dec($stock_item->stock_id);
-       qty_cell($stock_item->qty_received, false, $dec);
+       qty_cell($stock_item->quantity, false, $dec);
        label_cell($stock_item->units);
        amount_decimal_cell($stock_item->price);
        amount_cell($line_total);