<?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"];
}

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

if($searchtxt==''){
$query_rsodest = "SELECT * FROM stock_items WHERE quantity_available <= min_re_order_qty AND branch_id = '" . $_SESSION['branch_id'] . "' order by item";	
}

if ($searchtxt != '') {
    if ($typesearch == 'name') {
        $query_rsodest = "SELECT * FROM stock_items WHERE quantity_available <= min_re_order_qty AND item like '$searchtxt%' AND branch_id = '" . $_SESSION['branch_id'] . "' order by item";
    } if ($typesearch == 'price') {
        $query_rsodest = "SELECT * FROM stock_items WHERE quantity_available <= min_re_order_qty AND price = '$searchtxt' AND branch_id = '" . $_SESSION['branch_id'] . "' order by item";
    } if ($typesearch == 'qty_avail') {
        $query_rsodest = "SELECT * FROM stock_items WHERE quantity_available <= min_re_order_qty AND quantity_available = '$searchtxt' AND branch_id = '" . $_SESSION['branch_id'] . "' order by item";
    } if ($typesearch == 'item_category') {
        $query_rsodest = "SELECT * FROM stock_items WHERE quantity_available <= min_re_order_qty AND service = '$searchtxt' AND branch_id = '" . $_SESSION['branch_id'] . "' order by item";
    }
}

$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>Home</a>
                                <i class="fa fa-angle-right"></i>
                            </li>
                            <li>
                                <span>Stores</span>
                                <i class="fa fa-angle-right"></i>
                            </li>
                            <li>
                                <span>List of Items [Re-Order] </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; ?>');">
                                        <a href="#">
                                            <i class="icon-refresh"></i> Refresh</a>
                                    </li>
                                    <li onclick="exportReOrderItems('stores/export_items_re_order.php');">
                                        <a href="#">
                                            <i class="fa fa-download"></i> Export Items </a>
                                    </li>
                                    <li>
                                        <a data-toggle="modal" href="#large">
                                            <i class="fa fa-upload"></i> Import Items </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 ITEMS THAT NEED REPLENISHMENT </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('<?php echo $action; ?>');" 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 FROM service_items WHERE branch_id = '".$_SESSION['branch_id']."'");
                                             while ($row = mysql_fetch_assoc($qry)){ ?>
                                             <option value="<?php echo $row['service'];?>"><?php echo $row['service'];?></option>
                                            <?php }?>
                                        </select>
                                        <select class="table-group-action-input form-control input-inline input-small input-sm" name="typesearch" id="typesearch" onchange="$('.hide_all').hide();if(this.value=='item_category'){$('#searchtext_2').show()}else{$('#searchtext').show()}">
                                             <option value="">Select...</option>
                                             <option value="name"  <?php if($typesearch=="name"){echo "selected='selected'";}?>>Item Name</option>
                                             <option value="price"  <?php if($typesearch=="price"){echo "selected='selected'";}?>>Item Price</option>
                                             <option value="qty_avail"  <?php if($typesearch=="qty_avail"){echo "selected='selected'";}?>>Quantity Available</option>
                                             <option value="item_category" <?php if($typesearch=="item_category"){echo "selected='selected'";}?>>Item Category</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>ITEM NAME</th>
                                            <th>Rx CLAIM NAME</th>
                                            <th>ITEM CATEGORY</th>
                                            <th>PRICE</th>
                                            <th>MIN RE-ORDER QTY.</th>
                                            <th>RE-ORDER QTY.</th>
                                            <th>QUANTITY AVAIL.</th>
                                            <th style="text-align: center;">ACTION</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                       
                                         <?php if($totalRows_rsodest>0){ 
                                                    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="getpage('edit_item_details_procurement',<?php echo $start_row-1; ?>,<?php echo $totalRows_rsodest; ?>,<?php echo $row_rsodest['id']; ?>,'<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>');"><?php echo strtoupper($row_rsodest['item']); ?></td>
                  <td valign="top" style="cursor: pointer;" onclick="getpage('edit_item_details_procurement',<?php echo $start_row-1; ?>,<?php echo $totalRows_rsodest; ?>,<?php echo $row_rsodest['id']; ?>,'<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>');"><?php echo strtoupper($row_rsodest['ins_item_code']); ?></td>
                  <td valign="top" style="cursor: pointer;" onclick="getpage('edit_item_details_procurement',<?php echo $start_row-1; ?>,<?php echo $totalRows_rsodest; ?>,<?php echo $row_rsodest['id']; ?>,'<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>');"><?php echo strtoupper($row_rsodest['service']); ?></td>
                  <td valign="top" style="cursor: pointer;" onclick="getpage('edit_item_details_procurement',<?php echo $start_row-1; ?>,<?php echo $totalRows_rsodest; ?>,<?php echo $row_rsodest['id']; ?>,'<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>');"><?php echo 'GHS'. number_format($row_rsodest['price'],2); ?></td>
                  <td valign="top" style="cursor: pointer;text-align: right;" onclick="getpage('edit_item_details_procurement',<?php echo $start_row-1; ?>,<?php echo $totalRows_rsodest; ?>,<?php echo $row_rsodest['id']; ?>,'<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>');"><?php echo $row_rsodest['min_re_order_qty']; ?></td>
                  <td valign="top" style="cursor: pointer;text-align: right;" onclick="getpage('edit_item_details_procurement',<?php echo $start_row-1; ?>,<?php echo $totalRows_rsodest; ?>,<?php echo $row_rsodest['id']; ?>,'<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>');"><?php echo $row_rsodest['re_order_qty']; ?></td>
                  <td valign="top" style="cursor: pointer;text-align: right;" onclick="getpage('edit_item_details_procurement',<?php echo $start_row-1; ?>,<?php echo $totalRows_rsodest; ?>,<?php echo $row_rsodest['id']; ?>,'<?php echo $typesearch; ?>','<?php echo $searchtxt; ?>');"><?php echo $row_rsodest['quantity_available']; ?></td>
                  <td valign="top" style="cursor: pointer;text-align: center;"><span style="font-weight:bold; text-align:center;"> <a style="color:blue" href="#" onclick="getstep('edit_item_details_procurement',<?php echo $row_rsodest['id']; ?>);"> <i class="icon-info"></i> </a> </span></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>
                            </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('<?php echo $action; ?>');" 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="large" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <form name="form" id="form" class="form-horizontal" action="stores/import_re_order_items.php" method="POST" enctype="multipart/form-data">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">Item Importation & Quantity Top Up</h4>
            </div>
             
            <div class="modal-body">
                <table class="table">
                    <tr>
                        <td>Upload Excel File</td>
                        <td colspan="3"><input type="file" name="importfile_items" id="importfile_items"></td>
                    </tr>
                    <tr>
                        <td>Item Code</td>
                        <td><input name="item_code" id="item_code" class="form-control" value="B"></td>
                        <td>Item Name</td>
                        <td><input name="item_name" id="item_name" class="form-control" value="C"></td>
                    </tr>
                    <tr>
                        <td>Item Category</td>
                        <td><input name="item_category" id="item_category" class="form-control" value="F"></td>
                        <td>Item Price</td>
                        <td><input name="item_price" id="item_price" class="form-control" value="G"></td>
                    </tr>
                    <tr>
                        <td>Min-Re-Order Qty</td>
                        <td><input name="min_re_order_qty" id="min_re_order_qty" class="form-control" value="H"></td>
                        <td>Re-Order Qty</td>
                        <td><input name="re_order_qty" id="re_order_qty" class="form-control" value="I"></td>
                    </tr>
                    <tr>
                        <td>Quantity Ordered</td>
                        <td><input name="qty_ordered" id="qty_ordered" class="form-control" value="K"></td>
                        <td colspan="2"></td>
                    </tr>
                    <tr>
                        <td>Start Row</td>
                        <td><input name="start_row" id="start" class="form-control" value="8"></td>
                        <td>End Row</td>
                        <td><input name="end_row" id="end" class="form-control"></td>
                    </tr>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn dark btn-outline" data-dismiss="modal">Close</button>
                <button type="submit" class="btn green">Import Items</button>
            </div>
        </div>
        </form>
    </div>
</div>