<?php

function get_view_item_transaction_summary_type_details($stock_id, $from_date, $to_date, $type, $loc_code='') {
    
    $sql = "SELECT sv.type, sv.trans_no, sv.reference, sv.loc_code, 
                    sv.tran_date, sv.tran_unit, sv.tran_qty, sv.qty, 
                    bun.name base_unit_name,
                    tun.name tran_unit_name,
                    loc.location_name 
            FROM ".TB_PREF."stock_moves sv    
                JOIN ".TB_PREF."stock_master sm 
                    ON sm.stock_id = sv.stock_id 
                JOIN ".TB_PREF."item_units bun  
                    ON bun.abbr = sm.units 
                JOIN ".TB_PREF."item_units tun  
                    ON tun.abbr = sv.tran_unit  
                JOIN ".TB_PREF."locations loc  
                    ON loc.loc_code = sv.loc_code   
            WHERE sv.tran_date >= ".db_escape($from_date)." 
                AND sv.tran_date <= ".db_escape($to_date)." 
                AND sv.stock_id = ".db_escape($stock_id)." 
                AND sv.type = ".db_escape($type);
    
    if(strlen($loc_code) > 1){
        $sql .= " AND sv.loc_code = ".db_escape($loc_code);
    }
    
    $sql .= " ORDER BY sv.tran_date, sv.trans_no ";
    
    return db_query($sql, "get_view_item_transaction_summary_type_details"); 
}
