<?php
/**********************************************************************
***********************************************************************/
function update_employee($employee_code, $first_name, 
                $middle_name, $last_name,
                $initials,
				$date_of_birth, $nic,
				$passport, $passport_expiry_date, 
				$dl_no, $dl_expiry_date,
				$gender, $marital_status, 
				$nationality, $employee_type_id, $salary_group_id, $address,
                $joined_date , $resigned_date, $resigned_yesno,
				$work_telephone_no, $home_telephone_no, 
				$mobile_no, $office_mobile_no, $emergency_contact_details,
				$work_email_id, $other_email_ids, 
				$bank_account_name, $bank_account_no,
				$bank_id, $bank_branch_id, 
				$epf_no, $include_in_bank_file,$bank_basic_salary,$bank_incentive_salary, 
                                $inactive)
{
	                
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."employee_master 
                SET first_name = ".db_escape($first_name).", 
                    middle_name=".db_escape($middle_name).",
                    last_name=".db_escape($last_name).",
                    initials=".db_escape($initials).",
                    date_of_birth=".db_escape($date_of_birth).",
                    nic=".db_escape($nic).",
                    passport=".db_escape($passport).",
                    passport_expiry_date=".db_escape($passport_expiry_date).",
                    dl_no=".db_escape($dl_no).",
                    dl_expiry_date=".db_escape($dl_expiry_date).",
                    gender=".db_escape($gender).",
                    marital_status=".db_escape($marital_status).",
                    nationality=".db_escape($nationality).",
                    employee_type_id=".db_escape($employee_type_id).",
                    salary_group_id=".db_escape($salary_group_id).",    
                    address=".db_escape($address).",
                    date_of_joining=".db_escape($joined_date).",
                    date_of_resignation=".db_escape($resigned_date).",
                    resigned=".db_escape($resigned_yesno).",
                    work_telephone_no=".db_escape($work_telephone_no).",
                    home_telephone_no=".db_escape($home_telephone_no).",
                    mobile_no=".db_escape($mobile_no).",
                    office_mobile_no=".db_escape($office_mobile_no).",
                    emergency_contact_details=".db_escape($emergency_contact_details).",
                    work_email_id=".db_escape($work_email_id).",
                    other_email_ids=".db_escape($other_email_ids).",
                    bank_account_name=".db_escape($bank_account_name).",
                    bank_account_no=".db_escape($bank_account_no).",
                    bank_id=".db_escape($bank_id).",
                    bank_branch_id=".db_escape($bank_branch_id).",
                    epf_no=".db_escape($epf_no).",
                    include_in_bank_file=".db_escape($include_in_bank_file).",
                    bank_basic_salary=".db_escape($bank_basic_salary)." , 
                    bank_incentive_salary=".db_escape($bank_incentive_salary).",
                    inactive=".db_escape($inactive).",   
                    updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime)."
                WHERE employee_code=".db_escape($employee_code);

	db_query($sql, "The employee could not be updated");

}

function add_employee($employee_code, $first_name, 
                $middle_name, $last_name, 
                $initials,
				$date_of_birth, $nic,
				$passport, $passport_expiry_date, 
				$dl_no, $dl_expiry_date,
				$gender, $marital_status, 
				$nationality, $employee_type_id, $salary_group_id, $address,
                $joined_date , $resigned_date, $resigned,
				$work_telephone_no, $home_telephone_no, 
				$mobile_no, $office_mobile_no, $emergency_contact_details,
				$work_email_id, $other_email_ids, 
				$bank_account_name, $bank_account_no,
				$bank_id, $bank_branch_id, 
				$epf_no, $include_in_bank_file,$bank_basic_salary,$bank_incentive_salary,
                $inactive)
{
	$created_by = $_SESSION['wa_current_user']->user;
	$created_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."employee_master (
                    employee_code, first_name, 
                    middle_name, last_name, 
                    initials,
                    date_of_birth, nic,
                    passport, passport_expiry_date, 
                    dl_no, dl_expiry_date,
                    gender, marital_status, 
                    nationality, employee_type_id, 
                    salary_group_id, address,
                    date_of_joining, date_of_resignation, resigned,
                    work_telephone_no, home_telephone_no, 
                    mobile_no, office_mobile_no, emergency_contact_details,
                    work_email_id, other_email_ids, 
                    bank_account_name, bank_account_no,
                    bank_id, bank_branch_id, 
                    epf_no, include_in_bank_file,bank_basic_salary,bank_incentive_salary,
                    inactive, 
                    created_by, created_on)
		VALUES ("
                .db_escape($employee_code).", ".db_escape($first_name).","
                .db_escape($middle_name).", ".db_escape($last_name).","
                .db_escape($initials).","
                .db_escape($date_of_birth).", ".db_escape($nic).","
                .db_escape($passport).", ".db_escape($passport_expiry_date).","
                .db_escape($dl_no).", ".db_escape($dl_expiry_date).","
                .db_escape($gender).",".db_escape($marital_status).","
                .db_escape($nationality).",".db_escape($employee_type_id).","
                .db_escape($salary_group_id).",".db_escape($address).","
                .db_escape($joined_date).", ".db_escape($resigned_date).", ".db_escape($resigned).","
                .db_escape($work_telephone_no).", ".db_escape($home_telephone_no).","
                .db_escape($mobile_no).", ".db_escape($office_mobile_no).", ".db_escape($emergency_contact_details).","
                .db_escape($work_email_id).", ".db_escape($other_email_ids).","
                .db_escape($bank_account_name).", ".db_escape($bank_account_no).","
                .db_escape($bank_id).", ".db_escape($bank_branch_id).","
                .db_escape($epf_no).",".db_escape($include_in_bank_file).","
                .db_escape($bank_basic_salary).",".db_escape($bank_incentive_salary).","
                .db_escape($inactive).","
                .db_escape($created_by).",".db_escape($created_on).")";

	db_query($sql, "The employee could not be added");

}

function get_employee($employee_code)
{
	$sql = "SELECT * FROM ".TB_PREF."employee_master WHERE employee_code=".db_escape($employee_code);
	$result = db_query($sql,"an employee could not be retreived");

	return db_fetch($result);
}

function get_employee_record_by_id($employee_id)
{
	$sql = "SELECT * FROM ".TB_PREF."employee_master WHERE employee_id=".db_escape($employee_id);
	$result = db_query($sql,"get_employee_record_by_idd");

	return db_fetch($result);
}

function get_employees($employee_code = '')
{
	$sql = "SELECT employee_code, initials, first_name, last_name 
                FROM ".TB_PREF."employee_master ";
    
    if($employee_code != ''){
        $sql .= "WHERE employee_code = ".db_escape($employee_code)." ";
    }
    
    $sql .= "ORDER BY employee_code, last_name, first_name ";
    
	return db_query($sql,"employees could not be retreived");
}

function get_employees_in_batches($start, $length, $employee_code = null)
{
	$sql = "SELECT employee_code, initials, first_name, last_name "
                . "FROM ".TB_PREF."employee_master ";
    
    if($employee_code != null){
        $sql .= " WHERE employee_code = ".db_escape($employee_code);
    }
    
    $sql .= " ORDER BY employee_code, last_name "
                . "LIMIT ".$start.",".$length;
	return db_query($sql,"employees could not be retreived");
}

function get_employee_count($employee_code = null)
{
	$sql = "SELECT COUNT(employee_code) no_of_employees "
                . "FROM ".TB_PREF."employee_master ";
    
    if($employee_code != null){
        $sql .= " WHERE employee_code = ".db_escape($employee_code);
    }
	
        $result = db_query($sql,"Employees could not be counted");
        
        return db_fetch($result);
}

function check_nic($nic)
{
    $sql = "SELECT * FROM ".TB_PREF."employee_master WHERE nic=".db_escape($nic)." AND resigned = 0 ";
    
    $result = db_query($sql,"An employee could not be retreived");
    return db_fetch($result);
}

function check_bank_account_no($bank_id, $bank_account_no, $employee_code = null)
{
    $sql = "SELECT * FROM ".TB_PREF."employee_master 
           WHERE bank_id=".db_escape($bank_id)." AND
                    bank_account_no=".db_escape($bank_account_no);
    
    if($employee_code){
        $sql .= " AND employee_code <> ".db_escape($employee_code);
    }
    
    $result = db_query($sql,"An employee could not be retreived");
    return db_fetch($result);
}

?>
