<?php
 
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

function get_api_authentication($apikey, $username, $password, $providerid){
require('../Connections/courier.php');
mysql_select_db($database_courier, $courier);	

$query_rsodest = "select * from provider_api where apikey='$apikey' and username='$username' and password='$password' and provider_id='$providerid'";
$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
$row_rsodest = mysql_fetch_assoc($rsodest);
$totalRows_rsodest = mysql_num_rows($rsodest);

$answer=$totalRows_rsodest;

return $answer;	
}


function get_api_inscompany_activation($inscompany){
require('../Connections/courier.php');
mysql_select_db($database_courier, $courier);	

$query_rsodest = "SELECT * FROM insurance_company a inner join activation b on a.company_code=b.ins_company where a.status='Active' and a.company_code='$inscompany'";
$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
$row_rsodest = mysql_fetch_assoc($rsodest);
$totalRows_rsodest = mysql_num_rows($rsodest);

$answer=$totalRows_rsodest;

return $answer;	
}



function get_member_info($inscompany, $member_no){
require('../Connections/courier.php');
mysql_select_db($database_courier, $courier);
	

$query_rsodest = "select * from patient_info where ins_company='$inscompany' and member_no='$member_no'";
$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
$row_rsodest = mysql_fetch_assoc($rsodest);
$totalRows_rsodest = mysql_num_rows($rsodest);

if($totalRows_rsodest==0){

$connected = @fsockopen("www.google.com", 80);
if($connected){
require_once('../Connections/courier_live.php');
$query_rsodest_live = "SELECT * FROM patient_info  where member_no ='$member_no' and ins_company='$inscompany'";
//echo $query_rsodest_live;
$rsodest_live = mysql_query($query_rsodest_live, $courier_live) or die(mysql_error());
$totalRows_rsodest_live = mysql_num_rows($rsodest_live);

if($totalRows_rsodest_live>0){

$row_rsodest_live = mysql_fetch_assoc($rsodest_live);
				$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'];
				
			
				
			
		require('../Connections/courier_insert.php');	
		$query_insert='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.'");';

$rsodest_insert = mysql_query($query_insert, $courier_insert) or die(mysql_error());		

$query_rsodest = "select * from patient_info where ins_company='$inscompany' and member_no='$member_no'";
$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
$row_rsodest = mysql_fetch_assoc($rsodest);
$totalRows_rsodest = mysql_num_rows($rsodest);

$row_rsodest['total_row']=$totalRows_rsodest;

$answer=$row_rsodest;	
}

 
}else{

$row_rsodest['total_row']=0;	
$totalRows_rsodest=$row_rsodest;	
}
	
	
}else{
	
	
	
$row_rsodest['total_row']=$totalRows_rsodest;	
$totalRows_rsodest=$row_rsodest;
$answer=$totalRows_rsodest;
}

return $answer;	
}

function get_process_claim_no($inscompany, $member_no, $providerid, $typeofvisit, $providerid_id_for, $card_pin){
require('../Connections/courier.php');
mysql_select_db($database_courier, $courier);
	
$REQUESTMODE='API';
$INSCOMPANYCODE=$inscompany;
$MEMBERSHIPID=$member_no;
$PROVIDERID=$providerid;

if($providerid_id_for==''){
	$providerid_id_for=$PROVIDERID;
}



$query_rsodest_claim = "select *, DATE_FORMAT(datetimeadded, '%e %b %Y %r') as dateprocessing from process_claim_no where ins_company='$INSCOMPANYCODE' and member_no='$MEMBERSHIPID' and provider_id='$PROVIDERID' and status='In Use'";
$rsodest_claim = mysql_query($query_rsodest_claim, $courier) or die(mysql_error());
	$row_rsodest_claim = mysql_fetch_assoc($rsodest_claim);
	$totalRows_rsodest_claim = mysql_num_rows($rsodest_claim);
	
if($totalRows_rsodest_claim==0){
	
	
	$query_rsodest_provider_id = "select * from insurance_company where company_code='$INSCOMPANYCODE'";
$rsodest_provider_id = mysql_query($query_rsodest_provider_id, $courier) or die(mysql_error());
$row_rsodest_provider_id = mysql_fetch_assoc($rsodest_provider_id);
$totalRows_rsodest_provider_id = mysql_num_rows($rsodest_provider_id);

$use_pin=$row_rsodest_provider_id['use_pin'];

		if($use_pin=='yes'){
			$query_rsodest_pin = "select * from patient_info where ins_company='$INSCOMPANYCODE' and member_no='$MEMBERSHIPID' and card_pin='$card_pin'";
			
			//echo $query_rsodest_pin;
			//exit;
		
			$rsodest_pin = mysql_query($query_rsodest_pin, $courier) or die(mysql_error());
			$row_rsodest_pin = mysql_fetch_assoc($rsodest_pin);
			$totalRows_rsodest_pin = mysql_num_rows($rsodest_pin);
		}else{
		$totalRows_rsodest_pin=1;	
		}
		
		
if($totalRows_rsodest_pin>0){
	
	$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,32)].$characters[rand(0,32)];
		
		$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");
		$VALIDSALT=$characters[rand(0,32)].$characters[rand(0,32)].$characters[rand(0,32)].$characters[rand(0,32)];
		
		
		if($provider_id_for){
		$query_rsodest="INSERT INTO `process_claim_no` (`id`, `provider_id`, `ins_company`, `member_no`, `pass_salt`, `datetimeadded`, `date_added`, `request_mode`, `validation_salt`, `typeofvisit`, `attending_officer`, `provider_id_data_for`) VALUES (NULL, '$PROVIDERID', '$INSCOMPANYCODE', '$MEMBERSHIPID', '$PASSSALT', NOW(), CURDATE(), '$REQUESTMODE', '$VALIDSALT', '$typeofvisit', '$attending_officer', '$provider_id_for');";
		}else{
		$query_rsodest="INSERT INTO `process_claim_no` (`id`, `provider_id`, `ins_company`, `member_no`, `pass_salt`, `datetimeadded`, `date_added`, `request_mode`, `validation_salt`, `typeofvisit`, `attending_officer`) VALUES (NULL, '$PROVIDERID', '$INSCOMPANYCODE', '$MEMBERSHIPID', '$PASSSALT', NOW(), CURDATE(), '$REQUESTMODE', '$VALIDSALT', '$typeofvisit', '$attending_officer');";	
		}
		
		//echo $query_rsodest;
		$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
		$CLAIMID=mysql_insert_id();
		$lenclaim=strlen($CLAIMID);
		$lenprovider=strlen($PROVIDERID);
		$claimidzero=str_repeat("0",(4-$lenclaim)).$CLAIMID;
		$provideridzero=str_repeat("0",(4-$lenprovider)).$PROVIDERID;
			
	
		$CLAIMNUMBER=$INSCOMPANYCODE.$provideridzero.$claimidzero.$PASSSALT.date('y');
		
		
		
		
		$query_rsodest="update process_claim_no set process_claim_no='$CLAIMNUMBER' where id='$CLAIMID'";
		$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());
		
	$query_rsodest_claim = "select *, DATE_FORMAT(datetimeadded, '%e %b %Y %r') as dateprocessing  from process_claim_no where ins_company='$INSCOMPANYCODE' and member_no='$MEMBERSHIPID' and provider_id='$PROVIDERID' and id=$CLAIMID";
//echo $query_rsodest_claim;
	$rsodest_claim = mysql_query($query_rsodest_claim, $courier) or die(mysql_error());
	$row_rsodest_claim = mysql_fetch_assoc($rsodest_claim);
	$totalRows_rsodest_claim = mysql_num_rows($rsodest_claim);	
	
	$row_rsodest_claim['total_row']=1;
	$row_rsodest_claim['STATUSCODE']='003';
	$row_rsodest_claim['STATUSMSG']='Authorization Permitted. Process Claim Number Generated.';
		
	
}else{
	
	$row_rsodest_claim['total_row']=0;
	$row_rsodest_claim['STATUSCODE']='002';
	$row_rsodest_claim['STATUSMSG']='Authorization Permitted but WRONG CLIENT PIN. PLEASE TRY AGIAN.';
	$row_rsodest_claim['ins_company']=$INSCOMPANYCODE;
	$row_rsodest_claim['member_no']=$MEMBERSHIPID;
	
	
}



}else{
	
	$row_rsodest_claim['total_row']=1;
	$row_rsodest_claim['STATUSCODE']='003';
	$row_rsodest_claim['STATUSMSG']='Authorization Permitted. Process Claim Number Generated.';
	
	
}

$answer=$row_rsodest_claim;

return $answer;	
}

?>