Cleanups and changes in purchasing classes.
[fa-stable.git] / purchasing / includes / db / po_db.inc
index a18083ce7a02b49230e9d2897256191564cb7b1f..f48621fad266b85dd176d30a2321eafbbf4a9145 100644 (file)
@@ -15,10 +15,11 @@ function get_supplier_details_to_order(&$order, $supplier_id)
 {
        $sql = "SELECT curr_code, supp_name, tax_group_id, supp.tax_included, dimension_id, dimension2_id,
                        supp.credit_limit - Sum((ov_amount + ov_gst + ov_discount)) as cur_credit,
-                               terms.terms, terms.days_before_due, terms.day_in_following_month
+                               terms.terms, terms.days_before_due, terms.day_in_following_month, tg.tax_area
                FROM ".TB_PREF."suppliers supp
                         LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id
                         LEFT JOIN ".TB_PREF."payment_terms terms ON supp.payment_terms=terms.terms_indicator
+                        LEFT JOIN ".TB_PREF."tax_groups tg ON supp.tax_group_id=tg.id
                WHERE supp.supplier_id = ".db_escape($supplier_id)."
                GROUP BY
                          supp.supp_name";
@@ -39,7 +40,7 @@ function get_supplier_details_to_order(&$order, $supplier_id)
        $_POST['dimension2'] = $myrow["dimension2_id"];
 
        $order->set_supplier($supplier_id, $myrow["supp_name"], $myrow["curr_code"], 
-               $myrow["tax_group_id"], $myrow["tax_included"]);
+               $myrow["tax_group_id"], $myrow["tax_included"], $myrow["tax_area"]);
 }
 
 //----------------------------------------------------------------------------------------
@@ -98,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);
@@ -138,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). ",'"
@@ -162,9 +163,10 @@ function update_po(&$po_obj)
 
 function read_po_header($order_no, &$order)
 {
-       $sql = "SELECT po.*, supplier.*, loc.location_name 
+       $sql = "SELECT po.*, tg.tax_area, supplier.*, loc.location_name 
                FROM ".TB_PREF."purch_orders po,"
-                       .TB_PREF."suppliers supplier,"
+                       .TB_PREF."suppliers supplier
+                       LEFT JOIN ".TB_PREF."tax_groups tg ON supplier.tax_group_id=tg.id,"
                        .TB_PREF."locations loc
                WHERE po.supplier_id = supplier.supplier_id
                AND loc.loc_code = into_stock_location
@@ -181,7 +183,7 @@ function read_po_header($order_no, &$order)
        $order->order_no = $order_no;
 
        $order->set_supplier($myrow["supplier_id"], $myrow["supp_name"], $myrow["curr_code"],
-               $myrow['tax_group_id'], $myrow["tax_included"]);
+               $myrow['tax_group_id'], $myrow["tax_included"], $myrow["tax_area"]);
 
                $order->credit = get_current_supp_credit($order->supplier_id);
 
@@ -211,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) ";
@@ -230,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
 }