<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Attendants_Model extends CI_Model{
    
    var $tableName = 'attendants';
    
    function __construct() {
        parent::__construct();
    }
	
	
	
	
	
	     
    public function get_tmp_attendants($tam_id=''){
		//echo "warehouse_id:$warehouse_id";
        $this->db->select('ta.*');
		$this->db->from('tmp_attendants ta');
		
       // $this->db->join('user', 'salary.user_id = user.user_id');
       
		if($tam_id){
			$this->db->where("ta.tam_id","$tam_id");
		}
		
		
		 $this->db->order_by('ta.ta_id','asc');
        $query = $this->db->get();
		//echo $this->db->last_query();
        return $query->result();
    }
	
	
    
    public function get_attendants_list($start='',$length='',$search_key_val='',$srh_user_id='',$warehouse_id='',$srh_from_date='',$srh_to_date=''){
        $this->db->select('attendants.*,user.user_last_name,user.user_first_name');
//        $this->db->select('attendants.*');
        $this->db->join('user', 'attendants.user_id = user.user_id');
//        $this->db->join('mstr_sal_type', 'attendants.mstr_sal_type_id = mstr_sal_type.mstr_sal_type_id');
		if($search_key_val){		
            $this->db->where("attendants.warehouse_id = $warehouse_id AND (user.user_first_name LIKE '%$search_key_val%')");	
       	}
		if($srh_user_id){
			$this->db->where("user.user_id","$srh_user_id");
		}
		if($warehouse_id){
			$this->db->where("attendants.warehouse_id","$warehouse_id");
		}
		if($srh_to_date){
			$srh_to_date=date('Y-m-d',strtotime($srh_to_date));
			$this->db->where("date(attendants.att_date_time) <=",$srh_to_date);//("id !=",$id);
		}
		if($srh_from_date){
			$srh_from_date=date('Y-m-d',strtotime($srh_from_date));
			$this->db->where("date(attendants.att_date_time) >=",$srh_from_date);//("id !=",$id);
		}
		if($start!='' && $length!=''){
            if($length>0) $this->db->limit($length,$start);
        }
		// $this->db->order_by('attendants.att_date_time','desc');
		 $this->db->order_by('attendants.att_id','desc');
        $query = $this->db->get($this->tableName);
		//echo $this->db->last_query();
        return $query->result_array();
    }
    
    public function get_salary_pay_details($att_id){
        $this->db->select('attendants.*,user.user_first_name,ug.user_group_name');
        $this->db->join('user', 'attendants.user_id = user.user_id');
        $this->db->join('user_group ug', 'user.group_id = ug.user_group_id', 'left');
        $this->db->where('att_id', $att_id);
        $query = $this->db->get($this->tableName);
        return $query->row_array();
    }
    
    public function save_attendants($salary_pay_id, $data){
        if (!$salary_pay_id) {
//            echo 'addmodel';
            $this->db->insert($this->tableName, $data);
        } else {
//            echo 'edit_model';
            $this->db->where('att_id', $salary_pay_id);
            return $this->db->update($this->tableName, $data);
        }
    }
	
	
	    public function save_tmp_attendants($ta_id, $data){
        if (!$ta_id) {
//            echo 'addmodel';
            $this->db->insert('tmp_attendants', $data);
        } else {
//            echo 'edit_model';
            $this->db->where('ta_id', $ta_id);
            return $this->db->update('tmp_attendants', $data);
        }
    }
	
	
	 public function save_tmp_attendants_mst($tam_id, $data){
        if (!$tam_id) {
//            echo 'addmodel';
            $this->db->insert('tmp_attendants_mst', $data);
        } else {
//            echo 'edit_model';
            $this->db->where('tam_id', $tam_id);
            return $this->db->update('tmp_attendants_mst', $data);
        }
    }
	
    
    public function delete_attendants($id){
        $this->db->where('sp_id', $id);

        $this->db->delete($this->tableName);
    }
}