<?php
/**********************************************************************
***********************************************************************/


function get_item_material_group($stock_id)
{
    $sql = "SELECT img.*,                   
                    smun.name stock_unit_name,                    
                    mg.code material_group_code,                  
                    mgun.name material_group_unit_name,
                    mg.id mg_id                    
        
            FROM ".TB_PREF."stock_master sm 
                INNER JOIN ".TB_PREF."item_units smun ON
                        sm.units = smun.abbr
     
                LEFT JOIN ".TB_PREF."item_material_groups img ON
                        sm.stock_id = img.stock_id
                    
                LEFT JOIN ".TB_PREF."material_groups mg ON
                        img.material_group_id = mg.id   
                    
                LEFT JOIN ".TB_PREF."item_units mgun ON
                        mg.unit = mgun.abbr

            WHERE sm.stock_id = ".db_escape($stock_id);
    
    $result = db_query($sql);
    return db_fetch($result);
    
}

function get_item_material_group_record($stock_id)
{
    $sql = "SELECT *                   
            FROM ".TB_PREF."item_material_groups     
            WHERE stock_id = ".db_escape($stock_id);
    
    $result = db_query($sql);
    return db_fetch($result);
    
}


function update_item_material_group($stock_id, $material_group_id, $conversion_factor)
{    
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."item_material_groups
            SET stock_id=".db_escape($stock_id).", 
                material_group_id=".db_escape($material_group_id).",
                conversion_factor=".db_escape($conversion_factor).",	
                updated_by=".db_escape($updated_by).",
                updated_on=".db_escape($updated_on)." 
            WHERE stock_id=".db_escape($stock_id);
    
    db_query($sql);
 
}


function add_item_material_group($stock_id, $material_group_id, $conversion_factor)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."item_material_groups
        (stock_id, material_group_id, 
        conversion_factor, created_by, created_on)
        
        VALUES
        
        (".db_escape($stock_id).", ".db_escape($material_group_id).",
            ".db_escape($conversion_factor).", ".db_escape($created_by).", ".db_escape($created_on).")";
    
    db_query($sql);

}
  

function get_material_group_stock_items($material_group_id)
{
    $sql = "SELECT img.stock_id,
                    sm.description stock_item_description,
                    iu.name stock_item_unit_name,
                    img.conversion_factor

            FROM ".TB_PREF."item_material_groups img
                JOIN ".TB_PREF."stock_master sm ON
                    img.stock_id = sm.stock_id 
                JOIN ".TB_PREF."item_units iu ON
                    sm.units = iu.abbr 
                
            WHERE img.material_group_id=".db_escape($material_group_id);
    
    return db_query($sql);
 
}

function delete_item_material_group()
{
    $sql = "DELETE FROM ".TB_PREF."item_material_groups WHERE 1=1";
        
    db_query($sql);
}


  
?>