<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_hospitalization extends CI_Model {

	public function fetchHospitalization($user_id = null,$claim_id = null,$year = null,$month = null,$admin_status = null, $hod_status = null)
	{
		$this->db->select('a.*');
		// $this->db->select('a.*,c.name as patient_name,d.relations');
		if($user_id)$this->db->where('b.user_id',$user_id);
		if($claim_id)$this->db->where('a.claim_id',$claim_id);
		if($year)$this->db->where('b.year',$year);
		if($month)$this->db->where('b.month',$month);
		if($admin_status)$this->db->where('a.is_approve',$admin_status);
		if($hod_status)$this->db->where('a.is_hod_approved',$hod_status);
		$this->db->where('a.is_delete',0);
		if($user_id || $year || $month) $this->db->join('hr__claim b','b.id = a.claim_id AND b.is_delete = 0 AND b.is_submit = 1');

		$query = $this->db->get('hr__claim_hospitalization a');
		$results = ($query->result())?$query->result():null;
		
		if($results)
		{
			foreach($results as $key => $value):
				if($value->patient_id == $user_id)
				{
					$this->db->select('name');
					$this->db->where('id',$value->patient_id);
					$inner_query = $this->db->get('user');
					$results[$key]->patient_name = ($inner_query->result())?$inner_query->result()[0]->name:'';
					$results[$key]->relations = 'Self';
				}else
				{
					$this->db->select('name');
					$this->db->where('id',$value->patient_id);
					$this->db->where('user_id',$user_id);
					$family_query = $this->db->get('user__family_member');
					$results[$key]->patient_name = ($family_query->result())?$family_query->result()[0]->name:'';
					$results[$key]->relations = 'Family Members';
				}
			endforeach;
		}
		
		return $results;
	}

	public function fetchtSingleHospitalization($user_id = null,$claim_id = null,$year = null,$month = null)
	{
		$allocation = $this->fetchHospitalization($user_id,$claim_id,$year,$month);
		return $allocation[0];
	}

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

	public function createAttachment($input)
	{
		return ($this->db->insert_batch('hr__claim_hospitalization_attachment',$input))?true:false;
	}

	public function updateHospitalization($input,$where = array())
	{
		$this->db->set($input);
		foreach ($where as $key => $wh) {
			$this->db->where($key,$wh);
		}

		$this->db->update('hr__claim_hospitalization');
	}

	public function removeHospitalization($id)
	{
		$data 	= array('is_delete' => 1);
		$where 	= array('id' => $id);
		$this->updateHospitalization($data,$where);
	}

	public function fetchAttachment($hosp_id)
	{
		$this->db->where('hospitalization_id',$hosp_id);
		$this->db->where('is_delete',0);

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

	public function fetchDraft($user_id,$year)
	{
		$this->db->select('a.*,b.amount_claim');
		$this->db->where('a.user_id',$user_id);
		$this->db->where('a.year',$year);
		$this->db->where('b.is_approve',0);
		$this->db->join('hr__claim_hospitalization b','a.id = b.claim_id','left');

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

	public function fetchAttachmentByUniqueid($unique_id)
	{
		$this->db->select('a.*');
		$this->db->where('unique_id',$unique_id);
		$query = $this->db->get('hr__claim_hospitalization_attachment a');
		return ($query->result())?$query->result():null;
	}

	/**
	 * version 2.0 
	 * get claimed hospitalization amount
	 */
	public function get_panel_amount_no_prorate($year,$user_id)
	{
		$this->db->select('sum(a.amount) as amount');
		$this->db->where('a.date >= ',$year.'-01-01');
		$this->db->where('a.date <= ',$year.'-12-31');
		$this->db->where('a.is_delete',0);
		$this->db->where('a.staff_id',$user_id);
		$this->db->where('a.claim_type_id',2);

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

		return (!empty($query->result()))?$query->result()[0]->amount:0;
    }

	public function get_claimed_amount($year,$user_id)
	{
		$amount = 0;
		$hospitalizations = $this->fetchHospitalization($user_id,null,$year,null,null,null);

		if(!empty($hospitalizations)){
			foreach ($hospitalizations as $key => $row) 
			{
				/**
				 * situation :
				 * 1. admin not approve hod not approve (0 & 0) 		amount += amount_claim
				 * 2. admin reject and hod approve (2 & 0) 				amount +=  0
				 * 3. admin not approve and hod approve (0 & 1) 		amount += amount_claim
				 * 4. admin approve and hod approve (1 & 1)				amount += amount_approved
				 */

					if($row->is_approve == 2)
					{ 
						$amount += 0;
					}
					elseif ($row->is_approve == 1) 
					{
						$amount += $row->amount_approved;
					}else{
						$amount += $row->amount_claim;
					}
			}
		}
		return $amount;
	}

	public function get_amount_approved($year,$user_id)
	{
		$amount = 0;
		$hospitalizations = $this->fetchHospitalization($user_id,null,$year,null,null,null);
		if(!empty($hospitalizations)){
			foreach ($hospitalizations as $key => $row) 
			{
				if($row->is_approve == 1)
				{ 
					$amount += $row->amount_approved;
				}
			}
		}
		return $amount;
	}

	public function get_amount_pending($year,$user_id)
	{
		$amount = 0;
		$hospitalizations = $this->fetchHospitalization($user_id,null,$year,null,null,null);
		if(!empty($hospitalizations)){
			foreach ($hospitalizations as $key => $row) 
			{
				if($row->is_approve != 2 && $row->is_approve != 1)
				{ 
					$amount += $row->amount_claim;
				}
			}
		}
		return $amount;
	}
}
?>