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

class Model_claim_accomodation extends CI_Model 
{

    public function fetchAccomodation($claim_id = null)
    {

        $this->db->select('a.*,a.description as claim_description,b.attachment_name,b.file_type,b.file_ext,b.description,b.location,b.unique_id');
        if($claim_id) $this->db->where('a.claim_id',$claim_id); 
        $this->db->where('a.is_delete',0);
        $this->db->join('hr__claim_accommodation_receipts b','b.accommodation_id = a.id','left');
        
        $query = $this->db->get('hr__claim_accommodation a');
        $result = $query->result();
        if($result)
        {
            foreach($result as $key => $value)
            {
                $staff_movements = $this->fetchAccomodationMovement($value->id);
                $result[$key]->staff_movements = $staff_movements;
            }
        }else{
            return null;
        }
        return ($query->result() > 0)?$query->result():null;
    }

    public function fetchAccomodationMovement($accomodation_id)
    {
        $this->db->select('a.*,b.date,b.description,b.destination,c.movementType as type');
        $this->db->where('a.accommodation_id',$accomodation_id);
        $this->db->join('hr__movement_group b','b.id = a.movement_id');
        $this->db->join('hr__movement_type c','c.id = b.movementType');
        $query = $this->db->get(' hr__claim_accomodation_movement a');
        return ($query->result())?$query->result():null;
    }

    public function create($data)
    {
        $this->db->insert('hr__claim_accommodation',$data);
		return $this->db->insert_id();
    }

    public function createReceipt($data)
    {
        $this->db->insert('hr__claim_accommodation_receipts',$data);
        return $this->db->insert_id();
    }

    public function createBatchMovement($data)
    {
        $this->db->insert_batch('hr__claim_accomodation_movement',$data);
    }

    public function updateClaim($input,$where)
    {
		$this->db->set($input);
		foreach ($where as $key => $wh) {
			$this->db->where($key,$wh);
		}
        $this->db->update('hr__claim_accommodation');
        
    }

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