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

class Model_joining_group extends CI_Model {

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

	//-- read
	public function fetchJoiningGroup($id = null,$profiling_id = null, $is_delete = 0,$user_id = null)
	{
		$this->db->select('a.*,
			b.type as reason,
			c.position as position_hiring,
			c.grade as grade,
			c.level as level,
			d.name as user_reporting_name,
			e.name as branch,
			g.name as personnel_area,
			f.name as personal_subarea,
			h.type as employee_group,
			i.type as contract_element,
			c.grade as employee_subgroup_indicator,
			j.position as employee_subgroup_designation,
			k.action_efective_date as action_efective_date,
			l.type as action_type_name,
			m.designation as designation,
			n.name as business_entity
			');

		if($id) $this->db->where('a.id',$id);
		if($profiling_id) $this->db->where('a.profiling_id',$profiling_id);
		if($user_id) $this->db->where('k.user_id',$user_id);

		$this->db->where('a.is_delete',$is_delete);
		$this->db->join('_ref__hiring_reason b','b.id = a.reason_id','left');
		$this->db->join('company__position c','c.id = a.position_id','left');
		$this->db->join('user d','d.id = a.user_reporting_id','left');
		$this->db->join('company__branch e','e.id = a.branch_id','left');
		$this->db->join('company__division f','f.id = a.personal_subarea_id','left');
		$this->db->join('company__department g','g.id = a.personnel_area_id','left');
		$this->db->join('_ref__employee_group h','h.id = a.employee_group_id','left');
		$this->db->join('_ref__employee_contract_type i','i.id = a.contract_element_id','left');
		$this->db->join('company__position j','j.id = c.parent_id','left');
		$this->db->join('user__profiling k','k.id = a.profiling_id','left');
		$this->db->join('_ref__action_type l','l.id = k.action_type_id','left');
		$this->db->join('_ref__designation m','m.id = a.position_information','left');
		$this->db->join('company__business_entity n','n.id = a.business_entity_id','left');

		$this->db->order_by('k.action_efective_date,a.id','desc');

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

                log_message('error', $this->db->last_query());

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

	//-- update
	public function updateJoin($data,$profiling_id)
	{
		$this->db->where('a.profiling_id',$profiling_id);
		$this->db->update('user__joining_group a',$data);
	}
	
	//-- delete
	public function removeJoin($data,$id)
	{
		$this->db->where('id',$id);
		$this->db->set($data);
		$this->db->update('user__joining_group');
	}
}
?>