<?php
/**********************************************************************
***********************************************************************/
/*
	item_codes table is used to store both multiply foreign codes and 
	sale kits definition.
*/
function update_list_value($selected_id, $value_code, $value_name, $value_description, $display_order, $group_value_id, 
                            $text1, $text2, $amount1, $date1, $inactive=0)
{
	$sql = "UPDATE ".TB_PREF."list_values 
            SET
                value_code = ".db_escape($value_code).",
                value_name = ".db_escape($value_name).",
                value_description = ".db_escape($value_description).",
                display_order = ".db_escape($display_order).",
                group_value_id = ".db_escape($group_value_id).",
                text1 = ".db_escape($text1).",
                text2 = ".db_escape($text2).",
                amount1 = ".db_escape($amount1).",
                date1 = ".db_escape($date1).",
                inactive = ".db_escape($inactive)."
        	WHERE id = ".db_escape($selected_id);

	db_query($sql,"List value could not be updated");
}

function add_list_value($list_code, $value_code, 
                        $value_name, $value_description, $display_order, $group_value_id, 
                        $text1, $text2, $amount1, $date1, $inactive=0)
{
	$sql = "INSERT INTO ".TB_PREF."list_values
                    (list_code, value_code,  
                    value_name, value_description, 
                    display_order, group_value_id, 
                    text1, text2, 
                    amount1, date1, 
                    inactive) 
			VALUES( ".db_escape($list_code).",".db_escape($value_code).",
                    ".db_escape($value_name).",".db_escape($value_description).","
                    .db_escape($display_order).",".db_escape($group_value_id).","
                    .db_escape($text1).",".db_escape($text2).","
                    .db_escape($amount1).",".db_escape($date1).","
                    .db_escape($inactive).")";

	db_query($sql,"List value could not be added");
}

function delete_list_value($id)
{
    return;
    $sql="DELETE FROM ".TB_PREF."list_values WHERE id=".db_escape($id);
	db_query($sql,"an item code could not be deleted");
}

function get_list_value($id)
{
	$sql="SELECT * FROM ".TB_PREF."list_values WHERE id=".db_escape($id);

	$result = db_query($sql,"List value could not be retrieved");

	return db_fetch($result);
}

function get_all_list_values($list_code)
{
	$sql="SELECT lv.*, gn.value_name group_value_name  
        FROM ".TB_PREF."list_values lv 
                LEFT JOIN ".TB_PREF."list_values gn ON 
                        lv.group_value_id = gn.id  
		WHERE lv.list_code=".db_escape($list_code)."  
        ORDER BY lv.display_order, lv.value_name";

	$result = db_query($sql,"all list values could not be retrieved");

	return $result;
}

function get_list_values_range_on_id($list_code, $from_value_id=0, $to_value_id=0)
{
	$sql = "SELECT lv.*, gn.value_name group_value_name  
                FROM ".TB_PREF."list_values lv 
                        LEFT JOIN ".TB_PREF."list_values gn ON 
                                lv.group_value_id = gn.id  
                        WHERE lv.list_code=".db_escape($list_code);
        
        if($from_value_id > 0){
            $sql .= " AND lv.id >= ".db_escape($from_value_id);
        }
        
        if($to_value_id > 0){
            $sql .= " AND lv.id <= ".db_escape($to_value_id);
        }

        $sql .= " ORDER BY lv.id";

	$result = db_query($sql,"get_list_values_range_on_id");

	return $result;
}

function get_list_values_range_on_value_name($list_code, $from_value_name='', $to_value_name='')
{
	$sql = "SELECT lv.*, gn.value_name group_value_name  
                FROM ".TB_PREF."list_values lv 
                        LEFT JOIN ".TB_PREF."list_values gn ON 
                                lv.group_value_id = gn.id  
                        WHERE lv.list_code=".db_escape($list_code);
        
        if($from_value_name != ''){
            $sql .= " AND lv.value_name >= ".db_escape($from_value_name);
        }
        
        if($to_value_name != ''){
            $sql .= " AND lv.value_name <= ".db_escape($to_value_name);
        }

        $sql .= " ORDER BY lv.value_name";

	$result = db_query($sql,"get_list_values_range_on_value_name");

	return $result;
}

function get_existing_values($list_code,$value_code)
{
    
    $sql="SELECT DISTINCT * FROM ".TB_PREF."list_values  
		WHERE list_code= ".db_escape($list_code)." AND value_code=".db_escape($value_code);

	$result = db_query($sql,"Existing list values could not be retrieved");
	
	return $result;
}


function get_listvalue_by_code($list_code,$value_code)
{
    
    $sql="SELECT id, value_code, value_name FROM ".TB_PREF."list_values  
		WHERE list_code= ".db_escape($list_code)." AND value_code=".db_escape($value_code);

	$result = db_query($sql,"Listvalue could not be retrieved");
	
	return db_fetch($result);
}

function get_listvalue_by_name($list_code,$value_name)
{
    
    $sql="SELECT id, value_code, value_name FROM ".TB_PREF."list_values  
		WHERE list_code= ".db_escape($list_code)." AND value_name=".db_escape($value_name);

	$result = db_query($sql,"Listvalue could not be retrieved");
	
	return db_fetch($result);
}


function get_listvalue_by_id($id)
{
    
    $sql="SELECT id, value_code, value_name FROM ".TB_PREF."list_values  
		WHERE id = ".db_escape($id);

	$result = db_query($sql,"Listvalue could not be retrieved");
	
	return db_fetch($result);
}

function get_listvalue_name_by_id($id)
{
    
    $sql="SELECT value_name FROM ".TB_PREF."list_values  
		WHERE id = ".db_escape($id);

	$result = db_query($sql,"Listvalue could not be retrieved");
	
	return db_result_value($result, "value_name");
}

function get_list_type($list_code)
{
    $sql="SELECT list_type FROM ".TB_PREF."lists  
		WHERE 
              list_code= ".db_escape($list_code);

	$result = db_query($sql,"Listvalue could not be retrieved");
	
	return db_fetch($result);
}


function get_list($list_code)
{
    $sql="SELECT * FROM ".TB_PREF."lists  
		WHERE 
              list_code= ".db_escape($list_code);

	$result = db_query($sql,"List details could not be retrieved");
	
	return db_fetch($result);
}

function get_list_type_list_existing_value($list_type, $value_code)
{
    $sql="SELECT lv.value_code 
        FROM 
            ".TB_PREF."list_values lv, 
            ".TB_PREF."lists l
		WHERE 
                lv.list_code = l.list_code AND
              l.list_type= ".db_escape($list_type)." AND
                  lv.value_code = ".db_escape($value_code)
                  ;

	$result = db_query($sql,"Listvalue could not be retrieved");
	
	return db_fetch($result);
}

function add_list($list_type, $list_code, $list_name, $inactive, $group_list_code,
                    $text1_active, $text1_name, $text1_length,
                    $text2_active, $text2_name, $text2_length,
                    $amount1_active, $amount1_name, $amount1_decimals,
                    $date1_active, $date1_name)
{
	$sql = "INSERT INTO ".TB_PREF."lists
                    (list_type, list_code, 
                    list_name, inactive, group_list_code,
                    text1_active, text1_name, text1_length,
                    text2_active, text2_name, text2_length,
                    amount1_active, amount1_name, amount1_decimals,
                    date1_active, date1_name) 
			VALUES
                    (".db_escape($list_type).",".db_escape($list_code).","
                    .db_escape($list_name).",".db_escape($inactive).",".db_escape($group_list_code).","
                    .db_escape($text1_active).",".db_escape($text1_name).",".db_escape($text1_length).","
                    .db_escape($text2_active).",".db_escape($text2_name).",".db_escape($text2_length).","
                    .db_escape($amount1_active).",".db_escape($amount1_name).",".db_escape($amount1_decimals).","
                    .db_escape($date1_active).",".db_escape($date1_name).")";

	db_query($sql,"New List could not be added");
}



function update_list($selected_id, $list_type, $list_name, $inactive, $group_list_code,
                    $text1_active, $text1_name, $text1_length,
                    $text2_active, $text2_name, $text2_length,
                    $amount1_active, $amount1_name, $amount1_decimals,
                    $date1_active, $date1_name)
{
	$sql = "UPDATE ".TB_PREF."lists 
            SET                    
                list_type = ".db_escape($list_type).",
                list_name = ".db_escape($list_name).",
                inactive = ".db_escape($inactive).",
                group_list_code = ".db_escape($group_list_code).",

                text1_active = ".db_escape($text1_active).",
                text1_name = ".db_escape($text1_name).",
                text1_length = ".db_escape($text1_length).",

                text2_active = ".db_escape($text2_active).",
                text2_name = ".db_escape($text2_name).",
                text2_length = ".db_escape($text2_length).",

                amount1_active = ".db_escape($amount1_active).",
                amount1_name = ".db_escape($amount1_name).",
                amount1_decimals = ".db_escape($amount1_decimals).",

                date1_active = ".db_escape($date1_active).",
                date1_name = ".db_escape($date1_name)." 
        	WHERE list_id = ".db_escape($selected_id);

	db_query($sql,"Selected List could not be updated");
}

function delete_list($id)
{

    $sql="DELETE FROM ".TB_PREF."lists WHERE list_id=".db_escape($id);
	db_query($sql,"List could not be deleted");
}

function get_list_by_id($id)
{
	$sql="SELECT * FROM ".TB_PREF."lists WHERE list_id=".db_escape($id);

	$result = db_query($sql,"List could not be retrieved");

	return db_fetch($result);
}


function get_all_lists()
{
	$sql="SELECT li.list_id,
                li.list_type,
                li.list_code,
                li.list_name,
                li.inactive,
                li.group_list_code,
                gr.list_name group_name 
                     
        FROM ".TB_PREF."lists li 
                LEFT JOIN ".TB_PREF."lists gr ON                        
                        li.group_list_code = gr.list_code 
        ORDER BY li.list_name ";           

       
	return  db_query($sql,"all lists could not be retrieved");          
}


?>