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

class Model_claim_parking extends CI_Model 
{

    public function fetchParking($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_parking a')->result();

        if($result)
        {
            foreach($result as $key => $value)
            {   
                $result[$key]->attachment       = $this->fetchParkingAttachment($value->id);
                $result[$key]->staff_movement   = $this->fetchParkingMovement($value->id);
            }
            return $result;
        }else{
            return null;
        }
    }

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

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

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

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

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

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