<?php 
if (!isset($_SESSION)){
  session_start();
}
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);
$action=$_POST["act"];
$id = $_POST['id'];
$insurance_company_id = $_POST['insurer'];

$ins_company=$_POST['ins_company'];
$provider_id=$_SESSION['provider_id']; 
$member_no=$_POST['member_no']; 
$user_id=$_SESSION['myMM_Userid']; 
$provider_ref_no=$_POST['provider_ref_no']; 
$process_claim_no=$_POST['process_claim_no'];
$medical_note_id = $_POST['medical_note_id'];
$pull_type = $_POST['pull_type'];

$attending_officer=$_POST['attending_officer'];
if($attending_officer=='Other'){
$attending_officer=$_POST['attending_officer_other'];	
}
 
$diagnosis=$_POST['diagnosis_diseases']; 

$type_of_visit=$_POST['patienttype'];


$date_of_consultation=$_POST['date_of_consultation'];
if($type_of_visit=="In-Patient"){
$date_of_admission=$date_of_consultation;	
}  

$provider_item_id=$_POST['provider_item_id']; 



if($action=='diagnostic_edit_claim_details'){
$queryItems = "SELECT * FROM service_items WHERE id='$id'";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);

$provider_item_id=$arrayItems['id'];
$ins_item_code=$arrayItems['ins_item_code'];
$provider_id_current=$arrayItems['provider_id'];
$provider_id_master=$arrayItems['provider_id_master'];
$provider_id_data_entry=$arrayItems['provider_id_data_entry'];
$item=$arrayItems['item'];
$unit_price=$arrayItems['price'];
$item_class=$arrayItems['itemclass'];
$item_service=$arrayItems['service'];
$item_service_type=$arrayItems['service_type'];

$query_rsodest_claim_no = "select * from process_claim_no where process_claim_no='$process_claim_no'";
$rsodest_claim_no = mysql_query($query_rsodest_claim_no, $courier) or die(mysql_error());
$row_rsodest_claim_no = mysql_fetch_assoc($rsodest_claim_no);

$patient_no=$row_rsodest_claim_no['patient_no'];
$type_of_visit=$row_rsodest_claim_no['typeofvisit'];
$attending_officer=$row_rsodest_claim_no['attending_officer'];
$date_of_consultation=$row_rsodest_claim_no['date_added'];
$mode_of_payment_all = $row_rsodest_claim_no['mode_of_payment'];

if($mode_of_payment_all=='insurance'){$unit_price=$arrayItems["price_insurance"]; }
if($mode_of_payment_all=='company'){$unit_price=$arrayItems["price_company"];}
if($mode_of_payment_all=='cash'){$unit_price=$arrayItems["price"];}
if($mode_of_payment_all=='staff'){$unit_price=$arrayItems["price_staff"];}

if($mode_of_payment_all!='cash'){
$ins_company=$row_rsodest_claim_no['insurance_company'];	
$member_no=$row_rsodest_claim_no['insurance_no'];
$owing_status='Paid';
$type_of_payment='Credit to '.ucfirst($mode_of_payment_all);
}else{
$owing_status='Owing';	
}

if($pull_type == 'ALL'){
$queryItems = "SELECT * FROM claims_details WHERE process_claim_no='$process_claim_no' and provider_item_id='$provider_item_id'";
}else{
  $queryItems = "SELECT * FROM claims_details WHERE medical_note_id = '$medical_note_id' AND process_claim_no='$process_claim_no' and provider_item_id='$provider_item_id'";
}

$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);

if($totalRows>0){
	
}else{
	if($pull_type == 'ALL'){
        $med_ids = mysql_fetch_assoc(mysql_query("SELECT id FROM medical_history WHERE process_claim_no = '$process_claim_no' ORDER BY id DESC LIMIT 1", $courier));
        $medical_note_id = $med_ids['id'];
        }
        $query_insert = "INSERT INTO `claims_details` (`id`, `medical_note_id`, `ins_company`, `provider_id`, `provider_id_master`, `member_no`, `user_id`, `process_claim_no`,  `attending_officer`, `diagnosis`, `type_of_visit`, `date_of_consultation`, `date_of_admission`, `provider_item_id`, `ins_item_code`, `item`, `item_class`, `item_service`, `item_service_type`, `unit_price`, `date_added`, `date_added_short`, `qty`, `total_price`, `mode_of_payment`, `patient_no`, `payment_status`, `type_of_payment`) VALUES (NULL, '$medical_note_id', '$ins_company', '$provider_id', '$provider_id_master', '$member_no', '$user_id', '$process_claim_no', '$attending_officer', '$diagnosis', '$type_of_visit', '$date_of_consultation', '$date_of_admission',  '$provider_item_id', '$ins_item_code', '$item', '$item_class', '$item_service', '$item_service_type', '$unit_price', NOW(), CURDATE(), '1', '$unit_price', '$mode_of_payment_all', '$patient_no', '$owing_status', '$type_of_payment');";		
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
}
 include('view_items_claims_diagnostics.php'); 
}


if($action=='del_claim_details_diagnostics'){
$query_insert="delete from claims_details where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	include('view_items_claims_diagnostics.php');


}


////////////Forward Claim Details////////////
if($action=='forward_to_lab'){
$current_location='Laboratory';	
$action_status='Sent to Lab';
$details = mysql_fetch_assoc(mysql_query("SELECT mode_of_payment FROM process_claim_no WHERE process_claim_no = '$process_claim_no' LIMIT 1"));
if($details['mode_of_payment'] == 'cash'){
  $payment_status = 'Owing';
} else if($details['mode_of_payment'] == 'insurance'){
  $payment_status = 'Paid';
}

if($pull_type == 'ALL'){
$query_insert="update claims_details set payment_status='$payment_status', current_location='$current_location', action_status='$action_status' where item_service='$current_location' and process_claim_no='$process_claim_no'";
}else{
  $query_insert="update claims_details set payment_status='$payment_status', current_location='$current_location', action_status='$action_status' where medical_note_id='$medical_note_id' AND item_service='$current_location' and process_claim_no='$process_claim_no'";
}
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
// mysql_query("UPDATE process_claim_no SET session_status='$current_location' WHERE process_claim_no = '$process_claim_no'", $courier);
	include('view_items_claims_diagnostics.php');	
}

if($action=='reload_medical_claim_diagnostic'){
	include('view_items_claims_diagnostics.php');	
}





?>
