<?php
/**********************************************************************
***********************************************************************/
function write_employee_rates($selected, $rate_category, $rate)
{
    $user = $_SESSION['wa_current_user']->user;
    $date = date('Y-m-d H:i:s');
    
    if($selected != '')
    {
        $sql = "UPDATE ".TB_PREF."employee_rates 
                SET
                    description = ".db_escape($rate_category).",
                    rate = ".db_escape($rate).",
                    updated_by = ".db_escape($user).",
                    updated_on = ".db_escape($date)."
                WHERE 
                    id = ".db_escape($selected);
        
        db_query($sql, "Employee Rate could not be updated");
    }
    else
    {
        $sql = "INSERT INTO ".TB_PREF."employee_rates
                        (description,rate,created_by, created_on) 
                VALUES
                        (".db_escape($rate_category).",".db_escape($rate).","
                            .db_escape($user).",".db_escape($date).")";
        
        db_query($sql, "Employee Rate could not be added");
    }
    
}
////
function delete_employee_rates($id)
{
	$sql="DELETE FROM ".TB_PREF."employee_rates 
                    WHERE 
                        id=".db_escape($id);

	db_query($sql,"Selected employee rate could not be deleted");
}
////
function get_employee_rates($id)
{
	$sql="SELECT * FROM ".TB_PREF."employee_rates 
                WHERE 
                    id=".db_escape($id);

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

function get_all_employee_rates() {
    $sql = "SELECT * FROM ".TB_PREF."employee_rates ";
        
    return  db_query($sql, "Could not get employee rates");
}

function add_employee_rate($emp_rate_category, $from_date, $unit, $rate, $comment) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."employee_rates (emp_rate_category, from_date, rate,  
                                                       unit, comment, created_by, created_on) 
                VALUES  (".db_escape($emp_rate_category).", ".db_escape(date2sql($from_date)).", ".db_escape($rate).", 
                         ".db_escape($unit).", ".db_escape($comment).", ".db_escape($created_by).", ".db_escape($created_on).")";
        
        db_query($sql, "add_employee_rate");
        return db_insert_id();
}

function update_employee_rate($selected_id, $from_date, $unit, $rate, $comment) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."employee_rates 
                SET unit = ".db_escape($unit).",
                    rate = ".db_escape($rate).",
                    from_date = ".db_escape(date2sql($from_date)).",
                    comment = ".db_escape($comment).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)."
                WHERE id = ".db_escape($selected_id);

            db_query($sql, "update_employee_rate");
            return db_num_affected_rows();
}

function get_employee_rates_details($emp_rate_category) {
    $sql = "SELECT er.*, lv.value_name rate_category_name 
            FROM ".TB_PREF."employee_rates er 
                JOIN ".TB_PREF."list_values lv  
                    ON er.emp_rate_category = lv.id 
            WHERE emp_rate_category = ".db_escape($emp_rate_category);
        
    return  db_query($sql, "get_employee_rates_details");
}

function get_employee_rate_on_date_and_category($date, $emp_rate_category) {
    $sql = "SELECT * 
            FROM ".TB_PREF."employee_rates  
            WHERE emp_rate_category = ".db_escape($emp_rate_category)." 
                AND from_date <= ".db_escape(date2sql($date))." 
            ORDER BY from_date DESC
            LIMIT 1";

	$result = db_query($sql, "get_employee_rate_on_date_and_category");
	return db_fetch($result);
}

?>