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

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

if ($idd2 == '') {
  $idd2 = 'All';
  $status = "WHERE (a.supplier='".$_SESSION['branch_id']."' or a.delivery_location='".$_SESSION['branch_id']."')";
} else if ($idd2 == 'All') {
  $idd2 = 'All';
  $status = "WHERE (a.supplier='".$_SESSION['branch_id']."' or a.delivery_location='".$_SESSION['branch_id']."')";
} else if ($idd2 == 'Received') {
  $status = "WHERE (a.order_status_from='Accepted With No Rejections' OR a.order_status_from='Accepted With Rejections' OR a.order_status='Received') AND (a.supplier='".$_SESSION['branch_id']."' or a.delivery_location='".$_SESSION['branch_id']."')";
} else if ($idd2 == 'Supplied') {
  $status = "WHERE (a.order_status='In-Transit' OR a.order_status_from='Accepted With No Rejections' OR a.order_status_from='Accepted With Rejections') AND (a.supplier='".$_SESSION['branch_id']."' or a.delivery_location='".$_SESSION['branch_id']."')";
} else if($idd2=='Approved'){
  $status = "WHERE (a.order_status='Approved' OR a.order_status_from='Approved') AND (a.supplier='".$_SESSION['branch_id']."' or a.delivery_location='".$_SESSION['branch_id']."')";
} else if($idd2=='Unapproved'){
  $status = "WHERE (a.order_status='Unapproved' OR a.order_status='Awaiting' OR order_status_from='Awaiting' OR a.order_status_from='Unapproved') AND (a.supplier='".$_SESSION['branch_id']."' or a.delivery_location='".$_SESSION['branch_id']."')";
} else {
  $status = "WHERE a.order_status='$idd2' AND (a.supplier='".$_SESSION['branch_id']."' or a.delivery_location='".$_SESSION['branch_id']."')";
}

if ($typesearch == 'name' && $searchtxt != '') {
  $query_rsodest = "SELECT date_format(a.date_of_order,'%e %M %Y') as purchase_order_date, date_format(a.expected_date,'%e %M %Y') as purchase_expect_date, date_format(a.supplied_date,'%e %M %Y') as purchase_recieve_date, b.branch_name as supplier_name, c.branch_name as deliver_to, a.* FROM requisition_orders a LEFT JOIN branches b on a.supplier=b.id LEFT JOIN branches c on a.delivery_location=c.id left join purchase_order_details d on a.purchase_order_no = d.purchase_order_no $status AND d.item like '%$searchtxt%' AND a.order_status_from = 'Store Return' order by date_of_order DESC";
    
} else if ($typesearch == 'supplier' && $searchtxt != '') {
  $query_rsodest = "SELECT date_format(a.date_of_order,'%e %M %Y') as purchase_order_date, date_format(a.expected_date,'%e %M %Y') as purchase_expect_date, date_format(a.supplied_date,'%e %M %Y') as purchase_recieve_date, b.branch_name as supplier_name, c.branch_name as deliver_to, a.* FROM requisition_orders a LEFT JOIN branches b on a.supplier=b.id LEFT JOIN branches c on a.delivery_location=c.id $status AND b.branch_name like '%$searchtxt%' AND a.order_status_from = 'Store Return'order by date_of_order DESC";
} else {
  $query_rsodest = "SELECT date_format(a.date_of_order,'%e %M %Y') as purchase_order_date, date_format(a.expected_date,'%e %M %Y') as purchase_expected_date, date_format(a.supplied_date,'%e %M %Y') as purchase_received_date, b.branch_name as supplier_name, c.branch_name as deliver_to, a.* FROM requisition_orders a LEFT JOIN branches b on a.supplier=b.id LEFT JOIN branches c on a.delivery_location=c.id $status AND a.order_status_from = 'Store Return' order by date_of_order 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('list_requisition_orders','','<?php echo $idd2; ?>');">
          <a href="#">
            <i class="icon-refresh"></i> Refresh 
          </a>
        </li>
        <li onclick="getstep('add_new_requisition_order','','');">
          <a href="#">
            <i class="icon-plus"></i> Create New Requisition Order </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 <?php if($idd2 == 'Approved'){echo 'Sent';}else{echo $idd2;} ?> REQUISITION ORDERS </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; ?>','','<?php echo $idd2; ?>');" 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">
                    <select class="table-group-action-input form-control input-inline input-small input-sm" name="typesearch" id="typesearch">
                      <option value="">Select...</option>
                      <option value="supplier"  <?php if($typesearch=="surname"){echo "selected='selected'";}?>>Supplier</option>
                      <option value="name" <?php if($typesearch=="othernames"){echo "selected='selected'";}?>>Item Name</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>ORDER DATE</th>
                    <th>REQ. NO</th>
                    <th>SUPPLIER</th>
                    <th>DELIVERY BRANCH.</th>
                    <th>DUE DATE</th>
                    <th>DELIVERY BRANCH STATUS</th>
                    <th>SUPPLYING BRANCH STATUS</th>
                    <th style="text-align: center;">ACTION</th>
                  </tr>
                </thead>
                <tbody>
                 <?php
                 if ($row_rsodest['order_status_from'] == 'Accepted With Rejections') {
                   $action_status = 'view_requisition_order';
                 } else {
                   $action_status = 'view_requisition_order';
                 }
                 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="getstep('<?php echo $action_status; ?>','<?php echo $row_rsodest['purchase_order_no']; ?>');"><?php echo strtoupper($row_rsodest['purchase_order_date']); ?></td>
                     <td valign="top" style="cursor: pointer;" onclick="getstep('<?php echo $action_status; ?>','<?php echo $row_rsodest['purchase_order_no']; ?>');"><?php echo strtoupper($row_rsodest['purchase_order_no']); ?></td>
                     <td valign="top" style="cursor: pointer;" onclick="getstep('<?php echo $action_status; ?>','<?php echo $row_rsodest['purchase_order_no']; ?>');"><?php echo strtoupper($row_rsodest['supplier_name']); ?></td>
                     <td valign="top" style="cursor: pointer;" onclick="getstep('<?php echo $action_status; ?>','<?php echo $row_rsodest['purchase_order_no']; ?>');"><?php echo strtoupper($row_rsodest['deliver_to']); ?></td>
                     <td valign="top" style="cursor: pointer;" onclick="getstep('<?php echo $action_status; ?>','<?php echo $row_rsodest['purchase_order_no']; ?>');"><?php echo strtoupper($row_rsodest['purchase_expected_date']); ?></td>
                     <td valign="top" style="cursor: pointer;" onclick="getstep('<?php echo $action_status; ?>','<?php echo $row_rsodest['purchase_order_no']; ?>');">
                      <b style="color:<?php if($row_rsodest['order_status_from']=='Unapproved') { echo 'red';}else{echo 'green';} ?>"><?php echo strtoupper($row_rsodest['order_status_from']); ?></b>
                    </td> 
                    <td valign="top" style="cursor: pointer;" onclick="getstep('<?php echo $action_status; ?>','<?php echo $row_rsodest['purchase_order_no']; ?>');">
                      <b style="color:<?php if($row_rsodest['order_status']=='Unapproved'||$row_rsodest['order_status']=='Not Saved'||$row_rsodest['order_status']=='Accepted With Rejections') { echo 'red';}else{echo 'green';} ?>"><?php echo strtoupper($row_rsodest['order_status']); ?></b>
                    </td>
                    <td valign="top" style="cursor: pointer;">
                      <?php if($row_rsodest['order_status_from']=='Unapproved' && $_SESSION['branch_id']==$row_rsodest['delivery_location']){?>
                        <span style="font-weight:bold;"> <a style="color:blue" href="#" onclick="getstep('edit_requisition_order','<?php echo $row_rsodest['purchase_order_no']; ?>');"><i style="color: blue;" class="fa fa-edit"></i></a> | 
                          <span onclick="delInformtion('delete_requisition_order', '#currentarea', '<?php echo $row_rsodest['purchase_order_no']; ?>', 'Requisition Order No <?php echo $row_rsodest['purchase_order_no']; ?> ?', 'You are about to Delete Requisition Order No. <?php echo $row_rsodest['purchase_order_no']; ?> from the List of Requisition Orders.', 'Requisition Order No. <?php echo $row_rsodest['purchase_order_no']; ?> Deleted Successfully !');"> <i id="trash_<?php echo $row_rsodest['purchase_order_no']; ?>" class="icon-trash" style="color:red;"></i> <i style="display: none;" id="spin_refresh_<?php echo $row_rsodest['purchase_order_no']; ?>" class="fa fa-refresh fa-spin fa-fw"></i> </span>
                        </span>
                      <?php } ?>
                    </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>


