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

class Model_claim_mileage extends CI_Model {

	function fetchMileage($user_id = null,$claim_id = null,$year = null,$month = null)
	{
		$this->db->select('a.*');
		if($user_id)$this->db->where('b.user_id',$user_id);
		if($claim_id)$this->db->where('a.claim_id',$claim_id);
		if($year)$this->db->where('b.year',$year);
		if($month)$this->db->where('b.month',$month);
		$this->db->where('a.is_delete',0);

		if($user_id || $year || $month) $this->db->join('hr__claim b','b.id = a.claim_id');

		$query = $this->db->get('hr__claim_mileage a');

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

	function fetchSingleMileage($user_id = null,$claim_id = null,$year = null,$month = null)
	{
		$dentals = $this->fetchDental($user_id,$claim_id,$year,$month);
		return ($dentals)?$dentals[0]:null;
	}

	function createMileage($input)
	{
		$this->db->insert('hr__claim_mileage',$input);
		return $this->db->insert_id();
	}

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

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

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

	function removeMileage($id)
	{
		$data 	= array('is_delete' => 1);
		$where 	= array('id' => $id);
		$this->updateMileage($data,$where);	
	}

	function getAttachment($id)
	{
		$this->db->where('is_delete',0);
		$this->db->where('mileage_id',$id);
		$query = $this->db->get('hr__claim_mileage_attachment');

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