<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Model_claim_entertainment extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    public function fetch_entertainment($user_id,$claim_id,$year,$month)
    {
        $results = $this->db->select('a.*')
                            ->where('a.claim_id',$claim_id)
                            ->where('a.is_delete',0)
                            ->where('a.month',$month)
                            ->where('a.user_id',$user_id)
                            ->get('hr__claim_entertainment a')
                            ->result();
        if(sizeof($results) > 0)
        {
            foreach ($results as $key => $result) 
            {
                $results[$key]->attachments = $this->fetch_attachment($result->id);
            }
        }
        return (sizeof($results))?$results:null;
    }
    
    public function fetch_attachment($claim_entertainment_id)
    {
        $results = $this->db->select('a.*')
                            ->where('a.claim_entertainment_id',$claim_entertainment_id)
                            ->get('hr__claim_entertainment_attachment a')
                            ->result();
        return (sizeof($results))?$results:null;
    }

    public function create($inputs)
    {
        return ($this->db->insert('hr__claim_entertainment',$inputs))?$this->db->insert_id():false;
    }

    public function create_attachments($inputs)
    {
        return ($this->db->insert('hr__claim_entertainment_attachment',$inputs))?$this->db->insert_id():null;
    }

    public function update_entertainment($sets = array(), $where = array())
    {
        $this->db->set($sets);
		foreach ($where as $key => $wh) {
			$this->db->where($key,$wh);
		}
        $this->db->update('hr__claim_entertainment');
    }

    function getAttachmentByUniqueid($unique_id)
	{
		$this->db->select('a.*');
		$this->db->where('a.unique_id',$unique_id);
		$query = $this->db->get('hr__claim_entertainment_attachment a');
		return ($query->result())?$query->result():null;
	}
    
}
?>