<?php

function dashboard_get_period_grn_suppliers($from_date, $to_date) {
    
    $sql = "SELECT b.supplier_id , (i.unit_cost * i.qty_recd) grned_total 
            FROM ".TB_PREF."grn_batch b 
                JOIN ".TB_PREF."grn_items i 
                    ON i.grn_batch_id = b.id 
            WHERE delivery_date >= ".db_escape($from_date)." 
                AND delivery_date <= ".db_escape($to_date)." 
            GROUP BY b.supplier_id  
            ORDER BY grned_total DESC ";    
                
        return db_query($sql, "dashboard_get_period_grn_suppliers"); 
}

function dashboard_get_supplier_period_grns_total($supplier_id, $from_date, $to_date) {
    
    $sql = "SELECT SUM(i.unit_cost * i.qty_recd) grned_total 
            FROM ".TB_PREF."grn_batch b 
                JOIN ".TB_PREF."grn_items i 
                    ON i.grn_batch_id = b.id 
            WHERE delivery_date >= ".db_escape($from_date)." 
                AND delivery_date <= ".db_escape($to_date)." 
                AND supplier_id = ".db_escape($supplier_id);
    
                
        $result = db_query($sql, "dashboard_get_supplier_period_grns_total"); 
        
        return db_result_value($result, 'grned_total');
}
