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

class Model_statutory_detail extends CI_Model {


	public function fetchStatutory($user_id = null, $latest = false,$profiling_id = null)
	{
		//inner query 
		$this->db->select('MAX(a.profiling_id)');
		$this->db->from('user__statutory_detail a');
		if($user_id) $this->db->where('a.user_id',$user_id); 
		$this->db->where('a.is_deleted', 0);
		$max = $this->db->get_compiled_select(); 

		//outer query 
		$this->db->select('a.*,
						b.action_efective_date,
						c.type as action_type');

		if($user_id) $this->db->where('a.user_id',$user_id);
		
		if($profiling_id)
			$this->db->where('a.profiling_id',$profiling_id);
		else
			$this->db->where("a.profiling_id IN ($max)");

		$this->db->where('a.is_deleted',0);
		$this->db->join('user__profiling b','b.id = a.profiling_id','left');
		$this->db->join('_ref__action_type c','c.id = b.action_type_id','left');

		$query = $this->db->get('user__statutory_detail a');
		return ($query->num_rows() > 0)?$query->result():null;
	}

	public function createStatutory($input)
	{
		$this->db->insert('user__statutory_detail',$input);
		return $this->db->insert_id();
	}

	public function updateStatutory($input,$id)
	{
		$this->db->set($input);
		if($id) $this->db->where('id',$id);
		$this->db->update('user__statutory_detail');
	}
}
?>