<?php 
include('accesscheck2.php');
require_once('Connections/courier.php'); 
require_once('functions/adjudication_rules.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);

if(!function_exists("getinsurance")) {
function getinsurance($ins_code){
if($ins_code=='CASH' || $ins_code=='Staff'){
$ccode=$ins_code;	
}
else{
$query_rsodest_insurer = "SELECT * FROM insurance_company where company_code='$ins_code'";
$rsodest_insurer = mysql_query($query_rsodest_insurer) or die(mysql_error());
$row_rsodest_insurer=mysql_fetch_assoc($rsodest_insurer);
$ccode=$row_rsodest_insurer['company_name'];

}

return $ccode;
	
}
}

$action=$_POST["act"];
$id = $_POST['id'];
$provider_id=$_SESSION['provider_id']; 
$user_id=$_SESSION['myMM_Userid'];
$process_claim_no=$_POST['process_claim_no']; 

$details = mysql_fetch_assoc(mysql_query("SELECT * FROM process_claim_no WHERE process_claim_no='$process_claim_no'"));
$member_no=$details['insurance_no']; 
$patient_no=$details['patient_no']; 
$ins_company=$details['insurance_company'];
$insurance_claim_no = $details['insurance_claim_no'];
$type_of_visit = $details['typeofvisit'];
$date_of_consultation=$details['date_added'];
$mode_of_payment_all = $_POST['mode_of_payment_all'];
$attending_officer=$details['attending_officer'];
$attending_officer_id=$details['attending_officer_id'];
$diagnosis = $_POST['diagnosis_diseases']; 
$toothnos = $_POST['toothnos']; 

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

    $provider_item_id = $arrayItems['id'];
    $ins_item_code = $arrayItems['ins_item_code'];
    $item = $arrayItems['item'];
    list($unit_price, $mode_of_payment_all, $payment_status, $selling_unit, $selling_qty) = get_price($provider_item_id, $ins_company, $mode_of_payment_all);
    $item_class = $arrayItems['itemclass'];
    $item_service = $arrayItems['service'];
    $item_service_type = $arrayItems['service_type'];

    $queryItems = "SELECT * FROM claims_details WHERE 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) {
        $status = 'error';
        $message = strtoupper($item).' has already been added to the Spectacle Prescription List !';
    } else {
        $query_insert = "INSERT INTO `claims_details` (`id`, `mode_of_payment`, `ins_company`, `provider_id`, `member_no`, `patient_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`,`payment_status`, `toothnos`, `insurance_claim_no`) VALUES (NULL, '$mode_of_payment_all','$ins_company', '$provider_id', '$member_no', '$patient_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', '$payment_status', '$toothnos', '$insurance_claim_no')";
        $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());
        
        $status = 'success';
        $message = strtoupper($item).' has been added to the Spectacle Prescription List Successfully !';
    }
    
    echo $status . '@@@' . $message . '@@@';
    include('view_items_claims_spectacle_prescriptions.php');
}

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


if($action=='updateauthorization_claim_details_spectacle'){
$authorization_code = strtoupper($_POST["itemqty"]);

$queryItems2 = "select * from authorization_code where code='$authorization_code' and ins_company='$ins_company'";
//echo $queryItems2;
$resultItems2 = mysql_query($queryItems2) or die(mysql_error());
$arrayItems2 = mysql_fetch_assoc($resultItems2);
$total_arrayItems = mysql_num_rows($resultItems2);
$code_used=$arrayItems2['code_used'];

if($total_arrayItems>0){

if($code_used==''){
$query_insert="update claims_details set authorization_code='$authorization_code' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());

$queryItems2 = "select * from claims_details where id='$id'";
//echo $queryItems2;
$resultItems2 = mysql_query($queryItems2) or die(mysql_error());
$arrayItems2 = mysql_fetch_assoc($resultItems2);
$total_arrayItems = mysql_num_rows($resultItems2);

$provider_item_id=$arrayItems2['provider_item_id'];
$item=$arrayItems2['item'];
$rx_item_name=$arrayItems2['rx_item_name'];
$ins_com_code=$arrayItems2['ins_com_code'];
$ins_item_code=$arrayItems2['ins_item_code'];
$process_claim_no=$arrayItems2['process_claim_no'];


$query_insert="update authorization_code set provider_item_id='$provider_item_id', item='$item', rx_item_name='$rx_item_name', ins_com_code='$ins_com_code', ins_item_code='$ins_item_code', process_claim_no='$process_claim_no', code_used='yes' where code='$authorization_code' and ins_company='$ins_company' and (code_used='' or code_used is NULL)";
//echo $query_insert;	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
}else{
$wrongcode="<div style='color:red; font-size:14px;'><strong>Wrong Authorization Code. Code Already Used.</strong></div>";
echo $wrongcode;		
}

}else{
$wrongcode="<div style='color:red; font-size:14px;'><strong>Wrong Authorization Code</strong></div>";
echo $wrongcode;	
}


include('view_items_claims_spectacle.php');

}


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

    $queryItems2 = "select * from claims_details where id='$id'";
    $resultItems2 = mysql_query($queryItems2) or die(mysql_error());
    $arrayItems2 = mysql_fetch_assoc($resultItems2);

    $itemserviceid = $arrayItems2['provider_item_id'];

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

    $insurance_price = $row_rscheck['price'];

    if ($insurance_price > 0) {
        $query_insert = "update service_items_price set price='$qtydrug' where ins_company='$ins_company' and provider_item_id='$itemserviceid'";
    } else {
        $query_insert = "update service_items set price='$qtydrug' where id='$itemserviceid'";
    }

    $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());
    include('view_items_claims_spectacle.php');
}

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


if ($action == 'updateqty_claim_details_spectacle') {
    $value = $_POST["value"];
    $old_info = mysql_fetch_assoc(mysql_query("SELECT * FROM claims_details WHERE id = '$id'"));
    $item = $old_info['item'];
    $qty = $old_info['qty'];
    $unit_price = $old_info['unit_price'];

    if ($value > $qty) {
        $status = 'success';
        $message = "Quantity has been Increased from $qty to $value for " . strtoupper($item);
    } else if ($value < $qty && $value > 0) {
        $status = 'success';
        $message = "Quantity has been Decreased from $qty to $value for " . strtoupper($item);
    } else if ($value == 0) {
        $value = $qty;
        $status = 'error';
        $message = "Minimum Quantity can only be 1 (One) for " . strtoupper($item);
    }

    $query_insert = "update claims_details set qty='$value' where id='$id'";
    $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());

    echo $status . '@@@' . $message . '@@@';
    include('view_items_claims_spectacle_prescriptions.php');
}

if($action=='updateeyenumber_claim_details_spectacle'){
$eye = $_POST["value"];
$query_insert="update claims_details set eye_optical='$eye' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
include('view_items_claims_spectacle_prescriptions.php');
}


if($action=='updatesphere_claim_details_spectacle'){
$eye = $_POST["value"];
$query_insert="update claims_details set eye_spherical='$eye' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
include('view_items_claims_spectacle_prescriptions.php');
}

if($action=='updatecylinder_claim_details_spectacle'){
$eye = $_POST["value"];
$query_insert="update claims_details set eye_cylindrical='$eye' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
include('view_items_claims_spectacle_prescriptions.php');
}

if($action=='updateaxis_claim_details_spectacle'){
$eye = $_POST["value"];
$query_insert="update claims_details set eye_axis='$eye' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
include('view_items_claims_spectacle_prescriptions.php');
}

if($action=='updateadd_claim_details_spectacle'){
$eye = $_POST["value"];
$query_insert="update claims_details set eye_add='$eye' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
include('view_items_claims_spectacle_prescriptions.php');
}

if($action=='updatepd_claim_details_spectacle'){
$eye = $_POST["value"];
$query_insert="update claims_details set eye_pd='$eye' where id='$id'";	
$rsinsert=mysql_query($query_insert, $courier) or die(mysql_error());
include('view_items_claims_spectacle_prescriptions.php');
}


if($action=='reload_optical_claim_spectacle'){
	include('view_items_claims_spectacle_prescriptions.php');	
}

////////////Forward Claim Details////////////
if ($action == 'forward_to_cashier_spectacle' || $action=='forward_to_claims_spectacle') {
   ($action == 'forward_to_cashier_spectacle') ? $current_location = 'Cashier' : $current_location = 'Claims';
   ($action == 'forward_to_cashier_spectacle') ? $action_status = 'Sent to Cashier' : $action_status = 'Sent to Claims';
   $query_insert = "update claims_details set current_location='$current_location', action_status='$action_status' where (item_service='Optical') 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'");
   include('view_items_claims_spectacle.php');
}
