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

class Model_claim_allocation extends CI_Model 
{
	/**
	 * version 1.0 
	 * deprecated
	 * START: 
	 * TODO: 
	 * 	- remove all deprecated function when everything is confirm to be working fine without any errors 
	 */

	/**
	 * version 1.0 & 2.0
	 */
	public function allocate($insert)
	{
		$this->db->insert('hr__claim_allocation',$insert);
		//echo $this->db->last_query(); exit();
		return $this->db->insert_id();
	}

	public function allocateDetail($insert)
	{
		$this->db->insert('hr__claim_allocation_detail',$insert);
		return $this->db->insert_id();
	}
	
	public function fetchAllocation($user_id = null, $start_date = null, $end_date = null, $status = 0, $is_delete = 0)
	{
		//original
		// $this->db->select('a.*,
		// 					b.name,
		// 					c.description as dental_description,
		// 					d.description as hospitalization_description,
		// 					e.description as outpatient_description,
		// 					f.description as room_description,
		// 					g.claim_type_dental_optical_id as claim_type_dental_optical_id,g.claim_type_hospitalization_id as claim_type_hospitalization_id,g.claim_type_outpatient_id as claim_type_outpatient_id,g.claim_type_room_package_id as claim_type_room_id,
		// 					g.start_date,g.end_date,g.claim_type_dental_optical_amount,g.claim_type_hospitalization_amount,g.claim_type_outpatient_amount,g.claim_type_room_package_amount,
		// 					g.claim_type_dental_optical_prorate_amount,g.claim_type_hospitalization_prorate_amount,g.claim_type_outpatient_prorate_amount,g.claim_type_room_package_prorate_amount');
		
		$this->db->select
		(
			'a.*,
			b.name,
			c.description as dental_description,
			d.description as hospitalization_description,
			e.description as outpatient_description,
			f.description as room_description,
			g.claim_type_dental_optical_id as claim_type_dental_optical_id,g.claim_type_hospitalization_id as claim_type_hospitalization_id,g.claim_type_outpatient_id as claim_type_outpatient_id,g.claim_type_room_package_id as claim_type_room_id,
			g.start_date,g.end_date,g.claim_type_dental_optical_amount,g.claim_type_hospitalization_amount,g.claim_type_outpatient_amount,g.claim_type_room_package_amount,
			g.claim_type_dental_optical_prorate_amount,g.claim_type_outpatient_prorate_amount'
		);

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

		if($start_date) $this->db->where('g.start_date >=',date('Y-m-d',strtotime($start_date)));
		// if($end_date) $this->db->where('g.end_date <=',date('Y-m-d',strtotime($end_date)));
		// if($end_date) $this->db->where('g.end_date >=',date('Y-m-d',strtotime($end_date)));

		$this->db->where('a.status',$status);
		$this->db->where('a.is_delete',$is_delete);
		$this->db->join('user b','b.id = a.user_id');
		$this->db->join('hr__claim_allocation_detail g','a.id = g.allocation_id');
		$this->db->join('_ref__claim_type_detail c','c.id = g.claim_type_dental_optical_id');
		$this->db->join('_ref__claim_type_detail d','d.id = g.claim_type_hospitalization_id');
		$this->db->join('_ref__claim_type_detail e','e.id = g.claim_type_outpatient_id');
		$this->db->join('_ref__claim_type_detail f','f.id = g.claim_type_room_package_id');
		$this->db->order_by('g.end_date','asc');
		$query = $this->db->get('hr__claim_allocation a');
		return ($query->num_rows() > 0)? $query->result(): null;
	}

	public function fetchSingle($user_id = null, $start_date = null, $end_date = null, $status = 0, $is_delete = 0)
	{
		$result = $this->fetchAllocation($user_id, $start_date, $end_date, $status, $is_delete);
		return ($result)?$result[0]:null;
	}

	public function fetchBetweenDates($user_id = null, $start_date = null, $end_date = null, $status = 0, $is_delete = 0)
	{
		$start_date = date('Y-m-d',strtotime($start_date));
		$end_date 	= date('Y-m-d',strtotime($end_date));

		if($user_id)
		{

			//original 
			// $this->db->select
			// (
			// 	'a.*,
			// 	b.name,
			// 	g.start_date,g.end_date,
			// 	g.claim_type_dental_optical_amount as amount_dental,g.claim_type_dental_optical_prorate_amount as prorate_dental,
			// 	g.claim_type_hospitalization_amount as amount_hospitalization,g.claim_type_hospitalization_prorate_amount as prorate_hospitalization,
			// 	g.claim_type_outpatient_amount as amount_outpatient, g.claim_type_outpatient_prorate_amount as prorate_outpatient,
			// 	g.claim_type_room_package_amount as amount_room_package, g.claim_type_room_package_prorate_amount as prorate_room_package,
			// 	c.description as dental_description,
			// 	d.description as hospitalization_description,
			// 	e.description as outpatient_description,
			// 	f.description as room_description'
			// );

			//20190207
			$this->db->select
			(
				'a.*,
				b.name,
				g.start_date,g.end_date,
				g.claim_type_dental_optical_amount as amount_dental,g.claim_type_dental_optical_prorate_amount as prorate_dental,
				g.claim_type_hospitalization_amount as amount_hospitalization,
				g.claim_type_outpatient_amount as amount_outpatient, g.claim_type_outpatient_prorate_amount as prorate_outpatient,
				g.claim_type_room_package_amount as amount_room_package,

				c.description as dental_description,
				d.description as hospitalization_description,
				e.description as outpatient_description,
				f.description as room_description'
			);

			$this->db->where("('".$start_date."' BETWEEN g.start_date AND g.end_date)  AND a.user_id ='".$user_id."' AND a.is_delete = '".$is_delete."' AND a.status ='".$status."' ");
			$this->db->or_where("('".$end_date."' BETWEEN g.start_date AND g.end_date)  AND a.user_id ='".$user_id."' AND a.is_delete = '".$is_delete."' AND a.status ='".$status."' ");
			$this->db->or_where("(g.start_date BETWEEN '".$start_date."' AND '".$end_date."')  AND a.user_id ='".$user_id."' AND a.is_delete = '".$is_delete."' AND a.status ='".$status."' ");
			$this->db->or_where("(g.end_date BETWEEN '".$start_date."' AND '".$end_date."')  AND a.user_id ='".$user_id."' AND a.is_delete = '".$is_delete."' AND a.status ='".$status."' ");
		}else{
			/*
			original 
			$this->db->select
			(
				'a.*,
				b.name,
				g.start_date,g.end_date,
				g.claim_type_dental_optical_id as claim_type_dental_optical_id,g.claim_type_dental_optical_amount as amount_dental,g.claim_type_dental_optical_prorate_amount as prorate_dental,
				g.claim_type_hospitalization_amount as amount_hospitalization,g.claim_type_hospitalization_prorate_amount as prorate_hospitalization,
				g.claim_type_outpatient_amount as amount_outpatient, g.claim_type_outpatient_prorate_amount as prorate_outpatient,
				g.claim_type_room_package_amount as amount_room_package, g.claim_type_room_package_prorate_amount as prorate_room_package,
				c.description as dental_description,
				d.description as hospitalization_description,
				e.description as outpatient_description,
				f.description as room_description'
			);
			*/


			//20190207
			$this->db->select
			(
				'a.*,
				b.name,
				g.start_date,g.end_date,
				g.claim_type_dental_optical_id as claim_type_dental_optical_id,g.claim_type_dental_optical_amount as amount_dental,g.claim_type_dental_optical_prorate_amount as prorate_dental,
				g.claim_type_hospitalization_amount as amount_hospitalization,
				g.claim_type_outpatient_amount as amount_outpatient, g.claim_type_outpatient_prorate_amount as prorate_outpatient,
				g.claim_type_room_package_amount as amount_room_package,
				c.description as dental_description,
				d.description as hospitalization_description,
				e.description as outpatient_description,
				f.description as room_description'
			);
			
			$this->db->where("('".$start_date."' BETWEEN g.start_date AND g.end_date)  AND a.is_delete = '".$is_delete."' AND a.status ='".$status."' ");
			$this->db->or_where("('".$end_date."' BETWEEN g.start_date AND g.end_date) AND a.is_delete = '".$is_delete."' AND a.status ='".$status."' ");
			$this->db->or_where("(g.start_date BETWEEN '".$start_date."' AND '".$end_date."') AND a.is_delete = '".$is_delete."' AND a.status ='".$status."' ");
			$this->db->or_where("(g.end_date BETWEEN '".$start_date."' AND '".$end_date."') AND a.is_delete = '".$is_delete."' AND a.status ='".$status."' ");
		}
		
		$this->db->join('user b','b.id = a.user_id');
		$this->db->join('hr__claim_allocation_detail g','a.id = g.allocation_id');

		$this->db->join('_ref__claim_type_detail c','c.id = g.claim_type_dental_optical_id');
		$this->db->join('_ref__claim_type_detail d','d.id = g.claim_type_hospitalization_id');
		$this->db->join('_ref__claim_type_detail e','e.id = g.claim_type_outpatient_id');
		$this->db->join('_ref__claim_type_detail f','f.id = g.claim_type_room_package_id');

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

	}

	public function fetchSingleBetweenDates($user_id = null, $start_date = null, $end_date = null, $status = 0, $is_delete = 0)
	{
		$result = $this->fetchBetweenDates($user_id, $start_date, $end_date, $status, $is_delete);	
		return ($result)?$result[0]:null;
	}



	/**
	 * version 1.0 & version 2.0
	 */
	public function updateAllocation($where = null,$update = array())
	{
		$this->db->set($update);
		$this->db->where('id',$where);
		$this->db->update('hr__claim_allocation');
	}

	public function updateDetailAllocation($update = array(), $where = array())
	{
		$this->db->set($update);
		if(!empty($where))
		{
			foreach($where as $key => $value)
			{
				$this->db->where($key,$value);
			}
		}
		$this->db->update('hr__claim_allocation_detail');
	}

	/**
	 * version 1.0 & version 2.0
	 */
	public function removeAllocation($where = null,$update = array())
	{
		$this->updateAllocation($where,$update);
	}
	/**
	 * version 1.0
	 * deprecated
	 * END: 
	 */


	//--------------------------------------------------------------------------------------------------------------------------------//
	public function allocateProrate($insert)
	{
		$this->db->insert('hr__claim_allocation_prorate',$insert);
		return $this->db->insert_id();
	}
	
	public function updateProrate($where = array(), $update = array())
	{
		$this->db->set($update);
		foreach ($where as $key => $value) 
		{
			$this->db->where($key,$value);	
		}
		$this->db->update('hr__claim_allocation_prorate');
	}

	/**
	 * version 2.0 : get allocations for staff based on year 
	 */
	// public function get_allocation_for_year($year = null, $staff_id = null)
	// {
	// 	$this->db->select('a.*,c.name,c.position_id,c.date_joined,d.position');
	// 	if(!empty($year))$this->db->where('a.year',$year);
	// 	if(!empty($staff_id))$this->db->where('a.user_id',$staff_id);
	// 	// $this->db->join('user c','c.id = a.user_id');
	// 	$this->db->join('user__employment_detail c','c.user_id = a.user_id','left');
	// 	$this->db->join('company__position d','d.id = c.position_id','left');
	// 	$this->db->where('a.is_delete',0);
	// 	$query = $this->db->get('hr__claim_allocation a');

	// 	if(!empty($results = $query->result()))
	// 	{
	// 		foreach ($results as $key => $claim_allocation) 
	// 		{

	// 			$this->db->select('a.id,a.month,a.outpatient,a.dental_optical');
	// 			$this->db->where('a.claim_allocation_id',$claim_allocation->id);
	// 			$inner_query = $this->db->get('hr__claim_allocation_prorate a');

	// 			if(!empty($inner_results = $inner_query->result()))
	// 			{
	// 				$results[$key]->prorate = $inner_results;
	// 			}
				
	// 		}
	// 		// die(debug($results));
	// 		return $results;
	// 	}
	// 	return null;
	// }



	public function get_allocation_for_year($year = null, $staff_id = null)
	{
		$this->db->select('a.*,c.name,c.position_id,c.date_joined,d.position');
		if(!empty($year))$this->db->where('a.year',$year);
		if(!empty($staff_id))$this->db->where('a.user_id',$staff_id);
		
		$this->db->join('user__employment_detail c','c.user_id = a.user_id','left');
		$this->db->join('company__position d','d.id = c.position_id','left');
		$this->db->where('a.is_delete',0);
		$query = $this->db->get('hr__claim_allocation a');

		return (!empty($query->result()))?$query->result():null;

//off for no prorate 2021 by farid 20210311	
// echo $this->db->last_query();
		// if(!empty($results = $query->result()))
		// {
		// 	foreach ($results as $key => $claim_allocation) 
		// 	{
				
		// 		$this->db->select('a.id,a.month,a.outpatient,a.dental_optical,b.balanced_prorate as outpatient_balanced, c.balanced_prorate as dental_balanced');
		// 		$this->db->where('d.id',$claim_allocation->id);
		// 		$this->db->where('d.is_delete',0);
		// 		$this->db->join('hr__claim_allocation_prorate a','a.claim_allocation_id = d.id and d.is_delete = 0','left outer');
		// 		$this->db->join('view_claim_outpatient b','b.month = a.month and a.year = b.year and b.user_id = a.user_id','left outer');
		// 		$this->db->join('view_claim_dental_optical c','c.month = a.month and c.year = a.year and c.user_id = a.user_id','left outer');
		// 		$inner_query = $this->db->get('hr__claim_allocation d');

		// 		if(!empty($inner_results = $inner_query->result()))
		// 		{
		// 			$results[$key]->prorate = $inner_results;
		// 		}
				
		// 	}
			
		// 	return $results;
		// }
// echo $this->db->last_query();
		// return null;
	}







	public function get_allocation_type()
	{
		$this->db->where('id <= ',15);
		$query = $this->db->get('_ref__claim_type_detail');

		return (!empty($query->result()))?$query->result():null;
	}






	// /**
	//  * version 2.0 : create allocation for all staff based on profiling
	//  *  	backup : create_allocation()
	//  * 
	//  * 		issue  : 
	//  * 				1. had problem with allocation for staff that working experienced is more than or equal to 5 years
	//  * 				2. trainee and contract staff not eligible for medical claims 
	//  */
	// public function create_allocation($year = null)
	// {
	// 	$this->load->model('Admin/model_user','user');
	// 	if(empty($year))$year = date('Y');	
		
	// 	$claim_type_detail 		= $this->db->where('id <',16)->get('_ref__claim_type_detail')->result();

	// 	$outpatient_benefit 		= array($claim_type_detail[0],$claim_type_detail[1],$claim_type_detail[2],$claim_type_detail[3],$claim_type_detail[4],$claim_type_detail[5]);
	// 	$hospitalization_benefit 	= array($claim_type_detail[6],$claim_type_detail[7],$claim_type_detail[8]);
	// 	$room_package_benefit 		= array($claim_type_detail[9],$claim_type_detail[10],$claim_type_detail[11]);
	// 	$dental_benefit 			= array($claim_type_detail[12],$claim_type_detail[13],$claim_type_detail[14]);
		
	// 	/**
	// 	 * 1. fetch all staff with position
	// 	 */
	// 	$users = $this->user->getUsers(null);
	// 	if(!empty($users))
	// 	{
	// 		foreach ($users as $key => $user) 
	// 		{
				
	// 			/**
	// 			 * 2. create array for bulk insert
	// 			 */
	// 			if(!empty($user['position_id']) && !empty($user['date_joined']))
	// 			{
					
	// 				$claim_allocation = array
	// 				(
	// 					'user_id' 			=> $user['id'],
	// 					'year'				=> date('Y'),
	// 					'created_by'		=> $_SESSION['user']['id'],
	// 					'is_delete'			=> 0,
	// 				);
	// 				$duration = $this->getDuration(!empty($user['date_joined']),date('Y-m-d'));
					


	// 				if($user['position_category_id'] == 4 || $user['position_category_id'] == 5)
	// 				{
	// 					$position_type = 0;
	// 				}elseif ($user['position_category_id'] == 3) 
	// 				{
	// 					$position_type = 1;
	// 				}elseif ($user['position_category_id'] == 1 || $user['position_category_id'] == 2) 
	// 				{
	// 					$position_type = 2;
	// 				}else{
	// 					$position_type = 0; //set to lowest value position category is not equal to 
	// 				}
	// 				$claim_allocation['hospitalization'] 	= $hospitalization_benefit[$position_type]->amount;
	// 				$claim_allocation['room_package']		= $room_package_benefit[$position_type]->amount;

					
	// 				$claim_allocation_id = ($result = $this->db->select('id')->where('user_id',$user['id'])->where('year',date('Y'))->get('hr__claim_allocation')->result())?$result[0]->id:null;

	// 				if(empty($claim_allocation_id))
	// 				{
	// 					$this->db->insert('hr__claim_allocation',$claim_allocation);
	// 					$claim_allocation_id = $this->db->insert_id();
						

	// 					//only outpatient had parameter 5 years or not
	// 					if($duration >= 5)
	// 					{
	// 						for ($i=1; $i <= 12; $i++) 
	// 						{ 	
	// 							$prorate_claim = array
	// 							(
	// 								'claim_allocation_id' 	=> $claim_allocation_id,
	// 								'user_id'				=> $user['id'],
	// 								'year'					=> date('Y'),
	// 								'month'					=> $i,
	// 								'outpatient'			=> ($outpatient_benefit[$position_type*2+1]->amount/12)*$i,//1,3,5
	// 								'dental_optical'		=> ($dental_benefit[$position_type]->amount/12)*$i,
	// 								'created_by'			=> $_SESSION['user']['id'], 
	// 							);
	// 							$this->db->insert('hr__claim_allocation_prorate',$prorate_claim);
	// 						}

							
	// 					}else{ //less than 5 years 
							
	// 						//loop for 12 month
	// 						for ($i=1; $i <= 12; $i++) 
	// 						{ 
	// 							$prorate_claim = array
	// 							(
	// 								'claim_allocation_id' 	=> $claim_allocation_id, //farouq mungkin salah ini
	// 								'user_id'				=> $user['id'],
	// 								'year'					=> date('Y'),
	// 								'month'					=> $i,
	// 								'outpatient'			=> ($outpatient_benefit[$position_type*2]->amount/12)*$i, //0,2,4
	// 								'dental_optical'		=> ($dental_benefit[$position_type]->amount/12)*$i,
	// 								'created_by'			=> $_SESSION['user']['id'], 
	// 							);
	// 							$this->db->insert('hr__claim_allocation_prorate',$prorate_claim);
	// 						}
	// 					}
	// 				}
	// 			}else{
					
	// 				$claim_allocation_id = ($result = $this->db->select('id')->where('user_id',$user['id'])->where('year',date('Y'))->get('hr__claim_allocation')->result())?$result[0]->id:null;

	// 				if(empty($claim_allocation_id))
	// 				{
	// 					$claim_allocation = array
	// 					(
	// 						'user_id' 			=> $user['id'],
	// 						'year'				=> date('Y'),
	// 						'hospitalization' 	=> 0,
	// 						'room_package' 		=> 0,
	// 						'created_by'		=> $_SESSION['user']['id'],
	// 						'is_delete'			=> 0,
	// 					);
	// 					$this->db->insert('hr__claim_allocation',$claim_allocation);
	// 					$claim_allocation_id = $this->db->insert_id();

	// 					for ($i=1; $i <= 12; $i++) 
	// 					{ 
	// 						$prorate_claim = array
	// 						(
	// 							'claim_allocation_id' 	=> $claim_allocation_id,
	// 							'user_id'				=> $user['id'],
	// 							'year'					=> date('Y'),
	// 							'month'					=> $i,
	// 							'outpatient'			=> 0, //0,2,4
	// 							'dental_optical'		=> 0,
	// 							'created_by'			=> $_SESSION['user']['id'], 
	// 						);
	// 						$this->db->insert('hr__claim_allocation_prorate',$prorate_claim);
	// 					}
	// 				}
					
	// 			}
	// 		}
	// 	}
		
	// }
	

	

	/**
	 * version 2.0.1 : create allocation for all staff based on profiling
	 * 
	 * try to solve issue  : 
	 * 				1. had problem with allocation for staff that working experienced is more than or equal to 5 years
	 * 				2. trainee and contract staff not eligible for medical claims 
	 *
	 * update for 2021. No prorate for outpatient and dental optical. Updated by Farid 2021/02/19
	 */
	public function create_allocation($year = null)
	{
		$this->load->model('Admin/model_user','user');
		if(empty($year))$year = date('Y');	
		
		$claim_type_detail 		= $this->db->where('id <',16)->get('_ref__claim_type_detail')->result();
		$outpatient_benefit 		= array($claim_type_detail[0],$claim_type_detail[1],$claim_type_detail[2],$claim_type_detail[3],$claim_type_detail[4],$claim_type_detail[5]);
		$hospitalization_benefit 	= array($claim_type_detail[6],$claim_type_detail[7],$claim_type_detail[8]);
		$room_package_benefit 		= array($claim_type_detail[9],$claim_type_detail[10],$claim_type_detail[11]);
		$dental_benefit 			= array($claim_type_detail[12],$claim_type_detail[13],$claim_type_detail[14]);
		
		/**
		 * 1. fetch all staff with position
		 */
		$users = $this->user->getUsers(null); 
		
		if(!empty($users))
		{
			foreach ($users as $key => $user) 
			{
				
				/**
				 * 2. create array for bulk insert
				 */
				if(!empty($user['position_id']) && !empty($user['date_joined']))
				{
					
					$claim_allocation = array
					(
						'user_id' 			=> $user['id'],
						'year'				=> date('Y'),
						'year_joined'		=> (strtotime($user['date_joined']))?date('Y',strtotime($user['date_joined'])):0,
						'year_experienced'	=> (strtotime($user['date_joined']))?date('Y') - date('Y',strtotime($user['date_joined'])):0,
						'created_by'		=> $_SESSION['user']['id'],
						'is_delete'			=> 0,
					);
					// $duration = $this->getDuration(!empty($user['date_joined']),date('Y-m-d'));
					$duration = (strtotime($user['date_joined']))?date('Y') - date('Y',strtotime($user['date_joined'])):0;


					/**
					 * This is very bad because hard code their positions based on their parent category id
					 * and hr can add positions
					 */
					if($user['position_category_id'] == 4 || $user['position_category_id'] == 5) //executive and non executive
					{
						$position_type = 0;
					}elseif ($user['position_category_id'] == 3)  //middle management
					{
						$position_type = 1;
					}elseif ($user['position_category_id'] == 1 || $user['position_category_id'] == 2) //top management and management
					{
						$position_type = 2;
					}elseif($user['position_category_id'] == 20 || $user['position_category_id'] == 23 || $user['position_id'] == 20){ //contract staff and trainee
						$position_type = -1;
					}else{
						$position_type = 0; //set to lowest value position category is not equal to 
					}


					if($position_type < 0)
					{
						$claim_allocation['hospitalization'] 	= 0;
						$claim_allocation['room_package']		= 0;
						$claim_allocation['outpatient']			= 0;
						$claim_allocation['dental_optical']		= 0;
					}else{
						$claim_allocation['hospitalization'] 	= $hospitalization_benefit[$position_type]->amount;
						$claim_allocation['room_package']		= $room_package_benefit[$position_type]->amount;

						//off prorate for 2021 by Farid 2021/02/19
						if($duration >= 5){
							$claim_allocation['outpatient']			= $outpatient_benefit[$position_type*2+1]->amount;
							$claim_allocation['dental_optical']		= $dental_benefit[$position_type]->amount;
						}else{
							$claim_allocation['outpatient']			= $outpatient_benefit[$position_type*2]->amount;
							$claim_allocation['dental_optical']		= $dental_benefit[$position_type]->amount;
						}
						
					}

					$claim_allocation_id = ($result = $this->db->select('id')->where('user_id',$user['id'])->where('year',date('Y'))->get('hr__claim_allocation')->result())?$result[0]->id:null;

					if(empty($claim_allocation_id))
					{
						$this->db->insert('hr__claim_allocation',$claim_allocation);
						$claim_allocation_id = $this->db->insert_id();
						
						//off prorate for 2021 by Farid 2021/02/19
						//only outpatient had parameter 5 years or not
						// if($duration >= 5)
						// {
						// 	for ($i=1; $i <= 12; $i++) 
						// 	{ 	
						// 		$prorate_claim = array
						// 		(
						// 			'claim_allocation_id' 	=> $claim_allocation_id,
						// 			'user_id'				=> $user['id'],
						// 			'year'					=> date('Y'),
						// 			'month'					=> $i,
						// 			'outpatient'			=> ($position_type >= 0)?number_format((($outpatient_benefit[$position_type*2+1]->amount/12)*$i),2,'.',''):0,//1,3,5
						// 			'dental_optical'		=> ($position_type >= 0)?number_format((($dental_benefit[$position_type]->amount/12)*$i),2,'.',''):0,
						// 			'created_by'			=> $_SESSION['user']['id'], 
						// 		);
						// 		$this->db->insert('hr__claim_allocation_prorate',$prorate_claim);
						// 	}

							
						// }else{ //less than 5 years 
							
						// 	//loop for 12 month
						// 	for ($i=1; $i <= 12; $i++) 
						// 	{ 
						// 		$prorate_claim = array
						// 		(
						// 			'claim_allocation_id' 	=> $claim_allocation_id, //farouq mungkin salah ini
						// 			'user_id'				=> $user['id'],
						// 			'year'					=> date('Y'),
						// 			'month'					=> $i,
						// 			'outpatient'			=> ($position_type >= 0)?number_format((($outpatient_benefit[$position_type*2]->amount/12)*$i),2,'.',''):0, //0,2,4
						// 			'dental_optical'		=> ($position_type >= 0)?number_format((($dental_benefit[$position_type]->amount/12)*$i),2,'.',''):0,
						// 			'created_by'			=> $_SESSION['user']['id'], 
						// 		);
						// 		$this->db->insert('hr__claim_allocation_prorate',$prorate_claim);
						// 	}
						// }
					}


					// log_message('debug','testing');


				}else{
					
					$claim_allocation_id = ($result = $this->db->select('id')->where('user_id',$user['id'])->where('year',date('Y'))->get('hr__claim_allocation')->result())?$result[0]->id:null;

					if(empty($claim_allocation_id))
					{
						$claim_allocation = array
						(
							'user_id' 			=> $user['id'],
							'year'				=> date('Y'),
							'year_joined'		=> (strtotime($user['date_joined']))?date('Y',strtotime($user['date_joined'])):0,
							'year_experienced'	=> (strtotime($user['date_joined']))?date('Y') - date('Y',strtotime($user['date_joined'])):0,
							'hospitalization' 	=> 0,
							'room_package' 		=> 0,
							'outpatient' 		=> 0,
							'dental_optical' 	=> 0,
							'created_by'		=> $_SESSION['user']['id'],
							'is_delete'			=> 0,
						);
						$this->db->insert('hr__claim_allocation',$claim_allocation);
						$claim_allocation_id = $this->db->insert_id();

						//off prorate for 2021 by Farid 2021/02/19
						// for ($i=1; $i <= 12; $i++) 
						// { 
						// 	$prorate_claim = array
						// 	(
						// 		'claim_allocation_id' 	=> $claim_allocation_id,
						// 		'user_id'				=> $user['id'],
						// 		'year'					=> date('Y'),
						// 		'month'					=> $i,
						// 		'outpatient'			=> 0, //0,2,4
						// 		'dental_optical'		=> 0,
						// 		'created_by'			=> $_SESSION['user']['id'], 
						// 	);
						// 	$this->db->insert('hr__claim_allocation_prorate',$prorate_claim);
						// }
					}
					
				}
			}
		}
		
	}


	/**
	 * version 2.0 : set schedule for next year allocation 
	 */
	public function schedule_for_next_year()
	{
		
	}

	//not return correct value for user experience more than 5 years
	private function getDuration($date_from, $date_to) 
	{
		// $date_from = DateTime::createFromFormat('Y-m-d', $date_from);
		// $date_to   = DateTime::createFromFormat('Y-m-d', $date_to);
		// $duration = ($date_from && $date_to) ? $date_from->diff($date_to)->m + ($date_from->diff($date_to)->y*12) : 0;
		// return floor($duration/12);

		return (strtotime($date_from) && strtotime($date_to))?date('Y',strtotime($date_to)) - date('Y',strtotime($date_from)):0;
	}


}
?>