<!DOCTYPE html>
 <?php include '../includes/connection.php'; 
   include("../includes/header-links.php");
 ?>
<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>
       
        <!-- Bootstrap -->
        <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
        <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
        <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">


                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Un taken orders</div>
                                <div class="muted pull-right">Filter By Date</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
<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>
                                    <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="false" 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"}'>                                    <?php
include '../includes/connection.php';
                                   // Create connection

if( $_SESSION['role']=="Super Admin"){
  $sql= mysqli_query($con,"SELECT *,(select customer_name from sales where id=sales_details.order_id)as customer, (select product_name from products where id=sales_details.product_id)as product_name from sales_details where quantity_remaining>0 && branch_id='$branch'");

}else{
  $sql= mysqli_query($con,"SELECT *,(select customer_name from sales where id=sales_details.order_id)as customer, (select product_name from products where id=sales_details.product_id)as product_name from sales_details where quantity_remaining>0 && branch_id='$branch'");
}

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

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


                                   ?>
                                        <thead>
                                            <tr>
                                                     <th  width="10%">S/NO</th>
                                                    <th width="40%">Product</th>
                                                    <th width="20%">Quantity</th>
                                                    <th width="20%">Quantity To Be taken</th>
                                                    <th width="20%">Unit Price</th>
                                                    <th width="20%">Total Price</th>
                                                     <th width="20%">Customer</th>
                                                      <th width="20%">Date</th>
                                                      <th width="10%">Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                <?php
              $nid=0;
              while($row = mysqli_fetch_array($sql)) {
                $iddx=$row['id'];
                $nid++;
                        
                    ?>
                                            <tr>
                              <td><?php echo $nid; ?></td>
                              <td width="40%"><?php echo $row['product_name']; ?></td>
                              <td width="20%"><?php echo $row['quantity']; ?></td>
                              <td width="20%"><?php echo $row['quantity_remaining']; ?></td>
                              <td width="20%"><?php echo $row['selling_price']; ?></td>
                              <td width="20%"><?php echo $row['quantity']*$row['selling_price']; ?></td>
                              <td width="20%"><?php echo $row['customer']; ?></td>
                              <td width="20%"><?php echo $row['date_time']; ?></td>

                                                
                                                <td><a href="#edit<?php echo $iddx; ?>" data-toggle="modal" ><button class="btn btn-success"><li class=" icon icon-check icon-white"></li>Take</button>
                                                </a>
                                            </td>
                         <div id="edit<?php echo $iddx; ?>" class="modal hide">
                      <div class="modal-header">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3> Give Out product</h3>
                      </div>
                      <div class="modal-body">
                        
                        <form action="" method="POST" enctype="multipart/form-data">
                        <input type="hidden" value="<?php echo $iddx; ?>" name="editid">
                        <label>Quatity not take</label>
                        <div class="controls">
                          <input type="number" value="<?php echo  $row['quantity_remaining'] ?>" name="qtyremain" class="input-xlarge focused" readonly="readonly" style="width: 95%; height: 30px;">
                                          </div>
                    
                         <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        
                        <label>Quantity Given Out </label>
                         <div class="controls">
                        <input type="number"   value="<?php echo $row['quantity_remaining'] ?>" name="qtyaken" class=" input-xlarge focused " max="<?php echo $row['quantity_remaining'] ?>" style="width: 95%; height: 30px;">
                      </div>
                  </div>
                        <div class="form-actions">
                                          <button type="submit" name="pay" class="btn btn-primary">Sumbit </button>
                                          
                                        </div>  
                        </form>
                      </div>
                    </div>
                                                
                                            </tr>
               <?php                             
    }
    
    
} else {
    echo "0 results";
}

//mysqli_close($conn);
?>
<?php

if(isset($_POST['pay'])){
  include '../includes/connection.php';
$qtyaken=mysqli_real_escape_string($con,$_POST['qtyaken']);
$editid=mysqli_real_escape_string($con,$_POST['editid']);
$qtyremain=mysqli_real_escape_string($con,$_POST['qtyremain']);
$qremain=$qtyremain-$qtyaken;
  

$sqlstm = mysqli_query($con,"update sales_details set quantity_remaining=$qremain where id='$editid' ");


if($sqlstm) {

 echo "<script>
    
    window.location='untaken_orders.php';
    </script>";
 
} else {
    echo "Error ".mysqli_error($con);
}

  
}




?>
                                            
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../bootstrap/js/bootstrap.min.js"></script>
        <script src="../vendors/datatables/js/jquery.dataTables.min.js"></script>

<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>


        <script src="../assets/scripts.js"></script>
        <script src="../assets/DT_bootstrap.js"></script>
        <script>
        $(function() {
            
        });
        </script>
    </body>

</html>