<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_claim_travel extends CI_Model {

	public function fetchTravelClaim($user_id = null,$claim_id = null,$year = null,$month = null)
	{
		$this->db->select('a.*,sum(c.amount) as amount_claim');

		if($year)$this->db->where('b.year',$year);
		if($month)$this->db->where('b.month',$month);
		if($user_id)$this->db->where('b.user_id',$user_id);
		if($claim_id)$this->db->where('a.claim_id',$claim_id);

		$this->db->where('a.is_delete',0);

		if($user_id || $year || $month)$this->db->join('hr__claim b','b.id = a.claim_id');
		$this->db->join('hr__claim_travelling_detail c','c.claim_travelling_id = a.id','left');
		$this->db->group_by('id');
		$query = $this->db->get('hr__claim_travelling a');

		return ($query->result())?$query->result():null;
	}

	public function fetchSingleTravel($user_id = null,$claim_id = null,$year = null,$month = null)
	{
		$travel = $this->fetchTravelClaim($user_id,$claim_id,$year,$month);
		return ($travel)?$travel[0]:null;
	}

	public function fetchTravelDetail($id)
	{
		$this->db->select('a.*,b.description as claim_type,b.icon as claim_type_icon');
		$this->db->where('a.claim_travelling_id',$id);
		$this->db->join('_ref__claim_type_detail b','b.id = a.claim_type_detail_id');
		$query = $this->db->get('hr__claim_travelling_detail a');
		return ($query->result())?$query->result_array():null;
	}

	public function createTravel($input)
	{
		$this->db->insert('hr__claim_travelling',$input);
		return $this->db->insert_id();
	}

	public function updateTravel($input,$where = array())
	{
		$this->db->set($input);
		foreach ($where as $key => $wh) {
			$this->db->where($key,$wh);
		}

		$this->db->update('hr__claim_travelling');
	}

	public function removeTravel($id)
	{
		$data 	= array('is_delete' => 1);
		$where 	= array('id' => $id);
		$this->updateTravel($data,$where);
		return true;
	}

	//items
	public function createItem($input)
	{
		return ($this->db->insert_batch('hr__claim_travelling_detail',$input))?true:false;
	}

	//attachments
	public function createAttachment($input)
	{
		return ($this->db->insert_batch('hr__claim_travelling_attachment',$input))?true:false;
	}

	public function getAttachment($travel_id)
	{
		$this->db->select('a.*');
		$this->db->where('a.travelling_id',$travel_id);
		$query = $this->db->get('hr__claim_travelling_attachment a');
		return ($query->num_rows() > 0)?$query->result():null;
	}

	//travel route 
	public function createRoute($input)
	{
		return ($this->db->insert_batch('hr__claim_travelling_route',$input))?true:false;
	}

	public function getRoute($travel_id)
	{
		$this->db->select('a.*');
		$this->db->where('a.travel_id',$travel_id);
		$query = $this->db->get('hr__claim_travelling_route a');
		return ($query->num_rows() > 0)?$query->result():null;
	}


	/**
	 * INCIDENTAL EXPENSES
	 *  1. Accomodation
	 */
	//accomodation
	public function fetchAccomodationEntitlement($user_id)
	{
		$this->db->select('a.*');
		$this->db->where('a.user_id',$user_id);
		$result = $this->db->get('view_user_accomodation_entitlement a');
		return ($result->num_rows() > 0)?$result->result():null;
	}

	public function fetchAttachmentbyUniqueid($unique_id)
	{
		$this->db->select('a.*');
		$this->db->where('a.unique_id',$unique_id);
		$query = $this->db->get('hr__claim_travelling_attachment a');

		return ($query->result())?$query->result():null;
	}
}
?>