<?php 
include('accesscheck2.php');
require_once('Connections/courier.php'); 
require_once 'functions/functions.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 = mysql_real_escape_string($_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'];
$process_claim_no=$_POST['process_claim_no'];
$provider_ref_no=$_POST['provider_ref_no']; 
$symptoms_name = mysql_real_escape_string($_POST['symptoms_name']);
$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=='drug_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'];
$rx_item_name=$arrayItems['rx_item_name'];
$rx_brand_name=$arrayItems['rx_brand_name'];
$rx_cartegory=$arrayItems['rx_cartegory'];
$rx_prescription_name=$arrayItems['rx_prescription_name'];
$total_claim=1*$item_unit_price;
$total_award='';

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);

$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);
$totalRows_rsodest_claim_no = mysql_num_rows($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'];

if($totalRows>0){
	
}else{
        $details = mysql_fetch_assoc(mysql_query("SELECT mode_of_payment, insurance_company FROM process_claim_no WHERE process_claim_no = '$process_claim_no' LIMIT 1", $courier));
        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', '".$details['insurance_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', '".$details['mode_of_payment']."', '$patient_no', '$owing_status', '$type_of_payment');";	
        $rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
}
 include('view_items_claims_drugs.php'); 
}

if($action=='drug_edit_claim_details_history'){
list($id,$item_name) = explode("@@@", $id);
$rsodest_claim_no = mysql_query("select * from process_claim_no where process_claim_no='$process_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'];
$patient_id=$row_rsodest_claim_no['patient_id'];

$resultItems = mysql_query("SELECT * FROM patient_drug_history WHERE patient_id = '$patient_id' and drug_id='$id'") 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 `patient_drug_history` (medical_note_id,patient_id,patient_no,process_claim_no,drug_id,drug_name) VALUES ('$medical_note_id','$patient_id','$patient_no','$process_claim_no','$id','$item_name');";	
        $rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
}
 include('view_items_claims_drugs_history.php'); 
}

if($action=='medical_procedures_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($mode_of_payment_all=='cash'||$mode_of_payment_all=='staff'){
//    mysql_query("UPDATE process_claim_no SET session_status='Cashier' WHERE process_claim_no='$process_claim_no'", $courier);
}

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_medical_procedure.php'); 
}

if($action == 'update_medical_procedure_details'){
    $value = mysql_real_escape_string($_POST['id']);
    list($id, $field) = explode("@@@", mysql_real_escape_string($_POST['itemqty']));
    if($field == 'procedure_time'){
        $value = date('H:i', strtotime($value));
    }
    mysql_query("UPDATE claims_details SET $field = '$value' WHERE id = '$id'", $courier);
    include('view_items_claims_medical_procedure.php'); 
}

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


if($action=='patient_vitals_save_claim_details'){
    $details = mysql_fetch_assoc(mysql_query("select patient_id from process_claim_no where process_claim_no='$process_claim_no' LIMIT 1", $courier));
    $patient_id=$details['patient_id'];
    $td = date('Y-m-d h:i:s');
    $temperature = $_POST['temperature'];
    $pulse = $_POST['pulse'];
    $resp_rate = $_POST['resp'];
    $bp1 = $_POST['bp'];
    $bp2 = $_POST['bp2'];
    $bpsaturation = $_POST['bpsaturation'];
        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 patient_vitals (medical_note_id,user_id,patient_id,process_claim_no,temperature,pulse,resp,BP1,BP2,oxygen_saturation,date_records_taken) values('$medical_note_id','".$_SESSION['myMM_Userid']."','$patient_id','$process_claim_no','".$temperature."','".$pulse."','".$resp_rate."','".$bp1."','".$bp2."','".$bpsaturation."','".$td."')";
         $rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
 include('view_items_claims_patient_vitals.php'); 
}

if($action=='del_claim_details_vital_records'){
    $query_insert="delete from patient_vitals where id='$id'";	
    $rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
 include('view_items_claims_patient_vitals.php'); 
}


if($action=='allergies_history_edit_claim_details' || $action == "save_new_allergy_name"){
list($id,$item_name) = explode("@@@", $id);
$rsodest_claim_no = mysql_query("select * from process_claim_no where process_claim_no='$process_claim_no'", $courier) or die(mysql_error());
$row_rsodest_claim_no = mysql_fetch_assoc($rsodest_claim_no);
$patient_id=$row_rsodest_claim_no['patient_id'];
$reactions = mysql_real_escape_string($_POST['reaction_'.$id]);
if($reactions == 'other'){
   $reactions = mysql_real_escape_string($_POST['other_reactions_'.$id]); 
   $check = mysql_num_rows(mysql_query("SELECT * FROM allergy_reactions WHERE allergy_reaction = '$reactions'"));
   if($check < 1){
       mysql_query("INSERT INTO allergy_reactions(allergy_reaction) VALUES('$reactions')", $courier); 
   }
}
    
    if ($action == "save_new_allergy_name") {
        $item_name = mysql_real_escape_string($_POST['allergy_name']);
        $check_allergy = mysql_num_rows(mysql_query("SELECT * FROM allergy_data WHERE allergy_name='$item_name'", $courier));
        if ($check_allergy < 1 && !empty($item_name)) {
            $rsinsert = mysql_query("INSERT INTO `allergy_data` (`allergy_name`) VALUES ('$item_name')", $courier) or die(mysql_error());
        }
        $id = mysql_insert_id();
    }

$resultItems = mysql_query("SELECT * FROM allergies WHERE patient_id = '$patient_id' and allergy_name='$item_name'") 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 `allergies` (medical_note_id,patient_id,process_claim_no,allergy_id,allergy_name,reaction,user_id) VALUES ('$medical_note_id','$patient_id','$process_claim_no','$id','$item_name','$reactions','".$_SESSION['myMM_Userid']."');";	
        $rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
}
 include('view_items_claims_allergy_history.php'); 
}

if($action=='physical_examinations_edit_claim_details' || $action == "save_new_physical_examination"){
list($id,$item_name) = explode("@@@", $id);
$rsodest_claim_no = mysql_query("select * from process_claim_no where process_claim_no='$process_claim_no'", $courier) or die(mysql_error());
$row_rsodest_claim_no = mysql_fetch_assoc($rsodest_claim_no);
$patient_id=$row_rsodest_claim_no['patient_id'];
$units = mysql_real_escape_string($_POST['units_'.$id]);
$value = mysql_real_escape_string($_POST['item_value_'.$id]);
if($units == 'other'){
   $units = mysql_real_escape_string($_POST['other_units_'.$id]); 
   $check = mysql_num_rows(mysql_query("SELECT * FROM units WHERE unit = '$units'"));
   if($check < 1){
       mysql_query("INSERT INTO units(unit,area_specialisation) VALUES('$units','".$_SESSION['area_specialisation']."')", $courier); 
   }
}
    
    if ($action == "save_new_physical_examination") {
        $item_name = mysql_real_escape_string($_POST['item_name']);
        $check_unit = mysql_num_rows(mysql_query("SELECT * FROM physical_exams_data WHERE item_name='$item_name'", $courier));
        if ($check_unit < 1 && !empty($item_name)) {
            $rsinsert = mysql_query("INSERT INTO `physical_exams_data` (`item_name`,`area_specialisation`) VALUES ('$item_name','".$_SESSION['area_specialisation']."')", $courier) or die(mysql_error());
        }
        $id = mysql_insert_id();
    }

$resultItems = mysql_query("SELECT * FROM patient_physical_examinations WHERE process_claim_no = '$process_claim_no' and physical_examination='$item_name'") 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 `patient_physical_examinations` (medical_note_id,patient_id,process_claim_no,physical_examination,value,unit,user_id) VALUES ('$medical_note_id','$patient_id','$process_claim_no','$item_name','$value','$units','".$_SESSION['myMM_Userid']."');";	
        $rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
}
 include('view_items_claims_physical_examinations.php'); 
}

if($action=='del_claim_details_physical_examination'){
$query_insert="delete from patient_physical_examinations where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	include('view_items_claims_physical_examinations.php');
}

if($action=='del_claim_details_allergies_Reaction'){
$query_insert="delete from allergies where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	include('view_items_claims_allergy_history.php');
}


if($action=='symptoms_edit_claim_details' || $action == 'save_new_symptoms_name'){
$queryItems = "SELECT * FROM symptoms WHERE id='$id'";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);

$symptoms_id=$arrayItems['id'];
$symptoms=$arrayItems['symptoms'];
if($action == "save_new_symptoms_name"){
    $symptoms = $symptoms_name;
    $check_symptoms = mysql_num_rows(mysql_query("SELECT * FROM symptoms WHERE symptoms='$symptoms'",$courier));
    if($check_symptoms < 1 && !empty($symptoms)){
    $rsinsert=mysql_query("INSERT INTO `symptoms` (`symptoms`) VALUES ('$symptoms')", $courier) or die(mysql_error());
    }
}

$queryItems = "SELECT * FROM claims_details_symptoms WHERE process_claim_no='$process_claim_no' AND medical_note_id='$medical_note_id' and symptoms='$symptoms'";
$resultItems = mysql_query($queryItems) or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems);
$totalRows = mysql_num_rows($resultItems);

if($totalRows>0){
	
}else{
    if(!empty($symptoms)){
	$query_insert = "INSERT INTO `claims_details_symptoms` (`id`, `medical_note_id`, `process_claim_no`,  `symptoms`) VALUES (NULL, '$medical_note_id', '$process_claim_no', '$symptoms');";	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
    }
}
 include('view_items_claims_symptoms.php'); 
}

if($action=='query_diagnosis_edit_claim_details' || $action == 'save_new_query_diagnosis_name'||$action=='query_diagnosis_edit_claim_details_history'){
 
    
$disease_grp = mysql_real_escape_string($_POST['disease_grp']);
if ($disease_grp == 'diseases') {
        $disease_name = 'Disease';
        $disease_code = 'Code';
        $disease_id = 'DiseaseID';
        $disease_grpp = 'WHO';
    } else if ($disease_grp == 'diseases_stg') {
        $disease_name = 'tg_disease_name';
        $disease_code = 'icd_10_Code';
        $disease_id = 'id';
        $disease_grpp = 'STG';
    }

$resultItems2 = mysql_query("SELECT * FROM $disease_grp WHERE $disease_id='$id'") or die(mysql_error());
$arrayItems = mysql_fetch_assoc($resultItems2);
$disease_idd=$arrayItems[$disease_id];
$disease_namee=$arrayItems[$disease_name];
$disease_codee = $arrayItems[$disease_code];
//if($action == "save_new_query_diagnosis_name"){
//    $symptoms = $symptoms_name;
//    $check_symptoms = mysql_num_rows(mysql_query("SELECT * FROM symptoms WHERE symptoms='$symptoms'",$courier));
//    if($check_symptoms < 1 && !empty($symptoms)){
//    $rsinsert=mysql_query("INSERT INTO `symptoms` (`symptoms`) VALUES ('$symptoms')", $courier) or die(mysql_error());
//    }
//}
$details = mysql_fetch_assoc(mysql_query("SELECT patient_id,patient_no FROM process_claim_no WHERE process_claim_no ='$process_claim_no' LIMIT 1"));
if($action=='query_diagnosis_edit_claim_details'){
   $type = 'diagnosis';
   $url = 'view_items_claims_query_diagnosis.php';
$queryItems = "SELECT * FROM claims_details_query_diagnosis WHERE process_claim_no='$process_claim_no' AND medical_note_id='$medical_note_id' and disease_name='$disease_namee' and type='diagnosis'";
}else{
 $type = 'history';
 $url = 'view_items_claims_query_diagnosis_history.php';
 $queryItems = "SELECT * FROM claims_details_query_diagnosis WHERE patient_id='".$details['patient_id']."' and disease_name='$disease_namee' and type='history'";
}

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

if($totalRows>0){}else{
    if(!empty($disease_namee)){
	$query_insert = "INSERT INTO `claims_details_query_diagnosis` (`medical_note_id`,`disease_grp`, `disease_id`, `disease_code`, `disease_name`, `patient_id`, `process_claim_no`,`type`) VALUES ('$medical_note_id','$disease_grpp','$disease_idd','$disease_codee','$disease_namee','".$details['patient_id']."','$process_claim_no','$type');";	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
    }
}
 include($url); 
}



if($action=='updatedoseform_claim_details_drugs_history'){
$dose = $_POST["itemqty"];
list($id,$field) = explode("@@@", $id);
$query_insert="update patient_drug_history set $field='$dose' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
include('view_items_claims_drugs_history.php');

}

if($action=='updatedose_claim_details'){
$dose = $_POST["itemqty"];
$query_insert="update claims_details set dose='$dose' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());

include('view_items_claims_drugs.php');

}

if($action=='updatefrequency_claim_details' || $action == 'updatefrequency_claim_details_drugs'){
$frequency = $_POST["itemqty"];
$query_insert="update claims_details set frequency='$frequency' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());

include('view_items_claims_drugs.php');

}

if($action=='updatenoofdays_claim_details' || $action == 'updatenoofdays_claim_details_drugs'){
$noofdays = $_POST["itemqty"];	
$query_insert="update claims_details set no_of_days='$noofdays' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	
include('view_items_claims_drugs.php');

}

if($action=='updatedoseform_claim_details' || $action =='updatedoseform_claim_details_drugs'){
$doseform = $_POST["itemqty"];	
$query_insert="update claims_details set dosage_form='$doseform' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	
include('view_items_claims_drugs.php');

}

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


}

if($action=='del_claim_details_symptoms'){
$query_insert="delete from claims_details_symptoms where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	include('view_items_claims_symptoms.php');
}

if($action=='del_claim_details_query_diagnosis'){
$query_insert="delete from claims_details_query_diagnosis where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	include('view_items_claims_query_diagnosis.php');
}
if($action=='del_claim_details_query_diagnosis_history'){
$query_insert="delete from claims_details_query_diagnosis where id='$id'";
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	include('view_items_claims_query_diagnosis_history.php');
}

if($action=='del_claim_drug_prescriptions_history'){
$query_insert="delete from patient_drug_history where id='$id'";
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	include('view_items_claims_drugs_history.php');
}

if($action=='confirm_query_diagnosis_disease'){
 $check = mysql_fetch_assoc(mysql_query("select * from claims_details_query_diagnosis where id = '$id'", $courier))or die(mysql_error());
 $status = $check['status'];
 if($status == 'confirmed'){$new_status = '';} else {$new_status = 'confirmed';}
 $rsinsert=mysql_query("update claims_details_query_diagnosis SET status = '$new_status' where id='$id'", $courier) or die(mysql_error());
 if($rsinsert == TRUE){
    echo $new_status; 
 }
}

if ($action == 'save_patient_consultation') {
    list($patient_no, $patient_id) = explode("@@@", $_POST['id']);
    $medical_note = mysql_real_escape_string($_POST['hpc']);
    $social_history = mysql_real_escape_string($_POST['social_history']);
    $medical_note_id = mysql_real_escape_string($_POST['medical_note_id']);
    $referral_names = $_POST['referral_names'];
    
    $patient_status = mysql_real_escape_string($_POST['patient_status']);
    $title = mysql_real_escape_string($_POST['event_title']);
    $start_date = mysql_real_escape_string($_POST['schedule_start_date']);
    $start_time = date('H:i', strtotime(mysql_real_escape_string($_POST['start_time_picker'])));
    $end_time = date('H:i', strtotime(mysql_real_escape_string($_POST['end_time_picker'])));
    $description = mysql_real_escape_string($_POST['description']);
    $appointment_schedule = $_POST['appointment_schedule'];
    $send_sms = $_POST['send_sms'];
    $date_now = date('Y-m-d');
    $time_now = date('H:i:s');
    $date_of_review = mysql_real_escape_string($_POST['date_of_review_death']);
    $time_of_death = date('H:i', strtotime(mysql_real_escape_string($_POST['time_of_review_death'])));
    $referred_hospital = mysql_real_escape_string($_POST['referred_hospital_doctor']);
    $reason_for_detention = mysql_real_escape_string($_POST['reason_for_detention']);

    $check = mysql_query("SELECT * FROM medical_history WHERE process_claim_no = '$process_claim_no' ORDER BY id ASC LIMIT 1", $courier);
    $check_count = mysql_num_rows($check);
    if ($check_count > 0) {
        $first_note = mysql_fetch_assoc($check);
        $medical_note_id = $first_note['id'];
        if (!empty($medical_note)) {
            $insert = mysql_query("UPDATE medical_history SET history_presenting_complaints='$medical_note',social_history='$social_history' WHERE id='$medical_note_id' AND process_claim_no = '$process_claim_no'", $courier);
        }
    }else{
       $insert = mysql_query("INSERT INTO medical_history(history_presenting_complaints, social_history, process_claim_no) VALUES('$medical_note', '$social_history', '$process_claim_no')", $courier);
       $medical_note_id = mysql_insert_id();
       mysql_query("UPDATE claims_details_symptoms SET medical_note_id = '$medical_note_id' WHERE process_claim_no = '$process_claim_no' AND medical_note_id = '0'");
       mysql_query("UPDATE claims_details_query_diagnosis SET medical_note_id = '$medical_note_id' WHERE process_claim_no = '$process_claim_no' AND medical_note_id = '0'");
       mysql_query("UPDATE claims_details SET medical_note_id = '$medical_note_id' WHERE process_claim_no = '$process_claim_no' AND medical_note_id = '0'");
       mysql_query("UPDATE patient_physical_examinations SET medical_note_id = '$medical_note_id' WHERE process_claim_no = '$process_claim_no' AND medical_note_id = '0'");
       mysql_query("UPDATE patient_drug_history SET medical_note_id = '$medical_note_id' WHERE process_claim_no = '$process_claim_no' AND medical_note_id = '0'");
       mysql_query("UPDATE allergies SET medical_note_id = '$medical_note_id' WHERE process_claim_no = '$process_claim_no' AND medical_note_id = '0'");
       mysql_query("UPDATE patient_vitals SET medical_note_id = '$medical_note_id' WHERE process_claim_no = '$process_claim_no' AND medical_note_id = '0'");
       mysql_query("UPDATE consultation_drawings SET medical_note_id = '$medical_note_id' WHERE process_claim_no = '$process_claim_no' AND medical_note_id = '0'");
    }
    
    mysql_query("UPDATE process_claim_no SET action_date='$date_now',action_time='$time_now',review_date='$date_of_review',time_of_death='$time_of_death',referred_hospital='$referred_hospital',reason_for_detention='$reason_for_detention'  WHERE process_claim_no = '$process_claim_no'");
   
//   mysql_query("delete from medical_history_referral_place where process_claim_no = '$process_claim_no' AND medical_note_id = '$medical_note_id'", $courier);
//    foreach ($referral_names as $referral_name) {
//            mysql_query("INSERT INTO medical_history_referral_place(medical_note_id, referral_name, process_claim_no) VALUES('$medical_note_id' ,'$referral_name', '$process_claim_no')");
//    }
    
   if($appointment_schedule == 'on'){
    $check_2_exist = mysql_query("SELECT id FROM scheduler WHERE medical_note_id = '$medical_note_id'", $courier);
    $check_2 = mysql_num_rows($check_2_exist);
    if($check_2 > 0){
        $ff = mysql_fetch_assoc($check_2_exist);
        $sql = "AND NOT id = '".$ff['id']."'";
    }
    
    $check = mysql_query("SELECT * FROM scheduler WHERE start_date = '$start_date' AND (start_time <= '$end_time' AND end_time >= '$start_time') AND type='appointment' AND doctor_id = '".$_SESSION['myMM_Userid']."' $sql", $courier);
    if(mysql_num_rows($check) > 0){ ?>
        <div class="alert alert-danger alert-dismissable" style="border-radius: 0px">
                                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                                    <span style="font-weight: normal;">This Appointment Overlaps with <b><?php echo mysql_num_rows($check); ?></b> of your other Patient Appointments Schedules.</span> <strong style="cursor: pointer;" onclick="$('#clash_appointment_wrapper').slideToggle(function(){if($(this).is(':hidden')) {$('.form_action_buttons').show();}else{$('.form_action_buttons').hide();}});$('#main_appointment_wrapper').slideToggle();">View Details</strong>
	</div>
<div class="row" id="clash_appointment_wrapper" style="height:270px;overflow-y: scroll;display: none;">
        <?php
            while($row = mysql_fetch_assoc($check)){
                $row2 = mysql_fetch_assoc(mysql_query("SELECT picpath,insurance_no1,insurance_company1 FROM patient_information WHERE id = '".$row['patient_id']."'", $courier));
                $picpath=$row2['picpath']; 
                if(!$picpath){ 
                            if($row2['insurance_no1']){
                            $inspicpath=getpicpath($row2['insurance_company1']);
                            if($inspicpath){
                            $member_id=preg_replace("/[^0-9]/","",$row2['insurance_no1']);
                            $picpath=$inspicpath."/".ltrim($member_id,0).".jpg";
                            }else{
                            $picpath="images/profile.gif";		
                            }
                            }else{
                            $picpath="images/profile.gif";	
                            }
                }
                ?>
                <div class="col-md-12" style="height: 82px;">
                            <div class="todo-tasklist">
                                <div class="todo-tasklist-item todo-tasklist-item-border-green">
                                    <img class="todo-userpic pull-left" src="<?php echo $picpath; ?>" width="30px" height="30px">
                                    <div class="todo-tasklist-item-title">
                                         <?php echo $row['patient_name'];?> - (<?php echo $row['patient_no'];?>)
                                        
                                    </div>
                                    <div class="todo-tasklist-controls pull-left">
                                            <i class="icon-calendar"></i> <i><span class="todo-tasklist-date"> <?php echo date('jS F, Y g:i a', strtotime($row['start_date'] . ' ' . $row['start_time'])) . ' - ' . date('g:i a', strtotime($row['end_time'])); ?> </span></i>
                                    </div>
                                </div>
                            </div>
                </div>
                   
          <?php  } ?>
            </div>
<?php exit;
         }
    if($check_2 > 0){
      mysql_query("UPDATE scheduler SET title='$title',start_date='$start_date',start_time='$start_time',end_date='$start_date',end_time='$end_time',description='$description' WHERE medical_note_id = '$medical_note_id'", $courier);
    }else{
    $row = mysql_fetch_assoc(mysql_query("SELECT othernames, surname FROM patient_information WHERE id = '$patient_id' LIMIT 1", $courier));
    $patient_name = $row['surname'].' '.$row['othernames'];
    mysql_query("INSERT INTO scheduler(`doctor_id`, `title`, `type`, `start_date`, `start_time`, `end_date`, `end_time`, `allDay`, `new_old_event`,`description`,`patient_id`,`patient_no`,`patient_name`,`medical_note_id`) VALUES('".$_SESSION['myMM_Userid']."','$title','appointment','$start_date','$start_time','$start_date','$end_time','false', 'old', '$description','$patient_id','$patient_no','$patient_name','$medical_note_id')", $courier);
    }
    
    if($send_sms == 'on'){
            $sms_permission = mysql_fetch_assoc(mysql_query("SELECT * FROM sms_details", $courier));
            $patient_info = mysql_fetch_assoc(mysql_query("SELECT othernames,tel FROM patient_information WHERE id = '$patient_id'", $courier));
            $doc_info = mysql_fetch_assoc(mysql_query("SELECT title,surname,tel,area_specialisation FROM users WHERE id = '".$_SESSION['myMM_Userid']."'", $courier));
            
            $patient = $patient_info['othernames'];
            $patient_full = $patient_info['othernames'].' '.$patient_info['surname'];
            $doc = $doc_info['title'] . ' ' . $doc_info['surname'];
            $doc2 = $doc_info['title'] . ' ' . $doc_info['surname'].' - '.$doc_info['area_specialisation'];
            $sender = $sms_permission['sender_id'];
            
            $date = date('l, jS F Y', strtotime($start_date));
            $time = date('g:ia', strtotime($start_time));
            $time2 = date('g:ia', strtotime($end_time));

            $patient_phone = substr($patient_info['tel'], 0, 10);
            $doc_phone = substr($doc_info['tel'], 0, 10);
            
            if($check_2 < 1){
                $msg_patient = "Hello $patient, Your Appointment Schedule with $doc2 will be on $date at $time - $time2. Thank you.";
                $msg_doc = "Hello $doc, You just Scheduled an Appointment with $patient_full on $date at $time - $time2. Thank you.";
            } else {
                $msg_patient = "Hello $patient, Your Appointment Schedule with $doc2 has been rescheduled to $date at $time - $time2. Thank you.";
                $msg_doc = "Hello $doc, Your just rescheduled your Appointment with $patient_full to $date at $time - $time2. Thank you.";
            }
        
         SMS_API_RETURN($patient_phone, $msg_patient, $sender);
         SMS_API_RETURN($doc_phone, $msg_doc, $sender);
    }
}
    mysql_query("UPDATE process_claim_no SET consulting_room_status = '$patient_status',action_date='".date('Y-m-d')."',action_time='".date('H:i:s')."' WHERE patient_id = '$patient_id' AND process_claim_no = '$process_claim_no' ", $courier);

}


////////////Forward Claim Details////////////
if($action=='process_claim_details'){

$query_insert="update claims_details set attending_officer='$attending_officer', diagnosis='$diagnosis', status=NULL where medical_note_id='$medical_note_id' AND process_claim_no='$process_claim_no'";

$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());


function get_disease_drug_compliance($disease,$itemclass,$process_claim_no,$yesorno){
$query_claim_drug="SELECT * FROM disease_drug_compliance WHERE disease_name='$disease' and drugclass='$itemclass'";
//echo $query_claim_drug;
$rsclaim_drug= mysql_query($query_claim_drug) or die(mysql_error());
$row_rsclaim_drug = mysql_fetch_assoc($rsclaim_drug);
$totalRows_rsclaim_drug = mysql_num_rows($rsclaim_drug);
$itemclasstotal=$row_rsclaim_drug['maxpreamble'];
//echo $yesorno." - ".$disease;
if($totalRows_rsclaim_drug>0){
	$approved='Approved';
	
	////////
	$query_claim_drug="SELECT sum(qty) as qty FROM claims_details WHERE process_claim_no='$process_claim_no' and item_class='$itemclass'";
//echo $query_claim_drug;
$rsclaim_drug= mysql_query($query_claim_drug) or die(mysql_error());
$row_rsclaim_drug = mysql_fetch_assoc($rsclaim_drug);
$totalRows_rsclaim_drug = mysql_num_rows($rsclaim_drug);
$qtyclass=$row_rsclaim_drug['qty'];
//echo $qtyclass;

if($qtyclass<=$itemclasstotal){
	$approved='Approved';
}else{
	if($yesorno=='Approved'){	
	    $approved='Approved';
	}else{
		$approved='Pending';
	}
}

	
}else{
if($yesorno=='Approved'){	
	    $approved='Approved';
	}else{
		$approved='Pending';
	}
	
}


 return $approved;
}


function get_drug_days_compliance($member_no,$inscompany,$item_code,$dateconsulted,$yesorno){
$query_claim_drug="SELECT * FROM claims_previous_history WHERE ins_item_code='$item_code' and member_no='$member_no' and ins_company='$inscompany' and date_of_consultation<='$dateconsulted' order by date_of_consultation desc limit 0,1";
//echo $query_claim_drug;
$rsclaim_drug= mysql_query($query_claim_drug) or die(mysql_error());
$row_rsclaim_drug = mysql_fetch_assoc($rsclaim_drug);
$totalRows_rsclaim_drug = mysql_num_rows($rsclaim_drug);
$date_of_consultation=$row_rsclaim_drug['date_of_consultation'];
$no_of_days=$row_rsclaim_drug['no_of_days'];
//echo $date_of_consultation;
if($date_of_consultation){
$diff=strtotime($dateconsulted)-strtotime($date_of_consultation);
$daysdiff=floor($diff/(60*60*24));
}
//echo $daysdiff;
if($no_of_days>0){
$number_of_days=$no_of_days;	
}else{
$query_claim_drug="SELECT * FROM service_items WHERE ins_item_code='$item_code'";
//echo $query_claim_drug;
$rsclaim_drug= mysql_query($query_claim_drug) or die(mysql_error());
$row_rsclaim_drug = mysql_fetch_assoc($rsclaim_drug);
$totalRows_rsclaim_drug = mysql_num_rows($rsclaim_drug);
$number_of_days=$row_rsclaim_drug['number_of_days'];

}

if($number_of_days && $daysdiff){
if($daysdiff<$number_of_days){
$approved='Pending';	
}else{
$approved='Approved';
}
}


 return $approved;
}

function get_benefit_compliance($member_no,$typeofvisit,$inovice_number_class){
	$approved='Approved';

 return $approved;
}
	
/*$query_claim_invoice = "";
$rsclaim_invoice = mysql_query($query_claim_invoice) or die(mysql_error());
$row_rsclaim_invoice = mysql_fetch_assoc($rsclaim_invoice);
$totalRows_rsclaim_invoice = mysql_num_rows($rsclaim_invoice);	
$facility_name=$row_rsclaim_invoice['facility_name'];
$visittype=$row_rsclaim_invoice['out_in_patient_status'];
$consultdate=$row_rsclaim_invoice['dateconsult'];	*/

$query_claim_proccess = "select *,DATE_FORMAT(date_of_consultation, '%d %b %Y') as dateconsult from claims_details where medical_note_id = '$medical_note_id' AND process_claim_no='$process_claim_no' and  item_service='Drugs' order by id desc, item_class;";
$rsclaim_proccess = mysql_query($query_claim_proccess) or die(mysql_error());
$row_rsclaim_proccess = mysql_fetch_assoc($rsclaim_proccess);
$totalRows_rsclaim_proccess = mysql_num_rows($rsclaim_proccess);

$disease=explode(",",trim($row_rsclaim_proccess['diagnosis']));
$totaldisease=count($disease)-1;
//echo $totaldisease;


for ($i = 0; $i <= $totaldisease; $i++) {
//$iii=0;


do{	
$iii=$iii+1;
$itemclass1=$row_rsclaim_proccess['item_class'];
$provider_item_id=$row_rsclaim_proccess['provider_item_id'];
$dateconsulted1=$row_rsclaim_proccess['date_of_consultation'];
$facility_id1=$row_rsclaim_proccess['provider_id'];
$member_no1=$row_rsclaim_proccess['member_no'];
$claim_detail_id=$row_rsclaim_proccess['id'];
$ins_company=$row_rsclaim_proccess['ins_company'];
$ins_item_code=$row_rsclaim_proccess['ins_item_code'];

if($row_rsclaim_proccess['item_service']=='Drugs'){
//echo $drugcheck;
//echo $iii." - ";
$drugcheck[$iii]=get_disease_drug_compliance($disease[$i], $itemclass1,$process_claim_no,$drugcheck[$iii]);
$statusdrug=$drugcheck[$iii];
//echo " - ".$row_rsclaim_proccess['item_name']." - ".$drugcheck[$iii]."<br>";
//echo $statusdrug;
	if($statusdrug=='Approved'){
	$comment='Preliminary Okay';	
	$query_insert="update claims_details set status='$statusdrug', comment='$comment' where id='$claim_detail_id'";
	//echo $query_insert;	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	}
	
	if($statusdrug=='Pending'){
	$comment='Drug/Disease Compliance Problems';	
	$query_insert="update claims_details set status='$statusdrug', comment='$comment' where id='$claim_detail_id'";
	//echo $query_insert;	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	}

if($statusdrug=='Approved'){
$dayscheck[$iii]=get_drug_days_compliance($member_no1, $ins_company, $ins_item_code,$dateconsulted1,$dayscheck[$iii]);
$statusdrugdays=$dayscheck[$iii];

	if($statusdrugdays=='Approved'){
	$comment='Preliminary Okay';	
	$query_insert="update claims_details set status='$statusdrugdays', comment='$comment' where id='$claim_detail_id'";
	//echo $query_insert;	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	}
	
	if($statusdrugdays=='Pending'){
	$comment='Drug Treatment Duration Problems';	
	$query_insert="update claims_details set status='$statusdrugdays', comment='$comment' where id='$claim_detail_id'";
	//echo $query_insert;	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	}

}


}


else{
$statusdrug="Approved";	
}

$benefitscheck[$iii]=get_benefit_compliance($ins_member_no,$out_in_patient_status,$invoice_number);
$statusbenefits=$benefitscheck[$iii];


if($statusbenefits=='Approved'){
	$comment='Sufficient Funds Available';	
	//$query_insert="update claim_details set item_final_status='Preliminary Proccessed' where id='$claim_detail_id'";
	//echo $query_insert;	
	//$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	
	if($action=='submit_claim_details'){
		//$item_final_status="submitted";
	}else{
		//$item_final_status="Preliminary Proccessed";
	}
	//$query_insert="update claim_details set item_final_status='$item_final_status' where id='$claim_detail_id'";
	//echo $query_insert;	
	//$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	
	//echo	'<img src="./assets/img/correct.png" width="16" height="16"  alt="" title="'.$comment.'"/>';	
                      
	}
	
	if($statusbenefits=='Pending'){
	/*$comment='Insufficient Funds Available';	
	$query_insert="update claim_details set benefit_item_status='$statusbenefits', benefit_status_comment='$comment' where id='$claim_detail_id'";
	//echo $query_insert;	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
	
	if($action=='submit_claim_details'){
		$item_final_status="submitted";
	}else{
		$item_final_status="Preliminary Proccessed";
	}
	$query_insert="update claim_details set item_final_status='$item_final_status' where id='$claim_detail_id'";
	//echo $query_insert;	
	$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());*/
		//echo	'<img src="./assets/img/warning.png" width="16" height="16"  alt="" title="'.$comment.'"/>';	
	}

	

//$claim_itemclass=$row_rsclaim_proccess['item_class'];	
//$queryItems = "SELECT * FROM disease_drug_compliance WHERE disease_name='$disease_name'";


}  while ($row_rsclaim_proccess = mysql_fetch_assoc($rsclaim_proccess));
if($totalRows_rsclaim_proccess>0){
$row_rsclaim_proccess = mysql_fetch_assoc($rsclaim_proccess);	
mysql_data_seek($rsclaim_proccess,0);
}
$iii=-1;
//echo $iii."-start-";

}

$current_location='Pharmacy';
$item_service='Drugs';	
$action_status='Sent to Pharmacy';
    $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='$item_service' 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='$item_service' 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',pharmacy_status='pending' WHERE process_claim_no = '$process_claim_no'", $courier);
include('view_items_claims_drugs.php');

}

if ($action == 'fetch_list_of_hospitals') {
    $hospitals = array();
    $query = mysql_query("SELECT * FROM list_hospitals", $courier);
    while ($fetch = mysql_fetch_array($query, MYSQL_ASSOC)) {
        $e = array();
        $e['value'] = $fetch['hospital_name'];
        array_push($hospitals, $e);
    }
    echo json_encode($hospitals);
}
            

