<?php
session_start();
require_once('Connections/courier.php');
require_once 'functions/functions.php';
mysql_select_db($database_courier, $courier);
date_default_timezone_set("Africa/Accra");

//echo "SELECT * FROM provider_api WHERE branch_id='".$_SESSION['branch_id']."' ORDER BY id LIMIT 1"; exit();
$api_details = mysql_fetch_assoc(mysql_query("SELECT * FROM provider_api WHERE branch_id='".$_SESSION['branch_id']."' ORDER BY id LIMIT 1"));
$api_key = $api_details['apikey'];
$username = $api_details['username'];
$password = $api_details['password'];
$provider_id = $api_details['provider_id_master'];
$provider_id_for = $api_details['provider_id'];
$action = mysql_real_escape_string($_POST["act"]);
$memberNo = mysql_real_escape_string($_POST["insurance_no1"]);
$ins_company = mysql_real_escape_string($_POST["insurance_com1"]);
//$action123 = $_POST['act'];
//
//if ($action123 == "update_claim_details_administrative"){
//    $provider_item_id = $_POST['id'];
//    $api_details = mysql_fetch_assoc(mysql_query("SELECT provider_item_id FROM claims_details WHERE id ='$provider_item_id' ORDER BY id LIMIT 1"));
//
//
//    $provider_item_id1 = $api_details['provider_item_id'];
//    $api_details = mysql_fetch_assoc(mysql_query("SELECT ins_item_code FROM service_items WHERE id ='$provider_item_id1' ORDER BY id LIMIT 1"));
//    //echo "SELECT ins_item_code FROM service_items WHERE id ='$provider_item_id1' ORDER BY id LIMIT 1";
//    $provider_item_id12 = $_POST['ins_item_code'];
//}else{
//    $provider_item_id = $_POST['id'];
//    $api_details = mysql_fetch_assoc(mysql_query("SELECT ins_item_code FROM claims_details WHERE provider_item_id ='$provider_item_id' ORDER BY id LIMIT 1"));
//     $provider_item_id12 = $api_details['ins_item_code'];
//}





$diagnosis = 'REFRACTIVE ERROR';
$icdcode = 'H52';
$domain = $_SESSION['name_code'];
if($domain == 'east_legon'){
    $domain = 'eastlegon';
}
$url = "master.rxclaimclient.com/v3ClientAPIMaster/index.php";

//$url = "http://localhost/rxclaim_client_master/v3ClientAPIMaster/index.php";

 $provider_item_id = $_POST['id'];
$api_details = mysql_fetch_assoc(mysql_query("SELECT * FROM claims_details WHERE provider_item_id ='$provider_item_id' ORDER BY id LIMIT 1"));
$item_service_type = $api_details['sub_item_service_type'];
 $provider_item_id122 = $api_details['provider_item_id'];
if ($item_service_type == "" || is_null($item_service_type)){
     $provider_item_id = $_POST['id'];
    $api_details = mysql_fetch_assoc(mysql_query("SELECT * FROM claims_details WHERE id ='$provider_item_id' ORDER BY id LIMIT 1"));
    $item_service_type = $api_details['sub_item_service_type'];
    $provider_item_id1 = $api_details['provider_item_id'];

    if($item_service_type == 'Optical Frame'){
        $ins_item_code = 'OPT04402';
        $item = 'Frame';
    } else if($item_service_type == 'Optical Accessories'){
        $ins_item_code = 'OPT06116';
        $item = 'Optical Accessories (CASE, LENS CLEANERS, CORD)';
    } else if($item_service_type == 'Optical Contact Lens'){
        $ins_item_code = '04661';
        $item = 'Contact Lens';
    }
}else{
    if($item_service_type == 'Optical Frame'){
        $ins_item_code = 'OPT04402';
        $item = 'Frame';
    } else if($item_service_type == 'Optical Accessories'){
        $ins_item_code = 'OPT06116';
        $item = 'Optical Accessories (CASE, LENS CLEANERS, CORD)';
    } else if($item_service_type == 'Optical Contact Lens'){
        $ins_item_code = '04661';
        $item = 'Contact Lens';
    }
}


if($action == 'member_status'){
    $attending_officer_id = mysql_real_escape_string($_POST["attending_officer"]);
    if($attending_officer_id == '0'){ $attending_officer_id = '507';}
    $attending_officer_details = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id='$attending_officer_id'"));
    $attending_officer = $attending_officer_details['surname'].' '.$attending_officer_details['firstnames'];
    $params = array(
        "APIKEY"=> $api_key,
    "PROVIDERID"=> $provider_id,
    "USERNAME"=> $username,
    "PASSWORD"=> $password,
    "INSCOMPANYCODE"=> $ins_company,
    "METHOD"=> "MEMBERSTATUS",
    "MEMBERSHIPID"=> $memberNo,
    "USERID"=> $attending_officer_id,
    "ATTENDINGOFFICER"=> $attending_officer,
        "PROVIDERIDFOR"=> $provider_id_for
    );
    //echo $url ;
    //print_r(json_encode($params));

    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    $response = curl_exec($ch);
    $response = json_decode($response, TRUE);
    $member_status = $response['MEMBERSTATUS'];
    //print_r($response); exit();

    if (curl_exec($ch) === FALSE) {
        die("Curl failed: " . curl_error($ch));
    }

    if(!empty($member_status)){
    if($member_status == 'Active'){
        echo '1';
    }else{
        echo "Membership status is ". $member_status;
    }
}else{
    echo $response['STATUSMSG'];
    }
}


if($action_2 == 'processclaimno'){
    $attending_officer_id = mysql_real_escape_string($_POST["attending_officer"]);
    if($attending_officer_id == '0'){ $attending_officer_id = '507'; $attending_officer_id_0 = '0';}
    $attending_officer_details = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id='$attending_officer_id'"));
    $attending_officer = $attending_officer_details['surname'].' '.$attending_officer_details['firstnames'];
    $today = date("Y-m-d");
    $params = array(
        "APIKEY"=> $api_key,
    "PROVIDERID"=> $provider_id,
    "USERNAME"=> $username,
    "PASSWORD"=> $password,
    "INSCOMPANYCODE"=> $ins_company,
    "METHOD"=> "PROCESSCLAIMNO",
    "MEMBERSHIPID"=> $memberNo,
    "USERID"=> $attending_officer_id,
    "ATTENDINGOFFICER"=> $attending_officer,
    "TYPEOFVISIT"=> "Out-Patient",
    "PIN"=> "0",
    "DATEOFCONSULTATION"=> $today,
    "PREGNANCY_STATUS"=> "",
    "PREGNANCY_WEEKS"=> "",
    "PREGNANCY_EXPECTED_DELIVERY"=> "",
    "VISITTYPE"=> "",
    "NATURE_OF_VISIT"=> "",
    "SERVICE_SPECIALITY"=> "",
    "BOOKINGNUMBER"=> "13456446",
    "PROVIDERIDFOR"=> $provider_id_for

    );
    //print_r(json_encode($params));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    $response = curl_exec($ch);
    $response = json_decode($response, TRUE);
    $CLAIMNUMBER_API = $response['CLAIMNUMBER'];
     //echo $CLAIMNUMBER_API;

}


if($action_2 == 'addclaimitem'){
    if($attending_officer_id == '0'){ $attending_officer_id = '507';}
    $attending_officer_details = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id='$attending_officer_id'"));
    $attending_officer_name = $attending_officer_details['surname'].' '.$attending_officer_details['firstnames'];
    $today = date("Y-m-d");
    $provider_item_id = $_POST['id'];
    //echo$ins_item_code.'sds'; exit();
    $params = array(
        "APIKEY"=> $api_key,
        "PROVIDERID"=> $provider_id,
        "USERNAME"=> $username,
        "PASSWORD"=> $password,
        "INSCOMPANYCODE"=> $api_ins_company,
        "METHOD"=> "ADDCLAIM",
        "MEMBERSHIPID"=> $member_insurance_no,
        "USERID"=> $attending_officer_id,
        "ATTENDINGOFFICER"=> $attending_officer,
        "TYPEOFVISIT"=> "Out-Patient",
    "HMSNO"=> "01",
    "DATEOFCONSULTATION"=> $date_of_consultation,
    "CLAIMNUMBER"=> $insurance_claim_no,
    "DIAGNOSIS"=> "unknown",
    "ICDCODE"=> "0",
    "PROVIDERITEMID"=> $provider_item_id122,
    "QTY"=> "1",
    "UNITPRICE"=> $unit_price,
    "DATEOFADMISSION"=> "",
    "DATEOFDISCHARGE"=> "",
    "NOOFDAYS"=> "1",
    "DOSAGE"=> "1",
    "DOSAGEUNIT"=> "1",
    "FREQUENCY"=> "1",
    "PROVIDERIDFOR"=> $provider_id_for
    );
    //print_r(json_encode($params));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    $response = curl_exec($ch);
    $response = json_decode($response, TRUE);
    $status_code = $response['STATUSCODE'];
    $status_message = $response['STATUSMSG'];
}


if($action_2 == 'updateclaimitem'){
    if($attending_officer_id == '0'){ $attending_officer_id = '507';}
    $attending_officer_details = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id='$attending_officer_id'"));
    $attending_officer_name = $attending_officer_details['surname'].' '.$attending_officer_details['firstnames'];
    $params = array(
        "APIKEY"=> $api_key,
        "PROVIDERID"=> $provider_id,
        "USERNAME"=> $username,
        "PASSWORD"=> $password,
    "INSCOMPANYCODE"=> $api_ins_company,
    "METHOD"=> "UPDATECLAIM",
    "MEMBERSHIPID"=> $member_insurance_no,
    "USERID"=> $attending_officer_id,
    "ATTENDINGOFFICER"=> $attending_officer,
    "TYPEOFVISIT"=> "Out-Patient",
    "HMSNO"=> "01",
    "DATEOFCONSULTATION"=> $date_of_consultation,
    "CLAIMNUMBER"=> $insurance_claim_no,
    "DIAGNOSIS"=> $diagnosis,
    "ICDCODE"=> $icdcode,
    "PROVIDERITEMID"=> $provider_item_id1,
    "QTY"=> $api_qty,
    "UNITPRICE"=> $api_unit_price,
    "DATEOFADMISSION"=> "",
    "DATEOFDISCHARGE"=> "",
    "NOOFDAYS"=> "1",
    "DOSAGE"=> "1",
    "DOSAGEUNITS"=> "1",
    "FREQUENCY"=> "1",
    "PROVIDERIDFOR"=> $provider_id_for

);
    //print_r(json_encode($params)); exit();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    $response = curl_exec($ch);
    $response = json_decode($response, TRUE);
    $status_code = $response['STATUSCODE'];
    $status_message = $response['STATUSMSG'];
}


if($action_2 == 'deleteclaimitem'){
    if($attending_officer_id == '0'){ $attending_officer_id = '507';}
    $attending_officer_details = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id='$attending_officer_id'"));
    $attending_officer_name = $attending_officer_details['surname'].' '.$attending_officer_details['firstnames'];
    $params = array(
        "APIKEY"=> $api_key,
        "PROVIDERID"=> $provider_id,
        "USERNAME"=> $username,
        "PASSWORD"=> $password,
        "INSCOMPANYCODE"=> $api_ins_company,
        "METHOD"=> "DELETECLAIM",
        "MEMBERSHIPID"=> $member_insurance_no,
        "USERID"=> $attending_officer_id,
        "ATTENDINGOFFICER"=> $attending_officer_name,
        "TYPEOFVISIT"=> "Out-Patient",
        "HMSNO"=> "01",
        "DATEOFCONSULTATION"=> $date_of_consultation,
        "CLAIMNUMBER"=> "$insurance_claim_no",
        "DIAGNOSIS"=> "$diagnosis",
        "ICDCODE"=> $icdcode,
        "PROVIDERITEMID"=> $provider_item_id1,
        "QTY"=> "1",
        "PROVIDERIDFOR"=> $provider_id_for
);
    //print_r(json_encode($params)); exit();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    $response = curl_exec($ch);
    $response = json_decode($response, TRUE);
    $status_code = $response['STATUSCODE'];
    $status_message = $response['STATUSMSG'];
}