<?php
/**********************************************************************
***********************************************************************/

function add_employee_project_work($company_code, $employee_code, $project_id, 
                                    $month_id, $no_of_days , $comment)
{

        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
        
        
	$sql = "INSERT INTO ".TB_PREF."employee_project_work (
                    company_code, employee_code, 
                    project_id,  
                    month_id, no_of_days,
                    comment,
                    created_by, created_on) 
                VALUES ("
                    .db_escape($company_code).", ".db_escape($employee_code). ", "
                    .db_escape($project_id). ", "
                    .db_escape($month_id).", ".db_escape($no_of_days). ", "
                    .db_escape($comment). ", "
                    .db_escape($created_by).", ".db_escape($created_on).")";
        
        db_query($sql,"add_employee_project_work");
        
}

function update_employee_project_work($selected_id, $project_id, 
                                    $month_id, $no_of_days, 
                                    $comment)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."employee_project_work 
                SET 
                    month_id=".db_escape($month_id) . ",
                    no_of_days=".db_escape($no_of_days) . ",
                    project_id=".db_escape($project_id) . ",
                    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_project_work");
	
}

function delete_employee_project_work($selected_id)
{
    
	$sql = "DELETE FROM ".TB_PREF."employee_project_work WHERE id=".db_escape($selected_id);
        
	db_query($sql,"delete_employee_project_work");
}

function get_employee_month_project_work($company_code, $employee_code, $month_id)
{
    $sql = "SELECT epw.*,
                    pm.description project_description 
            FROM ".TB_PREF."employee_project_work epw 
                JOIN ".TB_PREF."projects_master pm  
                    ON epw.project_id = pm.project_id 
            WHERE epw.company_code = ".db_escape($company_code)." 
                AND epw.employee_code = ".db_escape($employee_code)." 
                AND epw.month_id = ".db_escape($month_id);

    $sql .= " ORDER By epw.id" ;
                    
    return db_query($sql, "get_employee_month_project_work");
}

function get_employee_project_work($selected_id)
{
    $sql = "SELECT * 
            FROM 
                ".TB_PREF."employee_project_work  
            WHERE id = ".$selected_id ;

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

    return db_fetch($result);
}


function get_existing_employee_project_work($company_code, $employee_code, $from_date, $to_date, $selected_id)
{
    $sql = "SELECT *
            FROM 
                ".TB_PREF."employee_project_work  
            WHERE 
		company_code = ".db_escape($company_code)." AND 
                employee_code = ".db_escape($employee_code)." AND 
                ((".db_escape($from_date)." <= from_date AND from_date <= ".db_escape($to_date).") OR 
                (".db_escape($from_date)." <= to_date AND to_date <= ".db_escape($to_date)."))";
    
    if($selected_id > 0)
    {
        $sql .= " AND id <> ".$selected_id;
    }

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

    return db_fetch($result);
}

function update_leave_scheduled($selected_id,   
                                    $leave_scheduled)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."employee_project_work 
                SET 
                    leave_scheduled=".db_escape($leave_scheduled) . ",
                    updated_by=".db_escape($updated_by) . ",
                    updated_on=".db_escape($updated_on) . " 
		WHERE id=".db_escape($selected_id);
        
	db_query($sql,"Scheduled leave could not be updated");
}


function check_employee_project_work_used($selected_id){
        
    $sql = "SELECT employee_project_work_id 
            FROM 
                ".TB_PREF."leaves  
            WHERE employee_project_work_id = ".db_escape($selected_id)."  
                LIMIT 1";

    $result = db_query($sql, "Used leave entitlement could not be retrieved");

    return db_fetch($result);
}

function update_employee_project_work_leave_used($employee_project_work_id, $leave_used)
{
    $sql = "UPDATE ".TB_PREF."employee_project_work 
                        SET leave_used = ".  db_escape($leave_used)."   
                        WHERE id=".db_escape($employee_project_work_id);
    
    db_query($sql, "Leave entitlement could not be update");
    
    return db_num_affected_rows();
    
}
 





?>