<?php 
//include('accesscheck2.php');
require_once('Connections/courier.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;
}
}
mysql_select_db($database_courier, $courier);

function get_outpatient_limit_balance($claimno,$memberno,$ins_company,$price, $current_id){
	
$query_check="select * from adjudication_rules where adjudication_rule='outpatient_limit' and  status='active' and ins_company='$ins_company'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$totalRows_rscheck = mysql_num_rows($rscheck);

if($totalRows_rscheck>0){

$approve[0]='yes';
$approve[1]='Approved';	

$query_check="select * from patient_info where member_no='$memberno' and ins_company='$ins_company'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);
$outpatient_balance=$row_rscheck['outpatient_balance'];
$outpatient_limit=$row_rscheck['outpatient_limit'];
$member_plan_id=$row_rscheck['member_plan_id'];
$employer_id=$row_rscheck['employer_id'];

if($outpatient_limit<=0){
$query_check="select * from company_plan where PlanID='$member_plan_id' and ins_company='$ins_company' and CompanyID='$employer_id'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);
$outpatient_limit=$row_rscheck['outpatient_limit'];
}

$query_check="select sum(qty*unit_price) as claim_total from claims_details where process_claim_no='$claimno'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);

$claim_total=$row_rscheck['claim_total'];

if($current_id){
$query_check_old_price="select sum(qty*unit_price) as claim_total from claims_details where id='$current_id'";
$rscheck_old_price= mysql_query($query_check_old_price) or die(mysql_error());
$row_rscheck_old_price = mysql_fetch_assoc($rscheck_old_price);

$oldprice=$row_rscheck_old_price['claim_total'];
}


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


			if($actual_balance<=$outpatient_limit){
			$approve[0]='yes';	
			$approve[1]='Approved';	
			}else{
			$approve[0]='no';
			$approve[1]='Out Patient Limit Exceeded. Item can not be added.';	
			}

}else{
$approve[0]='yes';
$approve[1]='Approved';			
}

return $approve;

	
}


function get_inpatient_limit_balance($claimno,$memberno,$ins_company,$price, $current_id){
	
$query_check="select * from adjudication_rules where adjudication_rule='inpatient_limit' and  status='active' and ins_company='$ins_company'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$totalRows_rscheck = mysql_num_rows($rscheck);

if($totalRows_rscheck>0){

$approve[0]='yes';
$approve[1]='Approved';	

$query_check="select * from patient_info where member_no='$memberno' and ins_company='$ins_company'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);
$inpatient_balance=$row_rscheck['inpatient_balance'];
$inpatient_limit=$row_rscheck['inpatient_limit'];
$member_plan_id=$row_rscheck['member_plan_id'];
$employer_id=$row_rscheck['employer_id'];

if($outpatient_limit<=0){
$query_check="select * from company_plan where PlanID='$member_plan_id' and ins_company='$ins_company' and CompanyID='$employer_id'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);
$inpatient_limit=$row_rscheck['inpatient_limit'];
}

$query_check="select sum(qty*unit_price) as claim_total from claims_details where process_claim_no='$claimno'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);

$claim_total=$row_rscheck['claim_total'];

if($current_id){
$query_check_old_price="select sum(qty*unit_price) as claim_total from claims_details where id='$current_id'";
$rscheck_old_price= mysql_query($query_check_old_price) or die(mysql_error());
$row_rscheck_old_price = mysql_fetch_assoc($rscheck_old_price);

$oldprice=$row_rscheck_old_price['claim_total'];
}


$actual_balance=$inpatient_balance+$claim_total+$price-$oldprice;


			if($actual_balance<=$inpatient_limit){
			$approve[0]='yes';	
			$approve[1]='Approved';	
			}else{
			$approve[0]='no';
			$approve[1]='In Patient Limit Exceeded. Item can not be added.';	
			}

}else{
$approve[0]='yes';
$approve[1]='Approved';			
}

return $approve;

	
}



function get_authorization($ins_company,$ins_item_code){
	
$query_check="select * from adjudication_rules where adjudication_rule='check_authorization' and  status='active' and ins_company='$ins_company'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$totalRows_rscheck = mysql_num_rows($rscheck);

if($totalRows_rscheck>0){

$queryItems = "SELECT * FROM service_items_authorization WHERE ins_item_code='$ins_item_code' and authorization_required='yes' and ins_company='$ins_company'";
//echo $queryItems;
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);

if($totalRows>0){
$authorization='yes';	
}	
}

return $authorization;

	
}


function get_price($itemid, $ins_company, $mode_of_payment_all) {
    $resultItems = mysql_query("SELECT * FROM service_items WHERE id='$itemid'") or die(mysql_error());
    $arrayItems = mysql_fetch_assoc($resultItems);
    if($arrayItems['item']=='Client Own Frame'){
        $unit_price = 0;
        $selling_unit = 0;
        $selling_qty = 0;
        $payment_status = 'Paid';
    }
    else if ($mode_of_payment_all == 'insurance' || $mode_of_payment_all == 'company') {
        $payment_status = 'Paid';
       //$abasa =  "select * from service_items_price where ins_company='$ins_company' and provider_item_id='$itemid'";
        $rscheck = mysql_query("select * from service_items_price where ins_company='$ins_company' and provider_item_id='$itemid'") or die(mysql_error());
        $totalRows_rscheck = mysql_num_rows($rscheck);
        if ($totalRows_rscheck > 0) {
            $row_rscheck = mysql_fetch_assoc($rscheck);
            $unit_price = $row_rscheck['price'];
            $selling_unit = $row_rscheck['selling_unit'];
            $selling_qty = $row_rscheck['selling_qty'];
            $exclude = $row_rscheck['exclusion'];
            if ($exclude == 'yes') {
                $unit_price=$arrayItems['price_'.$_SESSION['name_code']];
                $selling_unit = $arrayItems['selling_unit'];
                $selling_qty = $arrayItems['selling_qty'];
                $mode_of_payment_all = 'cash';
                $payment_status = 'Owing';
            }
        } else {
            if ($mode_of_payment_all == 'insurance' && $arrayItems["price_insurance"] > 0) {
                $unit_price = $arrayItems["price_insurance"];
                $selling_unit = $arrayItems['selling_unit_insurance'];
                $selling_qty = $arrayItems['selling_qty_insurance'];
            } else if ($mode_of_payment_all == 'company' && $arrayItems["price_company"] > 0) {
                $unit_price = $arrayItems["price_company"];
                $selling_unit = $arrayItems['selling_unit_company'];
                $selling_qty = $arrayItems['selling_qty_company'];
            } else {
                $unit_price = $arrayItems['price_'.$_SESSION['name_code']];
                $selling_unit = $arrayItems['selling_unit'];
                $selling_qty = $arrayItems['selling_qty'];
            }
        }
    } else if ($mode_of_payment_all == 'cash') {
        $unit_price = $arrayItems['price_'.$_SESSION['name_code']];
        $selling_unit = $arrayItems['selling_unit'];
        $selling_qty = $arrayItems['selling_qty'];
        $payment_status = 'Owing';
    } else if ($mode_of_payment_all == 'staff') {
        $payment_status = 'Paid';
        $selling_unit = $arrayItems['selling_unit_staff'];
        $selling_qty = $arrayItems['selling_qty_staff'];
        ($arrayItems["price_staff"] > 0) ? $unit_price = $arrayItems["price_staff"] : $unit_price = $arrayItems["price"];
        ($arrayItems["selling_unit_staff"] > 0) ? $selling_unit = $arrayItems["selling_unit_staff"] : $selling_unit = $arrayItems["selling_unit"];
        ($arrayItems["selling_qty_staff"] > 0) ? $selling_qty = $arrayItems["selling_qty_staff"] : $selling_qty = $arrayItems["selling_qty"];
    } else if ($mode_of_payment_all == 'gratis'){
        $unit_price = $arrayItems['price_'.$_SESSION['name_code']];
        $selling_unit = $arrayItems['selling_unit'];
        $selling_qty = $arrayItems['selling_qty'];
        $mode_of_payment_all = 'gratis';
        $payment_status = 'Unapproved';
    } else if ($mode_of_payment_all == 'credit sales'){
        $unit_price = $arrayItems['price_'.$_SESSION['name_code']];
        $selling_unit = $arrayItems['selling_unit'];
        $selling_qty = $arrayItems['selling_qty'];
        $mode_of_payment_all = 'credit sales';
        $payment_status = 'Unapproved';
    } else if ($mode_of_payment_all == 'replacement'){
        $unit_price = $arrayItems['price_'.$_SESSION['name_code']];
        $selling_unit = $arrayItems['selling_unit'];
        $selling_qty = $arrayItems['selling_qty'];
        $mode_of_payment_all = 'replacement';
        $payment_status = 'Unapproved';
    } else{
        $unit_price = $arrayItems['price_'.$_SESSION['name_code']];
        $selling_unit = $arrayItems['selling_unit'];
        $selling_qty = $arrayItems['selling_qty'];
        $payment_status = 'Owing';
    }
	
	if($selling_qty=='' || $selling_qty==0){
	$selling_qty=1;	
	}
    
    return array($unit_price, $mode_of_payment_all, $payment_status, $selling_unit, $selling_qty);
}



function get_item_price($itemid, $ins_company){
	
$query_check="select * from service_items_price where ins_company='$ins_company' and provider_item_id='$itemid'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$totalRows_rscheck = mysql_num_rows($rscheck);


if($totalRows_rscheck>0){

$unit_price=$row_rscheck['price'];	
	
}else{
	
$query_check="SELECT * FROM service_items WHERE id='$itemid'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$unit_price=$row_rscheck['price'];	
	
}

return $unit_price;

	
}

function get_insurance_price($itemid, $ins_company){
	
$query_check="select * from service_items_price where ins_company='$ins_company' and provider_item_id='$itemid'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$totalRows_rscheck = mysql_num_rows($rscheck);


if($totalRows_rscheck>0){
$unit_price=$row_rscheck['price'];	
}else{
$unit_price='';	
}

return $unit_price;

}

//function get_patient_outstanding_balance($member_no) {
//
//    $query_check = "select * from patient_information where card_number='$member_no'";
//    $rscheck = mysql_query($query_check) or die(mysql_error());
//    $row_rscheck = mysql_fetch_assoc($rscheck);
//    $totalRows_rscheck = mysql_num_rows($rscheck);
//
//    $total_deposits = (float) $row_rscheck['patient_balance'];
//
//    $query_check = "select sum(qty*unit_price) as total_owing from claims_details where member_no='$member_no' and mode_of_payment='cash' and payment_status='Owing'";
//    $rscheck = mysql_query($query_check) or die(mysql_error());
//    $row_rscheck = mysql_fetch_assoc($rscheck);
//    $totalRows_rscheck = mysql_num_rows($rscheck);
//
//    $total_owing = (float) $row_rscheck['total_owing'];
//
//    $outstanding_balance = $total_deposits - $total_owing;
//
//
//    return $outstanding_balance;
//}

function get_disease_limit_balance($claimno,$memberno,$ins_company,$price, $current_id){
	
$query_check="select * from adjudication_rules where adjudication_rule='disease_limit' and  status='active' and ins_company='$ins_company'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);	
$totalRows_rscheck = mysql_num_rows($rscheck);

if($totalRows_rscheck>0){
$approve[0]='yes';
$approve[1]='Approved';	
	
$query_check="select * from patient_info where member_no='$memberno' and ins_company='$ins_company'";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);
$member_plan_id=$row_rscheck['member_plan_id'];
$employer_id=$row_rscheck['employer_id'];	

$query_check="select * from claims_details_query_diagnosis where process_claim_no='$claimno' order by id";
$rscheck= mysql_query($query_check) or die(mysql_error());
$row_rscheck = mysql_fetch_assoc($rscheck);

do{
$disease_code=$row_rscheck['disease_code'];
	
$query_check_disease="select * from service_constraint_disease where icd_10_code='$disease_code' and PlanID='$member_plan_id' and ins_company='$ins_company' and CompanyID='$employer_id'";
$rscheck_disease= mysql_query($query_check_disease) or die(mysql_error());
$row_rscheck_disease = mysql_fetch_assoc($rscheck_disease);
$totalrow_rscheck_disease = mysql_num_rows($rscheck_disease);

$price_limit=$row_rscheck_disease['price_limit'];
$number_of_times=$row_rscheck_disease['number_of_times'];


if($totalrow_rscheck_disease>0){

$query_check_disease="select * from service_member_disease where icd_10_code='$disease_code' and member_no='$memberno' and ins_company='$ins_company'";
$rscheck_disease= mysql_query($query_check_disease) or die(mysql_error());
$row_rscheck_disease = mysql_fetch_assoc($rscheck_disease);
$totalrow_rscheck_disease = mysql_num_rows($rscheck_disease);

if($totalrow_rscheck_disease>0){
$balance=$row_rscheck_disease['balance'];
$use_number=$row_rscheck_disease['use_number'];
$consultation_date=$row_rscheck_disease['status_update_time'];

$query_check_disease="select sum(qty*unit_price) as claim_total, count(disease_code) as disease_total, b.disease_name from claims_details a LEFT JOIN claims_details_query_diagnosis b on a.process_claim_no=b.process_claim_no where a.member_no='$memberno' and b.disease_code='$disease_code' and a.date_of_consultation>='$consultation_date'";

//echo $query_check_disease;
$rscheck_disease= mysql_query($query_check_disease) or die(mysql_error());
$row_rscheck_disease = mysql_fetch_assoc($rscheck_disease);

$disease_claim_total=$row_rscheck_disease['claim_total'];
$disease_number_total=$row_rscheck_disease['disease_total'];
$disease_name=$row_rscheck_disease['disease_name'];

if($current_id){
$query_check_old_price="select sum(qty*unit_price) as claim_total from claims_details where id='$current_id'";
$rscheck_old_price= mysql_query($query_check_old_price) or die(mysql_error());
$row_rscheck_old_price = mysql_fetch_assoc($rscheck_old_price);

$oldprice=$row_rscheck_old_price['claim_total'];
}
//echo $oldprice;
$actual_balance=$balance+$disease_claim_total+$price-$oldprice;
//echo $balance."+".$disease_claim_total."+".$price."<br>";

if($actual_balance<=$price_limit){
			$approve[0]='yes';	
			$approve[1]='Approved';	
			}else{
			$approve[0]='no';
			$approve[1]=$disease_name.' Limit Exceeded. Item can not be added.';	
			}
			
			

}


	


}

if($approve[0]=='no'){
break;	
}


}while($row_rscheck = mysql_fetch_assoc($rscheck));




}else{
$approve[0]='yes';
$approve[1]='Approved';			
}

return $approve;

	
}

?>
