<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>
<!DOCTYPE html>
<html>
    <script type="text/javascript">
    function FormatCurrency(ctrl) {
                  //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys
                  if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {
                      return;
                  }

                  var val = ctrl.value;

                  val = val.replace(/,/g, "")
                  ctrl.value = "";
                  val += '';
                  x = val.split('.');
                  x1 = x[0];
                  x2 = x.length > 1 ? '.' + x[1] : '';

                  var rgx = /(\d+)(\d{3})/;

                  while (rgx.test(x1)) {
                      x1 = x1.replace(rgx, '$1' + ',' + '$2');
                  }

                  ctrl.value = x1 + x2;
              }

              function CheckNumeric() {
                  return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46;
              }
</script>

    <head>


		<?php
		include("../includes/header-links.php");
		?>
    <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        
    </head>
    
    <body>
        <?php include '../includes/header.php'; 
         
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['view_short_expiries'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
        <div class="container-fluid">
		
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span9" id="content">
					

                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
						
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Products About To Expire</div>
                            </div>
                            <div class="block-content collapse in">
							
                                <div class="span12">
<div class="row">
	<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

	</div>
	<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
	</div>		 
</div>
                                   <div class="table-toolbar">
                                  
                                      

                                   
                                   <?php
include '../includes/connection.php';
                                   // Create connection
$thismon=date('m');
$thisday=date('Y-m-d');
$thisyer=date('Y');
//if( $_SESSION['role']=="Super Admin")
//if( $permissions['export_short_expires'] == 1)
//{  
if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
$sql = mysqli_query($con,"SELECT * FROM purchases where  business_id='$business' && expiry_date!='' && quantity_remaining>0 order by expiry_date");
}else{
$sql = mysqli_query($con,"SELECT * FROM purchases where branch_id='$branch' && expiry_date!='' && quantity_remaining>0 order by expiry_date");

}
//$result = mysqli_query($con, $sql);

if (mysqli_num_rows($sql) > 0) {
    // output data of each row
    


                                   ?>
        </div>
                                    <div class="datatable-dashv1-list custom-datatable-overright">

                                      <?php
                                               // if($_SESSION['role']=="Super Admin"){
                                                if( $permissions['export_short_expires'] == 1){ 
                                                //if($permissions['business_id'] != 0 && $permissions['branch_id']==0){           
                                                  ?>
                                    <div id="toolbar">
                                        <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select>
                                    </div>
                                    <?php 
                                                }?>
                                    <table  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"
                                        data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>
                                        <thead>
                                            <tr>
                                                <th>S/NO</th>
                                                <th>Batch Number</th>
                                                <th>Product Name</th>
                                                <th>Quantity Bought</th>
                                                <th>Quantity Remaining</th>
                                                <th>Expiry Date</th>
                                                <th>Remaining Days</th>
<?php
                                               // if($_SESSION['role']=="Super Admin"){
                                                if( $permissions['mark_as_expired'] == 1){            
                                                  ?>
                                                <th>Action</th>
                                                 <?php 
                                                }?>
                                                
                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
                                        $n=0;
while($row = mysqli_fetch_assoc($sql)) {
	$n++;
  $produ=$row['product_id'];
  $pdctn=mysqli_fetch_array(mysqli_query($con,"SELECT product_name from products where id='$produ'"));
  $exp=strtotime($row['expiry_date']);
  $thistoday=strtotime($thisday);
  $range=($exp-$thistoday)/(86400);

   if ($range<90) {
     ?>

                                     <tr class="gradeU">
                                                <td><?php echo $n; ?></td>
                                               
                                                <td><?php echo $row['batch_no']; ?></td>
                                                <td><?php echo $pdctn['product_name']; ?></td>
                                                <td><?php echo $row['quantity']; ?></td>
                                                <td><?php echo $row['quantity_remaining']; ?>
<?php
                                               // if($_SESSION['role']=="Super Admin"){
                                                if( $permissions['edit_short_expires'] == 1){            
                                                  ?>
                                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                    <a href="#myModal<?php echo $row['id']?>" data-toggle='modal'><i class="icon-pencil icon-large"></i></a>
                                                     <?php 
                                                }?>

                                                  </td>
                                              
                                                <td><?php 
                                                
                                                echo $row['expiry_date']; ?>
                                                  
                                                </td>
                                                <td><?php 
                                                if ($range<0) {
                                                  echo 'expired'.' '.-1*$range.' days ago';
                                                }else{
                                                  echo $range;
                                                }
                                                 ?></td>
                                                 <?php
                                               // if($_SESSION['role']=="Super Admin"){
                                                if( $permissions['mark_as_expired'] == 1){            
                                                  ?>
                                                 <td>
                                                  <a onclick = "confirmationExpiry(this); return false;" href="confirm_expiry.php?id=<?php echo $row['id'];?>" ><button class="btn btn-warning">Mark as Expired</button></a></td>
                                                   <?php 
                                                }?>
                                                
                                               
                                               
                                            </tr>
                                               <div id="myModal<?php echo $row['id']?>" class="modal hide">
                                                  <div class="modal-header" style="background-color: #08c; color: white;">
                                                      <button data-dismiss="modal" class="close" type="button">&times;</button>
                                                      <h3>Update Quantity Remaining</h3>
                                                  </div>
                                                  <div class="modal-body">
                                                      <div>
                                                          <form action="" method="POST">
                                                              <label>Quantity Remaining</label>
                                                              <input class="form-control span10" type="hidden" name="editid" value="<?php echo $row['id']?>" step="any">
                                                              <input class="form-control span10" type="number" name="quantity_remaining" value="<?php echo $row['quantity_remaining']?>" step="any" max="<?php echo $row['quantity']?>">
                                              <button type="submit" class="btn btn-large btn-block btn-primary" name="save_changes">Save Changes</button>
                                                          </form>
                                                          <?php
                                          if(isset($_POST['save_changes'])){
                                              $quantity_remaining=$_POST['quantity_remaining'];
                                              $editid=$_POST['editid'];
                                              $sql1 = "UPDATE purchases SET quantity_remaining='$quantity_remaining' where id='$editid'";

                                          if (mysqli_query($conn, $sql1)) {
                                               ?><meta http-equiv="refresh" content="0;URL='short_expiry.php'" /><?php
                                          } else {
                                              echo "Error updating record: " . mysqli_error($conn);
                                          }

                                          }
                                                          ?>
                                                      </div>
                                                  </div>
                                              </div>

     <?php
   }

        ?>
                                            
                                            <?php


    }
} else {
    echo "0 results";
}

mysqli_close($con);


                                        ?>
                                            
                                        </tbody>
                                    </table>
                                  </div>
                                    
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <script type = "text/javascript">
    function confirmationExpiry(anchor){
        var conf = confirm("Are you sure you want To mark and confirm  expiry of this product");
        if(conf){
            window.location = anchor.attr("href");
        }
    } 
    
</script>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <!--==========Footer js======-->
		<?php require_once("../includes/footerjs.php");?>
        <!--==========Footer js======-->
       
 


<script src="../data-table-js/bootstrap-table.js"></script>
    <script src="../data-table-js/tableExport.js"></script>
    <script src="../data-table-js/data-table-active.js"></script>
    <script src="../data-table-js/bootstrap-table-editable.js"></script>
    <script src="../data-table-js/bootstrap-editable.js"></script>
    <script src="../data-table-js/bootstrap-table-resizable.js"></script>
    <script src="../data-table-js/colResizable-1.5.source.js"></script>
    <script src="../data-table-js/bootstrap-table-export.js"></script>
    </body>
</html>
