<?php
/**********************************************************************
***********************************************************************/
function add_employment_detail($employee_code, $designation, 
                                $employment_type_id, $job_category_id,
                                $work_start_date, $contract_start_date,
                                $contract_end_date)
{
	$created_by = $_SESSION['wa_current_user']->user;
	$created_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."employment_details (
                    employee_code, 
                    designation, 
                    employment_type_id, 
                    job_category_id, 
                    work_start_date, 
                    contract_start_date, 
                    contract_end_date, 
                    created_by, 
                    created_on) 
		VALUES ("
                .db_escape($employee_code).", "
                .db_escape($designation).", "
                .db_escape($employment_type_id).", "
                .db_escape($job_category_id).", "
                .db_escape($work_start_date).", "
                .db_escape($contract_start_date).", "
                .db_escape($contract_end_date).", "
                .db_escape($created_by).", "
                .db_escape($created_on).")";
	
	db_query($sql,"Employment detail could not be added");		
}

function update_employment_detail($selected_id, $designation, 
                                $employment_type_id, $job_category_id,
                                $work_start_date, $contract_start_date,
                                $contract_end_date)
{
	                
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
     	
    $sql = "UPDATE ".TB_PREF."employment_details "
                . "SET "
                    . "designation=".db_escape($designation).","
                    . "employment_type_id=".db_escape($employment_type_id).","
                    . "job_category_id=".db_escape($job_category_id).", "
                    . "work_start_date=".db_escape($work_start_date).","
                    . "contract_start_date=".db_escape($contract_start_date).","
                    . "contract_end_date=".db_escape($contract_end_date).","
                    . "updated_by=".db_escape($updated_by).","
                    . "updated_on=".db_escape($updated_on)
                . "WHERE "
                    . "id=".db_escape($selected_id);
	
	db_query($sql,"Employment detail could not be updated");		
}

function delete_employment_detail($id)
{
	$sql="DELETE FROM ".TB_PREF."employment_details WHERE id= ".db_escape($id);
    
	db_query($sql,"Employment detail record could not be deleted");			
}

function get_employment_details($employee_code)
{
	$sql = "SELECT 
                    ed.*, 
                    et.value_name employment_type_name, 
                    jc.value_name job_category    
		FROM "
                    .TB_PREF."employment_details ed, "
                    .TB_PREF."list_values et, "
                    .TB_PREF."list_values jc 
                WHERE 
                    ed.employment_type_id = et.id AND 
                    ed.job_category_id = jc.id 
                    AND ed.employee_code=".db_escape($employee_code)." 
                ORDER BY ed.employee_code";	
	
	return db_query($sql,"Employment details could not be retreived");
}

function get_employment_detail($selected_id)
{
	$sql = "SELECT 
                    ed.*, 
                    et.value_name employment_type, 
                    jc.value_name job_category    
		FROM "
                    .TB_PREF."employment_details ed, "
                    .TB_PREF."list_values et, "
                    .TB_PREF."list_values jc 
                WHERE 
                    ed.employment_type_id = et.id AND 
                    ed.job_category_id = jc.id 
                    AND ed.id=".db_escape($selected_id)." 
                ORDER BY ed.work_start_date DESC";	
	
	return db_query($sql,"Employment details could not be retreived");
}

function get_employee_current_designation($employee,$to_date)
{
    
        $sql = "SELECT IFNULL(edl.designation,'No Designation Entered') designation
            FROM ".TB_PREF."employment_details edl,
                 ".TB_PREF."employee_master em                
            WHERE em.employee_code = edl.employee_code AND 
                    edl.contract_start_date <= ".db_escape($to_date)." AND 
                    edl.contract_end_date >= ".db_escape($to_date)." AND 
                    em.employee_code =". db_escape($employee)."
                    ORDER BY edl.contract_start_date DESC LIMIT 1";
                
        
        $result=db_query($sql,"an employee could not be retreived");
        return db_fetch($result);
    
}


?>