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

class Model_claim_panel extends CI_Model {

	private $claim = 'hr__claim_panel_clinic';
	private $claim_detail = 'hr__claim_panel_clinic_detail';

	function createClaim($input)
	{
		$this->db->insert($this->claim,$input);
		return $this->db->insert_id();
	}

	function createClaimDetail($input)
	{
		$this->db->insert($this->claim_detail,$input);
		return $this->db->insert_id();
	}

	function fetchClaim($where = array())
	{
		$results = array();

		$this->db->select
		('
			a.*,
			b.name as panel_name,
			d.unique_id as attachment_id,
		');

		if (!empty($where)) {
		 	foreach ($where as $key => $value) {
		 		$this->db->where($key,$value);
		 	}
		}

		$this->db->join('customer b','b.id = a.panel_id','left');
		// $this->db->join($this->claim_detail.' c','c.panel_claim_id = a.id and c.is_delete = 0','left');
		$this->db->join('hr__claim_panel_attachment d','d.claim_panel_id = a.id and d.is_delete = 0','left');
		// $this->db->group_by('c.panel_claim_id');
		$query = $this->db->get($this->claim.' a');

		// die(debug($query->result()));
		if($query->num_rows() > 0)
		{
			foreach ($query->result() as $key => $row) 
			{
				$row->total_amount = $this->get_total_amount($row->id);
				array_push($results,$row);
			}
		}
		return (!empty($results))?$results:null;
	}

	function fetch_user_claim_by_invoice_date($user_id,$invoice_month,$invoice_year)
	{
		$result = $this->db->select(
					'a.invoice_chit,a.invoice_month,a.invoice_year,
					b.employee_name,b.patient_name,b.employee_ic_no,b.patient_ic_no,b.claim_type_id,b.treatment,b.amount,
					c.attachment_name,c.file_type,c.file_ext,c.description,c.location,c.unique_id')
				->where('a.invoice_month',$invoice_month)
				->where('a.invoice_year',$invoice_year)
				->where('a.is_delete',0)
				->where('b.is_delete',0)
				->where('c.is_delete',0)
				->join('hr__claim_panel_clinic_detail b','b.panel_claim_id = a.id and b.staff_id = '.$user_id)
				->join('hr__claim_panel_attachment c','c.claim_panel_id = a.id')
				->get('hr__claim_panel_clinic a')
				->result();

		return ($result)?$result:null;
	}

	/**
	 * not good
	 * lazy guy filter (farouq zakwan)
	 */
	function filter_claim($year = null , $month = null, $clinic = null)
	{
		$y_bool = true;
		$m_bool = true;
		$c_bool = true;

		$filtered_claims = array();
		$claims = $this->fetchClaim();

		foreach ($claims as $key => $claim) 
		{
			if($clinic != 'All')
			{
				if($claim->panel_id != $clinic)
				{
					$c_bool = false;
				}
			}

			if($claim->invoice_year != $year)
			{
				$y_bool = false;
			}

			if($claim->invoice_month != $month)
			{
				$m_bool = false;
			}

			if($y_bool && $m_bool && $c_bool)
			{
				array_push($filtered_claims,$claim);
			}
			
			$y_bool = true;
			$m_bool = true;
			$c_bool = true;
		}
		return $filtered_claims;
	}

	private function get_total_amount($claim_panel_id)
	{
		$query = $this->db->select('sum(amount) as total_amount')
			->where('panel_claim_id',$claim_panel_id)
			->where('is_delete',0)
			->get('hr__claim_panel_clinic_detail');
		return ($query->num_rows() > 0)?(!empty($query->result()[0]->total_amount))?$query->result()[0]->total_amount:0:0;
	}

	function getAttachmentByUniqueid($unique_id)
	{
		$query = $this->db->select('a.*')
				->where('a.unique_id',$unique_id)
				->get('hr__claim_panel_attachment a');
		return ($query->num_rows() > 0)?$query->result():null;
	}

	function fetchClaimDetail($where = array())
	{
		$this->db->select('a.*');

		if(!empty($where)){
			foreach ($where as $key => $value) {
				$this->db->where($key,$value);
			}
		}

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

	public function fetch_claim_detail($staff_id)
	{
		$this->db->select('a.*');
		$this->db->where('a.staff_id',$staff_id);
		$query = $this->db->get('hr__claim_panel_clinic_detail a');
		$results = $query->result();
		return (!empty($results))?$results:null;
	}



	function fetchClaimTypeMedical()
	{
		$this->db->select('a.id,a.code,a.name,a.description');
		$this->db->where('a.id <=',5);
		$query = $this->db->get('_ref__claim_type a');
		return ($query->num_rows() > 0)?$query->result():null;
	}

	function createAttachment($input)
	{
		$this->db->insert('hr__claim_panel_attachment',$input);
		return $this->db->insert_id();
	}

	function editClaimDetail($input,$id)
	{
		$this->db->set($input);
		$this->db->where('a.id',$id);
		$this->db->update($this->claim_detail.' a');
	}

	function fetchClinicDetailByYear($year,$user_id)
	{

		// $this->db->select('a.*,b.staff_id,b.amount,b.claim_type_id,c.code');
		$this->db->select('SUM(b.amount) as amount,b.claim_type_id,c.code');
		$this->db->where('b.staff_id',$user_id);
		$this->db->where('b.date >=',$year.'-01-01');
		$this->db->where('b.date <=',$year.'-12-31');
		$this->db->where('b.is_delete',0);
		$this->db->where('a.is_delete',0);
		$this->db->join('hr__claim_panel_clinic_detail b','b.panel_claim_id = a.id');
		$this->db->join('_ref__claim_type c','c.ID = b.claim_type_id');
		$this->db->group_by('b.claim_type_id');

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

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