<?php error_reporting(0);
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"];
$datatable_products_length = $_POST['datatable_products_length'];
if(empty($datatable_products_length)){$datatable_products_length = 50;}

$membership_no=$_POST["membership_no"];
$insurance_company=$_POST["insurance_company"];
$id=$_POST["id"];


if ($searchtxt == '' && $action == 'cashier_list_visits') {
    $query_rsodest = "SELECT DISTINCT pn.process_claim_no,	
DATE_FORMAT(pn.datetimeadded,'%e %b %Y %r') AS datenew,pn.mode_of_payment,pn.insurance_company,pn. STATUS,pn.typeofvisit,pn.session_status,pn.patient_no,p.id,p.surname,p.othernames,g.id AS idd,g.surname AS surnamee,g.othernames AS othernamess
FROM process_claim_no pn INNER JOIN claims_details b on pn.process_claim_no=b.process_claim_no LEFT JOIN patient_information p ON pn.patient_no = p.card_number LEFT JOIN guest_patients g ON pn.patient_no = g.card_number
WHERE pn. STATUS = 'In Use' AND pn.branch_id='".$_SESSION['branch_id']."' AND pn.session_status = 'Cashier' AND b.current_location='Cashier' AND (b.mode_of_payment='cash' OR b.mode_of_payment='credit sales') ORDER BY pn.datetimeadded DESC";
//  and (b.payment_status='Owing' or b.payment_status='Approved')
}

if ($searchtxt != '' && $action == 'cashier_list_visits') {
    if ($typesearch == 'surname') {
        $query_rsodest = "SELECT DISTINCT pn.process_claim_no,	
DATE_FORMAT(pn.datetimeadded,'%e %b %Y %r') AS datenew,pn.mode_of_payment,pn.insurance_company,pn. STATUS,pn.typeofvisit,pn.session_status,pn.patient_no,p.id,p.surname,p.othernames,g.id AS idd,g.surname AS surnamee,g.othernames AS othernamess
FROM process_claim_no pn INNER JOIN claims_details b on pn.process_claim_no=b.process_claim_no LEFT JOIN patient_information p ON pn.patient_no = p.card_number LEFT JOIN guest_patients g ON pn.patient_no = g.card_number
WHERE pn. STATUS = 'In Use' AND pn.branch_id='".$_SESSION['branch_id']."' AND pn.session_status = 'Cashier' AND b.current_location='Cashier' AND (b.mode_of_payment='cash' OR b.mode_of_payment='credit sales') AND (p.surname like '$searchtxt%' or g.surname like '$searchtxt%') ORDER BY pn.datetimeadded DESC";
    }
    if ($typesearch == 'othernames') {
        $query_rsodest = "SELECT DISTINCT pn.process_claim_no,	
DATE_FORMAT(pn.datetimeadded,'%e %b %Y %r') AS datenew,pn.mode_of_payment,pn.insurance_company,pn. STATUS,pn.typeofvisit,pn.session_status,pn.patient_no,p.id,p.surname,p.othernames,g.id AS idd,g.surname AS surnamee,g.othernames AS othernamess
FROM process_claim_no pn INNER JOIN claims_details b on pn.process_claim_no=b.process_claim_no LEFT JOIN patient_information p ON pn.patient_no = p.card_number LEFT JOIN guest_patients g ON pn.patient_no = g.card_number
WHERE pn. STATUS = 'In Use' AND pn.branch_id='".$_SESSION['branch_id']."' AND pn.session_status = 'Cashier' AND b.current_location='Cashier' AND (b.mode_of_payment='cash' OR b.mode_of_payment='credit sales') AND  (p.othernames like '$searchtxt%' or g.othernames like '$searchtxt%') ORDER BY pn.datetimeadded DESC";
    }

    if ($typesearch == 'patientid' || $typesearch == 'oldcardno') {
        $query_rsodest = "SELECT DISTINCT pn.process_claim_no,	
DATE_FORMAT(pn.datetimeadded,'%e %b %Y %r') AS datenew,pn.mode_of_payment,pn.insurance_company,pn. STATUS,pn.typeofvisit,pn.session_status,pn.patient_no,p.id,p.surname,p.othernames,g.id AS idd,g.surname AS surnamee,g.othernames AS othernamess
FROM process_claim_no pn INNER JOIN claims_details b on pn.process_claim_no=b.process_claim_no LEFT JOIN patient_information p ON pn.patient_no = p.card_number LEFT JOIN guest_patients g ON pn.patient_no = g.card_number
WHERE pn. STATUS = 'In Use' AND pn.branch_id='".$_SESSION['branch_id']."' AND pn.session_status = 'Cashier' AND b.current_location='Cashier' AND (b.mode_of_payment='cash' OR b.mode_of_payment='credit sales') AND (p.card_number like '$searchtxt%' or p.old_card_no like '$searchtxt%' or g.card_number like '$searchtxt%') ORDER BY pn.datetimeadded DESC";
    }

    if ($typesearch == 'member_no') {
        $query_rsodest = "SELECT DISTINCT pn.process_claim_no,	
DATE_FORMAT(pn.datetimeadded,'%e %b %Y %r') AS datenew,pn.mode_of_payment,pn.insurance_company,pn. STATUS,pn.typeofvisit,pn.session_status,pn.patient_no,p.id,p.surname,p.othernames,g.id AS idd,g.surname AS surnamee,g.othernames AS othernamess
FROM process_claim_no pn INNER JOIN claims_details b on pn.process_claim_no=b.process_claim_no LEFT JOIN patient_information p ON pn.patient_no = p.card_number LEFT JOIN guest_patients g ON pn.patient_no = g.card_number
WHERE pn. STATUS = 'In Use' AND pn.branch_id='".$_SESSION['branch_id']."' AND pn.session_status = 'Cashier' AND b.current_location='Cashier' AND (b.mode_of_payment='cash' OR b.mode_of_payment='credit sales') AND insurance_no  like '$searchtxt%' ORDER BY pn.datetimeadded DESC";
    }
}

$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('cashier_list_visits');">
                                        <a href="#">
                                            <i class="icon-refresh"></i> Refresh</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
</div>

<div class="row">
    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
        <div class="portlet light ">
            <div class="portlet-title">
                <div class="caption">
                    <span class="caption-subject bold uppercase font-dark">LIST OF PATIENTS (CASH PAYMENTS)</span>
                </div>
            </div>
            <div class="portlet-body form">
                    <div class="table-container">
                        <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 $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 $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 $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 $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('cashier_list_visits');" 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('cashier_list_visits'); return false;">
                                        <input type="text" name="searchtext" id="searchtext" class="table-group-action-input form-control input-inline input-small input-sm">
                                        <select 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>
                                        </select>
                                        <button class="btn btn-sm green table-group-action-submit"><i class="fa fa-search"></i> Search </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="table-scrollable">
                                <table class="table table-striped table-bordered table-hover dataTable no-footer" id="datatable_products" aria-describedby="datatable_products_info" role="grid">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>DATE TIME</th>
                                            <th>VISIT NO.</th>
                                            <th>PATIENT NAME</th>
                                            <th>PATIENT NO.</th>
                                            <th>MODE OF PAYMENT</th>
                                            <th>CURRENT LOCATION</th>
                                            <th>CLIENT</th>
                                            <th style="text-align: center;">ACTION</th>
                                        </tr>
                                    </thead>
                                       <tbody>
                                                    <?php
                                                    do {
                                                        $start_row=$start_row+1;?>
                                                        <tr>
                  <td height="23" valign="top" class="count"><span><?php echo $start_row-1; ?></span></td>
                  <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_payments','<?php echo $row_rsodest['patient_no'].'@@@'.$row_rsodest['id'].'@@@'.$row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');"><?php echo strtoupper($row_rsodest['datenew']); ?></td>
                  <td valign="top" onclick="getstep('view_patient_payments','<?php echo $row_rsodest['patient_no'].'@@@'.$row_rsodest['id'].'@@@'.$row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');" style="cursor: pointer;"><?php echo strtoupper($row_rsodest['process_claim_no']); ?></td>
                  <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_payments','<?php echo $row_rsodest['patient_no'].'@@@'.$row_rsodest['id'].'@@@'.$row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');"><?php echo strtoupper($row_rsodest['surname']." ".$row_rsodest['othernames']). strtoupper($row_rsodest['surnamee']." ".$row_rsodest['othernamess']); ?></td>
                  <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_payments','<?php echo $row_rsodest['patient_no'].'@@@'.$row_rsodest['id'].'@@@'.$row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');"><?php echo strtoupper($row_rsodest['patient_no']); ?></td>
                  <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_payments','<?php echo $row_rsodest['patient_no'].'@@@'.$row_rsodest['id'].'@@@'.$row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');"><?php
				  
				  if($row_rsodest['mode_of_payment']=='insurance' || $row_rsodest['mode_of_payment']=='company'){
				   echo getinsurance($row_rsodest['insurance_company']);
				   
				  }else{
					  echo strtoupper($row_rsodest['mode_of_payment']);
				  }?></td>
                  <td valign="top" onclick="getstep('view_patient_payments','<?php echo $row_rsodest['patient_no'].'@@@'.$row_rsodest['id'].'@@@'.$row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');" style="cursor: pointer;"><?php if($row_rsodest['session_status']=='cashier'){
echo "ACCOUNTS"; 
}else if($row_rsodest['session_status']==''){
echo "CONSULTING ROOM"; 	
}else{
echo strtoupper($row_rsodest['session_status']); 
}?></td>
                  <td valign="top" style="cursor: pointer;" onclick="getstep('view_patient_payments','<?php echo $row_rsodest['patient_no'].'@@@'.$row_rsodest['id'].'@@@'.$row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');"><?php echo strtoupper($row_rsodest['typeofvisit']); ?></td>
                  <td valign="top" style="cursor: pointer;text-align: center;" onclick="getstep('view_patient_payments','<?php echo $row_rsodest['patient_no'].'@@@'.$row_rsodest['id'].'@@@'.$row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');"><span style="font-weight:bold;"> <a style="color:blue" href="#" onclick="getstep('view_patient_visits','<?php echo $row_rsodest['process_claim_no']; ?>','<?php echo $row_rsodest['typeofvisit']; ?>');"><i class="icon-info"></i></a> </span></td>
                   </tr>
                  <?php } while ($row_rsodest = mysql_fetch_assoc($rsodest)); ?>
                  </tbody>
                                </table>
                            </div>
                            
                            <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 $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 $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 $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 $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">
                                        <label> 
                                            <span class="seperator">|</span>View By 
                                            <select onchange="getstep('cashier_list_visits');" name="datatable_products_length_2" id="datatable_products_length_2" 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> </div>
                                </div>
                                <div class="col-md-5 col-sm-12"></div>
                            </div>
                        </div>
                    </div>

            </div>
        </div>
    </div>
</div>


