<?php
/**********************************************************************
***********************************************************************/

function get_import_purch_order_costing_detail_table_content_from_db($order_no){
    $sql = "SELECT DISTINCT ipocd.id unic_id, ipocd.*,
                   pod.*,
                   ipo.*, 
                   sm.description stock_master_description 
            FROM ".TB_PREF."purch_order_details pod 
                LEFT JOIN ".TB_PREF."import_purch_orders ipo
                    ON pod.order_no = ipo.order_no 
                LEFT JOIN ".TB_PREF."import_purch_order_costing_details ipocd 
                    ON ipocd.order_no = ipo.order_no 
                    AND ipocd.po_detail_item = pod.po_detail_item 
                JOIN ".TB_PREF."stock_master sm 
                    ON pod.item_code = sm.stock_id  
                
            WHERE pod.order_no = ".db_escape($order_no)."";

    return db_query($sql, "could not get_import_purch_order_costing_detail_table_content_from_db");
}   

function get_import_purch_order_costing_detail_table_content($order_no){
    $sql = "SELECT pod.*,
                   ipo.*,
                   sm.description stock_master_description 
            FROM ".TB_PREF."purch_order_details pod 
                LEFT JOIN ".TB_PREF."import_purch_orders ipo
                    ON pod.order_no = ipo.order_no 
                RIGHT JOIN ".TB_PREF."stock_master sm 
                    ON pod.item_code = sm.stock_id  
            WHERE pod.order_no = ".db_escape($order_no)."";

    return db_query($sql, "could not get_import_purch_order_costing_detail_table_content");
}    

function get_line_total($po_detail_item){
    $sql = "SELECT order_unit_price * order_unit_qty line_total  
            FROM ".TB_PREF."purch_order_details 
            WHERE po_detail_item = ".db_escape($po_detail_item)."";

    $result = db_query($sql, "could not get_line_total");
    $row = db_fetch($result);
    return $row['line_total'];
}

function get_purch_order_lines_total($order_no){
    $sql = "SELECT SUM(order_unit_price * order_unit_qty) po_total 
            FROM ".TB_PREF."purch_order_details 
            WHERE order_no = ".db_escape($order_no)."";

    $result = db_query($sql, "could not get_purch_order_lines_total");
    return db_fetch($result);
}

function get_total_in_rs($order_no){
    $sql = "SELECT SUM(order_unit_price * order_unit_qty) po_total 
            FROM ".TB_PREF."purch_order_details 
            WHERE order_no = ".db_escape($order_no)."";

    $result = db_query($sql, "could not get_total_in_rs");
    return $result['po_total'];
}

function get_po_line_ids($order_no){
    $sql = "SELECT po_detail_item 
            FROM ".TB_PREF."purch_order_details 
            WHERE order_no = ".db_escape($order_no)."";

    return db_query($sql, "could not get_po_line_ids");
}

function get_po_line_id_with_value($order_no){
    $sql = "SELECT CONCAT(value_name, '_', po_detail_item) value_name,
                   value , po_detail_item 
            FROM ".TB_PREF."import_purch_order_costing_details 
            WHERE order_no = ".db_escape($order_no)." 
            ORDER BY po_detail_item";

    return db_query($sql, "could not get_po_line_id_with_value");
}

function add_import_purch_order_costing_detail($order_no, $po_line_id, $value_name, $value){
    $sql = "INSERT INTO ".TB_PREF."import_purch_order_costing_details  
                   (order_no, 
                    po_detail_item, 
                    value_name, 
                    value)
            VALUES (".db_escape($order_no).","
                     .db_escape($po_line_id).","
                     .db_escape($value_name).","
                     .db_escape($value).")";     
    
    db_query($sql, "Could not add_import_purch_order_costing_detail.");   
    if(!(db_insert_id() > 0)){
        return array("success" => false, "message" => "Could not add import purch order costing detail. PO Line ID #$po_line_id and name $value_name");
    }
    return array("success" => true, "message" => "");
}

function get_import_purch_order_costing_row_values($order_no, $po_detail_item){
    $sql = "SELECT value, 
                   po_detail_item, 
                   value_name,
                   CONCAT(value_name, '_', po_detail_item) post_name 
            FROM ".TB_PREF."import_purch_order_costing_details  
            WHERE order_no = ".db_escape($order_no)."
                AND po_detail_item = ".db_escape($po_detail_item)."";
    return db_query($sql, "could not get_import_purch_order_costing_row_values");
    db_fetch($result);
}

function get_import_purch_order_costing_details_po_line_ids($order_no){
    $import_purch_order_costing_details_po_line_ids_array = array();
    $sql = "SELECT DISTINCT po_detail_item 
                   
            FROM ".TB_PREF."import_purch_order_costing_details  
            WHERE order_no = ".db_escape($order_no)." 
                AND po_detail_item > 0 ";
    $result = db_query($sql, "could not get_import_purch_order_costing_details_po_line_ids");
    while($row = db_fetch($result)){
        $import_purch_order_costing_details_po_line_ids_array[] = $row['po_detail_item'];
    }
    return $import_purch_order_costing_details_po_line_ids_array;
}

function set_common_inputs($order_no, $value_name){
    $sql = "SELECT value
            FROM ".TB_PREF."import_purch_order_costing_details  
            WHERE order_no = ".db_escape($order_no)."
                AND value_name = ".db_escape($value_name)."";
    
    $result = db_query($sql, "could not set_common_inputs");
    $row = db_fetch($result);
    return $row['value'];
}

function import_purch_order_costing_details_has_db_records($order_no){
    $sql = "SELECT COUNT(id) count 
            FROM ".TB_PREF."import_purch_order_costing_details  
            WHERE order_no = ".db_escape($order_no)."";
    
    $result = db_query($sql, "could not import_purch_order_costing_details_has_db_records");
    $row = db_fetch($result);
    return $row['count'];
}

function delete_import_purch_order_costing_detail_lines($order_no){
    $po_lines = import_purch_order_costing_details_has_db_records($order_no);
    if($po_lines > 0){
        $sql = "DELETE FROM ".TB_PREF."import_purch_order_costing_details  
                WHERE order_no = ".db_escape($order_no);
        db_query($sql, "could not delete_import_purch_order_costing_detail_lines");
    
        if(!(db_num_affected_rows() > 0)){
            return array("success" => false, "message" => "Could not delete import purch order costing detail lines. PO #$order_no");
        }
    }
    
    return array("success" => true, "message" => "");
}

?>