<?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"];
$datatable_products_length = $_POST['datatable_products_length'];
if(empty($datatable_products_length)){$datatable_products_length = 50;}
if ($typesearch == 'item_category') {
 $searchtxt = $_POST["searchtext_2"];
}

if (($searchtxt == '' || $searchtxt == 'undefined')) {
    $query_rsodest = "SELECT c.*,c.job_number AS main_job_number,g.lens_id_count,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,a.patient_id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew,j.* FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no LEFT 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 j.status='open'  AND a.patient_status = 'In Use'  AND g.item_service_type='Optical Lens' AND payment_status='Paid' AND po_status is null GROUP BY c.job_number ORDER BY j.id DESC";
}

if ($searchtxt != '') {
    if ($typesearch == 'job_no') {
        $query_rsodest = "SELECT c.*,c.job_number AS main_job_number,g.lens_id_count,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,a.patient_id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew,j.* FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no LEFT 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 j.status='open'  AND a.patient_status = 'In Use' AND c.job_number = '$searchtxt'  AND g.item_service_type='Optical Lens' AND payment_status='Paid' AND po_status is null GROUP BY c.job_number ORDER BY j.id DESC";
    }
    if ($typesearch == 'claim_no') {
        $query_rsodest = "SELECT c.*,c.job_number AS main_job_number,g.lens_id_count,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,a.patient_id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew,j.* FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no LEFT 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 j.status='open'  AND a.patient_status = 'In Use' AND a.process_claim_no like '%$searchtxt%'  AND g.item_service_type='Optical Lens' AND payment_status='Paid' AND po_status is null GROUP BY c.job_number ORDER BY j.id DESC";
    }

    if ($typesearch == 'patientid') {
        $query_rsodest = "SELECT c.*,c.job_number AS main_job_number,g.lens_id_count,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,a.patient_id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew,j.* FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no LEFT 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 j.status='open'  AND a.patient_status = 'In Use' AND b.card_number='$searchtxt'  AND g.item_service_type='Optical Lens' AND payment_status='Paid' AND po_status is null GROUP BY c.job_number ORDER BY j.id DESC";
    }
    if ($typesearch == 'surname') {
        $query_rsodest = "SELECT c.*,c.job_number AS main_job_number,g.lens_id_count,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,a.patient_id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew,j.* FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no LEFT 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 j.status='open'  AND a.patient_status = 'In Use' AND b.surname like '$searchtxt%'  AND g.item_service_type='Optical Lens' AND payment_status='Paid' AND po_status is null GROUP BY c.job_number ORDER BY j.id DESC";
    }
    if ($typesearch == 'othernames') {
        $query_rsodest = "SELECT c.*,c.job_number AS main_job_number,g.lens_id_count,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,a.patient_id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew,j.* FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no LEFT 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 j.status='open'  AND a.patient_status = 'In Use' AND b.othernames like '%$searchtxt%'  AND g.item_service_type='Optical Lens' AND payment_status='Paid' AND po_status is null GROUP BY c.job_number ORDER BY j.id DESC";
    }
    if ($typesearch == 'branch_name') {
        $query_rsodest = "SELECT c.*,c.job_number AS main_job_number,g.lens_id_count,a.process_claim_no,a.patient_status,a.mode_of_payment,a.datetimeadded,a.patient_id as patient_id,DATE_FORMAT(a.datetimeadded,'%e %b %Y %r') AS datenew,j.* FROM job_numbers c left join process_claim_no a on c.process_claim_no=a.process_claim_no LEFT 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 j.status='open'  AND a.patient_status = 'In Use' AND a.branch_id = '$searchtxt'  AND g.item_service_type='Optical Lens' AND payment_status='Paid' AND po_status is null GROUP BY c.job_number ORDER BY j.id 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;
}

$suppliers = mysql_query("SELECT * FROM suppliers");
while($s_rows = mysql_fetch_assoc($suppliers)){
    $s_options.= '<option value="'.$s_rows['id'].'">'.$s_rows['supplier'].'</option>';
} ?>


<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 id="refresh_btn" onclick="getstep('list_of_lens_prescription');">
                                        <a href="#">
                                            <i class="icon-refresh"></i> Refresh</a>
                                    </li>
                                    <li onclick="getstep('add_item_details');">
                                        <a href="#">
                                            <i class="icon-plus"></i> Add New Item </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 ALL LENS RECEIVED </span>
                </div>

                <div class="actions">
                    <a class="btn red" onclick="generateLensPO('generate_all_purchase_order','0','Generate Purchase Order', 'Are you sure you want to generate purchase order', 'Generate Purchase Order');return false;"> Generate Purchase Order <i class="fa fa-check"></i> </a>
                </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('list_of_all_items_procurement','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('list_of_all_items_procurement','<?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('list_of_all_items_procurement','<?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('list_of_all_items_procurement','<?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('list_of_all_items_procurement');" 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('<?php echo $action; ?>'); 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!="item_category"){echo "";}else{ ?> none <?php } ?>;">
                                        <select class="table-group-action-input form-control input-inline input-small input-sm hide_all" name="searchtext_2" id="searchtext_2" style="display: <?php if($typesearch=="item_category"){echo "";}else{ ?> none <?php } ?>;">
                                             <option value="">Select Category</option>
                                             <?php $qry = mysql_query("SELECT DISTINCT service_type FROM stock_items WHERE branch_id = '".$_SESSION['branch_id']."'");
                                             while ($row = mysql_fetch_assoc($qry)){ ?>
                                             <option value="<?php echo $row['service_type'];?>"><?php echo $row['service_type'];?></option>
                                            <?php }?>
                                        </select>
                                        <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="job_no" <?php if($typesearch=="job_no" ||($typesearch=='')){echo "selected='selected'";}?>>Job No.</option>
                                                     <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">
                            <form name="" id="">
                                <table class="table table-striped table-bordered dataTable no-footer" id="datatable_products" aria-describedby="datatable_products_info" role="grid">
                                    <thead>
                                        <tr>
                                            <th></th>
                                            <th>SN</th>
                                            <th>CONSULT. DATE</th>
                                            <th>VISIT NO.</th>
                                            <th>PATIENT NAME</th>
                                            <th>PATIENT NO.</th>
                                            <th>PAYMENT STATUS</th>
                                            <th>JOB NO.</th>
                                            <th>LENS PRESCRIPTION</th>
                                            <th style="width:20%;">SUPPLIER</th>
                                        <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                         <?php if($totalRows_rsodest>0){ 
                                                    do { $start_row=$start_row+1;?>
                                                        <tr>
                            <td height="23" valign="top" class="main_job_number_<?php echo str_replace("/","_",$row_rsodest['main_job_number']); ?>">
                                    <input type="hidden" name="item_id_<?php echo $row_rsodest['main_job_number']; ?>" value="<?php echo $row_rsodest['provider_item_id']; ?>">
                                    <input type="hidden" name="process_claim_no_<?php echo $row_rsodest['main_job_number']; ?>" value="<?php echo $row_rsodest['process_claim_no']; ?>">
                                    <label class="mt-checkbox item_check_box_job_no">
                                            <input onchange="valueChanged(this,'<?php echo str_replace('/','_',$row_rsodest['main_job_number']);?>');" type="checkbox" class="item_check_box" multiple="" name="lens_job_no[]" value="<?php echo $row_rsodest['main_job_number']; ?>">
                                        <span></span>
                                    </label>
                            </td>
                            <td height="23" valign="top" class="main_job_number_<?php echo str_replace("/","_",$row_rsodest['main_job_number']); ?>"><span><?php echo $start_row-1; ?></span></td>
                            <td valign="top" style="cursor: pointer;" class="main_job_number_<?php echo str_replace("/","_",$row_rsodest['main_job_number']); ?>"><?php echo date('jS M, Y',  strtotime($row_rsodest['datetimeadded'])); ?></td>
                            <td valign="top" style="cursor: pointer;" class="main_job_number_<?php echo str_replace("/","_",$row_rsodest['main_job_number']); ?>"><?php echo $row_rsodest['process_claim_no']; ?></td>
                            <td onclick="getclaimsdetails('view_items_details','<?php echo $row_rsodest['process_claim_no']; ?>_<?php echo str_replace('/','_',$row_rsodest['main_job_number']);?>');" valign="top" style="cursor: pointer;"><?php list($jo,$ko,$mb, $othernames, $surname) = patient_sub_info($row_rsodest['patient_no'], $courier); echo $othernames .' '. $surname;  ?></td>
                            <td onclick="getclaimsdetails('view_items_details','<?php echo $row_rsodest['process_claim_no']; ?>_<?php echo str_replace('/','_',$row_rsodest['main_job_number']);?>');" valign="top" style="cursor: pointer;"><?php if(!empty($row_rsodest['patient_no'])){ echo strtoupper($row_rsodest['patient_no']);}else{ echo strtoupper($row_rsodest['patient_no']);} ?></td>
                            <td valign="top" style="cursor: pointer;color:green;font-weight:bold;" class="main_job_number_<?php echo str_replace("/","_",$row_rsodest['main_job_number']); ?>"><?php list($lens_prescription, $receipt_no) = getLensPrescription($row_rsodest['process_claim_no'], $row_rsodest['main_job_number']); echo 'Paid'; ?></td>
                            <td valign="top" style="cursor: pointer;" class="main_job_number_<?php echo str_replace("/","_",$row_rsodest['main_job_number']); ?>"><?php echo strtoupper($row_rsodest['main_job_number']); ?></td>
                            <td valign="top" style="cursor: pointer;font-weight:bold" class="main_job_number_<?php echo str_replace("/","_",$row_rsodest['main_job_number']); ?>"> 
                                    <?php 
                                    echo strtoupper($lens_prescription);
                                     ?> 
                                     <i onclick="getSimpleData('pro_lens_prescription_details','<?php echo $row_rsodest['process_claim_no']; ?>_<?php echo $row_rsodest['lens_id_count']; ?>','#lens_pres_modal_content');" data-toggle="modal" href="#lens_pres_modal" style="color:blue;float:right" class="fa fa-info"></i>
                            </td>
                            <td valign="top" style="cursor: pointer;" class="main_job_number_<?php echo str_replace("/","_",$row_rsodest['main_job_number']); ?>">
                                                        <select name="supplier_<?php echo $row_rsodest['main_job_number'];?>" id="" class="form-control">
                                                            <option value="0">Select Supplier</option>
                                                            <?php echo $s_options; ?>
                                                        </select>
                            </td>
                            
                                                        </tr>
                                         <?php } while ($row_rsodest = mysql_fetch_assoc($rsodest)); } else { echo '<tr><td colspan="10" style="color:red;text-align:center;">No Records Found.</td></tr>';} ?>
                                    </tbody>
                                </table>
                                    </form>
                            </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('list_of_all_items_procurement','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('list_of_all_items_procurement','<?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('list_of_all_items_procurement','<?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('list_of_all_items_procurement','<?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('list_of_all_items_procurement');" 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>



            <div class="modal fade bs-modal-lg" id="lens_pres_modal" tabindex="-1" role="dialog" aria-hidden="true">
                                            <div class="modal-dialog modal-lg">
                                                <div class="modal-content">
                                                    <div class="modal-body" id="lens_pres_modal_content"> 


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