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

class Model_user_contract_element extends CI_Model {

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


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

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

		$this->db->where('a.is_delete',0);
		$this->db->join('_ref__contract_type b','b.id = a.contract_hiring_type_id');
		$this->db->join('_ref__contract_confirmation c','c.id = a.confirmation');
		$this->db->join('user__profiling d','d.id = a.profiling_id','left');
        $this->db->join('_ref__action_type e','e.id = d.action_type_id','left');


		$query = $this->db->get('user__contract_element a');

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

	}

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

	public function updateContract($input,$id = null,$profiling_id = null)
	{
		$this->db->set($input);
		if(!empty($id)) $this->db->where('id',$id);
		if(!empty($profiling_id))$this->db->where('profiling_id',$profiling_id);
		$this->db->update('user__contract_element');
	}
}
?>