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

class Model_claim_toll extends CI_Model 
{
    public function fetchToll($claim_id)
    {
        $this->db->select('a.*');
        $this->db->where('a.claim_id',$claim_id);
        $this->db->where('a.is_delete',0);
        
        $result = $this->db->get('hr__claim_toll a')->result();
        
        if($result)
        {
            foreach($result as $key => $value)
            {
                $result[$key]->attachments          = $this->fetchTollAttachment($value->id);
                $result[$key]->staff_movements      = $this->fetchTollMovement($value->id);
            }
            return $result;
        }else{
            return null;
        }
    }

    public function fetchTollMovement($claim_toll_id)
    {
        $this->db->select('a.*,b.date,b.description,b.destination,c.movementType as type');
        $this->db->where('a.claim_toll_id',$claim_toll_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_toll_movement a');
        return ($query->result())?$query->result():null;
    }

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

    public function create($data)
    {
        $this->db->insert('hr__claim_toll',$data);
        return $this->db->insert_id();
    }
    
    public function createAttachment($data)
    {
        $this->db->insert_batch('hr__claim_toll_attachment',$data);
        return true;
    }

    public function createMovement($data)
    {
        $this->db->insert_batch('hr__claim_toll_movement',$data);
        return true;
    }

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

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

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