<?php

function dashboard_get_delivery_lead_time_suppliers($from_date, $to_date) {
    
    $sql = "SELECT b.supplier_id, (SUM(DATEDIFF(b.delivery_date, pod.delivery_date)) / COUNT(b.id)) ave   
            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 
            WHERE b.delivery_date >= ".db_escape($from_date)." 
                AND b.delivery_date <= ".db_escape($to_date)." 
            GROUP BY b.supplier_id  
            ORDER BY ave  DESC ";    
                
        return db_query($sql, "dashboard_get_period_grn_suppliers"); 
}

function dashboard_get_supplier_average_delivery_lead_time($supplier_id, $from_date, $to_date) {
    
    $sql = "SELECT b.supplier_id, (SUM(DATEDIFF(b.delivery_date, pod.delivery_date)) / COUNT(b.id)) ave   
            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 
            WHERE b.delivery_date >= ".db_escape($from_date)." 
                AND b.delivery_date <= ".db_escape($to_date)." 
                AND b.supplier_id = ".db_escape($supplier_id)." 
            GROUP BY b.supplier_id  ";    
    
                
        $result = db_query($sql, "dashboard_get_supplier_period_grns_total"); 
        
        return db_result_value($result, 'ave');
}
