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

class Model_claim_transportation extends CI_Model {

    public function fetchTransportation($claim_id)
    {
        $this->db->select('a.*,b.type as transportation_type');
        $this->db->where('a.claim_id',$claim_id);
        $this->db->where('a.is_delete',0);
        $this->db->join('hr__ref_claim_travelling_vehicle b','b.id = a.transportation_id');
        $result = $this->db->get('hr__claim_transportation a')->result();
        if($result > 0)
        {
            foreach($result as $key => $value)
            {
                $movement   = $this->fetchTransportationMovements($value->id);
                $attachment = $this->fetchTransportationAttachment($value->id);

                $result[$key]->staff_movements = $movement;
                $result[$key]->attachments = $attachment;
            }
            return $result;
        }else
        {
            return null;
        }
    }

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

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

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

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

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

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