Purchasse Order Inquire: finalized oders are hidden by default.
[fa-stable.git] / purchasing / includes / db / po_db.inc
index 30978da91afddffbcceab24c6099f99a897bfe43..aa705cd779fa5def2cda7659d35773c49831edc8 100644 (file)
@@ -13,7 +13,7 @@
 
 function get_supplier_details_to_order(&$order, $supplier_id)
 {
-       $sql = "SELECT curr_code, supp_name, tax_group_id, supp.tax_included, supp.tax_algorithm,
+       $sql = "SELECT curr_code, supp_name, tax_group_id, supp.tax_included,
                        supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1) 
                                * (ov_amount + ov_gst + ov_discount),0)) as cur_credit,
                                terms.terms, terms.days_before_due, terms.day_in_following_month
@@ -38,19 +38,25 @@ function get_supplier_details_to_order(&$order, $supplier_id)
        $_POST['curr_code'] = $myrow["curr_code"];
 
        $order->set_supplier($supplier_id, $myrow["supp_name"], $myrow["curr_code"], 
-               $myrow["tax_group_id"], $myrow["tax_included"], $myrow["tax_algorithm"]);
+               $myrow["tax_group_id"], $myrow["tax_included"]);
 }
 
 //----------------------------------------------------------------------------------------
 
 function delete_po($po)
 {
+       global $Refs;
+
+       begin_transaction();
        hook_db_prevoid($po, ST_PURCHORDER);
        $sql = "DELETE FROM ".TB_PREF."purch_orders WHERE order_no=".db_escape($po);
        db_query($sql, "The order header could not be deleted");
 
        $sql = "DELETE FROM ".TB_PREF."purch_order_details WHERE order_no =".db_escape($po);
        db_query($sql, "The order detail lines could not be deleted");
+
+       $Refs->restore_last(ST_PURCHORDER, $po);
+       commit_transaction();
 }
 
 //----------------------------------------------------------------------------------------
@@ -182,7 +188,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_algorithm"]);
+               $myrow['tax_group_id'], $myrow["tax_included"]);
 
                $order->credit = get_current_supp_credit($order->supplier_id);
 
@@ -291,9 +297,8 @@ function get_short_info($stock_id)
 }
 
 function get_sql_for_po_search_completed($from, $to, $supplier_id=ALL_TEXT, $location=ALL_TEXT,
-       $order_number = '', $stock_id = '')
+       $order_number = '', $stock_id = '', $also_closed=false)
 {
-
        $sql = "SELECT 
                porder.order_no, 
                porder.reference, 
@@ -303,8 +308,14 @@ function get_sql_for_po_search_completed($from, $to, $supplier_id=ALL_TEXT, $loc
                porder.ord_date, 
                supplier.curr_code, 
                Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
-               porder.into_stock_location
-               FROM ".TB_PREF."purch_orders as porder, "
+               porder.into_stock_location,
+               chk.isopen
+               FROM ".TB_PREF."purch_orders as porder
+                               LEFT JOIN (
+                                       SELECT order_no, SUM(quantity_ordered-quantity_received + quantity_ordered-qty_invoiced) isopen
+                                       FROM ".TB_PREF."purch_order_details
+                                       GROUP BY order_no
+                               ) chk ON chk.order_no=porder.order_no,"
                        .TB_PREF."purch_order_details as line, "
                        .TB_PREF."suppliers as supplier, "
                        .TB_PREF."locations as location
@@ -314,7 +325,7 @@ function get_sql_for_po_search_completed($from, $to, $supplier_id=ALL_TEXT, $loc
 
        if ($supplier_id != ALL_TEXT)
                $sql .= "AND supplier.supplier_id=".$supplier_id." ";
-       if (isset($order_number) && $order_number != "")
+       if ($order_number != "")
        {
                $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
        }
@@ -331,7 +342,7 @@ function get_sql_for_po_search_completed($from, $to, $supplier_id=ALL_TEXT, $loc
                {
                        $sql .= " AND porder.into_stock_location = ".db_escape($location);
                }
-               if (isset($selected_stock_item))
+               if ($stock_id !== '')
                {
                        $sql .= " AND line.item_code=".db_escape($stock_id);
                }
@@ -340,14 +351,14 @@ function get_sql_for_po_search_completed($from, $to, $supplier_id=ALL_TEXT, $loc
 
        }
 
+       if (!$also_closed)
+               $sql .= " AND isopen";
        $sql .= " GROUP BY porder.order_no";
        return $sql;
 }
 
-function get_sql_for_po_search($from, $to, $supplier_id=ALL_TEXT, $location=ALL_TEXT)
+function get_sql_for_po_search($from, $to, $supplier_id=ALL_TEXT, $location=ALL_TEXT, $order_number='', $stock_id='')
 {
-       global $all_items, $order_number, $selected_stock_item;;
-       
        $sql = "SELECT 
                porder.order_no, 
                porder.reference,
@@ -359,8 +370,7 @@ function get_sql_for_po_search($from, $to, $supplier_id=ALL_TEXT, $location=ALL_
                Sum(line.unit_price*line.quantity_ordered) AS OrderValue,
                Sum(line.delivery_date < '". date2sql(Today()) ."'
                AND (line.quantity_ordered > line.quantity_received)) As OverDue
-               FROM "
-                       .TB_PREF."purch_orders as porder, "
+               FROM ".TB_PREF."purch_orders as porder,"
                        .TB_PREF."purch_order_details as line, "
                        .TB_PREF."suppliers as supplier, "
                        .TB_PREF."locations as location
@@ -369,7 +379,7 @@ function get_sql_for_po_search($from, $to, $supplier_id=ALL_TEXT, $location=ALL_
                AND location.loc_code = porder.into_stock_location
                AND (line.quantity_ordered > line.quantity_received) ";
 
-       if (isset($order_number) && $order_number != "")
+       if ($order_number != "")
        {
                $sql .= "AND porder.reference LIKE ".db_escape('%'. $order_number . '%');
        }
@@ -386,9 +396,9 @@ function get_sql_for_po_search($from, $to, $supplier_id=ALL_TEXT, $location=ALL_
                        $sql .= " AND porder.into_stock_location = ".db_escape($location);
                }
 
-               if (isset($selected_stock_item))
+               if ($stock_id != '')
                {
-                       $sql .= " AND line.item_code=".db_escape($selected_stock_item);
+                       $sql .= " AND line.item_code=".db_escape($stock_id);
                }
                if ($supplier_id != ALL_TEXT)
                        $sql .= " AND supplier.supplier_id=".db_escape($supplier_id);