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

class Model_claim_subsistence_allowance extends CI_Model {

    public function fetchSubsistence($claim_id)
    {
        $this->db->select('a.*');
        $this->db->where('a.claim_id',$claim_id);
        $this->db->where('a.is_delete',0);

        $result = $query = $this->db->get('hr__claim_subsistence a')->result();
        if($result)
        {
            foreach($result as $key => $value)
            {
                $movement = $this->fetchSubsistenceMovement($value->id);
                $result[$key]->staff_movements = $movement;
            }
            return $result;
        }else{
            return null;
        }
        
    }

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

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

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

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