<?php
/**********************************************************************
***********************************************************************/
function update_employee_department($selected, $department_id, $rate_category_id, $start_date, $end_date)
{
    $user = $_SESSION['wa_current_user']->user;
    $date = date('Y-m-d H:i:s');
    
    if($selected != '')
    {
        $sql = "UPDATE ".TB_PREF."employee_departments 
                SET
                    department_id = ".db_escape($department_id).",
                    rate_category_id = ".db_escape($rate_category_id).",
                    start_date=".db_escape(date2sql($start_date)).",
                    end_date=".db_escape(date2sql($end_date)).",
                    updated_by = ".db_escape($user).",
                    updated_on = ".db_escape($date)."
                WHERE 
                    id = ".db_escape($selected);
        
        db_query($sql, "Employee Department could not be updated");
    }
}

function add_employee_department($employee_code, $department_id, $rate_category_id, $start_date, $end_date)
{
    $user = $_SESSION['wa_current_user']->user;
    $date = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."employee_departments
                        (employee_code, 
                        department_id, rate_category_id, 
                        start_date, end_date, 
                        created_by, created_on) 
                VALUES
                        (".db_escape($employee_code).","
                        .db_escape($department_id).",".db_escape($rate_category_id).","
                        .db_escape(date2sql($start_date)).",".db_escape(date2sql($end_date)).","
                        .db_escape($user).",".db_escape($date).")";
        
        db_query($sql, "Employee Department could not be added");    
}
////
function delete_employee_department($id)
{
	$sql="DELETE FROM ".TB_PREF."employee_departments 
                    WHERE 
                        id=".db_escape($id);

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

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

function get_all_employee_departments($employee_code) {
    $sql = "SELECT ed.*,
                    lv.value_name department_name,
                    er.description rate_category_name 
        FROM ".TB_PREF."employee_departments ed, 
                ".TB_PREF."employee_rates er, 
                ".TB_PREF."list_values lv
        WHERE 
            ed.department_id = lv.id AND 
            ed.rate_category_id = er.id AND 
            ed.employee_code = ".  db_escape($employee_code);
        
    return  db_query($sql, "Could not get employee departments");
}

function get_employee_curent_departments($employee,$to_date)
{
        $sql = "SELECT  ed.*, 
                    IFNULL(d.value_name,'No Department Entered') department_name                    
            FROM ".TB_PREF."employee_departments ed 
                    INNER JOIN ".TB_PREF."employee_master em ON 
                            ed.employee_code = em.employee_code 
                    INNER JOIN ".TB_PREF."list_values d ON 
                            ed.department_id = d.id                     
                    WHERE ed.start_date <= ".db_escape($to_date)." AND 
                          ed.end_date >= ".db_escape($to_date)." AND
                          ed.employee_code =". db_escape($employee)." 
                    ORDER BY ed.start_date DESC LIMIT 1";
        
        $result=db_query($sql,"an employee could not be retreived");
        return db_fetch($result);
    
    
}

?>