<?php
/**********************************************************************
***********************************************************************/
/*
	item_codes table is used to store both multiply foreign codes and 
	sale kits definition.
*/
function update_table_value($selected_id, $upto, 
                        $calculation_type, $calculation_value, 
                        $salary_total_id, $fixed_value, $value2)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
	$sql = "UPDATE ".TB_PREF."value_tables SET
                        upto = ".db_escape($upto).",
                        calculation_type = ".db_escape($calculation_type).",
                        calculation_value = ".db_escape($calculation_value).",
                        salary_total_id = ".db_escape($salary_total_id).",
                        fixed_value = ".db_escape($fixed_value).",
                        value2 = ".db_escape($value2).",
                        updated_by = ".db_escape($updated_by).",
                        updated_on = ".db_escape($updated_on)." 
        	WHERE id = ".db_escape($selected_id);

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

function add_table_value($table_id, $upto, 
                        $calculation_type, $calculation_value, 
                        $salary_total_id, $fixed_value, $value2)
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
    
	$sql = "INSERT INTO ".TB_PREF."value_tables (
                        table_id, upto, 
                        calculation_type, calculation_value, 
                        salary_total_id, fixed_value, 
                        value2,
                        created_by, created_on) 
                VALUES(".$table_id.",".($upto).", "
	  		.db_escape($calculation_type).",".$calculation_value.", "
                        .$salary_total_id.",".$fixed_value.", "
                        .$value2.", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

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

function update_value_table_all($table_id, $accumulated_total)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
	$sql = "UPDATE ".TB_PREF."value_tables SET
                        accumulated_total = ".db_escape($accumulated_total)."  
        	WHERE table_id = ".db_escape($table_id);

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


function delete_table_value($id)
{

    $sql="DELETE FROM ".TB_PREF."value_tables WHERE id=".db_escape($id);
    
    db_query($sql,"Table value could not be deleted");
}

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

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

	return db_fetch($result);
}

function get_all_table_values($table_id)
{
	$sql="SELECT vt.*,   
                        IFNULL((SELECT stn.value_name 
                            FROM ".TB_PREF."list_values stn 
                            WHERE stn.id = vt.salary_total_id),'') salary_total_name  
                FROM ".TB_PREF."value_tables vt   
		WHERE table_id = ".db_escape($table_id);

	$result = db_query($sql,"All table values could not be retrieved");

	return $result;
}


?>