<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class employee_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->employee = TBL_EMPLOYEE;
    }
    
    public function list_all() {
        $query = ', CASE WHEN ISNULL(dob) THEN "" ELSE DATE_FORMAT(dob,"%d-%m-%Y") END as dob, CASE WHEN ISNULL(date_of_join) THEN "" ELSE DATE_FORMAT(date_of_join,"%d-%m-%Y") END as date_of_join';
        $this->db->select('*, CASE status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str' . $query ,FALSE);
        $this->db->where('status !=', 2);
        $this->db->order_by('id', 'desc');
        return $this->db->get($this->employee)->result();
    }
    
    public  function insert($data){  
        $this->db->insert($this->employee, $data);  
        return $this->db->insert_id();
    }   
    
    public function update($data, $id){
        $this->db->where('id', $id);
        $this->db->update($this->employee, $data);
        return $this->db->affected_rows();
    }
    
    public function get_by_id($id) {
        $query = ', CASE WHEN ISNULL(dob) THEN "" ELSE DATE_FORMAT(dob,"%d-%m-%Y") END as dob';
        $query .= ', CASE WHEN ISNULL(date_of_join) THEN "" ELSE DATE_FORMAT(date_of_join,"%d-%m-%Y") END as date_of_join';
        $query .= ', CASE WHEN ISNULL(passport_valid_to) THEN "" ELSE DATE_FORMAT(passport_valid_to,"%d-%m-%Y") END as passport_valid_to';
        $query .= ', CASE WHEN ISNULL(date_of_app) THEN "" ELSE DATE_FORMAT(date_of_app,"%d-%m-%Y") END as date_of_app';
        $query .= ', CASE WHEN ISNULL(date_of_expired) THEN "" ELSE DATE_FORMAT(date_of_expired,"%d-%m-%Y") END as date_of_expired';
        $query .= ', CASE WHEN ISNULL(date_of_renewal) THEN "" ELSE DATE_FORMAT(date_of_renewal,"%d-%m-%Y") END as date_of_renewal';
        $query .= ', CASE WHEN ISNULL(insurance_valid_to) THEN "" ELSE DATE_FORMAT(insurance_valid_to,"%d-%m-%Y") END as insurance_valid_to';
        
        $this->db->select('*, CASE status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str' . $query, FALSE);
        $this->db->where('id', $id);
        return $this->db->get($this->employee)->row();
    }
   
    /* public function employee_name_exists($name = '', $id = '') {
        $this->db->where('name', trim($name));
        if($id != '')
            $this->db->where('id !=', $id);
       
        $this->db->where('status !=', 2);
        if($this->db->get($this->employee)->num_rows())
            return 1;
        else 
            return 0;
    } */
   
    public function employee_key_exists($url_key, $id) {
        if (!empty($id)) {
            $this->db->where('id !=', $id);
        }
        $this->db->where('url_key', $url_key);
        return $this->db->get($this->employee)->num_rows();
    }
    
    public function check_fields_exists($fields,$val,$id) {
        if($fields == 'passport_no') {
           $this->db->where('passport_no',$val); 
        } else if($fields == 'work_permit_no'){
           $this->db->where('work_permit_no',$val);
        } else {
            $this->db->where('security_bond_no',$val);
        }
        
        if(!empty($id)) 
            $this->db->where('id !=',$id);
        
        $this->db->where('status !=',2);
        $getData = $this->db->get($this->employee);
        if($getData->num_rows() > 0)
            return 1;
        
        else
            return 0;
        
    }
}
?>