<?php

define('MyConst', TRUE);

include 'class/Authentication.php';

include'class/MemberInfo.php';

include'class/ProccessClaimNo.php';

include 'class/AddClaim.php';

include'class/Benefits.php';

include'class/ItemList.php';

include'class/SubmitClaim.php';

include'class/AddUser.php';

include'class/Policy.php';

include'class/Insurance.php';

include'method.php';



//instantiations

$authentication=new Authentication($mysqlLocal);

$member=new MemberInfo($mysqlLocal,$mysqlLive);

$process_claim_no=new ProccessClaimNo($mysqlLocal);

$claiminfo=new AddClaim($mysqlLocal);

$ben=new Benefits($mysqlLocal,$mysqlLive);

$items=new ItemList($mysqlLocal);

$addclaim=new AddClaim($mysqlLocal);

$sub=new SubmitClaim($mysqlLocal,$mysqlLive);

$addUser=new AddUser($mysqlLocal);

$pol=new Policy($mysqlpo);

$inscomps=new Insurance($mysqlLocal);

$er=$pol->checkAPIExpiry($APIKEY,$PROVIDERID);



if (in_array("777", $er)) {

    # code...





$api_authentication=$authentication->get_api_authentication($APIKEY, $USERNAME, $PASSWORD, $PROVIDERID);

$member_info1=$member->get_member_info($INSCOMPANYCODE, $MEMBERSHIPID);

if($api_authentication==0){

    $STATUSCODE="001";

    $STATUSMSG="Login Failed. Please Contact your IT administrator";

    $data = array(

                "STATUSCODE"=>"$STATUSCODE",

                "STATUSMSG"=>"$STATUSMSG"

            );

            

                

}else{



$api_inscompany_activation=$authentication->get_api_inscompany_activation($INSCOMPANYCODE,$PROVIDERID);

if ($METHOD=="ADDUSER" || $METHOD=="INSURANCELIST") {

    # code...

 $api_inscompany_activation=1;   

}



if($api_inscompany_activation==0){

    

    $STATUSCODE="002";

        $STATUSMSG="Authorization Permitted but Insurance Company Not Active on RX Claim. Please Contact your IT administrator.";

        $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );          

    

        

    } else{



  //switch

   

		switch ($METHOD) {

            case 'MEMBERSTATUS':

                # code...            

            

            if ($member_info1==0) {

            	# code...

            	$STATUSCODE="002";

		        $STATUSMSG="Authorization Permitted but Member Data not found. Please Contact your IT administrator.";

		        $data = array(

					"STATUSCODE"=>"$STATUSCODE",

					"STATUSMSG"=>"$STATUSMSG"

				);	

            }elseif ($USERID=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide User's Id";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($ATTENDINGOFFICER=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide Attending Officer's Name";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($process_claim_no->checkUserid($USERID,$ATTENDINGOFFICER)==false) {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Invalid User's Id ";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }else{



            	$member->api_transaction_request($METHOD, $STATUSCODE, $STATUSMSG, $PROVIDERID, $MEMBERSHIPID, $APIKEY, $USERNAME, $USERID);

                

            	foreach ($member_info1 as $member_info) {

            		# code...

        $INSCOMPANYCODE=$member_info['ins_company'];

		$MEMBERSHIPID=$member_info['member_no'];

		$MEMBERPLAN=$member_info['member_plan'];

		$MEMBERPLANID=$member_info['member_plan_id'];

		$MEMBERID=$member_info['member_id'];

		$SURNAME=$member_info['surname'];

		$OTHERNAMES=$member_info['othernames'];

		$SEX=$member_info['sex'];

		$DATEOFBIRTH=$member_info['date_of_birth'];

		$EMPLOYER=$member_info['employer'];

		$EMPLOYERID=$member_info['employer_id'];

		$EXPIRYDATE=$member_info['expiry_date'];

		$MEMBERSTATUS=$member_info['status'];

		$PLAN=$member_info['member_plan'];

            	}



        if($EXPIRYDATE<=date('Y-m-d')){

			

			$EXPIRYDATE=$member->get_company_expiry($EMPLOYERID,$INSCOMPANYCODE);

         }



		

		

		if($EXPIRYDATE<date('Y-m-d')){

		$MEMBERSTATUS='Expired';	

		}





		$checkplan=$member->getPlan($PLAN,$PROVIDERID);

		if ($checkplan==0) {

			# code...

			$MEMBERSTATUS='Inactive - Benefit Plan not Supported';

		}



				$data = array(

					"STATUSCODE"=>"003",

					"STATUSMSG"=>"Authorisation Permitted",

					"INSCOMPANYCODE"=>"$INSCOMPANYCODE",

					"MEMBERSHIPID"=>"$MEMBERSHIPID",

					"MEMBERID"=>"$MEMBERID",

					"MEMBERPLAN"=>"$MEMBERPLAN",

					"MEMBERPLANID"=>"$MEMBERPLANID",

					"SURNAME"=>"$SURNAME",

					"OTHERNAMES"=>"$OTHERNAMES",

					"SEX"=>"$SEX",

					"DATEOFBIRTH"=>"$DATEOFBIRTH",

					"EMPLOYER"=>"$EMPLOYER",

					"EXPIRYDATE"=>"$EXPIRYDATE",

					"MEMBERSTATUS"=>"$MEMBERSTATUS",

					

				);

		

		

            }

                break;

           //Generate Process Claim No

            case 'PROCESSCLAIMNO':

                # code...

           // $member_info1=$member->get_member_info($INSCOMPANYCODE, $MEMBERSHIPID);

            

            if ($member_info1==0) {

                # code...

                $STATUSCODE="002";

                $STATUSMSG="Authorization Permitted but Member Data not found. Please Contact your IT administrator.";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );  

            }elseif ($USERID=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide User's Id";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($ATTENDINGOFFICER=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide Attending Officer's Name";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($process_claim_no->checkUserid($USERID,$ATTENDINGOFFICER)==false) {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Invalid User's Id ";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }else{



                $member->api_transaction_request($METHOD, $STATUSCODE, $STATUSMSG, $PROVIDERID, $MEMBERSHIPID, $APIKEY, $USERNAME, $USERID);

                foreach ($member_info1 as $member_info) {

                    # code...

        $INSCOMPANYCODE=$member_info['ins_company'];

        $MEMBERSHIPID=$member_info['member_no'];

        $MEMBERPLAN=$member_info['member_plan'];

        $MEMBERPLANID=$member_info['member_plan_id'];

        $MEMBERID=$member_info['member_id'];

        $SURNAME=$member_info['surname'];

        $OTHERNAMES=$member_info['othernames'];

        $SEX=$member_info['sex'];

        $DATEOFBIRTH=$member_info['date_of_birth'];

        $EMPLOYER=$member_info['employer'];

        $EMPLOYERID=$member_info['employer_id'];

        $EXPIRYDATE=$member_info['expiry_date'];

        $MEMBERSTATUS=$member_info['status'];

        $PLAN=$member_info['member_plan'];

                }



        if($EXPIRYDATE<=date('Y-m-d')){

            

            $EXPIRYDATE=$member->get_company_expiry($EMPLOYERID,$INSCOMPANYCODE);

         }



        

        

        if ($USERID=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide User's Id";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($ATTENDINGOFFICER=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide Attending Officer's Name";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        } elseif ($process_claim_no->checkUserid($USERID,$ATTENDINGOFFICER)==false) {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Invalid User's Id ";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($TYPEOFVISIT=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide Type Of Visit";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }





        elseif($EXPIRYDATE<date('Y-m-d')){ 

        $STATUSCODE="002";

                $STATUSMSG="Inactive - Policy Has Expired";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

   

        }elseif ($member->getPlan($PLAN,$PROVIDERID)==0) {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Inactive - Benefit Plan not Supported";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );



        }else{



            if($TYPEOFVISIT=='Out-Patient' || $TYPEOFVISIT=='In-Patient'){



              /*

              For checking benefits

                if ($TYPEOFVISIT=='Out-Patient') {

                    # code...

                    $member_out_benefits=$ben->get_outpatient_limit_balance($MEMBERSHIPID,$INSCOMPANYCODE,"0",$current_id);



                }



                */

              

              $process_claim=$process_claim_no->get_process_claim_no($INSCOMPANYCODE, $MEMBERSHIPID, $PROVIDERID, $TYPEOFVISIT, $USERID,$ATTENDINGOFFICER);

             

              $INSCOMPANYCODE=$process_claim['ins_company'];

              $MEMBERSHIPID=$process_claim['member_no'];

              $STATUSCODE=$process_claim['STATUSCODE'];

              $STATUSMSG=$process_claim['STATUSMSG'];

              $TYPEOFVISIT=$process_claim['typeofvisit'];

              $CLAIMNUMBER=$process_claim['process_claim_no'];

        

        

        

    

    

        

        

        $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG",

                    "INSCOMPANYCODE"=>"$INSCOMPANYCODE",

                    "MEMBERPLANID"=>"$MEMBERPLANID",

                    "MEMBERPLAN"=>"$MEMBERPLAN",

                    "SURNAME"=>"$SURNAME",

                    "OTHERNAMES"=>"$OTHERNAMES",

                    "SEX"=>"$SEX",

                    "DATEOFBIRTH"=>"$DATEOFBIRTH",

                    "EMPLOYER"=>"$EMPLOYER",

                    "EXPIRYDATE"=>"$EXPIRYDATE",

                    "MEMBERSTATUS"=>"$MEMBERSTATUS",

                    "CLAIMNUMBER"=>"$CLAIMNUMBER",

                    "TYPEOFVISIT"=>"$TYPEOFVISIT"

                    

                );

            }else{

           

        $STATUSCODE="008";

        $STATUSMSG="Authorization Permitted. No Process Claim No. Generated. Type of Visit is Wrong. It should be Out-Patient or In-Patient.";

        $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );  



            }



        }



            }

                break;

             case 'ITEMLIST':

                # code...

        $STATUSCODE="003";

        $STATUSMSG="Authorization Permitted.";

        $dat=$items->getItems();

        $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG",

                    "ITEMLIST"=>$dat

                );  



                break;

			case'INSURANCELIST':

			

			$data=$inscomps->insList();

			break;

            case 'ADDCLAIM':

            case 'UPDATECLAIM':

            case 'DELETECLAIM':

                # code...

            if ($member_info1==0) {

                # code...

                $STATUSCODE="002";

                $STATUSMSG="Authorization Permitted but Member Data not found. Please Contact your IT administrator.";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );  

            }elseif ($USERID=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide User's Id";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($ATTENDINGOFFICER=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide Attending Officer's Name";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($process_claim_no->checkUserid($USERID,$ATTENDINGOFFICER)==false) {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Invalid User's Id ";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ( $CLAIMNUMBER=="" || $ATTENDINGOFFICER=='' || $TYPEOFVISIT=="" || $DATEOFCONSULTATION=="" || $DIAGNOSIS=="" || $ICDCODE=="" ||$PROVIDERITEMID=="" || $QTY=="" || $UNITPRICE=="" || $USERID=="" ) {

                # code...

                $STATUSCODE="008";

        $STATUSMSG="CLAIMNUMBER,ATTENDINGOFFICER,TYPEOFVISIT,DATEOFCONSULTATION,DIAGNOSIS,PROVIDERITEMID,QTY,USERID, UNITPRICE and ICDCODE Must Not Be Empty";

        $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );  



            }else {

                # code...

                $member->api_transaction_request($METHOD, $STATUSCODE, $STATUSMSG, $PROVIDERID, $MEMBERSHIPID, $APIKEY, $USERNAME, $USERID);

                      foreach ($member_info1 as $member_info) {

                    # code...

        $INSCOMPANYCODE=$member_info['ins_company'];

        $MEMBERSHIPID=$member_info['member_no'];

        $MEMBERPLAN=$member_info['member_plan'];

        $MEMBERPLANID=$member_info['member_plan_id'];

        $MEMBERID=$member_info['member_id'];

        $SURNAME=$member_info['surname'];

        $OTHERNAMES=$member_info['othernames'];

        $SEX=$member_info['sex'];

        $DATEOFBIRTH=$member_info['date_of_birth'];

        $EMPLOYER=$member_info['employer'];

        $EMPLOYERID=$member_info['employer_id'];

        $EXPIRYDATE=$member_info['expiry_date'];

        $MEMBERSTATUS=$member_info['status'];

        $PLAN=$member_info['member_plan'];

                }



        if($EXPIRYDATE<=date('Y-m-d')){

            

            $EXPIRYDATE=$member->get_company_expiry($EMPLOYERID,$INSCOMPANYCODE);

         }



         if($EXPIRYDATE<date('Y-m-d')){ 

        $STATUSCODE="002";

                $STATUSMSG="Inactive - Policy Has Expired";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

   

        }elseif ($member->getPlan($PLAN,$PROVIDERID)==0) {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Inactive - Benefit Plan not Supported";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );



        }else{

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

                # code...

                $step=$ben->get_claim_expire($CLAIMNUMBER);

            if ($step[0]=="no") {

                # code...

                $STATUSCODE="002";

                $STATUSMSG=$answer[1];

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

            }elseif ($step[0]=="yes") {

                # code...

              $data =  $addclaim->addClaims($INSCOMPANYCODE,$PROVIDERID,$MEMBERSHIPID,$CLAIMNUMBER,$PROVIDERITEMID,$PROVIDERITEMNAME,$QTY,$UNITPRICE,$ATTENDINGOFFICER,$TYPEOFVISIT,$USERID,$ICDCODE,$DIAGNOSIS,$DATEOFCONSULTATION,$DATEOFADMISSION,$DATEOFDISCHARGE,$DOSAGE,$DOSAGEUNITS,$FREQUENCY,$NOOFDAYS,$METHOD);



            }



            }else{



               $data =  $addclaim->addClaims($INSCOMPANYCODE,$PROVIDERID,$MEMBERSHIPID,$CLAIMNUMBER,$PROVIDERITEMID,$PROVIDERITEMNAME,$QTY,$UNITPRICE,$ATTENDINGOFFICER,$TYPEOFVISIT,$USERID,$ICDCODE,$DIAGNOSIS,$DATEOFCONSULTATION,$DATEOFADMISSION,$DATEOFDISCHARGE,$DOSAGE,$DOSAGEUNITS,$FREQUENCY,$NOOFDAYS,$METHOD);

 

            }

            



        }





            }

           

                break;



        case 'SUBMITCLAIM':

        if ($member_info1==0) {

                # code...

                $STATUSCODE="002";

                $STATUSMSG="Authorization Permitted but Member Data not found. Please Contact your IT administrator.";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );  

            }elseif ($USERID=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide User's Id";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($ATTENDINGOFFICER=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide Attending Officer's Name";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }elseif ($process_claim_no->checkUserid($USERID,$ATTENDINGOFFICER)==false) {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Invalid User's Id ";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }else{



                $member->api_transaction_request($METHOD, $STATUSCODE, $STATUSMSG, $PROVIDERID, $MEMBERSHIPID, $APIKEY, $USERNAME, $USERID);

                foreach ($member_info1 as $member_info) {

                    # code...

        $INSCOMPANYCODE=$member_info['ins_company'];

        $MEMBERSHIPID=$member_info['member_no'];

        $MEMBERPLAN=$member_info['member_plan'];

        $MEMBERPLANID=$member_info['member_plan_id'];

        $MEMBERID=$member_info['member_id'];

        $SURNAME=$member_info['surname'];

        $OTHERNAMES=$member_info['othernames'];

        $SEX=$member_info['sex'];

        $DATEOFBIRTH=$member_info['date_of_birth'];

        $EMPLOYER=$member_info['employer'];

        $EMPLOYERID=$member_info['employer_id'];

        $EXPIRYDATE=$member_info['expiry_date'];

        $MEMBERSTATUS=$member_info['status'];

        $PLAN=$member_info['member_plan'];

                }

            

            $data=$sub->subClaims($INSCOMPANYCODE,$PROVIDERID, $MEMBERSHIPID, $CLAIMNUMBER);

            }



        break;

        case 'ADDUSER':

                # code...

        if ($FIRSTNAME=="" || $SURNAME=="" || $ROLE=="") {

            # code...

            $STATUSCODE="002";

                $STATUSMSG="Provide FIRSTNAME,SURNAME And ROLE ";

                $data = array(

                    "STATUSCODE"=>"$STATUSCODE",

                    "STATUSMSG"=>"$STATUSMSG"

                );

        }else{

            

            $data=$addUser->addNow($FIRSTNAME,$SURNAME,$ROLE);

        }

        

                break;

     

     /*       

            

                # code...

            include('update_claim.php');

                break;

            

            case 'SUBMITCLAIM':

                # code...

            include('submit_claim.php');

                break;

            case 'OTHER':

                # code...

            include('other.php');

                break;

            

            default:

                # code...

                break;



                */

        }

	

       

    }



}



}else{

    $data=$er;

}





switch ($RESPONSETYPE) {

    case 'JSON':

        # code...

    print_r(json_encode($data));

        break;

    case 'XML':

        # code...

    $xml = new XmlWriter();

    $xml->openMemory();

    $xml->startDocument('1.0', 'UTF-8');

    $xml->startElement('response');



   function write(XMLWriter $xml, $data){



    foreach($data as $key => $value){

        if(is_array($value)){

            $xml->startElement($key);

            write($xml, $value);

            $xml->endElement();

            continue;

        }

        $xml->writeElement($key, $value);

    }

   }

   write($xml, $data);



  $xml->endElement();

  print_r($xml->outputMemory(true));

        break;

    case 'ARRAY':

        # code...

    print_r($data);

        break;

    

    default:

        # code...

    print_r(json_encode($data));



        break;

}







?>