<?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 = $_POST['id'];

$userid=$_SESSION['myMM_Userid'];
date_default_timezone_set("Africa/Accra");
$today = date('Y-m-d h:i:s');

$old_card = strtoupper($_POST['oldcard']);
$surname=strtoupper($_POST["surname"]);
$othernames=strtoupper($_POST["othernames"]);
$dateofbirth=$_POST['dateofbirth'];
$dob=$_POST["dob"];
$sex=$_POST["sex"];
$fstatus=$_POST["fstatus"];
$marital_status=$_POST["marital_status"];
$country_of_origin=$_POST["country_of_origin"];
$home_language=$_POST["home_language"];
$tel=$_POST["tel"];
$tel2=$_POST["tel2"];
$department_name=$_POST["department_name"];
$email=$_POST["email"];
$referred_by=strtoupper($_POST["referred_by"]);
$tel_of_referer=$_POST["tel_of_referer"];
$referer_relationship=$_POST["referer_relationship"];

$type_of_client=$_POST["type_of_client"];
if($type_of_client == 'insurance'){
    $insurance_no1=$_POST["insurance_no1"];
    $insurance_company1 = $_POST['insurance_com1'];
} else if($type_of_client == 'company'){
    $insurance_company1 = $_POST['company_name'];
    $insurance_no1=$_POST["staff_no"];
} else if($type_of_client == 'staff'){
    $insurance_no1=$_POST["staff_no"];
}

$occupation=strtoupper($_POST["occupation_".$type_of_client]);
$employer=strtoupper($_POST["employer"]);

$staff_no = strtoupper($_POST['company_staff_no']);
$home_address=$_POST["radd"];
$title=$_POST["title"];

$postal_address=$_POST["paddress"];
$next_of_kin_name=$_POST["next_of_kin_name"];
$next_of_kin_relationship=$_POST["next_of_kin_relationship"];
$next_of_kin_tel=$_POST["next_of_kin_tel"];
$next_of_kin_address=$_POST["next_of_kin_address"];
$short_code=$_SESSION['provider_short_code'];

$picpath=$_POST["filepath0"];
if(empty($picpath)){
    $picpath = 'images/profile.gif';
}


if ($action == 'records_insert_patient') {
    $query_insert = "INSERT INTO `patient_information` (`id`,`short_code`,`old_card_no`,`card_number`, `title`, `surname`, `othernames`, `dateofbirth`, `sex`,`pregnancy_status`, `marital_status`, `country_of_origin`, `home_language`, `tel`, `email`, `referred_by`, `tel_of_referer`, `referer_relationship`, `type_of_client`, `insurance_company1`,`insurance_no1`,`occupation`, `employer`, `medifem_staff_no`,`home_address`,`postal_address`, `next_of_kin_name`, `next_of_kin_relationship`, `next_of_kin_tel`, `next_of_kin_address`, `picpath`,`datetimeadded`,`user`,`department_name`,`tel2`) VALUES (NULL, '$short_code', '$old_card', NULL,'$title', '$surname', '$othernames', '$dateofbirth', '$sex','$fstatus', '$marital_status', '$country_of_origin', '$home_language', '$tel', '$email', '$referred_by', '$tel_of_referer', '$referer_relationship', '$type_of_client','$insurance_company1', '$insurance_no1','$occupation', '$employer', '$medstaffno', '$home_address','$postal_address', '$next_of_kin_name', '$next_of_kin_relationship', '$next_of_kin_tel', '$next_of_kin_address', '$picpath','$today','$userid','$department_name','$tel2');";
    $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());
    $id = mysql_insert_id();

    $pid = $id - 1;
    $alphabet_no = intval($pid / 9999);
    $f = fmod($pid, 9999);
    $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");
    $startalphabet = $characters[$alphabet_no];
    $lennum = strlen($f);
    $patient_id = $_SESSION['provider_short_code'] . $startalphabet . str_repeat("0", (4 - $lennum)) . $f;
    $query_insert = "update patient_information set card_number='$patient_id', session_status='Records' where id='$id'";
    $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());
    
    include('view_patient_visits.php');
}

//guest patient add
if ($action == 'records_guest_insert_patient') {
    $surname=strtoupper($_POST["surname1"]);
    $othernames=strtoupper($_POST["othernames1"]);
    $dateofbirth=$_POST['dateofbirth1'];
    $sex=$_POST["sex1"];
    $tel=$_POST["tel1"];
    $email=$_POST["email1"];

    $short_code = $_SESSION['provider_short_code'];
    $test = mysql_query("SELECT * FROM guest_patients where tel='$tel'");
    $row_rsodest = mysql_fetch_assoc($test);
    $test_total_rows = mysql_num_rows($test);

    $query_insert = "INSERT INTO `guest_patients` (`id`,`short_code`, `surname`, `othernames`, `dateofbirth`, `sex`,`pregnancy_status`, `marital_status`, `tel`, `email`, `datetimeadded`,`user`) VALUES (NULL, '$short_code', '$surname', '$othernames', '$dateofbirth', '$sex','$fstatus', '$marital_status', '$tel', '$email','$today','$userid');";
    $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());

    $id = mysql_insert_id();

    $pid = $id - 1;
    $alphabet_no = intval($pid / 2000);
    $f = fmod($pid, 2000);

    $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");
    $startalphabet = $characters[$alphabet_no];
    $lennum = strlen($f);
    $patient_id = "G" . $_SESSION['provider_short_code'] . $startalphabet . str_repeat("0", (3 - $lennum)) . $f;
    $query_insert = "update guest_patients set card_number='$patient_id', session_status='Records' where id='$id'";
    $rsinsert = mysql_query($query_insert, $courier) or die(mysql_error());


    include ('view_normal_patient_visits.php');
}


if ($action == "records_edit_patient") {
    $occupation = $_POST['occupation'];
    $update_records = "update patient_information set title='$title', old_card_no ='$old_card',surname='$surname', othernames='$othernames',dateofbirth='$dateofbirth',sex='$sex',pregnancy_status='$fstatus',marital_status='$marital_status',country_of_origin='$country_of_origin', home_language='$home_language', tel='$tel', email='$email', referred_by='$referred_by', tel_of_referer='$tel_of_referer',referer_relationship='$referer_relationship', type_of_client='$type_of_client', insurance_company1='$insurance_company1', insurance_no1='$insurance_no1', occupation='$occupation', employer='$employer', medifem_staff_no='$medstaffno', home_address='$home_address', postal_address='$postal_address', next_of_kin_name='$next_of_kin_name', next_of_kin_relationship='$next_of_kin_relationship', next_of_kin_tel='$next_of_kin_tel', next_of_kin_address='$next_of_kin_address', picpath='$picpath',department_name='$department_name',tel2='$tel2' WHERE id='$id'";
    $rsupdate_records = mysql_query($update_records, $courier) or die(mysql_error());
}

if ($action == "records_normal_edit_patient") {
    $update_records = "update guest_patients set surname='$surname', othernames='$othernames',dateofbirth='$dateofbirth',sex='$sex',marital_status='$marital_status', tel='$tel', email='$email', picpath='$picpath' WHERE id='$id'";
    $rsupdate_records = mysql_query($update_records, $courier) or die(mysql_error());
}

if($action == 'load_page_client_signature'){
    $process_claim_no = $_POST['id'];?>

<div id="wPaint" style="position:relative;height:400px; background-color:#7a7a7a; margin:0px auto 20px auto;"></div>
        <script type="text/javascript">
          var images = [
              'uploads/wPaint.png',
            ];
            function saveImg(image) {
              var _this = this;
              $.ajax({
                type: 'POST',
                url: 'getstep.php',
                data: {image: image,act: 'save_patient_signature',type: 'patient_signature',patient_details: '<?php echo $process_claim_no; ?>'},
                success: function (msg) {
                  _this._displayStatus('<span style="color:#000000;font-size:15px"> Signature Saved Successfully <span>');
                   $('#patient_signature_wrapper_<?php echo $process_claim_no; ?>').html(msg);
                }
              });
            }

            // init wPaint
            $('#wPaint').wPaint({
              menuOffsetLeft: 130,
              menuOffsetTop: 0,
              bg: '#FFFFFF',
              lineWidth:   '1',
              strokeStyle: '#000000',
              saveImg: saveImg
            });
    </script>

<?php }

 if ($action == 'save_patient_signature') {
    $process_claim_no = mysql_real_escape_string($_POST['patient_details']);
    $comment = mysql_real_escape_string($_POST['comment']);
    $type = mysql_real_escape_string($_POST['type']);
    $patient_info = mysql_fetch_assoc(mysql_query("SELECT patient_id, patient_no FROM process_claim_no WHERE process_claim_no = '$process_claim_no'"));
    $patient_id = $patient_info['patient_id'];
    $patient_no = $patient_info['patient_no'];

    $image = imagecreatefrompng($_POST['image']);
    $id = uniqid();
    $pic = 'administrative/patient_signatures/patient-' . $process_claim_no . $id . time() . '.png';
    $pic2 = 'patient_signatures/patient-' . $process_claim_no . $id . time() . '.png';
    imagealphablending($image, false);
    imagesavealpha($image, true);
    imagepng($image, $pic);

    $check = mysql_num_rows(mysql_query("SELECT * FROM patient_signature_files WHERE process_claim_no = '$process_claim_no' AND type='$type'"));
    if ($check > 0) {
            mysql_query("UPDATE patient_signature_files SET signature_path='$pic2',comment='$comment',user_id='".$_SESSION['myMM_Userid']."' WHERE process_claim_no = '$process_claim_no' AND type='$type'");
    } else {
        mysql_query("INSERT INTO patient_signature_files (patient_id, patient_no, process_claim_no, signature_path, type, user_id, comment) VALUES('$patient_id', '$patient_no', '$process_claim_no', '$pic2', '$type', '".$_SESSION['myMM_Userid']."', '$comment')", $courier);
    }
    $all_drawings = mysql_query("SELECT * FROM patient_signature_files WHERE process_claim_no = '$process_claim_no' AND type='$type' LIMIT 1", $courier);
    $i = 1;?>
        <div class="demo-gallery">
            <ul id="lightgallery" class="list-unstyled" style="width: auto;">
    <?php while ($row = mysql_fetch_assoc($all_drawings)) { ?>
                <li class="col-xs-6 col-sm-4 col-md-3" data-responsive="administrative/<?php echo $row['signature_path']; ?> 375, administrative/<?php echo $row['signature_path']; ?> 480, administrative/<?php echo $row['signature_path']; ?> 800" data-src="administrative/<?php echo $row['signature_path']; ?>" data-sub-html="<h4><?php echo date('D, jS F, Y',  strtotime($row['date_time'])); ?></h4><p></p>">
                            <a href="">
                                <img style="height:50px" class="img-responsive" src="administrative/<?php echo $row['signature_path']; ?>">
                            </a>
                        </li>
        <?php $i++; } ?>
            </ul>
        </div>

    <script type="text/javascript">
             $(document).ready(function(){
                $('#lightgallery').lightGallery({
                    cssEasing: 'cubic-bezier(0.680, -0.550, 0.265, 1.550)'
                });
            });
    </script>
<?php }


if($action == 'send_sms_patient_information'){ ?>
    <div class="portlet-title">
            <div class="caption caption-md">
                <i class="icon-globe theme-font hide"></i>
                <span class="caption-subject font-blue-madison bold uppercase">SEND INSTANT SMS TO PATIENT </span>
            </div>
        </div>
        <div class="portlet-body form">
            <form role="form" action="#" id="send_sms_patient">
                <div class="form-body">
                    <div class="row">
                        <div class="col-lg-12 col-xs-12 col-md-12">
                            <textarea onkeypress="SMSCounter(this.value.length)" name="message" id="message" class="form-control" rows="3" style="height: 150px;"></textarea>
                            <p class="caption-subject font-blue-madison bold uppercase">
                                     <span id="remaining" style="font-weight: bold;">160 characters remaining</span>
                                     <span id="" style="font-weight: bold;color:#000"> || </span>
                                     <span id="messages" style="font-weight: bold;">1 message(s)</span>
                                     <input type="hidden" value="" id="nosms" name="nosms">
                                     <span id="" style="font-weight: bold;float: right">160 / per sms</span>
                            </p>
                        </div>
                    </div>
                </div>
                <div class="form-actions right">
                    <button type="reset" class="btn default"> Remove Text </button>
                    <a class="btn green" onclick="saveInformation2('send_sms_patient', '<?php echo $id; ?>', '#send_sms_patient', '');return false;"> Send SMS </a>
                </div>
            </form>
        </div>
   <?php }

 if ($action == 'send_sms_patient') {
    $id = $_POST['id'];
    $message = $_POST['message'];
    $patient_info = mysql_fetch_assoc(mysql_query("SELECT tel FROM patient_information WHERE id='$id'"));
    $sender_id = 'ROBERTSONS';
    $phone_number = substr($patient_info['tel'], 0, 10);
    SMS_API_RETURN($phone_number, $message, $sender_id);
    echo 'Success! SMS has been sent to Client Successfully.@@@success@@@';
}

if($action == 'follow_up_patient_information'){ 
    ?>
    <div class="col-md-12">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a href="#tab_1_1" data-toggle="tab" aria-expanded="true"> ENTER A FOLLOW UP </a>
                </li>
                <li class="">
                    <a href="#tab_1_2" data-toggle="tab" aria-expanded="false">  ALL FOLLOW UPS </a>
                </li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="tab_1_1">
                    <div class="portlet light bordered" style="min-height:194px;" id="view_items_response_wrapper_2">
                        <div class="portlet-body form">
                            <form role="form" action="#" name="follow_up_patient" id="follow_up_patient">
                                <div class="form-body">
                                    <div class="row">
                                        <div class="col-lg-12 col-xs-12 col-md-12">
                                            <textarea name="follow_up_comment" id="follow_up_comment" class="form-control" rows="3" style="height: 200px;"></textarea>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-actions right">
                                    <button type="reset" class="btn default"> Remove Text </button>
                                    <a class="btn green" onclick="saveInformation2('save_follow_up_message', '<?php echo $id; ?>', '#follow_up_patient', '');return false;"> Save Follow Up </a>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                        
                <div class="tab-pane" id="tab_1_2">
                        <div class="portlet light bordered" style="min-height:194px;" id="view_items_response_wrapper_2">
                            <div class="portlet-title">
                                <div class="caption">
                                    <span class="caption-subject font-blue-madison" style="font-size: 14px;"> List of All Follow Ups </span>
                                </div>
                            </div>
                            <div class="portlet-body">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th></th>
                                            <th>Date Time</th>
                                            <th>Follow Up Comment</th>
                                            <th>Made By</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                            <?php
                                            list($patient_id, $process_claim_no) = explode("@@@", $id);
                                            $all_follow_ups = mysql_query("SELECT * FROM patient_follow_up WHERE patient_id='$patient_id'");
                                            $i = 1;
                                            if(mysql_num_rows($all_follow_ups)){
                                            while ($row = mysql_fetch_assoc($all_follow_ups)) {
                                                ?>
                                                <tr>
                                                    <td><?php echo $i; ?></td>
                                                    <td><?php echo date('jS F, Y g:i a', strtotime($row['datetime'])); ?></td>
                                                    <td><?php echo $row['follow_up_comment']; ?></td>
                                                    <td><?php echo UserFullName($row['user_id'], $courier); ?></td>
                                                </tr>
                                                 <?php $i++;
                                            } }else{ ?> 
                                            <tr><td colspan="9" style="color:red;"><div class="none">No Records Found.</div></td></tr> 
                                            <?php } ?>
                                        </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                        
                    </div>
                </div>
                
            </div>
        </div>
    <?php }
    
    
 if ($action == 'save_follow_up_message') {
    list($id, $process_claim_no) = explode("@@@", $_POST['id']);
    $follow_up_comment = mysql_real_escape_string($_POST['follow_up_comment']);
    $insert = mysql_query("INSERT INTO patient_follow_up(process_claim_no, patient_id, follow_up_comment, user_id) VALUES('$process_claim_no', '$id', '$follow_up_comment', '".$_SESSION['myMM_Userid']."')");
    if ($insert == TRUE) {
        echo 'Success! Follow Up Comment saved Successfully.@@@success@@@';
    } else {
        echo 'Oops! Failed to Save Follow Up Comment.@@@error@@@';
    }
}

if($action == 'pick_up_patient_information_nnnnnn'){ 
     list($id, $process_claim_no) = explode("@@@", $_POST['id']);
     $owings_info = mysql_fetch_assoc(mysql_query("SELECT SUM(qty*unit_price) AS total_price FROM claims_details WHERE process_claim_no='$process_claim_no' AND (mode_of_payment='cash' OR mode_of_payment='credit sales') AND (payment_status='Owing' OR payment_status='Approved')"));
     $owings = $owings_info['total_price'];

    $patient_balance_info = mysql_fetch_assoc(mysql_query("SELECT patient_balance FROM patient_information WHERE id='$id' AND patient_balance < 0;")); 
    $patient_balance = $patient_balance_info['patient_balance'] * -1;
    $total_owing = $owings + $patient_balance;?>

    <div class="col-md-12">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a href="#tab_1_1" data-toggle="tab" aria-expanded="true"> NEW PICK UP / <b style="color:#e7505a">CLOSE VISIT</b> </a>
                </li>
                <li class="">
                    <a href="#tab_1_2" data-toggle="tab" aria-expanded="false">  ALL PICK UPS </a>
                </li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="tab_1_1">
                    <div class="portlet light bordered" style="min-height:194px;" id="view_items_response_wrapper_2">
                        <div class="portlet-body form">
                            <?php if($total_owing > 0){ ?>
                            <div class="alert" style="height: 200px;text-align: center;font-size: 20px;color: #a94442">
                                <center>  
                                    <strong>Oops!</strong> This Client has an Outstanding Payment of  <strong>GHS <?php echo number_format($total_owing,2);?></strong> 
                                    <p style="font-size: 15px;">Outstanding Payment must be cleared before Pick Up can be made.</p>
                                </center>
                                <br>
                                <div class="btn-group btn-group-devided" data-toggle="buttons">
                                            <label onclick="getForwardPatient('forward_to_cashier_administrative', '#view_items_response_wrapper_2', '<?php echo $process_claim_no; ?>', 'Are You Sure?', 'You want to Forward this Client to Cashier?');" class="btn red btn-outline btn-circle btn-sm active">
                                                <input type="radio" name="options" class="toggle" id="option2">Forward to Cashier <i class="fa fa-forward"></i>
                                            </label>
                                        </div>
                            </div>  
                           <?php }else { ?>
                            <form role="form" action="#" name="pick_up_patient" id="pick_up_patient">
                                <div class="form-body">
                                    <div class="row">
                                        <div class="col-lg-12 col-xs-12 col-md-12">
                                          <div id="wPaint" style="position:relative;height:250px; background-color:#7a7a7a; margin:0px auto 20px auto;"></div>  
                                              <script type="text/javascript">
          var images = [
              'uploads/wPaint.png',
            ];
            function saveImg2(image) {
              var _this = this;
              $.ajax({
                type: 'POST',
                url: 'getstep.php',
                data: {image: image,act: 'save_patient_signature',type: 'pick_up_signature',comment: $('#pick_up_comment').val(),patient_details: '<?php echo $process_claim_no; ?>'},
                success: function (msg) {
                  _this._displayStatus('<span style="color:#000000;font-size:15px"> Signature Saved Successfully <span>');
                   $('#patient_signature_wrapper_<?php echo $process_claim_no; ?>').html(msg);
                }
              });
            }

            // init wPaint
            $('#wPaint').wPaint({
              menuOffsetLeft: 130,
              menuOffsetTop: 0,
              bg: '#FFFFFF',
              lineWidth:   '1',
              strokeStyle: '#000000',
              saveImg: saveImg2
            });
    </script>
    <textarea placeholder="Any Additional Note/Comment" name="pick_up_comment" id="pick_up_comment" class="form-control" rows="3" style="height: 100px;"></textarea>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-actions right">
                                    <button type="reset" class="btn default"> Remove Text </button>
                                    <a class="btn green" onclick="if(confirm('Do you really want to Close this Visit?')){saveInformation2('save_pick_up_message', '<?php echo $process_claim_no; ?>', '#pick_up_patient', '');return false;}"> Confirm Pick Up </a>
                                </div>
                            </form>
                            <?php } ?>
                        </div>
                    </div>
                </div>
                        
                <div class="tab-pane" id="tab_1_2">
                        <div class="portlet light bordered" style="min-height:194px;" id="view_items_response_wrapper_2">
                            <div class="portlet-body">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th></th>
                                            <th>Pick Up Date</th>
                                            <th>Pick Up Comment</th>
                                            <th>Signature</th>
                                            <th>By</th>
                                            <th style="text-align: center;">Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                            <?php
                                            list($patient_id, $process_claim_no) = explode("@@@", $id);
                                            $all_follow_ups = mysql_query("SELECT * FROM patient_signature_files WHERE patient_id='$id' AND type='pick_up_signature' ORDER BY id DESC");
                                            $i = 1;
                                            if(mysql_num_rows($all_follow_ups)){
                                            while ($row = mysql_fetch_assoc($all_follow_ups)) {
                                                ?>
                                                <tr>
                                                    <td><?php echo $i; ?></td>
                                                    <td><?php echo date('jS F, Y g:i a', strtotime($row['date_time'])); ?></td>
                                                    <td><?php echo $row['comment']; ?></td>
                                                    <td><?php if(!empty($row['signature_path'])){ echo '<span style="color:green;">Signed</span>';}else{ echo '<span style="color:red;">Not Signed</span>';} ?> </td>
                                                    <td><?php echo UserFullName($row['user_id'], $courier); ?></td>
                                                    <td style="text-align: center"><label class="btn green btn-outline btn-circle btn-sm active">Pick Up Confirmed <i class="fa fa-check"></i></label></td>
                                                </tr>
                                                 <?php $i++;
                                            } }else{ ?> 
                                            <tr><td colspan="9" style="color:red;"><div class="none">No Records Found.</div></td></tr> 
                                            <?php } ?>
                                        </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                        
                    </div>
                </div>
                
            </div>
        </div>
    <?php }

    if($action == 'pick_up_patient_information'){
        $process_claim_no = $_POST['id'];
        $sql = mysql_query("SELECT c.*,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew,g.item FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no inner join claims_details g ON c.job_number=g.job_number left join job_activities_status j ON a.process_claim_no=j.process_claim_no WHERE c.audit_status<>'none' AND a.process_claim_no='$process_claim_no' AND g.item_service_type='Optical Frame' GROUP BY c.job_number ORDER BY a.datetimeadded DESC");
        
        $sql2_check = mysql_num_rows(mysql_query("SELECT a.process_claim_no FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no inner join claims_details g ON c.job_number=g.job_number left join job_activities_status j ON a.process_claim_no=j.process_claim_no WHERE a.patient_status = 'In Use' AND c.audit_status<>'none' AND a.process_claim_no='$process_claim_no' AND g.item_service_type='Optical Frame' AND pick_up_status <> 'Picked Up by Client' GROUP BY c.job_number"));

        // $sql = mysql_query("SELECT c.*,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no left join patient_information b on a.patient_no=b.card_number inner join claims_details g ON c.job_number=g.job_number left join job_activities_status j ON a.process_claim_no=j.process_claim_no WHERE c.audit_status='Job Received' AND a.patient_status = 'In Use' AND c.audit_status<>'none' AND a.process_claim_no='$process_claim_no' GROUP BY c.job_number ORDER BY a.datetimeadded DESC");

        $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'];
        $visit_status = $row_rsodest_claim_no['patient_status'];

        $owings_info = mysql_fetch_assoc(mysql_query("SELECT SUM(qty*unit_price) AS total_price FROM claims_details WHERE process_claim_no='$process_claim_no' AND (mode_of_payment='cash' OR mode_of_payment='credit sales') AND (payment_status='Owing' OR payment_status='Approved') AND item <> 'BALANCE OWING'"));
        $owings = $owings_info['total_price'];

        $patient_balance_info = mysql_fetch_assoc(mysql_query("SELECT patient_balance FROM patient_information WHERE id='$patient_id';")); 
        // $patient_balance = $patient_balance_info['patient_balance'] * -1;
        $patient_balance = $patient_balance_info['patient_balance'];
        if($patient_balance > 0){
            $total_owing = $owings - $patient_balance;
        }else if($patient_balance < 0){
            $total_owing = $owings + ($patient_balance*-1);
        }
         
        ?>

        <div class="portlet-title">
            <div class="caption caption-md">
                <i class="icon-globe theme-font hide"></i>
                <span class="caption-subject font-blue-madison bold uppercase"> PENDING JOBS FOR PICK UPS </span>
            </div>
        </div>

        <?php if($total_owing > 0){ ?>
                            <div class="alert" style="height: 200px;text-align: center;font-size: 20px;color: #a94442">
                                <center>  
                                    <strong>Sorry!</strong> This Client has an Outstanding Payment of  <strong>GHS <?php echo number_format($total_owing,2);?></strong> 
                                    <p style="font-size: 15px;">Outstanding Payment must be cleared before Pick Up can be made.</p>
                                </center>
                                <div class="btn-group btn-group-devided" data-toggle="buttons">
                                            <label onclick="getForwardPatient('forward_to_cashier_administrative', '#view_items_response_wrapper_2', '<?php echo $process_claim_no; ?>', 'Are You Sure?', 'You want to Forward this Client to Cashier?');" class="btn red btn-outline btn-circle btn-sm active">
                                                <input type="radio" name="options" class="toggle" id="option2">Forward to Cashier <i class="fa fa-forward"></i>
                                            </label>
                                        </div>
                            </div>  
        <?php }else if($total_owing < 0){ ?>
            
            <div class="alert" style="height: 50px;text-align: center;font-size: 20px;color: #00b300">
                                <center>  
                                    <strong>Note!</strong> This Client has an Overpayment of  <strong>GHS <?php echo number_format($total_owing*-1,2);?></strong> 
                                </center>
                            </div> 

        <?php } ?>

        <div class="portlet-body form">
            <form role="form" action="#" id="send_sms_patient">
                <div class="form-body">
                    <div class="row">
                        <div class="col-lg-12 col-xs-12 col-md-12">
                            <table class="table table-bordered">
                                <tr>
                                    <th>Date</th>
                                    <th>Job No.</th>
                                    <th>Frame Name</th>
                                    <th>Lens Type</th>
                                    <th>Job Status</th>
                                    <th>Pick Up Date</th>
                                    <th>Comment</th>
                                    <th>Status</th>
                                </tr>
                                <tbody>
                                    <?php
                                    while($row_rsodest = mysql_fetch_assoc($sql)){ ?>
                                        <tr>
                                            <td><?php echo $row_rsodest['datenew'];?></td>
                                            <td><?php echo $row_rsodest['job_number'];?></td>
                                            <td><?php echo $row_rsodest['item'];?></td>
                                            <td><?php $item_name = getJobLensPrescription($process_claim_no, $row_rsodest['job_number']); echo $item_name;?></td>
                                            <td style="cursor: pointer;font-weight: bold;color:green;word-wrap: break-word;"><?php echo strtoupper($row_rsodest['audit_status']);  if($row_rsodest['audit_status']=='Job In-Transit' && !empty($row_rsodest['audit_status_other'])){ echo '-<b style="color:red;">'. strtoupper($row_rsodest['audit_status_other']);} ?> <i style="color:red;"> - <?php echo branchName($row_rsodest['status_location_id'],$courier); ?></i> </td>
                                            <td><?php if(!empty($row_rsodest['pick_up_date'])){ echo date('jS F,Y g:ia',strtotime($row_rsodest['pick_up_date']));}?></td>
                                            <td><?php echo $row_rsodest['pick_up_comment'];?></td>
                                            <td>
                                            <?php if($row_rsodest['pick_up_status'] == 'Picked Up by Client'){
                                                echo '<b style="color:green;">'.$row_rsodest['pick_up_status'].'</b>';
                                            }else{ ?>
                                                <select name="pick_up_option_<?php echo $row_rsodest['job_number']; ?>" id="pick_up_option_<?php echo $row_rsodest['job_number'];?>" class="form-control" onchange="if(this.value=='Picked Up by Client'){$('#pick_up_comment_<?php echo str_replace('/','_',$row_rsodest['job_number']);?>').show();}else{$('#pick_up_comment_<?php echo str_replace('/','_',$row_rsodest['job_number']);?>').hide();}">
                                                    <option value="">Select Option</option>
                                                    <option selected value="Pending Pick Up">Pending Pick Up</option>
                                                    <option value="Picked Up by Client">Picked Up by Client</option>
                                                </select>
                                            <?php } ?>
                                            </td>
                                        </tr>
                                        <tr style="display:none;" id="pick_up_comment_<?php echo str_replace("/","_",$row_rsodest['job_number']);?>">
                                        <td colspan="10">
                                            <textarea style="border:1px solid #000;" class="form-control" name="pick_up_comment_<?php echo $row_rsodest['job_number'];?>" id="pick_up_comment_<?php echo $row_rsodest['job_number'];?>" placeholder="Enter Any Pick Up Comment Here"></textarea>
                                        </td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                            
                        </div>
                    </div>
                </div>
                <?php 
                
                if($total_owing <= 0 && $visit_status == 'In Use') { 
                // if($sql2_check < 1 && $total_owing <= 0 && $visit_status == 'In Use') { 
                    
                    ?>
                 <div class="form-actions right">
                                    <a class="btn red lg" onclick="if(confirm('Do you really want to Close this Visit?')){saveInformation2('save_pick_up_message', '<?php echo $process_claim_no; ?>', '#pick_up_patient', '');return false;}"> Close this Visit </a>
                                </div>
                <?php } else if($sql2_check > 0){ ?>
                    <div class="form-actions right">
                    <a class="btn green" onclick="saveInformation2('save_lens_pick_up', '<?php echo $process_claim_no; ?>', '#send_sms_patient', '');return false;"> Save Information  </a>
                </div>
               <?php }else if($visit_status=='Done'){
                        echo '<center><label style="color:red;text-align:25px;font-weight:bold">VISIT CLOSED SUCCESSFULLY</label></center>';
               } ?>
            </form>
        </div>

    <?php }
    
     if ($action == 'save_pick_up_message') {
        $process_claim_no = mysql_real_escape_string($_POST['id']);
        $comment = mysql_real_escape_string($_POST['pick_up_comment']);
        $type = 'pick_up_signature';
        $patient_info = mysql_fetch_assoc(mysql_query("SELECT patient_id, patient_no FROM process_claim_no WHERE process_claim_no = '$process_claim_no'"));
        $patient_id = $patient_info['patient_id'];
        $patient_no = $patient_info['patient_no'];

        $check = mysql_num_rows(mysql_query("SELECT * FROM patient_signature_files WHERE process_claim_no = '$process_claim_no' AND type='$type'"));
        if ($check > 0) {
            mysql_query("UPDATE patient_signature_files SET comment='$comment',user_id='" . $_SESSION['myMM_Userid'] . "' WHERE process_claim_no = '$process_claim_no' AND type='$type'");
        } else {
            mysql_query("INSERT INTO patient_signature_files (patient_id, patient_no, process_claim_no, type, comment, user_id) VALUES('$patient_id', '$patient_no', '$process_claim_no', '$type', '$comment', '" . $_SESSION['myMM_Userid'] . "')", $courier);
        }
         $insert = mysql_query("UPDATE process_claim_no SET patient_status='Done' WHERE patient_no = '$patient_no'");
        //  $insert = mysql_query("UPDATE process_claim_no SET patient_status='Done' WHERE process_claim_no = '$process_claim_no'");
        if ($insert == TRUE) {
            echo 'Success! Pick Up Confirmed.@@@success@@@';
        } else {
            echo 'Oops! There was a problem with the Pick Up.@@@error@@@';
        }
    }
    
    
if ($action == 'fetch_cancellation_items') {
    $items_all = $_POST['item_ids_'];
    if (count($items_all) > 0) {
    foreach ($items_all as $id) {
        $ids .= "id='$id' OR ";
    }
    $ids = substr($ids, 0,-4);
}
    $all_activiities = mysql_query("SELECT * FROM claims_details WHERE ($ids)");?>
    <form name="form_items" id="form_items" class="form-horizontal">
            <table class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>Date Time</th>
                    <th>Item Name</th>
                    <th style="text-align: right;">Qty</th>
                    <th style="text-align: right;">Price</th>
                    <th style="text-align: right;">Total</th>
                    <th style="text-align: center;">Status</th>
                    <th style="text-align: center;">Reason</th>
                    <th style="text-align: center;">
                        <label class="mt-checkbox mt-checkbox-outline" ><input id="parent_box" checked="" type="checkbox" onchange="checkBoxes2();"><span></span></label>
                    </th>
                </tr>
            </thead>
            <tbody>
    <?php
    $i = 1;
    while ($row = mysql_fetch_assoc($all_activiities)) { ?>
                        <tr>
                            <td><?php echo date('jS F, Y g:i a', strtotime($row['date_added'])); ?></td>
                            <td><?php echo strtoupper($row['item']); ?></td>
                            <td style="text-align: right;"><?php echo $row['qty']; ?></td>
                            <td style="text-align: right;"><?php echo $row['unit_price']; ?></td>
                            <td style="text-align: right;"><?php echo ($row['qty'] * $row['unit_price']); ?></td>
                            <td style="text-align: center;"><?php echo $row['payment_status']; ?></td>
                            <td style="text-align: center;"><input type="text" name="reason_<?php echo $row['id'];?>" id="" placeholder="Reason" class="form-control"></td>
                            <td style="text-align: center;"> <label class="mt-checkbox mt-checkbox-outline"><input checked="" value="<?php echo $row['id']; ?>" name="item_ids_ids[]" multiple="" class="item_check_box_2" type="checkbox"><span></span></label></td>
                        </tr>
        <?php $i++;
    } ?>
            </tbody>
        </table>
    </form>
                    
                    
    <?php
}

if ($action == 'forward_for_approval_cancel_item') {
    $items_all = $_POST['item_ids_ids'];
    $patient_no = $_POST['id'];
    if (count($items_all) > 0) {
        foreach ($items_all as $id) {
           $row = mysql_fetch_assoc(mysql_query("SELECT * FROM claims_details WHERE id='$id'"));
           $reason = $_POST['reason_'.$id]; 
           $insert = mysql_query("INSERT IGNORE INTO items_cancel_swap (patient_no,process_claim_no,item,claim_id,qty,unit_price,initiated_by,branch_id,type,reason) VALUES('$patient_no','" . $row['process_claim_no'] . "','" . $row['item'] . "','$id','" . $row['qty'] . "','" . $row['unit_price'] . "','" . $_SESSION['myMM_Fullname'] . "','" . $_SESSION['branch_id'] . "','Cancellation','$reason')");
        }
        if ($insert == TRUE) {
            echo 'Success! Items sent for Approval Successfully.@@@success@@@';
        } else {
            echo 'Oops! There was a problem.@@@error@@@';
        }
    }
}

if ($action == 'forward_for_approval_refund') {
    list($patient_no, $process_claim_no) = explode("@@@", $_POST['id']);
    $refund_amount = $_POST['refund_amount'];
    $refund_reason = $_POST['refund_reason'];
    mysql_query("INSERT INTO refund_payments (patient_no,process_claim_no,item,qty,unit_price,initiated_by,branch_id,reason) VALUES('$patient_no','$process_claim_no','General Refund','1','$refund_amount','" . $_SESSION['myMM_Fullname'] . "','" . $_SESSION['branch_id'] . "','$refund_reason')");
    echo 'Refund Sent Successfully for Approval!@@@success@@@';
}

if($action == 'get_lens_prescription' || $action == "get_lens_prescription_2"){
    list($process_claim_no, $id) = explode("_",$_POST['id']);
    if(empty($id)){
    $optical_info = mysql_fetch_assoc(mysql_query("SELECT * FROM optical_examination WHERE process_claim_no ='$process_claim_no' LIMIT 1", $courier));
   }else{
    $optical_info = mysql_fetch_assoc(mysql_query("SELECT * FROM optical_examination WHERE process_claim_no ='$process_claim_no' AND lens_id_count='$id'", $courier));
   }
    
   $check_visit = mysql_fetch_assoc(mysql_query("SELECT attending_officer_id,patient_status FROM process_claim_no WHERE process_claim_no='$process_claim_no'"));
   $attending_officer_id = $check_visit['attending_officer_id'];
   $visit_status = $check_visit['patient_status'];?>

 <div class="modal-body"> 
    <div class="portlet light bordered">
    <?php //if(!empty($id)){ ?>
                                        <div class="portlet-title">
                                        <div class="actions">
                                <div class="btn-group btn-group-devided" data-toggle="buttons"> 
                                <?php if($visit_status == 'In Use' && $action == 'get_lens_prescription'){ ?>                            
                                    <label onclick="calculateLensPrice('calculate_lens_price','<?php echo $process_claim_no; ?>', '<?php echo  $id; ?>', '#lens_price_details');" class="btn green btn-outline btn-circle btn-sm active" data-toggle="modal" href="#large">
                                            <input type="radio" name="options" class="toggle" id="option2"> Calculate Price <i class="fa fa-print"></i>
                                        </label>
                                <?php } ?>
                                    <label onclick="getprintPrescription('<?php echo $process_claim_no; ?>', '<?php echo  $id; ?>', '');" class="btn red btn-outline btn-circle btn-sm active" data-toggle="modal" href="#large">
                                            <input type="radio" name="options" class="toggle" id="option2"> Print Prescription <i class="fa fa-print"></i>
                                        </label>
                                </div>
                            </div>
                            <label style="color:red;"><i>1. Please click on the Save Prescription button at the buttom after any changes. <br> 2. Close this pop up page <br> 3. Open Pop up page again before clicking on the button to calculate the price</i></label>
                                        </div>
<?php //} ?> 
                            <div class="row">
                                <div class="col-md-12" id="lens_price_details">
                                </div>
                            </div>

                                            <div class="portlet-body form">
                                        <div class="form-body">
                                            <div class="row">
                                                 <div class="col-md-12">
                                                 <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>&nbsp;</th>
                                <th> Sphere </th>
                                <th> Cylinder </th>
                                <th> Axis </th>
                                <th> VA </th>
                                <th> Add </th>
                                <th> VA </th>
                                <th> PD </th>
                                <th nowrap>  &nbsp;
                                    <label class="mt-checkbox"><input onclick="Apply_od_os();" id="apply_od_os" type="checkbox">
                                                    <span></span>
                                                </label> </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td> <label> RIGHT </label> </td> 
                                <?php if($attending_officer_id!=0 && $action == 'get_lens_prescription'){ ?>
                                <td> 
                                <input type="hidden" name="od_sphere" id="od_sphere" class="form-control" value="<?php echo $optical_info['od_sphere']; ?>"> <?php echo $optical_info['od_sphere']; ?> </td> 
                                <td> 
                                <input type="hidden" name="od_cylinder" id="od_cylinder" class="form-control" value="<?php echo $optical_info['od_cylinder']; ?>"> <?php echo $optical_info['od_cylinder']; ?></td>
                                <?php } else{ ?>
                                <td> 
                                    <select name="od_sphere" id="od_sphere" class="">
                                            <option value="NULL">NULL</option>
                                            <option <?php if($optical_info['od_sphere']=='PLANO'){ echo 'selected=""';} ?> value='PLANO'>PLANO</option>
                                            <?php for($i=0.25;$i<=30;$i+=0.25){ ?>
                                            <option <?php if($optical_info['od_sphere']=='-'.number_format($i,2)){ echo 'selected=""';} ?> value="-<?php echo number_format($i,2); ?>">-<?php echo number_format($i,2); ?></option>
                                            <?php } ?>
                                            <?php for($i=0.25;$i<=30;$i+=0.25){ ?>
                                            <option <?php if($optical_info['od_sphere']=='+'.number_format($i,2)){ echo 'selected=""';} ?> value="+<?php echo number_format($i,2); ?>">+<?php echo number_format($i,2); ?></option>
                                            <?php } ?>
                                    </select>
                                    <!-- <input type="text" name="od_sphere" id="od_sphere" class="form-control" value="<?php echo $optical_info['od_sphere']; ?>"> -->
                                 </td> 
                                <td> 
                                <select name="od_cylinder" id="od_cylinder" class="">
                                            <option value="NULL">NULL</option>
                                            <option <?php if($optical_info['od_cylinder']==''){ echo 'selected=""';} ?> value=''>EMPTY</option>
                                            <option <?php if($optical_info['od_cylinder']=='PLANO'){ echo 'selected=""';} ?> value='PLANO'>PLANO</option>
                                            <?php for($i=0.25;$i<=30;$i+=0.25){ ?>
                                            <option <?php if($optical_info['od_cylinder']=='-'.number_format($i,2)){ echo 'selected=""';} ?> value="-<?php echo number_format($i,2); ?>">-<?php echo number_format($i,2); ?></option>
                                            <?php } ?>
                                            <?php for($i=0.25;$i<=30;$i+=0.25){ ?>
                                            <option <?php if($optical_info['od_cylinder']=='+'.number_format($i,2)){ echo 'selected=""';} ?> value="+<?php echo number_format($i,2); ?>">+<?php echo number_format($i,2); ?></option>
                                            <?php } ?>
                                    </select>
                                    <!-- <input type="text" name="od_cylinder" id="od_cylinder" class="form-control" value="<?php echo $optical_info['od_cylinder']; ?>"> -->
                                </td>
                                <?php } ?>
                                <td> <input type="text" name="od_axis" id="od_axis" class="form-control" value="<?php echo $optical_info['od_axis']; ?>"> </td> 
                                <td> <input type="text" name="od_va" id="od_va" class="form-control" value="<?php echo $optical_info['od_va']; ?>"> </td> 
                                <td> <input type="text" name="od_add" id="od_add" class="form-control" value="<?php echo $optical_info['od_add']; ?>"> </td> 
                                <td> <input type="text" name="od_va2" id="od_va2" class="form-control" value="<?php echo $optical_info['od_va2']; ?>"> </td> 
                                <td> <input type="text" name="od_pd" id="od_pd" class="form-control" value="<?php echo $optical_info['od_pd']; ?>"> </td> 
                                <td></td> 
                            </tr>
                            <tr>
                                <td> <label> LEFT </label> </td> 
                                <?php if($attending_officer_id!=0 && $action=='get_lens_prescription'){ ?>
                                <td> <input type="hidden" name="os_sphere" id="os_sphere" class="form-control" value="<?php echo $optical_info['os_sphere']; ?>"> <?php echo $optical_info['os_sphere']; ?></td> 
                                <td> <input type="hidden" name="os_cylinder" id="os_cylinder" class="form-control" value="<?php echo $optical_info['os_cylinder']; ?>"> <?php echo $optical_info['os_cylinder']; ?></td>
                                <?php }else{ ?>
                                <td> 
                                <select name="os_sphere" id="os_sphere" class="">
                                            <option <?php if($optical_info['os_cylinder']=='NULL'){ echo 'selected=""';} ?> value="NULL">NULL</option>
                                            <option <?php if($optical_info['os_sphere']=='PLANO'){ echo 'selected=""';} ?> value='PLANO'>PLANO</option>
                                            <?php for($i=0.25;$i<=30;$i+=0.25){ ?>
                                            <option <?php if($optical_info['os_sphere']=='-'.number_format($i,2)){ echo 'selected=""';} ?> value="-<?php echo number_format($i,2); ?>">-<?php echo number_format($i,2); ?></option>
                                            <?php } ?>
                                            <?php for($i=0.25;$i<=30;$i+=0.25){ ?>
                                            <option <?php if($optical_info['os_sphere']=='+'.number_format($i,2)){ echo 'selected=""';} ?> value="+<?php echo number_format($i,2); ?>">+<?php echo number_format($i,2); ?></option>
                                            <?php } ?>
                                    </select>
                                    <!-- <input type="text" name="os_sphere" id="os_sphere" class="form-control" value="<?php echo $optical_info['os_sphere']; ?>">  -->
                                </td> 
                                <td> 
                                <select name="os_cylinder" id="os_cylinder" class="">
                                            <option <?php if($optical_info['os_cylinder']=='NULL'){ echo 'selected=""';} ?> value="NULL">NULL</option>
                                            <option <?php if($optical_info['os_cylinder']==''){ echo 'selected=""';} ?> value=''>EMPTY</option>
                                            <option <?php if($optical_info['os_cylinder']=='PLANO'){ echo 'selected=""';} ?> value='PLANO'>PLANO</option>
                                            <?php for($i=0.25;$i<=30;$i+=0.25){ ?>
                                            <option <?php if($optical_info['os_cylinder']=='-'.number_format($i,2)){ echo 'selected=""';} ?> value="-<?php echo number_format($i,2); ?>">-<?php echo number_format($i,2); ?></option>
                                            <?php } ?>
                                            <?php for($i=0.25;$i<=30;$i+=0.25){ ?>
                                            <option <?php if($optical_info['os_cylinder']=='+'.number_format($i,2)){ echo 'selected=""';} ?> value="+<?php echo number_format($i,2); ?>">+<?php echo number_format($i,2); ?></option>
                                            <?php } ?>
                                    </select>
                                    <!-- <input type="text" name="os_cylinder" id="os_cylinder" class="form-control" value="<?php echo $optical_info['os_cylinder']; ?>"> -->
                                </td>
                                <?php } ?>
                                <td> <input type="text" name="os_axis" id="os_axis" class="form-control" value="<?php echo $optical_info['os_axis']; ?>"> </td> 
                                <td> <input type="text" name="os_va" id="os_va" class="form-control" value="<?php echo $optical_info['os_va']; ?>"> </td> 
                                <td> <input type="text" name="os_add" id="os_add" class="form-control" value="<?php echo $optical_info['os_add']; ?>"> </td> 
                                <td> <input type="text" name="os_va2" id="os_va2" class="form-control" value="<?php echo $optical_info['os_va2']; ?>"> </td> 
                                <td> <input type="text" name="os_pd" id="os_pd" class="form-control" value="<?php echo $optical_info['os_pd']; ?>"> </td> 
                                <td></td>
                            </tr>
                        </tbody>
                    </table>
                                                </div> 
                                            </div>
                                           <hr>  
                                           
                                           <div class="row">
                                           <div class="col-md-12">
                             <div class="col-md-4">
                                 <div class="row">
                                     <div class="form-group">
                                         <label class="bold">GLASS</label>
                                         <div class="mt-radio-list">
                                             <?php
                                             $patient_lens_type = mysql_fetch_assoc(mysql_query("SELECT lens_type,sight_reading FROM patient_lens_type WHERE process_claim_no='$process_claim_no' AND lens_id_count='$id'"));
                                             $glass_lens = mysql_query("SELECT * FROM lens_types WHERE parent_name='GLASS'");
                                             while ($row = mysql_fetch_assoc($glass_lens)) { ?>
                                                 <label class="mt-radio mt-radio-outline"> <?php echo $row['lens_name']; ?>
                                                     <input <?php if($patient_lens_type['lens_type']== 'Glass - '.$row['lens_name']){ echo 'checked=""'; } ?> onchange="lensPrescriptions(this.value);" type="radio" value="Glass - <?php echo $row['lens_name']; ?>" name="lens_type">
                                                     <span></span>
                                                 </label>
                                                <?php } ?>
                                         </div>
                                     </div>
                                     <div class="form-group">
                                         <label class="bold">PLASTIC</label>
                                         <div class="mt-radio-list">
                                             <?php
                                             $plastic_lens = mysql_query("SELECT * FROM lens_types WHERE parent_name='PLASTIC'");
                                             while ($row = mysql_fetch_assoc($plastic_lens)) {
                                                 ?>
                                                 <label class="mt-radio mt-radio-outline"> <?php echo $row['lens_name']; ?>
                                                     <input <?php if($patient_lens_type['lens_type']=='Plastic - '.$row['lens_name']){ echo 'checked=""'; } ?> onchange="lensPrescriptions(this.value);" type="radio" value="Plastic - <?php echo $row['lens_name']; ?>" name="lens_type">
                                                     <span></span>
                                                 </label>
<?php } ?>
                                         </div>
                                     </div>

                                <div class="form-group">
                                         <label class="bold">SIGHT/READING</label>
                                         <div class="mt-radio-list">
                                         <label class="mt-radio mt-radio-outline"> FOR SIGHT
                                                     <input <?php if($patient_lens_type['sight_reading']=='SIGHT'){ echo 'checked=""'; } ?> onchange="" type="radio" value="SIGHT" name="sight_reading">
                                                     <span></span>
                                                 </label>
                                         </div>
                                        
                                         <div class="mt-radio-list">
                                         <label class="mt-radio mt-radio-outline"> FOR READING
                                                     <input <?php if($patient_lens_type['sight_reading']=='READING'){ echo 'checked=""'; } ?> onchange="" type="radio" value="READING" name="sight_reading">
                                                     <span></span>
                                                 </label>
                                         </div>
                                </div>

                                 </div>
                             </div>
                             <div class="col-md-4">
                                 <div class="row">
                                     <div class="form-group">
                                         <label class="bold">POLYCARBONATE/OTHER</label>
                                         <div class="mt-radio-list">
                                             <?php
                                             $poly_lens = mysql_query("SELECT * FROM lens_types WHERE parent_name='POLYCARBONATE/OTHER'");
                                             while ($row = mysql_fetch_assoc($poly_lens)) {
                                                 ?>
                                                 <label class="mt-radio mt-radio-outline"> <?php echo $row['lens_name']; ?>
                                                     <input <?php if($patient_lens_type['lens_type']=='Polycarbonate - '.$row['lens_name']){ echo 'checked=""'; } ?> onchange="lensPrescriptions(this.value);" type="radio" value="Polycarbonate - <?php echo $row['lens_name']; ?>" name="lens_type">
                                                     <span></span>
                                                 </label>
<?php } ?>
                                         </div>
                                     </div>
                                     <div class="form-group">
                                         <label class="bold">CONTACT LENS</label>
                                         <div class="mt-radio-list">
<?php
$contact_lens = mysql_query("SELECT * FROM lens_types WHERE parent_name='CONTACT LENS'");
while ($row = mysql_fetch_assoc($contact_lens)) {
    ?>
                                                 <label class="mt-radio mt-radio-outline"> <?php echo $row['lens_name']; ?>
                                                     <input <?php if($patient_lens_type['lens_type']=='Contact Lens - '.$row['lens_name']){ echo 'checked=""'; } ?> onchange="lensPrescriptions(this.value);" type="radio" value="Contact Lens - <?php echo $row['lens_name']; ?>" name="lens_type">
                                                     <span></span>
                                                 </label>
<?php } ?>
                                         </div>
                                     </div>
                                 </div>
                             </div>
                             <div class="col-md-4">
                                 <div class="row">
                                     <div class="form-group">
                                         <label class="bold">LENS TYPE/ADDITIONS</label>
                                         <div class="mt-checkbox-list">
<?php
$additons = mysql_query("SELECT * FROM lens_types WHERE parent_name='ADDITIONS'");
$additons_extra = mysql_query("SELECT * FROM lens_types WHERE parent_name='EXTRA ADDITIONS'");
$lens_extra = mysql_query("SELECT * FROM lens_types WHERE parent_name='EXTRA LENS'");
            $tint_extra = mysql_query("SELECT * FROM lens_types WHERE parent_name='EXTRA OPTIMIZE TINT'");
            $optimize_tint1 = mysql_query("SELECT * FROM lens_types WHERE parent_name='OPTIMIZE TINT'");

            $getTint = mysql_fetch_assoc(mysql_query("SELECT * FROM patient_lens_addition a INNER JOIN lens_types b ON b.lens_name = a.addition WHERE a.process_claim_no='$process_claim_no' AND b.parent_name = 'OPTIMIZE TINT'"));

            ?>


<select class="form-control" name="lens_type_main" id="lens_type_main">
    <option value="">Select Lens Type</option>
<?php while ($row = mysql_fetch_assoc($additons)) {  ?> 
    <option <?php if($row['lens_name']==$optical_info['lens_type']){ echo 'selected=""';} ?> value="<?php echo $row['lens_name']; ?>"><?php echo $row['lens_name']; ?></option>
                                                 <!-- <label class="mt-checkbox mt-checkbox-outline"> <?php echo $row['lens_name']; ?> -->
                                                     <!-- <input <?php //checkPatientLensAddition($process_claim_no, $row['lens_name'], $id); ?> class="addition_lens" onchange="displayLensPrescription();" type="checkbox" value="<?php echo $row['lens_name']; ?>" name="lens_addition[]" multiple=""> -->
                                                     <!-- <span></span> -->
                                                 <!-- </label> -->
        
<?php } ?>
</select>
<hr>
    <select class="form-control" name="lens_index" id="lens_index">
        <option value="">Select Option</option>
    <?php while ($row = mysql_fetch_assoc($lens_extra)) { ?>
            <option <?php if($row['lens_name']==$optical_info['lens_index']){ echo 'selected=""';} ?> value="<?php echo $row['lens_name']; ?>"><?php echo $row['lens_name']; ?></option>
              <?php } ?>
             </select>
             <hr>
             <label class="bold">OPTIMIZE TINT</label>
             <select class="form-control" name="lens_optimize" id="lens_optimize">
              <option value="">Select Option</option>
              <?php while ($row = mysql_fetch_assoc($optimize_tint1)) { ?>
               <option <?php if($row['lens_name']==$getTint['addition']){ echo 'selected=""';} ?> value="<?php echo $row['lens_name']; ?>"><?php echo $row['lens_name']; ?></option>
    <?php } ?>
    </select>
    <!-- <hr>
    <select class="form-control" name="optimize_tint" id="optimize_tint">
        <option value="">Select Option</option>
    <?php while ($row = mysql_fetch_assoc($tint_extra)) { ?>
            <option <?php if($row['lens_name']==$optical_info['lens_index']){ echo 'selected=""';} ?> value="<?php echo $row['lens_name']; ?>"><?php echo $row['lens_name']; ?></option>
    <?php } ?>
    </select> -->
    <hr>
<?php
while ($row = mysql_fetch_assoc($additons_extra)) { ?>  
                                                 <label class="mt-checkbox mt-checkbox-outline"> <?php echo $row['lens_name']; ?>
                                                     <input <?php checkPatientLensAddition($process_claim_no, $row['lens_name'], $id); ?> class="addition_lens" onchange="displayLensPrescription();" type="checkbox" value="<?php echo $row['lens_name']; ?>" name="lens_addition[]" multiple="">
                                                     <span></span>
                                                 </label>
<?php } ?>
                                         </div>
                                     </div> 
                                 </div> 
                             </div>
                         </div>
                     </div>
                 </div>
                                                
                                                <div class="row">
                                                    <div class="alert bold" id="">
                                                        <span id="lens_description_wrapper"> </span>
                                                        <span> <?php
                                                            $lens_addition = PatientLensAddition($process_claim_no,$id);
                                                            echo $patient_lens_type['lens_type']; echo ' '.$optical_info['lens_type'].' '; if(!empty($optical_info['lens_index'])){ echo '('.$optical_info['lens_index'].')';} if(!empty($lens_addition)){ ?> - Additions (<span id="lens_description_wrapper2"><?php echo $lens_addition; ?></span>) <?php } ?> </span>
                                                    </div>

                                                <div class="col-md-12 ">
        <div class="portlet light bordered">
            <div class="portlet-title">
                <div class="caption">
                    <i class="fa fa-bolt font-blue-hoki"></i>
                    <span class="caption-subject bold font-blue-hoki"> Contact Lens Prescription </span>
                </div>
                <div class="portlet-body">
                  <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th> R </th>
                                <th> L </th>
                                <th> Base Curve </th>
                                <th> Diameter </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td> <input type="text" name="r_eye" id="r_eye" class="form-control" value="<?php echo $optical_info['r_eye']; ?>"> </td> 
                                <td> <input type="text" name="l_eye" id="l_eye" class="form-control" value="<?php echo $optical_info['l_eye']; ?>"> </td> 
                                <td> <input type="text" name="base_curve" id="base_curve" class="form-control" value="<?php echo $optical_info['base_curve']; ?>"> </td> 
                                <td> <input type="text" name="diameter" id="diameter" class="form-control" value="<?php echo $optical_info['diameter']; ?>"> </td> 
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

                                                <!-- <hr> -->
                                                <button type="button" class="btn red btn-outline active" data-dismiss="modal">Close</button>
                                                <input type="reset" value="Reset" class="btn red btn-outline active">
                                        <?php if($visit_status == 'In Use'){ ?>     
                                        <button id="save_patient_consultation" onclick="save_consultation('save_patient_prescription','<?php echo $process_claim_no; ?>@@@<?php echo $patient_id; ?>','<?php echo $id;?>','','0');return false;" class="btn btn green pull-right"> Save Prescription <i class="icon-ok"></i></button>
                                        <?php } ?>
                                          </div>
                                            </div>
                                            </div>
                                            </div>

<!-- <div class="modal-footer">
                                                        
                                                        <button type="button" class="btn green">Save changes</button>
                                                    </div> -->


<?php }


if($action == "calculate_lens_price"){
    $process_claim_no = $_POST['id'];
    $id = $_POST['id2'];
    list($lens_name, $lens_price) = calculateLensPrice($process_claim_no, $id);

    $row_rsodest_claim_no = mysql_fetch_assoc(mysql_query("select mode_of_payment from process_claim_no where process_claim_no='$process_claim_no'"));
    $mode_of_payment_all = $row_rsodest_claim_no['mode_of_payment'];
    
    $query_rsodest_claim_no = mysql_fetch_assoc(mysql_query("select * from process_claim_no where process_claim_no='$process_claim_no'"));
    $mode_of_payment = $query_rsodest_claim_no['mode_of_payment'];?>

    <table class="table">
        <tr>
            <td><?php echo $lens_name; ?></td>
            <td> 
                <input readonly value="<?php echo $lens_price; ?>" class="form-control" type="text" name="main_lens_price" id="main_lens_price"> 
                <input type="hidden" name="lens_name" id="lens_name" value="<?php echo $lens_name; ?>">
            </td>
        </tr>
        <?php if($mode_of_payment_all == 'insurance' || $mode_of_payment_all == 'company'){ ?>
        <tr>
            <td><?php echo 'Top Up Amount' ?> <b>(CASH or GRATIS)</b></td>
            <td>
                 <input onchange="calculateCashTop('<?php echo $lens_price; ?>',this.value);" placeholder="CASH or GRATIS" class="form-control" type="text" name="lens_cash_top_up" id="lens_cash_top_up"> 
                 <select class="form-control" name="mode_of_payment_lens" id="mode_of_payment_lens">
                        <option value="cash">Cash</option>
                        <option value="gratis">Gratis</option>
                        <option value="credit sales">Credit Sales</option>
                 </select>
                 </td>
        </tr>
        <?php  } ?>
        <tr>
            <td>&nbsp;</td>
            <td id="save_lens_btn"><a class="btn green" onclick="MainsearchItemsName('save_lens_price_details','#view_items_response_wrapper_2','<?php echo $process_claim_no.'@@@'.$id; ?>','no_field');$('#save_lens_btn').html('<b>Lens Saved Successfully</b>');return false;">Add to Bill</a></td>
        </tr>
    </table>
    <?php
}

if($action == 'save_lens_price_details'){
    list($process_claim_no, $id) = explode("@@@",$_POST['item_search']);
    $main_lens_price = $_POST['main_lens_price'];
    $lens_cash_top_up = $_POST['lens_cash_top_up'];
    $lens_name = $_POST['lens_name'];
    $user_id=$_SESSION['myMM_Userid'];

    $arrayItems = mysql_fetch_assoc(mysql_query("SELECT * FROM service_items LIMIT 1"));
    $provider_item_id=0;
    $ins_item_code='04661';
    $provider_id = $arrayItems['provider_id'];
    $provider_id_master=$arrayItems['provider_id_master'];
    $item=$lens_name;
    $rx_item_name=$lens_name;
    $rx_brand_name=$lens_name;
    $rx_cartegory='';
    $rx_prescription_name=$lens_name;
    $item_cash_price = $main_lens_price;
    $item_class='';
    $item_service='Optical Service';
    $item_service_type='Optical Lens';

    $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'];
    $member_insurance_no=$row_rsodest_claim_no['insurance_no'];
    $type_of_visit=$row_rsodest_claim_no['typeofvisit'];
    $attending_officer=$row_rsodest_claim_no['attending_officer'];
    $attending_officer_id=$row_rsodest_claim_no['attending_officer_id'];
    $date_of_consultation=$row_rsodest_claim_no['date_added'];
    $insurance_claim_no=$row_rsodest_claim_no['insurance_claim_no'];
    $ins_company=$row_rsodest_claim_no['insurance_company'];
    $api_ins_company=$row_rsodest_claim_no['insurance_company'];
    $patient_no = $row_rsodest_claim_no['patient_no'];
    // $main_mode_of_payment = $row_rsodest_claim_no['mode_of_payment'];
    $mode_of_payment_all = $_POST['mode_of_payment_lens'];
    $main_mode_of_payment = $_POST['mode_of_payment_all'];
    if(empty($mode_of_payment_all) || $mode_of_payment_all==NULL){
        $mode_of_payment_all='cash';
    }

    // echo $main_mode_of_payment; exit;;

    if($main_mode_of_payment == 'insurance' || $main_mode_of_payment == 'company'){
        $payment_status = 'Paid';
    } else if($main_mode_of_payment=='cash'){
        $payment_status = 'Owing';
    } else if($main_mode_of_payment == 'gratis' || $main_mode_of_payment=='credit sales'){
        $payment_status = 'Unapproved';
    }
    
    
    if($mode_of_payment_all == 'gratis' || $mode_of_payment_all=='credit sales'){
        $payment_status2 = 'Unapproved';
    } else if($mode_of_payment_all == 'insurance' || $mode_of_payment_all == 'company'){
        $payment_status2 = 'Paid';
    } else if($mode_of_payment_all == 'cash'){
        $payment_status2 = 'Owing';
    }

    $check_exist = mysql_query("SELECT * FROM claims_details WHERE process_claim_no='$process_claim_no' AND lens_id_count='$id'");
    if(mysql_num_rows($check_exist) > 0){
        mysql_query("DELETE FROM claims_details WHERE process_claim_no='$process_claim_no' AND lens_id_count='$id'");
    }
    if($lens_cash_top_up > 0){
        $query_insert = "INSERT INTO `claims_details` (`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`, `branch_id`,`lens_id_count`) VALUES (NULL, '$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 (Cash Top Up)', '$item_class', '$item_service', '$item_service_type', '$lens_cash_top_up', NOW(), CURDATE(), '1', '$lens_cash_top_up', '$mode_of_payment_all', '$patient_no', '$payment_status2', '$type_of_payment','".$_SESSION['branch_id']."','$id');";
        $rsinsert=mysql_query($query_insert) or die(mysql_error());
    }

    $query_insert = "INSERT INTO `claims_details` (`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`, `branch_id`, `lens_id_count`) VALUES (NULL, '$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', '$main_lens_price', NOW(), CURDATE(), '1', '$main_lens_price', '$main_mode_of_payment', '$patient_no', '$payment_status', '$type_of_payment','".$_SESSION['branch_id']."','$id');";	
    $rsinsert=mysql_query($query_insert) or die(mysql_error());


    if ($top_up == 'yes' || $mode_of_payment_all == 'insurance'){
        $action_2 = 'addclaimitem';
        include('api_integration.php');
        if($status_code == '008'){

        }
    }
    include('view_items_claims_administrative.php');
}

if($action == 'save_lens_pick_up'){
    $process_claim_no = $_POST['id'];
    $pick_up_option = $_POST['pick_up_option_'.$job_number];
    $pick_up_comment = $_POST['pick_up_comment_'.$job_number];
    
    $sql = mysql_query("SELECT c.job_number FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no inner join claims_details g ON c.job_number=g.job_number left join job_activities_status j ON a.process_claim_no=j.process_claim_no WHERE a.patient_status = 'In Use' AND c.audit_status<>'none' AND a.process_claim_no='$process_claim_no' AND g.item_service_type='Optical Frame' AND c.pick_up_status='Pending Pick Up' GROUP BY c.job_number");
    
    while($r = mysql_fetch_assoc($sql)){
        $job_number = $r['job_number'];
        $pick_up_option = $_POST['pick_up_option_'.$job_number];
        $pick_up_comment = $_POST['pick_up_comment_'.$job_number];
        if($pick_up_option == 'Picked Up by Client'){
        $picked = mysql_query("UPDATE job_numbers SET pick_up_date=now(),pick_up_status='$pick_up_option',pick_up_comment='$pick_up_comment' WHERE process_claim_no='$process_claim_no' AND job_number='$job_number'");
        }
    }

    if($picked == true){
        echo 'Success! Job Picked up successfully@@@success@@@';
        // $action = 'pick_up_patient_information';
        // include('process_patient.php');
    }else{
        echo 'Error! Something went wrong@@@error@@@';
    }
}

if($action == 'fetch_all_lens_prescription' || $action=="fetch_all_lens_prescription_2"){ 
    if($action == 'fetch_all_lens_prescription'){
        $fetch_all_lens_prescription = 'fetch_all_lens_prescription';
        $get_lens_prescription = 'get_lens_prescription';
    }else{
        $fetch_all_lens_prescription = 'fetch_all_lens_prescription_2';
        $get_lens_prescription='get_lens_prescription_2';
    }
    $process_claim_no = $_POST['id'];
    $optical_info_query = mysql_query("SELECT * FROM optical_examination WHERE process_claim_no ='$process_claim_no'", $courier);
    $row_rsodest_claim_no = mysql_fetch_assoc(mysql_query("select * from process_claim_no where process_claim_no='$process_claim_no'"));
    $visit_status = $row_rsodest_claim_no['patient_status'];
  
   if($visit_status == 'In Use' && $action == "fetch_all_lens_prescription"){ ?>
        <div class="btn-group btn-group-devided pull-right" data-toggle="buttons">                             
    <label data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');" class="btn green btn-outline btn-circle btn-sm active" data-toggle="modal" href="#large">
            <input type="radio" name="options" class="toggle" id="option2"> Add Lens Prescription <i class="fa fa-plus"></i>
        </label>
        <label id="refresh_tn_lens_presc" onclick="getSimpleData('<?php echo $fetch_all_lens_prescription; ?>', '<?php echo $process_claim_no;?>', '#all_prescription_wrapper');" class="btn red btn-outline btn-circle btn-sm active">
                                            <input type="radio" name="options" class="toggle" id="option2"> Refresh <i class="fa fa-refresh"></i>
                                        </label>
        <br>
</div>
<?php } ?>

         <?php if(mysql_num_rows($optical_info_query) > 0){ ?>
                <table class="table table-bordered">
                <tr>
                    <th>&nbsp;</th>
                    <th>&nbsp;</th>
                    <th style="text-align:center;"> Sphere </th>
                    <th style="text-align:center;"> Cylinder </th>
                    <th style="text-align:center;"> Axis </th>
                    <th style="text-align:center;"> VA </th>
                    <th style="text-align:center;"> Add </th>
                    <th style="text-align:center;"> VA </th>
                    <th style="text-align:center;"> PD </th>
                    <th style="text-align:center;"> TYPE </th>
                    <th style="text-align:center;"> LENS </th>
                    <th colspan="2" style="text-align:center;"> </th>
                </tr>
                <tbody>
                        <?php
                        $i=1;
                        while($optical_info = mysql_fetch_assoc($optical_info_query)){
                            $patient_lens_type = mysql_fetch_assoc(mysql_query("SELECT lens_type FROM patient_lens_type WHERE process_claim_no='".$optical_info['process_claim_no']."' AND lens_id_count='".$optical_info['lens_id_count']."'"));
                            ?>
                            <tr>
                                                                <td rowspan="2"><?php echo $i; ?></td>
                                                                <td>RIGHT</td>
                                                                <td><?php echo $optical_info['od_sphere']; ?></td>
                                                                <td><?php echo $optical_info['od_cylinder']; ?></td>
                                                                <td><?php echo $optical_info['od_axis']; ?></td>
                                                                <td><?php echo $optical_info['od_va']; ?></td>
                                                                <td><?php echo $optical_info['od_add']; ?></td>
                                                                <td><?php echo $optical_info['od_va2']; ?></td>
                                                                <td><?php echo $optical_info['od_pd']; ?></td>
                                                                <td rowspan="2" style="text-align:center;"><?php getSightRead($process_claim_no,$optical_info['lens_id_count']); ?></td>
                                                                <td rowspan="2">
                                                                <div class="alert bold" id="">
                                                                    <span id="lens_description_wrapper"> </span>
                                                                    <span style="font-size:12px;font-weight:normal"> <?php
                                                                        $lens_addition = PatientLensAddition($process_claim_no,$optical_info['lens_id_count']);
                                                                        echo $patient_lens_type['lens_type']; echo ' '.$optical_info['lens_type'].' '; if(!empty($optical_info['lens_index'])){ echo '('.$optical_info['lens_index'].')';} if(!empty($lens_addition)){ ?> - Additions (<span id="lens_description_wrapper2"><?php echo $lens_addition; ?></span>) <?php } ?> </span>
                                                                 </div>
                                                                </td>
                                                                <td rowspan="2" data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription_2', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');" style="text-align:center;"><i class="fa fa-pencil"></i></td>
                                                                <td rowspan="2" onclick="if(confirm('Delete this Lens Prescription?')){getSimpleData('delete_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');}" style="text-align:center;color:red;cursor:pointer"><i class="fa fa-trash"></i></td>
                                                            </tr>
                                                            <tr>
                                                                <td>LEFT</td>
                                                                <td><?php echo $optical_info['os_sphere']; ?></td>
                                                                <td><?php echo $optical_info['os_cylinder']; ?></td>
                                                                <td><?php echo $optical_info['os_axis']; ?></td>
                                                                <td><?php echo $optical_info['os_va']; ?></td>
                                                                <td><?php echo $optical_info['os_add']; ?></td>
                                                                <td><?php echo $optical_info['os_va2']; ?></td>
                                                                <td><?php echo $optical_info['os_pd']; ?></td>
                                                            </tr>
                       <?php $i++; } ?>
                </tbody>
                </table>
         <?php } 

}

if($action== 're_open_close_visit'){
    $id = mysql_real_escape_string($_POST['id']);
    $sql = mysql_query("UPDATE process_claim_no SET patient_status='In Use' WHERE process_claim_no='$id'"); 
}

if($action == 'lens_frame_replacement'){ 
list($process_claim_no, $item,$item_id) = explode("@@@",$_POST['id']);
$sql = mysql_query("SELECT * FROM claims_details WHERE process_claim_no='$process_claim_no' and item <> 'BALANCE OWING' and item_service_type<>'Optical Consultation' order by item_service_type");
?>
<div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h3 class="modal-title" id="cash_top_up_title"><?php echo $item; ?> - REPLACEMENT</h3>
        </div>
        <div class="modal-body">
            <form name="form" id="form" class="form-horizontal">
                <table class="table table-bordered table-striped">
                <?php while($row = mysql_fetch_assoc($sql)){ ?>
                <tr>
                    <td><input onchange="$('#replacement_item_id').val(this.value);" type="radio" name="item_name" id="item_name" value="<?php echo $row['id'];?>"></td>
                    <td style="font-size:15px;"> <?php echo $row['item'];?> </td>
                    <td style="font-size:15px;"> <?php echo $row['item_service_type'];?> </td>
                </tr>
                <?php } ?>
            </table>
            </form>
        </div>
        <div class="modal-footer">
        <button id="close_modal_id" type="button" class="btn red" data-dismiss="modal">Close</button>
            <button data-dismiss="modal" type="button" class="btn green" onclick="searchItemsName('administrative_edit_claim_details_administrative','#view_items_response_wrapper_2','<?php echo $item_id; ?>','no_field');">Save Replacement</button>
        </div>

<?php
}

if($action == 'delete_lens_prescription'){ 
    list($process_claim_no,$id) = explode("_",$_POST['id']);
    mysql_query("DELETE FROM patient_lens_type WHERE process_claim_no='$process_claim_no' AND lens_id_count='$id'");
    mysql_query("DELETE FROM patient_lens_addition WHERE process_claim_no='$process_claim_no' AND lens_id_count='$id'");
    mysql_query("DELETE FROM optical_examination WHERE process_claim_no='$process_claim_no' AND lens_id_count='$id'");

}

if ($action == 'records_edit_patient_dob') {
    $id = $_POST['id'];
    $value = $_POST['value'];
    mysql_query("UPDATE patient_information SET `dateofbirth` = '$value' WHERE id='$id'");
}

if ($action == 'records_edit_patient_occupation') {
    $id = $_POST['id'];
    $value = $_POST['value'];
    mysql_query("UPDATE patient_information SET `occupation` = '$value' WHERE id='$id'");
}


if($action == "fetch_all_lens_prescription_front_desk"){
    list($id, $process_claim_no) = explode('@@@', $_POST['id']);
    $optical_info_query = mysql_query("SELECT * FROM optical_examination WHERE process_claim_no ='$process_claim_no'", $courier);
    $optical_info_query_2 = mysql_query("SELECT * FROM optical_examination WHERE process_claim_no <> '$process_claim_no' AND patient_id='$id'", $courier);

     if(mysql_num_rows($optical_info_query) > 0){ ?>
        <table class="table table-bordered">
<tr>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th style="text-align:center;"> Sphere </th>
<th style="text-align:center;"> Cylinder </th>
<th style="text-align:center;"> Axis </th>
<th style="text-align:center;"> VA </th>
<th style="text-align:center;"> Add </th>
<th style="text-align:center;"> VA </th>
<th style="text-align:center;"> PD </th>
<th style="text-align:center;"> TYPE </th>
<th style="text-align:center;"> LENS </th>
<th colspan="2" style="text-align:center;"> </th>
</tr>
<tbody>
<?php
$i=1;
while($optical_info = mysql_fetch_assoc($optical_info_query)){ 
    $patient_lens_type = mysql_fetch_assoc(mysql_query("SELECT lens_type FROM patient_lens_type WHERE process_claim_no='".$optical_info['process_claim_no']."' AND lens_id_count='".$optical_info['lens_id_count']."'"));
    ?>
<tr>
                            <td rowspan="2" data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $i; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');">RIGHT</td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['od_sphere']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['od_cylinder']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['od_axis']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['od_va']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['od_add']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['od_va2']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['od_pd']; ?></td>
                            <td rowspan="2" style="text-align:center;" data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php getSightRead($process_claim_no,$optical_info['lens_id_count']); ?></td>
                            <td rowspan="2" data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');">
                            <div class="alert bold" id="">
                                <span id="lens_description_wrapper"> </span>
                                <span style="font-size:12px;font-weight:normal"> <?php
                                    $lens_addition = PatientLensAddition($process_claim_no,$optical_info['lens_id_count']);
                                    echo $patient_lens_type['lens_type']; echo ' '.$optical_info['lens_type'].' '; if(!empty($optical_info['lens_index'])){ echo '('.$optical_info['lens_index'].')';} if(!empty($lens_addition)){ ?> - Additions (<span id="lens_description_wrapper2"><?php echo $lens_addition; ?></span>) <?php } ?> </span>
                             </div>
                            </td>
                            <td rowspan="2" data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');" style="text-align:center;"><i class="fa fa-pencil"></i></td>
                            <td rowspan="2" onclick="if(confirm('Delete this Lens Prescription?')){getSimpleData('delete_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');}" style="text-align:center;color:red;cursor:pointer"><i class="fa fa-trash"></i></td>
                        </tr>
                        <tr>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');">LEFT</td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['os_sphere']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['os_cylinder']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['os_axis']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['os_va']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['os_add']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['os_va2']; ?></td>
                            <td data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $process_claim_no.'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');"><?php echo $optical_info['os_pd']; ?></td>
                        </tr>
<?php $i++; } ?>
</tbody>
</table>
     <?php } ?> 
    
     <?php if(mysql_num_rows($optical_info_query_2) > 0){ ?>
        <table class="table table-bordered">
        <tr>
                <th colspan="20" style="text-align:center;">Lens Prescription History</th>
            </tr>
<tr>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th style="text-align:center;"> Sphere </th>
<th style="text-align:center;"> Cylinder </th>
<th style="text-align:center;"> Axis </th>
<th style="text-align:center;"> VA </th>
<th style="text-align:center;"> Add </th>
<th style="text-align:center;"> VA </th>
<th style="text-align:center;"> PD </th>
<th style="text-align:center;"> TYPE </th>
<th style="text-align:center;"> LENS </th>
<th colspan="2" style="text-align:center;"> </th>
</tr>
<tbody>
<?php
$i=1;
while($optical_info = mysql_fetch_assoc($optical_info_query_2)){
    $patient_lens_type = mysql_fetch_assoc(mysql_query("SELECT lens_type FROM patient_lens_type WHERE process_claim_no='".$optical_info['process_claim_no']."' AND lens_id_count='".$optical_info['lens_id_count']."'"));
    ?>
<tr>
                            <td rowspan="2"><?php echo $i; ?></td>
                            <td>RIGHT</td>
                            <td><?php echo $optical_info['od_sphere']; ?></td>
                            <td><?php echo $optical_info['od_cylinder']; ?></td>
                            <td><?php echo $optical_info['od_axis']; ?></td>
                            <td><?php echo $optical_info['od_va']; ?></td>
                            <td><?php echo $optical_info['od_add']; ?></td>
                            <td><?php echo $optical_info['od_va2']; ?></td>
                            <td><?php echo $optical_info['od_pd']; ?></td>
                            <td rowspan="2" style="text-align:center;"><?php getSightRead($optical_info['process_claim_no'],$optical_info['lens_id_count']); ?></td>
                            <td rowspan="2">
                            <div class="alert bold" id="">
                                <span id="lens_description_wrapper"> </span>
                                <span style="font-size:12px;font-weight:normal"> <?php
                                    $lens_addition = PatientLensAddition($optical_info['process_claim_no'],$optical_info['lens_id_count']);
                                    echo $patient_lens_type['lens_type']; echo ' '.$optical_info['lens_type'].' '; if(!empty($optical_info['lens_index'])){ echo '('.$optical_info['lens_index'].')';} if(!empty($lens_addition)){ ?> - Additions (<span id="lens_description_wrapper2"><?php echo $lens_addition; ?></span>) <?php } ?> </span>
                             </div>
                            </td>
                            <td rowspan="2" data-toggle="modal" href="#large_lens" onclick="getSimpleData('get_lens_prescription', '<?php echo $optical_info['process_claim_no'].'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');" style="text-align:center;"><i class="fa fa-pencil"></i></td>
                            <td rowspan="2" onclick="if(confirm('Delete this Lens Prescription?')){getSimpleData('delete_lens_prescription', '<?php echo $optical_info['process_claim_no'].'_'.$optical_info['lens_id_count'] ?>', '#lens_prescription_wrapper');}" style="text-align:center;color:red;cursor:pointer"><i class="fa fa-trash"></i></td>
                        </tr>
                        <tr>
                            <td>LEFT</td>
                            <td><?php echo $optical_info['os_sphere']; ?></td>
                            <td><?php echo $optical_info['os_cylinder']; ?></td>
                            <td><?php echo $optical_info['os_axis']; ?></td>
                            <td><?php echo $optical_info['os_va']; ?></td>
                            <td><?php echo $optical_info['os_add']; ?></td>
                            <td><?php echo $optical_info['os_va2']; ?></td>
                            <td><?php echo $optical_info['os_pd']; ?></td>
                        </tr>
<?php $i++; } ?>
</tbody>
</table>
     <?php } 
}