<?php
/**********************************************************************
***********************************************************************/
function add_alternate_unit($stock_id, $alternate_unit, $base_unit, $convertion_factor, $inactive)
{
                $id_user = $_SESSION['wa_current_user']->user;
                $datetime = date('Y-m-d H:i:s');
                
        $sql = "INSERT INTO ".TB_PREF."alternate_units (stock_id, alternate_unit, base_unit, convertion_factor,
                        inactive, created_by, created_on) 
                VALUES (".db_escape($stock_id).", ".db_escape($alternate_unit).", 
                        ".db_escape($base_unit).", ".db_escape($convertion_factor).", 
                        ".db_escape($inactive).",
                        ".db_escape($id_user).", ".db_escape($datetime) .")";
                                

        db_query($sql,"Alternate unit could not be added");		
}
//
function update_alternate_unit($alternate_unit_id, $alternate_unit, $convertion_factor, $inactive)
{
                $id_user = $_SESSION['wa_current_user']->user;
                $datetime = date('Y-m-d H:i:s');
                
        $sql = "UPDATE ".TB_PREF."alternate_units SET 
                    alternate_unit=".db_escape($alternate_unit).", 
                    convertion_factor=".db_escape($convertion_factor).", 
                    updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime).",
                    inactive=".db_escape($inactive)." 
                WHERE id=".db_escape($alternate_unit_id);

        db_query($sql,"Alternate unit could not be updated");		
}
//
function get_alternate_unit_details($alternate_unit_id)
{
	$sql = "SELECT * FROM ".TB_PREF."alternate_units WHERE id=".db_escape($alternate_unit_id);
	
	$result = db_query($sql,"Alternate Unit could not be retreived");
	
	return db_fetch($result);
}

function get_alternate_units($stock_id, $include_base_unit = true)
{
	$sql = "SELECT au.* 
		FROM ".TB_PREF."alternate_units au, 
                ".TB_PREF."item_units iu
		WHERE au.alternate_unit = iu.abbr AND
                au.stock_id=".db_escape($stock_id);
    
    if(!$include_base_unit){
        $sql .= " AND 
                    NOT (alternate_unit = base_unit AND convertion_factor = 1)";
    }
	
	return db_query($sql,"Alternate Units could not be retreived");
}

function get_alternate_unit_all_details($stock_id)
{
	$sql = "SELECT au.*, aun.name alternate_unit_name, bun.name base_unit_name  
            FROM ".TB_PREF."alternate_units au, 
                    ".TB_PREF."item_units aun,
                    ".TB_PREF."item_units bun 
            WHERE au.alternate_unit = aun.abbr AND 
                    au.base_unit = bun.abbr AND 
                    au.stock_id=".db_escape($stock_id);
	
	return db_query($sql,"Alternate unit details could not be retreived");
}

function get_alternate_unit_info($stock_id, $alternate_unit)
{
	$sql = "SELECT id, alternate_unit, convertion_factor 
            FROM ".TB_PREF."alternate_units 
            WHERE stock_id=".db_escape($stock_id)." AND
                       alternate_unit = ".db_escape($alternate_unit)." ";
	
	$result = db_query($sql,"Alternate Unit infomation could not be retreived");
	
	return db_fetch($result);
}

function get_alternate_unit_id($stock_id, $alternate_unit)
{
	$sql = "SELECT id FROM ".TB_PREF."alternate_units WHERE stock_id=".db_escape($stock_id)." AND
                       alternate_unit = ".db_escape($alternate_unit)." ";
	
	$result = db_query($sql,"Alternate Unit could not be retreived");
	
	return db_fetch($result);
}

function get_convertion_factor($alternate_unit, $stock_id)
{
	$sql = "SELECT convertion_factor 
            FROM ".TB_PREF."alternate_units WHERE alternate_unit=".db_escape($alternate_unit)." AND
                            stock_id=".db_escape($stock_id)." ";
	
	$result = db_query($sql,"Alternate Unit could not be retreived");
	
	return db_fetch($result);
}

function get_convertion_factor2($stock_id, &$alternate_unit, &$convesion_factor)
{
	$sql = "SELECT convertion_factor 
            FROM ".TB_PREF."alternate_units WHERE alternate_unit=".db_escape($alternate_unit)." AND
                            stock_id=".db_escape($stock_id)." ";
	
	$result = db_query($sql,"Alternate Unit could not be retreived");
	
	$cf = db_fetch($result);
    if($cf){
        $convesion_factor = $cf['convertion_factor'];
    }
    else{
        $itm = get_item_edit_info($stock_id);
        $convesion_factor = 1;
        if (isset($itm['units']) && strlen($itm['units']) > 0) {
            $alternate_unit = $itm['units'];
        }
    }
}

function get_unit_name($alternate_unit)
{
	$sql = "SELECT name 
            FROM ".TB_PREF."item_units WHERE abbr=".db_escape($alternate_unit)." ";
	
	$result = db_query($sql,"Unit name could not be retreived");
	
	return db_fetch($result);
}


function delete_alternate_units($stock_id, $alternate_unit)
{
	$sql="DELETE FROM ".TB_PREF."alternate_units 
        WHERE stock_id=".db_escape($stock_id)." AND 
            alternate_unit = ".db_escape($alternate_unit);

	db_query($sql,"Alternate Unit could not be deleted");
    return db_num_affected_rows();
}


?>