<?php

if (count(get_included_files()) == 0){

include 'connections/customPDO.php';

}



class Benefits{





private $process_claim_no;

private	$mysqllocal;

private	$mysqllive;

private $member_no;

private $inscompany;



function Benefits($local,$live)

{

	# code...

	$this->mysqllocal=$local;

	$this->mysqllive=$live;

}



function get_claim_expire($process_claim_no){



$this->process_claim_no=$this->mysqllocal->quote($process_claim_no);



$query_rsodest_claimdays =$this->mysqllocal->selectData("select *, datediff(CURDATE(),date_added) as claimdays from process_claim_no where process_claim_no=".$this->process_claim_no);



$typeofvisit=$row_rsodest_claimdays['typeofvisit'];

$claimdays=$row_rsodest_claimdays['claimdays'];

$datetimeadded=$row_rsodest_claimdays['datetimeadded'];

//echo $datetimeend;

if ($typeofvisit=="Out-Patient") {

	# code...

if($claimdays>7){

	$answer[0]='no';

	$answer[1]='Claim Cannot be Edited. Life Span of Claim has Expired.';



	}else{

	$answer[0]='yes';

	$answer[1]='Approved';

	}



}else{

	$answer[0]='yes';

	$answer[1]='Approved';

}



return $answer;

}















function get_outpatient_limit_balance($claimno,$memberno,$ins_company,$price,$current_id){



$answer[0]='yes';

$answer[1]='Approved';

$this->process_claim_no=$this->mysqllocal->quote($claimno);

 $this->member_no=$this->mysqllocal->quote($memberno);

$this->inscompany=$this->mysqllocal->quote($ins_company);



$query_check=$this->mysqllocal->selectData("select * from patient_info where member_no=".$this->member_no." and ins_company=".$this->inscompany);

foreach ($query_check as $row_rscheck) {

	# code...

$outpatient_balance=$row_rscheck['outpatient_balance'];

$member_plan_id=$row_rscheck['member_plan_id'];

$member_plan=$row_rscheck['member_plan'];

$employer_id=$row_rscheck['employer_id'];

$family_type=$row_rscheck['family_type'];

$no_of_family=$row_rscheck['no_of_family'];

}





$query_check=$this->mysqllocal->selectData("select * from company_plan where PlanID='$member_plan_id' and ins_company=".$this->inscompany." and CompanyID='$employer_id'");

//echo $query_check;

if ($query_check==false) {

	# code...

	$query_check=$this->mysqllive->selectData("select * from company_plan where PlanID='$member_plan_id' and ins_company=".$this->inscompany." and CompanyID='$employer_id'");



 if ($query_check==false) {

$answer[0]='no';

$answer[1]='Company Plan Not Found';

 }else{

 	foreach ($query_check as $key) {

 		# code...

 		$CompanyID=$key["CompanyID"];

 		$Company=$key["Company"];

 		$plan_id=$key["plan_id"];

 		$PlanID=$key["PlanID"];

 		$ins_company=$key["ins_company"];

 		$inpatient_limit=$key["inpatient_limit"];

 		$inpatient_limit1=$key["inpatient_limit1"];

 		$inpatient_limit2=$key["inpatient_limit2"];

 		$inpatient_limit3=$key["inpatient_limit3"];

 		$inpatient_limit4=$key["inpatient_limit4"];

 		$inpatient_limit5=$key["inpatient_limit5"];

 		$outpatient_limit=$key["outpatient_limit"];

 		$outpatient_limit1=$key["outpatient_limit1"];

 		$outpatient_limit2=$key["outpatient_limit2"];

 		$outpatient_limit3=$key["outpatient_limit3"];

 		$outpatient_limit4=$key["outpatient_limit4"];

 		$outpatient_limit5=$key["outpatient_limit5"];

 		$amount_coverage=$key["amount_coverage"];

 		$red_inpatient_limit=$key["red_inpatient_limit"];

 		$yellow_inpatient_limit=$key["yellow_inpatient_limit"];

 		$red_outpatient_limit=$key["red_outpatient_limit"];

 		$yellow_outpatient_limit=$key["yellow_outpatient_limit"];

 		$inpatient_limit_percentage=$key["inpatient_limit_percentage"];

 		$outpatient_limit_percentage=$key["outpatient_limit_percentage"];

 		$table_type=$key["table_type"];

 		$row_rscheck=$key;



 	}

 	//print_r($row_rscheck);

 	$this->mysqllocal->insertData("INSERT INTO `company_plan` (  `CompanyID`, `Company`, `plan_id`, `PlanID`, `Plan`, `ins_company`,`inpatient_limit`, `inpatient_limit1`, `inpatient_limit2`, `inpatient_limit3`, `inpatient_limit4`, `inpatient_limit5`, `outpatient_limit`,`outpatient_limit1`, `outpatient_limit2`, `outpatient_limit3`, `outpatient_limit4`, `outpatient_limit5`, `amount_coverage`,`red_inpatient_limit`, `yellow_inpatient_limit`, `red_outpatient_limit`, `yellow_outpatient_limit`, `inpatient_limit_percentage`,`outpatient_limit_percentage`, `table_type`) VALUES ( '$CompanyID', '$Company', '$plan_id', '$PlanID', '$ins_company', '$inpatient_limit', '$inpatient_limit1', '$inpatient_limit2','$inpatient_limit3', '$inpatient_limit4', '$inpatient_limit5','$outpatient_limit','$outpatient_limit1','$outpatient_limit2','$outpatient_limit3', '$outpatient_limit4', '$outpatient_limit5','$amount_coverage', '$red_inpatient_limit', '$yellow_inpatient_limit', '$red_outpatient_limit', '$yellow_outpatient_limit', '$inpatient_limit_percentage', '$outpatient_limit_percentage','$table_type' )");

 }



}else{



$row_rscheck=$query_check[0];	

}





if($no_of_family==1 || $no_of_family==''){$limit_type='outpatient_limit';}

if($no_of_family>1 && $no_of_family<=4){$limit_type='outpatient_limit'.($no_of_family-1);}

if($no_of_family>4){$limit_type='outpatient_limit4';}

$outpatient_limit= $row_rscheck[$limit_type];



echo $outpatient_limit."<br>"; 



if($outpatient_limit<=0 || $outpatient_limit=='' || $outpatient_limit=='NULL'){



$query_check=$this->mysqllocal->selectData("select * from health_plans where ins_company=".$this->inscompany." and plan_name='$member_plan'");

//echo $query_check;

if ($query_check==false) {

	# code...

	$query_check=$this->mysqllive->selectData("select * from health_plans where ins_company=".$this->inscompany." and plan_name='$member_plan'");

	if ($query_check==false) {

$answer[0]='no';

$answer[1]='Health Plan Not Found';

	}else{

foreach ($query_check as $key) {

	# code...

	$PlanID=$key["PlanID"];

	$plan_name=$key["plan_name"];

	$ins_company=$key["ins_company"];

	$user_id=$key["user_id"];

	$out_patient_limit=$key["out_patient_limit"];

	$out_patient_limit1=$key["out_patient_limit1"];

	$out_patient_limit2=$key["out_patient_limit2"];

	$out_patient_limit3=$key["out_patient_limit3"];

	$out_patient_limit4=$key["out_patient_limit4"];

	$out_patient_limit5=$key["out_patient_limit5"];

	$in_patient_limit=$key["in_patient_limit"];

	$in_patient_limit1=$key["in_patient_limit1"];

	$in_patient_limit2=$key["in_patient_limit2"];

	$in_patient_limit3=$key["in_patient_limit3"];

	$in_patient_limit4=$key["in_patient_limit4"];

	$in_patient_limit5=$key["in_patient_limit5"];

	$amount_coverage=$key["amount_coverage"];

	$effective_date=$key["effective_date"];

	$date_time=$key["date_time"];

	$table_type=$key["table_type"];

    $row_rscheck=$key;

}



$this->mysqllocal->insertData("INSERT INTO `health_plans` ( `PlanID`, `plan_name`, `ins_company`, `user_id`, `out_patient_limit`, `out_patient_limit1`, `out_patient_limit2`, `out_patient_limit3`, `out_patient_limit4`, `out_patient_limit5`, `in_patient_limit`, `in_patient_limit1`, `in_patient_limit2`, `in_patient_limit3`, `in_patient_limit4`, `in_patient_limit5`, `amount_coverage`, `effective_date`, `date_time`, `table_type`) VALUES ( '$PlanID', '$plan_name', '$ins_company', '$user_id', '$out_patient_limit', '$out_patient_limit1', '$out_patient_limit2', '$out_patient_limit3', '$out_patient_limit4', '$out_patient_limit5', '$in_patient_limit', '$in_patient_limit1', '$in_patient_limit2', '$in_patient_limit3', '$in_patient_limit4', '$in_patient_limit5', '$amount_coverage', '$effective_date', now(), 'health_plans')");





	}



}else{

	$row_rscheck=$query_check[0];

}

//$outpatient_limit=$row_rscheck['out_patient_limit'];

if($no_of_family==1 || $no_of_family==''){$limit_type='out_patient_limit';}

if($no_of_family>1 && $no_of_family<=4){$limit_type='out_patient_limit'.($no_of_family-1);}

if($no_of_family>4){$limit_type='out_patient_limit4';}

$outpatient_limit= $row_rscheck[$limit_type];

}

//echo $outpatient_limit."<br>";





$query_check=$this->mysqllocal->selectData("select sum(qty*unit_price) as claim_total from claims_details where process_claim_no=".$this->process_claim_no);

$row_rscheck = $query_check[0];



$claim_total=$row_rscheck['claim_total']-$old_price;



if($current_id){

$query_check_old_price=$this->mysqllocal->selectData("select sum(qty*unit_price) as claim_total from claims_details where id='$current_id'");



$row_rscheck_old_price = $rscheck_old_price[0];



$oldprice=$row_rscheck_old_price['claim_total'];



$claim_total=$claim_total-$oldprice;

}



//echo "$outpatient_balance+$claim_total+$price.<br>";

$actual_balance=$outpatient_balance+$claim_total+$price;



//echo $actual_balance;

			if($actual_balance<$outpatient_limit && $outpatient_limit>0){

			$answer[0]='yes';

			$answer[1]='Approved';

			}else{



			if($outpatient_limit>0){

			$answer[0]='no';

			$answer[1]='Out Patient Limit Exceeded. Item can not be added.';

			if($current_id){

				$answer[1]='Out Patient Limit Exceeded. Item cannot be modified.';

			}

			}else{

			$answer[0]='yes';

			$answer[1]='Approved';

			}





			}



return $answer;





}

}

?>