<?php

function dashboard_get_period_price_variance_items($from_date, $to_date) { 
    
    $sql = "SELECT ((MAX(pod.unit_price) - MIN(pod.unit_price)) / MIN(pod.unit_price)) min_max_variance, pod.item_code,
                    u.name base_unit_name 
            FROM ".TB_PREF."purch_order_details pod 
                JOIN ".TB_PREF."stock_master sm    
                    ON sm.stock_id = pod.item_code  
                JOIN ".TB_PREF."item_units u   
                    ON u.abbr = sm.units 
            WHERE unit_price > 0 
                AND delivery_date >= ".db_escape($from_date)." 
                AND delivery_date <= ".db_escape($to_date)." 
            GROUP BY item_code  
            ORDER BY min_max_variance DESC ";
                
        return db_query($sql, "dashboard_get_period_price_variance_items"); 
}

function dashboard_get_item_period_max_price_details($item_code, $from_date, $to_date) {
    
    $sql = "SELECT (unit_price) max_price, item_code, delivery_date, po_detail_item   
            FROM ".TB_PREF."purch_order_details  
            WHERE unit_price > 0 
                AND delivery_date >= ".db_escape($from_date)." 
                AND delivery_date <= ".db_escape($to_date)." 
                AND item_code = ".db_escape($item_code)." 
            ORDER BY unit_price DESC 
            LIMIT 1";
    
                
        $result = db_query($sql, "dashboard_get_item_period_max_price_details"); 
        
        return db_fetch($result);
}

function dashboard_get_item_period_min_price_details($item_code, $from_date, $to_date) {
    
    $sql = "SELECT (unit_price) min_price, item_code, delivery_date, po_detail_item   
            FROM ".TB_PREF."purch_order_details  
            WHERE unit_price > 0 
                AND delivery_date >= ".db_escape($from_date)." 
                AND delivery_date <= ".db_escape($to_date)." 
                AND item_code = ".db_escape($item_code)." 
            ORDER BY unit_price  
            LIMIT 1";
    
                
        $result = db_query($sql, "dashboard_get_item_period_min_price_details"); 
        
        return db_fetch($result);
}
