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

class Model_claim_allocation_detail extends CI_Model 
{

    public function fetchAllocation($user_id,$year)
    {
        if(!$user_id)return false;

        $this->db->select
                    (
                    'SUM(dental_prorate) as dental_prorate, 
                    SUM(hospitalization_prorate) as hospitalization_prorate, 
                    SUM(outpatient_prorate) as outpatient_prorate, 
                    SUM(room_package_prorate) as package_prorate'
                    );
        $this->db->where('user_id',$user_id);
        $this->db->where('YEAR(start_date)',$year);
        $this->db->where('YEAR(end_date)',$year);
        // $this->db->where('a.is_date_between',1);

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

    /**
     * get value of approved claim by hod to calculate for prorate
     */
    public function get_amount_approved_dental($year, $user_id)
    {
        $this->db->select('SUM(a.amount_approved) as amount_approved');
        $this->db->where('a.is_delete',0);
        $this->db->where('a.is_approve',1);
        $this->db->where('YEAR(a.date)',$year); 
        $this->db->where('b.user_id',$user_id);
        $this->db->join('hr__claim b','b.id = a.claim_id','left');

        $query = $this->db->get('hr__claim_dental a');
        return ($query->result() > 0)?$query->result()[0]:0;
    }

    public function get_amount_approved_hospitalization($year,$user_id)
    {
        $this->db->select('SUM(a.amount_approved) as amount_approved');
        $this->db->where('a.is_delete',0);
        $this->db->where('a.is_approve',1);
        $this->db->where('YEAR(a.date)',$year); 
        $this->db->join('hr__claim b','b.id = a.claim_id','left');
        $query = $this->db->get('hr__claim_hospitalization a');
        return ($query->result() > 0)?$query->result()[0]:0;
    }

    public function get_amount_approved_outpatient($year,$user_id)
    {
        $this->db->select('SUM(a.amount_approved) as amount_approved');
        $this->db->where('a.is_delete',0);
        $this->db->where('a.is_approve',1);
        $this->db->where('YEAR(a.date)',$year); 
        $this->db->join('hr__claim b','b.id = a.claim_id','left');

        $query = $this->db->get('hr__claim_outpatient a');
        return ($query->result() > 0)?$query->result()[0]:0;
    }

    public function get_amount_approved_room_package($year,$user_id)
    {
        // $this->db->select('SUM(a.amount_approved) as amount_approved');
        // $this->db->where('a.is_delete',0);
        // $this->db->where('a.is_approve',1);
        // $this->db->where('YEAR(a.date)',$year); 

        // $query = $this->db->get('hr__claim_outpatient a');
        // return ($query->result() > 0)?$query->result()[0]:0;  
        return 0; 
    }

    public function calcBalancedAllocation($year,$month,$user_id)
    {
        $start_date = '01-01-'.$year;
        $end_date   = '31-12-'.$year; 
        $curr_date  = '01-'.$month.'-'.$year; 
        $allocations= [];

        $this->db->select('a.*');
        $this->db->where('a.start_date >=',date('Y-m-d',strtotime($start_date)));
        $this->db->where('a.end_date <= ',date('Y-m-d',strtotime($end_date)));
        $this->db->where('b.user_id',$user_id);
        $this->db->join('hr__claim_allocation b','b.id = a.allocation_id','left');
        $result = $this->db->get('hr__claim_allocation_detail a')->result();

        $outpatient = 0;$dental_optical = 0;
        if($result)
        {
            foreach($result as $key => $value)
            {
                if(!(date('Y-m-d',strtotime($value->start_date)) > date('Y-m-d',strtotime($curr_date))))
                {
                    if(date('Y-m-d',strtotime($value->end_date)) > date('Y-m-d',strtotime($curr_date)))
                    {
                        $no_of_month = $this->nb_mois($value->start_date,$curr_date);
                    }else{
                        $no_of_month = $this->nb_mois($value->start_date,$value->end_date);
                    }
                    $outpatient += $no_of_month * number_format($value->claim_type_outpatient_prorate_amount,2);
                    $dental_optical += $no_of_month * number_format($value->claim_type_dental_optical_prorate_amount,2);
                }
            }
            $outpatient_claims      = $this->calcOutpatientClaims($year,$user_id);
            $dental_optical_claims  = $this->calcDentalOpticalClaims($year,$user_id);

            return array('outpatient' => $outpatient-$outpatient_claims,'dental_optical' => $dental_optical-$dental_optical_claims);
        }else{
            return null;
        }
        return ($result)?$result:null;  
    }




     //util
    private function calcOutpatientClaims($year,$user_id)
    {
        $outpatient = 0;
        $this->db->select('a.*');
        $this->db->where('a.is_delete',0);
        $this->db->where('a.is_delete',0);
        $this->db->join('hr__claim b','b.id = a.claim_id','left');
        $results = $this->db->get('hr__claim_outpatient a')->result();

        if($results) foreach($results as $key => $value)
        {
            if($value->is_approve)
            {
                $outpatient += $value->amount_approved;
            }else{
                $outpatient += $value->amount_claim;
            }
        }

        return $outpatient;
    }

    private function calcDentalOpticalClaims($year,$user_id)
    {
        $dental_optical = 0;
        $this->db->select('a.*');
        $this->db->where('a.is_delete',0);
        $this->db->where('b.is_delete',0);
        $this->db->join('hr__claim b','b.id = a.claim_id','left');
        $results = $this->db->get('hr__claim_dental a')->result();

        if($results) foreach($results as $key => $value)
        {
            if($value->is_approve)
            {
                $dental_optical += $value->amount_approved;
            }else{
                $dental_optical += $value->amount_claim;
            }
        }

        return $dental_optical;
    }

    private function nb_mois($date1, $date2)
    {
        $begin = new DateTime( $date1 );
        $end = new DateTime( $date2 );
        // $end = $end->modify( '+1 month' );

        $interval = DateInterval::createFromDateString('1 month');

        $period = new DatePeriod($begin, $interval, $end);
        $counter = 0;
        foreach($period as $dt) {
            $counter++;
        }

        return $counter;
    }
}
?>