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

$searchtxt=$_POST["searchtext"];

$typesearch=$_POST["typesearch"];

$id = mysql_real_escape_string($_POST['id']);



$datatable_products_length = $_POST['datatable_products_length'];

if(empty($datatable_products_length)){$datatable_products_length = 20;}



if ($id == 'incoming_patients') {

  $patients = 'Incoming Clients';

} else if ($id == 'discharged') {

  $patients = 'Completed Clients';

} else if ($id == 'discharged_review') {

  $patients = 'Clients Discharged To Come for Review';

} else if ($id == 'pending_diagnostic') {

  $patients = 'Pending Clients - Eg: Awaiting Spectacles';

} else if ($id == 'pending_spectacles') {

  $patients = 'Pending Clients - Eg: Awaiting Spectacles';

}



if ($action == 'list_visits_optical') {

  if ($id == 'incoming_patients') {

    $query_rsodest = "SELECT distinct(a.process_claim_no), a.*,b.title,b.surname, b.othernames, b.id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew FROM process_claim_no a LEFT JOIN patient_information b ON a.patient_no = b.card_number INNER JOIN claims_details c ON a.process_claim_no=c.process_claim_no 

    WHERE a.attending_officer_id <> '0' and (a.consulting_room_status = 'awaiting' or a.consulting_room_status = 'pending') and a.patient_status='In Use' and (a.typeofvisit='Out Patient' or a.typeofvisit = 'Out-Patient') AND (c.payment_status='Paid' or c.payment_status='Approved') AND a.branch_id='".$_SESSION['branch_id']."' ORDER BY a.id ASC, a.datetimeadded DESC";

  } else if ($id == 'discharged') {

    $query_rsodest = "SELECT a.*,b.title,b.surname, b.othernames, b.id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew

    FROM process_claim_no a INNER JOIN patient_information b ON a.patient_no = b.card_number WHERE a.attending_officer_id <> '0' and (a.consulting_room_status = 'discharged' OR a.consulting_room_status='discharged_review') AND a.branch_id='".$_SESSION['branch_id']."' ORDER BY a.datetimeadded DESC";

  } else {

    $query_rsodest = "SELECT a.*,b.title,b.surname, b.othernames, b.id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew

    FROM process_claim_no a INNER JOIN patient_information b ON a.patient_no = b.card_number WHERE a.attending_officer_id <> '0' and a.consulting_room_status = '$id' AND a.branch_id='".$_SESSION['branch_id']."' ORDER BY a.id ASC, a.datetimeadded DESC";

  }

}



else if ($action == 'list_visits_optical_search') {

  $search_info = "";

  if ($typesearch == 'surname') {

    $search_info = "surname like '$searchtxt%'";

  } else if ($typesearch == 'othernames') {

    $search_info = "othernames like '%$searchtxt%'";

  } else if ($typesearch == 'patientid' || $typesearch == 'oldcardno') {

    $search_info = "(card_number='$searchtxt' or old_card_no='$searchtxt')";

  } else if ($typesearch == 'member_no') {

    $search_info = "insurance_no  like '$searchtxt%'";

  } else if ($typesearch == 'filter') {

    $start_date = $_POST['start_date'] ;

    $end_date = $_POST['end_date'];

    $search_info = "date_added >= '$start_date' AND date_added <='$end_date'";

  }



  if ($id == 'incoming_patients') {

    $query_rsodest = "SELECT distinct(a.process_claim_no), a.*,b.title,b.surname, b.othernames, b.id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew FROM process_claim_no a LEFT JOIN patient_information b ON a.patient_no = b.card_number INNER JOIN claims_details c ON a.process_claim_no=c.process_claim_no

    WHERE a.attending_officer_id <> '0' and (a.consulting_room_status = 'awaiting' or a.consulting_room_status = 'pending') and a.patient_status='In Use' and (a.typeofvisit='Out Patient' or a.typeofvisit = 'Out-Patient')  AND (c.payment_status='Paid' or c.payment_status='Approved') and $search_info ORDER BY a.id ASC, a.datetimeadded DESC";

  } else if ($id == 'admitted') {

    $query_rsodest = "SELECT a.*,b.title,b.surname, b.othernames, b.id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew

    FROM process_claim_no a INNER JOIN patient_information b ON a.patient_no = b.card_number WHERE a.attending_officer_id <> '0' and (a.consulting_room_status = '$id' OR a.typeofvisit='In Patient' OR a.typeofvisit='In-Patient') and $search_info ORDER BY a.id ASC, a.datetimeadded DESC";

  } else {

    $query_rsodest = "SELECT a.*,b.title,b.surname, b.othernames, b.id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew

    FROM process_claim_no a INNER JOIN patient_information b ON a.patient_no = b.card_number WHERE a.attending_officer_id <> '0' and a.consulting_room_status = '$id' and $search_info ORDER BY a.id ASC, a.datetimeadded DESC";

  }

}



//$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());

//$totalRows_rsodest = mysql_num_rows($rsodest);

//$row_rsodest = mysql_fetch_assoc($rsodest);





$pagerows = $datatable_products_length;

$rsodest = mysql_query($query_rsodest, $courier) or die(mysql_error());

$totalRows_rsodest = mysql_num_rows($rsodest);



$total_pages = ceil($totalRows_rsodest / $pagerows);

$pagenum = $_POST['page_num'];

if (!(isset($pagenum))) {

  $pagenum = 1;

}

if ($pagenum < 1) {

  $pagenum = 1;

} elseif ($pagenum > $total_pages) {

  $pagenum = $total_pages;

}

$start_row = (($pagenum - 1) * $pagerows) + 1;

$end_row = $start_row + $pagerows;



if ($end_row > $totalRows_rsodest) {

  $end_row = $totalRows_rsodest;

}

$rsodest = mysql_query($query_rsodest . ' limit ' . (($pagenum - 1) * $pagerows) . ',' . $pagerows);

if ($totalRows_rsodest > 0) {

  $row_rsodest = mysql_fetch_assoc($rsodest);

} else {

  $start_row = 0;

}



?>



<div class="page-bar">

  <ul class="page-breadcrumb">

    <li>

      <i class="icon-home"></i>

      <a href="index.html">Home</a>

      <i class="fa fa-angle-right"></i>

    </li>

    <li>

      <span>Dashboard</span>

    </li>

  </ul>

  <div class="page-toolbar">

    <div class="btn-group pull-right">

      <button type="button" class="btn btn-fit-height grey-salt dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="true"> Actions

        <i class="fa fa-angle-down"></i>

      </button>

      <ul class="dropdown-menu pull-right" role="menu">

        <li onclick="getstep('<?php echo $action?>','<?php echo $id; ?>');">

          <a href="#">

            <i class="icon-refresh"></i> Refresh</a>

          </li>

        </ul>

      </div>

    </div>

  </div>
  <div class="row">

    <div class="col-md-12" id="patient_sub_page">

      <!-- BEGIN Portlet PORTLET-->

      <div class="portlet light bordered" style="min-height: 400px;">

       <div class="portlet-title">

        <div class="caption">

          <span class="caption-subject bold uppercase font-dark">LIST OF <?php echo $patients;?> </span>

        </div>

      </div>

      <div class="portlet-body" id="patient_list_wrapper">

       <div id="datatable_products_wrapper" class="dataTables_wrapper dataTables_extended_wrapper no-footer">

         <div class="row">

          <div class="col-md-7 col-sm-12">

            <div class="dataTables_paginate paging_bootstrap_extended" id="datatable_products_paginate">

              <div class="pagination-panel"> Page 

                <a onClick="getpage('<?php echo $action?>','1','<?php echo $totalRows_rsodest; ?>','<?php echo $id;?>','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default prev" title="First"><i class="fa fa-angle-double-left"></i></a>

                <a onClick="getpage('<?php echo $action?>','<?php echo ($pagenum-1); ?>','<?php echo $totalRows_rsodest; ?>','<?php echo $id;?>','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default prev" title="Prev"><i class="fa fa-angle-left"></i></a>

                <input readonly="" value="<?php echo $pagenum; ?>" type="text" class="pagination-panel-input form-control input-mini input-inline input-sm" maxlenght="5" style="text-align:center; margin: 0 5px;">

                <a onClick="getpage('<?php echo $action?>','<?php echo ($pagenum+1); ?>','<?php echo $totalRows_rsodest; ?>','<?php echo $id;?>','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default next" title="Next"><i class="fa fa-angle-right"></i></a> 

                <a onClick="getpage('<?php echo $action?>','<?php echo $total_pages; ?>','<?php echo $totalRows_rsodest; ?>','<?php echo $id;?>','<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>')" href="#" class="btn btn-sm default next" title="Next"><i class="fa fa-angle-double-right"></i></a> 

                of <span class="pagination-panel-total"><?php echo $total_pages;?></span>

              </div>

            </div>

            <div class="dataTables_length" id="datatable_products_length">

             <form name="form" id="form" method="post">

              <label> 

                <span class="seperator">|</span>View By 

                <select onchange="getstep('<?php echo $action?>','<?php echo $id; ?>');" name="datatable_products_length" id="datatable_products_length" aria-controls="datatable_products" class="form-control input-xsmall input-sm input-inline">

                  <option <?php if($pagerows == 10){echo 'selected=""';} ?> value="10">10</option>

                  <option <?php if($pagerows == 20){echo 'selected=""';} ?> value="20">20</option>

                  <option <?php if($pagerows == 50){echo 'selected=""';} ?> value="50">50</option>

                  <option <?php if($pagerows == 100){echo 'selected=""';} ?> value="100">100</option>

                  <option <?php if($pagerows == 150){echo 'selected=""';} ?> value="150">150</option>

                </select> 

              records</label>

              <span class="seperator">|</span> Found : <b><?php echo $totalRows_rsodest; ?> records </b> 

            </form>

          </div>

        </div>

        <div class="col-md-5 col-sm-12">

          <div class="table-group-actions pull-right">

            <form name="form" id="form" method="post" action="" onsubmit="getstep('list_visits_optical_search','<?php echo $id; ?>'); return false;">

              <input type="text" name="searchtext" id="searchtext" class="table-group-action-input form-control input-inline input-small input-sm hide_all" style="display: <?php if($typesearch!="filter"){echo "";}else{ ?> none <?php } ?>;">

              <div id="searchtext_2" class="table-group-action-input hide_all" style="display: <?php if($typesearch=="filter"){echo "";}else{ ?> none <?php } ?>;">

               <input readonly="" required="" id="form_start_date" name="start_date" type="text" class="form-control date_time_picker" placeholder="Start Date">

               <input readonly="" required="" id="form_end_date" name="end_date" type="text" class="form-control date_time_picker" placeholder="End Date">

             </div>

             <select onchange="$('.hide_all').hide();if(this.value=='filter'){$('#searchtext_2').show()}else{$('#searchtext').show()}" class="table-group-action-input form-control input-inline input-small input-sm" name="typesearch" id="typesearch">

              <option value="">Select...</option>

              <?php if($action!='list_guest_patients'){ ?>

               <option value="patientid" <?php if($typesearch=="patientid" ||($typesearch=='')){echo "selected='selected'";}?>>Patient ID/Old Card No.</option>

               <option value="member_no"  <?php if(($typesearch=="member_no")){echo "selected='selected'";}?>>Insurance/Staff No.</option> <?php } ?>

               <option value="surname"  <?php if($typesearch=="surname"){echo "selected='selected'";}?>>Surname</option>

               <option value="othernames" <?php if($typesearch=="othernames"){echo "selected='selected'";}?>>Othernames</option>

               <option value="filter" <?php if($typesearch=="filter"){echo "selected='selected'";}?>>Filter By Date</option>

             </select>

             <button class="btn btn-sm green table-group-action-submit"><i class="fa fa-search"></i> Search </button>

           </form>

         </div>

       </div>

     </div>     

   </div>

   <br>

   <?php

   if($totalRows_rsodest > 0){

     do {

       list($row_rsodest['picpath'], $row_rsodest['insurance_no1'], $row_rsodest['insurance_company1']) = patient_sub_info($row_rsodest['patient_no'], $courier);

       $picpath="../".FOLDER_NAME.$row_rsodest['picpath']; 

       if(!$picpath){ 

         $picpath="../".FOLDER_NAME."images/profile.gif";	

       }?>  

       <?php  $medical_note_id = 0; ?>

       <div class="mt-comments" style="cursor: pointer;" onclick="process_information('view_medical_history_details','<?php echo $row_rsodest['patient_no'].'@@@'. $row_rsodest['patient_id'];?>', '<?php echo $row_rsodest['process_claim_no'];?>', 'currentarea','<?php echo $medical_note_id;?>');">

        <?php if($id == 'incoming_patients'){ ?>

          <div class="mt-comment" style="background-color: #FAFAFA;border: 1px solid #eee;">

            <div class="mt-comment-img">

              <img style="width:40px;height: 40px;" src="<?php echo $picpath;?>"> 

            </div>

            <div class="mt-comment-body">

              <div class="mt-comment-info">

                <span class="mt-comment-author"> <?php echo strtoupper($row_rsodest['title'].' '.$row_rsodest['surname']." ".$row_rsodest['othernames']); ?> - ( <?php echo $row_rsodest['patient_no'];?> ) </span>

                <span class="mt-comment-date"><?php echo timeAgo($row_rsodest['datetimeadded']).' ago'; ?></span>

              </div>

              <div class="mt-comment-text"> VISIT NO : <?php echo $row_rsodest['process_claim_no'];?> </div>

              <div class="mt-comment-details">

                <span class="mt-comment-status mt-comment-status-pending">

                  <?php if($row_rsodest['status']=='In Use'){$statuscolor='green';}if($row_rsodest['status']=='Submitted'){$statuscolor='red';}echo "<span style='color:$statuscolor; font-weight:bold;'>".strtoupper($row_rsodest['status'])."</span>";?>

                </span><ul class="mt-comment-actions" style="display: block"><li><a href="#">View</a></li></ul>

              </div>

            </div>

          </div>

        <?php } else if($id == 'discharged'||$id=='discharged_review'){ ?>

          <div class="mt-comment" style="background-color: #FAFAFA;border: 1px solid #eee;">

            <div class="mt-comment-img">

              <img style="width:40px;height: 40px;" src="<?php echo $picpath;?>"> 

            </div>

            <div class="mt-comment-body">

              <div class="mt-comment-info">

                <span class="mt-comment-author"> <?php echo strtoupper($row_rsodest['title'].' '.$row_rsodest['surname']." ".$row_rsodest['othernames']); ?> - ( <?php echo $row_rsodest['patient_no'];?> ) </span>

                <span class="mt-comment-date"><?php echo timeAgo($row_rsodest['action_date'].' '.$row_rsodest['action_time']).' ago'; ?></span>

              </div>

              <div class="mt-comment-text"> VISIT NO : <?php echo $row_rsodest['process_claim_no'];?> </div>

              <div class="mt-comment-details">

                <?php if($id == 'discharged_review'){ ?>

                  <span class="mt-comment-status mt-comment-status-approved">

                    <?php 

                    $today = strtotime(date('Y-m-d'));

                    $review_date = strtotime($row_rsodest['review_date']);

                    if($today < $review_date){echo 'REVIEW IN '. timeAgo($row_rsodest['review_date']).' Time';

                  }else if($today > $review_date){echo '<span style="color:red;">CLIENT WAS SUPPOSE TO REPORT FOR A REVIEW '. timeAgo($row_rsodest['review_date']).' Ago. </span>';  

                }else if($today == $review_date){echo 'CLIENT HAS A REVIEW TODAY';}

                ?>

              </span>

            <?php }else{ ?>

              <span class="mt-comment-status mt-comment-status-pending">DISCHARGED</span>  

            <?php } ?>

            <ul class="mt-comment-actions" style="display: block"><li><a href="#">View</a></li></ul>

          </div>

        </div>

      </div>

    <?php } else if($id == 'pending_observation'){ ?>

      <div class="mt-comment" style="background-color: #FAFAFA;border: 1px solid #eee;">

        <div class="mt-comment-img">

          <img style="width:40px;height: 40px;" src="<?php echo $picpath;?>"> 

        </div>

        <div class="mt-comment-body">

          <div class="mt-comment-info">

            <span class="mt-comment-author"> <?php echo strtoupper($row_rsodest['title'].' '.$row_rsodest['surname']." ".$row_rsodest['othernames']); ?> - ( <?php echo $row_rsodest['patient_no'];?> ) </span>

            <span class="mt-comment-date"><?php echo timeAgo($row_rsodest['action_date'].' '.$row_rsodest['action_time']).' ago'; ?></span>

          </div>

          <div class="mt-comment-text"> VISIT NO : <?php echo $row_rsodest['process_claim_no'];?> </div>

          <div class="mt-comment-text"> REASON : <?php echo $row_rsodest['reason_for_detention'];?> </div>

          <div class="mt-comment-details">

            <span class="mt-comment-status mt-comment-status-approved">DETAINED FOR OBSERVATION<?php echo $row_rsodest['referred_hospital']?></span>  

            <ul class="mt-comment-actions" style="display: block"><li><a href="#">View</a></li></ul>

          </div>

        </div>

      </div>

    <?php } else if($id == 'pending_diagnostic'){ ?>

      <div class="mt-comment" style="background-color: #FAFAFA;border: 1px solid #eee;">

        <div class="mt-comment-img">

          <img style="width:40px;height: 40px;" src="<?php echo $picpath;?>"> 

        </div>

        <div class="mt-comment-body">

          <div class="mt-comment-info">

            <span class="mt-comment-author"> <?php echo strtoupper($row_rsodest['title'].' '.$row_rsodest['surname']." ".$row_rsodest['othernames']); ?> - ( <?php echo $row_rsodest['patient_no'];?> ) </span>

            <span class="mt-comment-date"><?php echo timeAgo($row_rsodest['action_date'].' '.$row_rsodest['action_time']).' ago'; ?></span>

          </div>

          <div class="mt-comment-text"> VISIT NO : <?php echo $row_rsodest['process_claim_no'];?> </div>

          <div class="mt-comment-details">

            <span class="mt-comment-status mt-comment-status-approved">AWAITING DIAGNOSTIC RESULTS</span>  

            <ul class="mt-comment-actions" style="display: block"><li><a href="#">View</a></li></ul>

          </div>

        </div>

      </div>

    <?php }  ?>   

  </div>

<?php }  while ($row_rsodest = mysql_fetch_assoc($rsodest));

} else{ ?>

 <div class="mt-comments" style="cursor: pointer;">

  <div class="mt-comment" style="background-color: #FAFAFA;border: 1px solid #eee;">

    <div class="mt-comment-img"></div>

    <div class="mt-comment-body">

      <div class="mt-comment-info">

        <span class="mt-comment-author" style="color: red;"> NO CLIENTS FOUND </span>

        <span class="mt-comment-date"></span>

      </div>

      <div class="mt-comment-text"></div>

      <div class="mt-comment-details"></div>

    </div>

  </div>

</div>

<?php  } ?>



</div>

</div>

</div>

</div>







