<?php


function view_supplier_delivery_lead_time_details($supplier_id, $from_date, $to_date) {
    
    $sql = "SELECT DATEDIFF(b.delivery_date, pod.delivery_date) lead_time,    
                    b.delivery_date grn_delivery_date, b.id grn_batch_id,
                    pod.delivery_date pod_delivery_date, pod.order_no, pod.po_detail_item pod_line_id,
                    i.item_code, i.id grn_line_id, i.tran_unit, i.tran_qty,
                    u.name unit_name 
            FROM ".TB_PREF."grn_items i   
                JOIN ".TB_PREF."purch_order_details pod   
                    ON pod.po_detail_item = i.po_detail_item  
                JOIN ".TB_PREF."grn_batch b  
                    ON i.grn_batch_id = b.id 
                JOIN ".TB_PREF."item_units u   
                    ON u.abbr = i.tran_unit 
            WHERE b.delivery_date >= ".db_escape($from_date)." 
                AND b.delivery_date <= ".db_escape($to_date)." 
                AND b.supplier_id = ".db_escape($supplier_id)." 
            ORDER BY lead_time DESC ";    
    
                
        return db_query($sql, "view_supplier_delivery_lead_time_details");  
        
}