Cleanups and changes in purchasing classes.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 13 Jul 2019 10:08:38 +0000 (12:08 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 19 Aug 2019 10:41:58 +0000 (12:41 +0200)
purchasing/includes/db/grn_db.inc
purchasing/includes/db/po_db.inc
purchasing/includes/po_class.inc
purchasing/includes/purchasing_db.inc
purchasing/includes/supp_trans_class.inc
purchasing/includes/ui/invoice_ui.inc
purchasing/po_entry_items.php
purchasing/po_receive_items.php
taxes/tax_calc.inc

index 948f2c0664617efa60803ea722e8c9937b95c9e6..26b5bc7e58a35afdfea7b6ff166021c6926a540c 100644 (file)
@@ -148,7 +148,7 @@ function write_grn(&$po)
                        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,
+                       $grn_item = write_grn_item($grn, $order_line->po_item_id,
                                $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;
@@ -363,24 +363,15 @@ function read_grn_items_to_order($grn_batch, &$order)
        {
 
                while ($myrow = db_fetch_assoc($result))
-               {
+               {
                        if ($myrow['qty_recd'] == 0 && $myrow['quantity_inv'] == 0)
                                continue; // We will not have empty credited rows.
-                       if (is_null($myrow["units"]))
-                       {
-                               $units = "";
-                       }
-                       else
-                       {
-                               $units = $myrow["units"];
-                       }
-
-                       $order->add_to_order($order->lines_on_order, $myrow["item_code"],
-                               $myrow["qty_recd"],$myrow["description"], $myrow["unit_price"],$units,
+
+                       $order->add_to_order($myrow["item_code"],
+                               $myrow["qty_recd"],$myrow["description"], $myrow["unit_price"],
                                sql2date($myrow["req_del_date"]), $myrow["quantity_inv"],
-                               $myrow["quantity_received"], $myrow["quantity_ordered"], $myrow["id"]);
+                               $myrow["quantity_received"], $myrow["quantity_ordered"], $myrow["id"], $myrow["po_detail_item"]);
 
-                       $order->line_items[$order->lines_on_order-1]->po_detail_rec = $myrow["po_detail_item"];
                } /* line po from purchase order details */
        } //end of checks on returned data set
 }
index eb26570f2cc4674c1652f6b40ff8db2e0b6ac806..f48621fad266b85dd176d30a2321eafbbf4a9145 100644 (file)
@@ -99,7 +99,7 @@ function add_po(&$po_obj)
                db_escape($po_line->price) . ", " .
                db_escape($po_line->quantity). ")";
                db_query($sql, "One of the purchase order detail records could not be inserted");
-               $po_obj->line_items[$line_no]->po_detail_rec = db_insert_id();
+               $po_obj->line_items[$line_no]->po_item_id = db_insert_id();
      }
 
        $Refs->save(ST_PURCHORDER, $po_obj->order_no, $po_obj->reference);
@@ -139,7 +139,7 @@ function update_po(&$po_obj)
     {
        $sql = "INSERT INTO ".TB_PREF."purch_order_details (po_detail_item, order_no, item_code, 
                description, delivery_date, unit_price, quantity_ordered, quantity_received) VALUES ("
-                       .db_escape($po_line->po_detail_rec ? $po_line->po_detail_rec : 0). ","
+                       .db_escape($po_line->po_item_id ? $po_line->po_item_id : 0). ","
                        .$po_obj->order_no . ","
                        .db_escape($po_line->stock_id). ","
                        .db_escape($po_line->item_description). ",'"
@@ -213,7 +213,8 @@ function read_po_items($order_no, &$order, $open_items_only=false)
        $sql = "SELECT poline.*, units
                FROM ".TB_PREF."purch_order_details poline
                        LEFT JOIN ".TB_PREF."stock_master item  ON poline.item_code=item.stock_id
-               WHERE order_no =".db_escape($order_no);
+               WHERE order_no =".db_escape($order_no)
+                       ." AND poline.quantity_ordered!=0";
 
     if ($open_items_only)
                $sql .= " AND (poline.quantity_ordered > poline.quantity_received) ";
@@ -232,21 +233,12 @@ function read_po_items($order_no, &$order, $open_items_only=false)
                        if ($data['supplier_description'] != "")
                                $myrow['description'] = $data['supplier_description'];
                }               
-            if (is_null($myrow["units"]))
-            {
-                       $units = "";
-            }
-            else
-            {
-                $units = $myrow["units"];
-            }
-
-            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["quantity_ordered"])) {
-                               $order->line_items[$order->lines_on_order-1]->po_detail_rec = $myrow["po_detail_item"];
-                       }
+
+               $order->add_to_order($myrow["item_code"],
+                       $myrow["quantity_ordered"],$myrow["description"],
+               $myrow["unit_price"], sql2date($myrow["delivery_date"]),
+                       $myrow["qty_invoiced"], $myrow["quantity_received"],
+                       $myrow["quantity_ordered"], 0, $myrow["po_detail_item"]);
         } /* line po from purchase order details */
     } //end of checks on returned data set
 }
index 0053b0b1578cc44cb16d118ee9043ea6baa985a5..54ae516474d7424bda75467470b447346bf54f5b 100644 (file)
@@ -9,12 +9,13 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
-/* Definition of the purch_order class to hold all the information for a purchase order and delivery
+/*
+       This class serves as cart for PO or GRN.
 */
 
 class purch_order 
 {
-
+       // db interface
        var $trans_type; // order/grn/invoice (direct)
        var $supplier_id;
        var $Comments;
@@ -28,16 +29,18 @@ class purch_order
        var $alloc; // sum of payments allocated
        var $tax_included; // type of prices
 
+       var $due_date;          // for grn this is delivery date
+       var $order_no;          // for order modification, grn batch
+       var $ex_rate;           // for grn
+
        var $line_items;
+       //----
+
        var $curr_code;
        var $supplier_name;
-       var $due_date;
-       var $order_no; /*Only used for modification of existing orders otherwise only established when order committed */
-       var $lines_on_order = 0;
        var $credit;
        var $tax_group_id;
        var $terms;
-       var $ex_rate;
        var $cash_account;
        var $dimension,
                $dimension2;
@@ -47,12 +50,12 @@ class purch_order
        var $prepayments = array();
 
        var $fixed_asset = false;
-       var $grn_id ;   // grn batch id used in edition only
+       var $grn_id   // grn batch id used in edition only
 
        function __construct()
        {
                $this->line_items = array();
-               $this->lines_on_order = $this->order_no = $this->supplier_id = 0;
+               $this->order_no = $this->supplier_id = 0;
                $this->tax_group_id = find_domestic_tax_group(); // prevent tax errors until supplier is selected
        }
        
@@ -66,16 +69,10 @@ class purch_order
                $this->tax_area = $tax_area;
        }
        
-       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)
+       function add_to_order($stock_id, $qty, $item_descr, $price, $req_del_date, $qty_inv, $qty_recd, $qty_ordered=0, $grn_item_id=0, $po_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, $qty_ordered, $grn_item_id);
-                       $this->lines_on_order++;
-                       return 1;
-               }
-               return 0;
+               $this->line_items[] = new po_line_details($stock_id, $item_descr, $qty, $price,
+                       $req_del_date, $qty_inv, $qty_recd, $qty_ordered, $grn_item_id, $po_item_id);
        }
 
        function update_order_item($line_no, $qty, $price, $req_del_date, $description="")
@@ -102,8 +99,6 @@ class purch_order
        {
        unset($this->line_items);
                $this->line_items = array();
-               
-               $this->lines_on_order = 0;  
                $this->order_no = 0;
        }
 
@@ -232,30 +227,28 @@ class purch_order
 
 class po_line_details 
 {
-
-       var $line_no;
-       var $po_detail_rec;
+       //db interface
+       var $po_item_id;
        var $grn_item_id;
        var $stock_id;
        var $item_description;
        var $price;
        var $units;
        var $req_del_date;
-
-       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 $quantity;          // this document line quantity
+       //---
+
        var $descr_editable;
        var $vat_category;
+       var $gl_amounts;        // splited line value (after call to split_line_value method)
 
-       function __construct($line_no, $stock_item, $item_descr, $qty, $prc, $uom, $req_del_date, 
-               $qty_inv, $qty_recd, $qty_ordered=0, $grn_item_id=0)
+       function __construct($stock_item, $item_descr, $qty, $prc, $req_del_date, 
+               $qty_inv, $qty_recd, $qty_ordered=0, $grn_item_id=0, $po_item_id=0)
        {
-               /* Constructor function to add a new LineDetail object with passed params */
-               $this->line_no = $line_no;
                $this->stock_id = $stock_item;
                $item_row = get_item($stock_item);
                if (!$item_row) 
@@ -269,7 +262,6 @@ class po_line_details
                $this->quantity = $qty;
                $this->req_del_date = $req_del_date;
                $this->price = $prc;
-//             $this->units = $uom;
                $this->units = $item_row["units"];
                $this->qty_received = $qty_recd;
                $this->qty_inv = $qty_inv;
@@ -277,6 +269,7 @@ class po_line_details
                $this->grn_item_id = $grn_item_id;
                $this->vat_category = $item_row["vat_category"];
                $this->qty_ordered = $qty_ordered;
+               $this->po_item_id = $po_item_id;
        }
        
        //
index cffd2210d5320bf9d71bd1ebe0d79e7d4c4ffe60..53d73dbc2f7a69b3de9581655db520d57022f531 100644 (file)
@@ -191,14 +191,14 @@ function add_direct_supp_trans($cart)
        $inv->ov_amount = $inv->ov_gst = $inv->ov_discount = 0;
        $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,
+               $inv->add_grn_to_trans($line->grn_item_id, $line->po_item_id, $line->stock_id,
                        $line->item_description, $line->quantity, 0, $line->quantity,
                        $line->price, $line->price, true, get_unit_cost($line->stock_id), '');
                $total += round2(($line->quantity * $line->price), user_price_dec());
        }
        $inv->tax_overrides = $cart->tax_overrides;
        if (!$inv->tax_included) {
-               $taxes = $inv->get_taxes($inv->tax_group_id, 0, false);
+               $taxes = $inv->get_taxes();
                foreach( $taxes as $taxitem) {
                        $total += isset($taxitem['Override']) ? $taxitem['Override'] : $taxitem['Value'];
                }
index 107890df0692debc201f6da032e2a596cfd614e9..31b00b8a42bb65f9b0845771f8e6297a3fc8936d 100644 (file)
@@ -16,31 +16,34 @@ include_once($path_to_root . "/taxes/tax_calc.inc");
 
 class supp_trans 
 {
+       //db interface
+       var $trans_no;
+       var $trans_type;        // invoice or credit
+       var $supplier_id;
+       var $reference;
+       var $supp_reference;
+       var $tran_date;
+       var $due_date;
+       var $ov_amount;
+       var $ov_discount;
+       var $ov_gst;
+       var $ex_rate;
+
+       var $tax_included;
 
        var $grn_items; /*array of objects of class grn_item using the id as the pointer */
        var $gl_codes; /*array of objects of class gl_codes using a counter as the pointer */
-       var $supplier_id;
+       var $Comments;
+       //---
+
        var $supplier_name;
        var $terms;
-       var $ex_rate;
        
        var $tax_description;
        var $tax_group_id;
-       var $tax_included;
-       
-       var $trans_type;        // invoice or credit
-       var $trans_no;
 
-       var $Comments;
-       var $tran_date;
-       var $due_date;
        var $src_docs = array();                // source invoice for this credit note (if any)
 
-       var $supp_reference;
-       var $reference;
-       var $ov_amount;
-       var $ov_discount;
-       var $ov_gst;
        var $credit = 0;
        var $currency;
        var $tax_overrides = array();           // array of taxes manually inserted during sales invoice entry
@@ -63,10 +66,10 @@ class supp_trans
                $this->gl_codes = array();
                if ($trans_no) {
                        read_supp_invoice($trans_no, $trans_type, $this);
+
                        if ($trans_type == ST_SUPPCREDIT)
-                       {
                                $this->src_docs = find_src_invoices($trans_no);
-                       }
+
                        read_supplier_details_to_trans($this, $this->supplier_id);
                }
        }
@@ -119,27 +122,18 @@ class supp_trans
                $this->gl_codes = array();
        }
 
-    function get_taxes($tax_group_id=null, $shipping_cost=0, $gl_codes=true)
+    function get_taxes($gl_codes=true)
     {
        $items = array();
        $prices = array();
 
-        if ($tax_group_id == null)
-               $tax_group_id = $this->tax_group_id;
-
-               // preload the taxgroup
-               $tax_group = get_tax_group_items_as_array($tax_group_id);
-
         foreach ($this->grn_items as $ln_itm) 
         {
                $items[] = $ln_itm->item_code;
-               $prices[] =round( ($ln_itm->this_quantity_inv * $ln_itm->chg_price),
-                               user_price_dec());
+               $prices[] = round( $ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
         }
 
-        if ($tax_group_id == null)
-               $tax_group_id = $this->tax_group_id;
-        $taxes = get_tax_for_items($this->trans_type, $items, $prices, $shipping_cost, $tax_group_id, 
+        $taxes = get_tax_for_items($this->trans_type, $items, $prices, 0, $this->tax_group_id, 
                $this->tax_included);
 
                if (isset($this->tax_overrides))
@@ -149,7 +143,7 @@ class supp_trans
                        }
 
                // Taxes included in gl_codes table have exact value, but count to overrides as well.
-               // Therefore when we want to now taxes only for items (gl_codes==false),
+               // Therefore when we want to know taxes only for items (gl_codes==false),
                // we have to subtract gl_taxes from override values.
                foreach ($this->gl_codes as $gl_code)
                {
@@ -162,7 +156,6 @@ class supp_trans
                                        $taxes[$index]['Override'] -= $gl_code->amount;
                        }
                }
-
         return $taxes;
     }
        //
@@ -172,14 +165,8 @@ class supp_trans
     {
        $total = 0;
 
-               // preload the taxgroup !
-               if ($tax_group_id != null)
-                       $tax_group = get_tax_group_items_as_array($tax_group_id);
-               else
-                       $tax_group = null;
-
                foreach ($this->grn_items as $ln_itm)
-               $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $tax_group)),
+               $total += round(($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $this->trans_type)),
                         user_price_dec());
 
                foreach ($this->gl_codes as $gl_line)
@@ -189,6 +176,7 @@ class supp_trans
                }
                return $total;
     }
+
        //
        //      Returns transaction total 
        //
@@ -207,6 +195,10 @@ class supp_trans
                return $total;
        }
 
+       /*
+               Split line value to cost and taxes.
+               Stores calculated amounts in $line->gl_amounts array.
+       */
        function split_line_values()
        {
                // split nominal line values
@@ -248,16 +240,19 @@ class grn_item
 all the info to do the necessary entries without looking up ie additional queries of the database again */
 
        var $id;
+
+       var $gl_code;
+
        var $po_detail_item;
        var $item_code;
        var $item_description;
+       var $this_quantity_inv;
+       var $chg_price;
+
        var $qty_recd;
        var $prev_quantity_inv;
-       var $this_quantity_inv;
        var $order_price;
-       var $chg_price;
        var $std_cost_unit;
-       var $gl_code;
        var $tax_included;
        var $gl_amounts;        // splited line value (after call to split_line_value method
        var $vat_category;
@@ -274,9 +269,9 @@ all the info to do the necessary entries without looking up ie additional querie
                $this->qty_recd = $qty_recd;
                $this->prev_quantity_inv = $prev_quantity_inv;
                $this->this_quantity_inv = $this_quantity_inv;
-               $this->order_price =$order_price;
+               $this->order_price = $order_price;              // price on order
                $this->chg_price = $chg_price;
-               $this->std_cost_unit = $std_cost_unit;
+               $this->std_cost_unit = $std_cost_unit;  // provisional cost for clearing
                $this->gl_code = $gl_code;
                $this->tax_included = $tax_included;
 
@@ -284,11 +279,11 @@ all the info to do the necessary entries without looking up ie additional querie
                $this->vat_category = $opts['vat_category'];
        }
 
-       function full_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
-       {
-               return get_full_price_for_item($trans_type, $this->item_code, 
-                 $this->chg_price, $tax_group_id, $this->tax_included);
-       }
+//     function full_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
+//     {
+//             return get_full_price_for_item($trans_type, $this->item_code, 
+//               $this->chg_price, $tax_group_id, $this->tax_included);
+//     }
 
        function taxfree_charge_price($tax_group_id, $trans_type=ST_PURCHORDER)
        {
@@ -296,12 +291,6 @@ all the info to do the necessary entries without looking up ie additional querie
                  $tax_group_id, $this->tax_included);
        }
 
-       function taxfree_value($tax_group_id, $trans_type=ST_PURCHORDER)
-       {
-               return get_tax_free_price_for_item($trans_type, $this->item_code, $this->this_quantity_inv * $this->chg_price, 
-                 $tax_group_id, $this->tax_included, $tax_group);
-       }
-
        /*
                Splits item value to parts posted to GL.
        */
index 7dafc8a9f14c8710750aa9f774f67848caa18211..8299aa0e1ab7a2191db506816bb7c3dd2f507f23 100644 (file)
@@ -174,7 +174,7 @@ function invoice_totals(&$supp_trans)
        start_table(TABLESTYLE, "width='95%'");
        label_row(_("Sub-total:"), price_format( $supp_trans->ov_amount), "colspan=$colspan align=right", "align=right");
 
-    $taxes = $supp_trans->get_taxes($supp_trans->tax_group_id);
+    $taxes = $supp_trans->get_taxes();
     $tax_total = display_edit_tax_items($taxes, $colspan, $supp_trans->tax_included, 0, true);
 
     $display_total = price_format($supp_trans->ov_amount + $tax_total);
index 0a79185514aaf447e884076caa84fc0204b12cd5..2207fa74d232750e23fa033e686ee146c8b69ecb 100644 (file)
@@ -311,9 +311,8 @@ function handle_add_new_item()
 
                        if ($allow_update)
                        {
-                               $_SESSION['PO']->add_to_order (count($_SESSION['PO']->line_items), $_POST['stock_id'], input_num('qty'), 
-                                       get_post('stock_id_text'), //$myrow["description"], 
-                                       input_num('price'), '', // $myrow["units"], (retrived in cart)
+                               $_SESSION['PO']->add_to_order($_POST['stock_id'], input_num('qty'), 
+                                       get_post('stock_id_text'), input_num('price'), 
                                        $_SESSION['PO']->trans_type == ST_PURCHORDER ? $_POST['req_del_date'] : '', 0, 0);
 
                                unset_form_variables();
index 5d3bff5d6c1f7b3ff61305dd63849618f3ae6f47..36bb7ca04cc76c7a56fe84c554b7f40b0ee6444f 100644 (file)
@@ -99,7 +99,7 @@ function display_po_receive_items()
 
     if (count($_SESSION['PO']->line_items) > 0 )
     {
-               foreach ($_SESSION['PO']->line_items as $ln_itm)
+               foreach ($_SESSION['PO']->line_items as $line_no => $ln_itm)
                {
 
                        alt_table_row_color($k);
@@ -130,7 +130,7 @@ function display_po_receive_items()
                                qty_cell($qty_outstanding, false, $dec);
 
                        if ($mod_grn || $qty_outstanding > 0)
-                               qty_cells(null, $ln_itm->line_no, number_format2($ln_itm->quantity, $dec), "align=right", null, $dec);
+                               qty_cells(null, $line_no, number_format2($ln_itm->quantity, $dec), "align=right", null, $dec);
                        else
                                label_cell(number_format2($ln_itm->quantity, $dec), "align=right");
 
@@ -313,23 +313,23 @@ function process_receive_po()
 
 if (isset($_POST['Update']) || isset($_POST['ProcessGoodsReceived']))
 {
-       /* if update quantities button is hit page has been called and ${$line->line_no} would have be
+       /* if update quantities button is hit page has been called and ${$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)
+       foreach ($_SESSION['PO']->line_items as $line_no => $line)
        {
         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));
+               $_POST[$line_no] = max($_POST[$line_no], 0);
+               if (!check_num($line_no))
+                       $_POST[$line_no] = number_format2(0, get_qty_dec($line->stock_id));
 
                if (!isset($_POST['tran_date']) || $_POST['tran_date'] == "")
                        $_POST['tran_date'] = new_doc_date();
 
-               $_SESSION['PO']->line_items[$line->line_no]->quantity = input_num($line->line_no);
+               $_SESSION['PO']->line_items[$line_no]->quantity = input_num($line_no);
 
                if (isset($_POST[$line->stock_id . "Desc"]) && strlen($_POST[$line->stock_id . "Desc"]) > 0)
                {
-                       $_SESSION['PO']->line_items[$line->line_no]->item_description = $_POST[$line->stock_id . "Desc"];
+                       $_SESSION['PO']->line_items[$line_no]->item_description = $_POST[$line->stock_id . "Desc"];
                }
         }
        }
index 9f6664ceeb84fd77dc8b9e5133b018a8e5c798f4..323f92e0ad845711eb7a43797306716450c59891 100644 (file)
@@ -101,9 +101,10 @@ function get_base_taxdata($stock_id, $group_id)
        Returned array contains calculated values for GL postings and tax registration:
                'Net' - value without tax,
                'Tax' - tax sum,
-               'Cost' - cost value (can be higher then Net value) used in parallel postings,
+               'Cost' - cost value (can be higher then Net value),
                'vat_category' -  stock tax category
-       and detailed info for any tax applicable tax (any array with numeric key):
+
+       and (with numeric keys) detailed info for any applicable tax rate:
                'tax_type_id' - tax type id
                'Value' - charged tax value
                'Deductible' - tax deductible (can be lower than Value for special goods)