<?php

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

include 'connections/customPDO.php';

}

class MemberInfo{

	

	private	$mysqllocal;

	private	$mysqllive;

	private $inscompany;

	private $member_no;

	private $companyid;

	private $providerid;







function MemberInfo($local,$live){

   $this->mysqllocal=$local;



   if ($this->mysqllocal->is_connected()==true) {

   	# code...

   $this->mysqllive=$live;

   }

}







function get_member_info($inscompany, $member_no){



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

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

 

 if ($this->mysqllocal->is_connected()==true) {

    	//echo "string";

    $result=$this->mysqllive->selectData("SELECT * FROM patient_info WHERE ins_company=".$this->inscompany." and member_no=".$this->member_no);

    }else{

    	$result="offline";

    }





if ($result=="offline") {

	# code...

	$resultlocal=$this->mysqllocal->selectData("SELECT * FROM patient_info WHERE ins_company=".$this->inscompany." and member_no=".$this->member_no);

    

    if ($resultlocal==false) {

    	# code...

    	$answer=0;

    }else{

    	$answer=$resultlocal;

    }



}elseif($result==false){

$answer=0;

}else{

	foreach ($result as $row_rsodest_live) {

      		# code...

      		    $ins_company=$row_rsodest_live['ins_company'];

				$member_no=$row_rsodest_live['member_no'];

				$old_member_no=$row_rsodest_live['old_member_no'];

				$member_plan=$row_rsodest_live['member_plan'];

				$surname=$row_rsodest_live['surname'];

				$othernames=$row_rsodest_live['othernames'];

				$sex=$row_rsodest_live['sex'];

				$date_of_birth=$row_rsodest_live['date_of_birth'];

				$telephone_no=$row_rsodest_live['telephone_no'];

				$email=$row_rsodest_live['email'];

				$employer=$row_rsodest_live['employer'];

				$employer_id=$row_rsodest_live['employer_id'];

				$employer_type=$row_rsodest_live['employer_type'];

				$expiry_date=$row_rsodest_live['expiry_date'];

				$status=$row_rsodest_live['status'];

				$status_reason=$row_rsodest_live['status_reason'];

				$member_id=$row_rsodest_live['member_id'];

				$card_pin=$row_rsodest_live['card_pin'];

				$rx_member_id=$ins_company.$member_no;

				$member_plan_id=$row_rsodest_live['member_plan_id'];

				$outpatient_limit=$row_rsodest_live['outpatient_limit'];

				$inpatient_limit=$row_rsodest_live['inpatient_limit'];

				$outpatient_balance=$row_rsodest_live['outpatient_balance'];

				$inpatient_balance=$row_rsodest_live['inpatient_balance'];

				$member_type=$row_rsodest_live['member_type'];

				$principal_id=$row_rsodest_live['principal_id'];



      	}



      	$resultlocal=$this->mysqllocal->selectData("SELECT * FROM patient_info WHERE ins_company=".$this->inscompany." and member_no=".$this->member_no);





      		if ($resultlocal==false) {



      	$expiry_date=$this->get_company_expiry($employer_id, $ins_company);

      	

      	$this->mysqllocal->insertData('INSERT INTO `patient_info` (`id`, `ins_company`, `member_no`, `member_plan`, `surname`, `othernames`, `sex`, `date_of_birth`, `telephone_no`, `email`, `employer_id`, `employer`, `employer_type`, `expiry_date`, `status`, `status_reason`, `status_update_time`, `card_pin`, `member_id`, `rx_member_id`, `member_plan_id`, `member_type`, `principal_id`, `outpatient_limit`, `inpatient_limit`, `outpatient_balance`, `inpatient_balance`) VALUES (NULL, "'.$ins_company.'", "'.$member_no.'", "'.$member_plan.'", "'.$surname.'", "'.$othernames.'", "'.$sex.'", "'.$date_of_birth.'", "'.$telephone_no.'", "'.$email.'", "'.$employer_id.'", "'.$employer.'", "'.$employer_type.'", "'.$expiry_date.'", "'.$status.'", "'.$status_reason.'", CURRENT_TIMESTAMP, "'.$card_pin.'", "'.$member_id.'", "'.$rx_member_id.'", "'.$member_plan_id.'", "'.$member_type.'", "'.$principal_id.'", "'.$outpatient_limit.'", "'.$inpatient_limit.'", "'.$outpatient_balance.'", "'.$inpatient_balance.'")');



      }else{

      	$expiry_date=$this->get_company_expiry($employer_id, $ins_company);

      	$this->mysqllocal->insertData("UPDATE patient_info SET status='$status',expiry_date='$expiry_date' WHERE member_no=".$this->member_no);

      }



      $answer=$result;









}

    

    return $answer;



    }





function api_transaction_request($METHOD, $STATUSCODE, $STATUSMSG, $PROVIDERID, $MEMBERSHIPID, $APIKEY, $USERNAME, $USERID)

{

	# code...

	$characters = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z", "1","2","3","4","5","6","7","8","9");

	$PASSSALT=$characters[rand(0,35)].$characters[rand(0,35)].$characters[rand(0,35)].$characters[rand(0,35)].$characters[rand(0,35)].$characters[rand(0,35)].$characters[rand(0,35)];

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

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

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

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

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

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

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

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

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

	$this->mysqllocal->insertData("INSERT INTO `api_transaction_request` ( `method`,`request_status_code`, `request_status_msg`, `provider_id`, `member_id`, `apikey`, `username`, `user_id`, `pass_salt`, `date_time_request`) VALUES ( $METHOD, $STATUSCODE, $STATUSMSG, $PROVIDERID, $MEMBERSHIPID, $APIKEY, $USERNAME, $USERID, $PASSSALT, NOW())");

	



}





function get_company_expiry($companyid, $inscompany)

{

	# code...

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

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

    if ($this->mysqllocal->is_connected()==true ) {

	$result=$this->mysqllive->selectData("SELECT * FROM company WHERE ins_company=".$this->inscompany." and COMPANYID=".$this->companyid);

	if ($result==false) {

		# code...

		$expiry_date="2017-12-12";

	}else{

		foreach ($result as $value) {

			# code...

			$expiry_date=$value["PolicyEndDate"];

			$Company=$value["Company"];

			$CompanyID=$value["CompanyID"];

			$ins_company=$value["ins_company"];

		}



    $resultlocal=$this->mysqllocal->selectData("SELECT * FROM company WHERE ins_company=".$this->inscompany." and COMPANYID=".$this->companyid);

    if ($resultlocal==false) {

		# code...

		$this->mysqllocal->insertData("INSERT INTO company(Company,CompanyID,PolicyEndDate,ins_company)VALUES('$Company','$CompanyID','$expiry_date','$ins_company')");

	}else{

		$this->mysqllocal->insertData("UPDATE company SET PolicyEndDate='$expiry_date' WHERE ins_company=".$this->inscompany." and COMPANYID=".$this->companyid);

	}



	}

    }else{

    	$resultlocal=$this->mysqllocal->selectData("SELECT * FROM company WHERE ins_company=".$this->inscompany." and COMPANYID=".$this->companyid);

    	if ($resultlocal==false) {

		# code...

		$expiry_date="2017-12-12";

	}else{

		foreach ($resultlocal as $value) {

			# code...

			$expiry_date=$value["PolicyEndDate"];

		}

	}

    }

	



	return $expiry_date;



}





function getPlan($plan,$providerid)

{

	# code...

    





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

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

	$result=$this->mysqllocal->selectData("SELECT * FROM provider_api_plans where status='Active' and plan=".$plan." and provider_id=".$this->providerid);

	if ($result==false) {

		# code...

		$ans=0;

	}else{

		$ans=1;

	}

	return $ans;

	

}



    



}



?>