<?php

function dashboard_get_period_grn_items($from_date, $to_date) {
    
    $sql = "SELECT i.item_code , (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 i.item_code   
            ORDER BY grned_total DESC ";    
                
        return db_query($sql, "dashboard_get_period_grn_items"); 
}

function dashboard_get_item_period_grns_total($item_code, $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 i.item_code = ".db_escape($item_code);
    
                
        $result = db_query($sql, "dashboard_get_item_period_grns_total"); 
        
        return db_result_value($result, 'grned_total');
}
