<!DOCTYPE html>
<?php include '../includes/connection.php';
include("../includes/header-links.php");
 ?>
<html>
    
    <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="span9" id="content">


                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Spoilt Stock</div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                
                                    
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                         <div class="span12">
                                            
                                    <div class="span4">
                                        <label>Date from</label>
                                        <input type="date" name="datefrom" id="dfrom" onchange="filterData(this.value)">
                                    </div>
                                    <div class="span4">
                                        <label>Date To</label>
                                        <input type="date" name="dateto" id="dto" onchange="filterdate2(this.value)" disabled>
                                    </div>
                                            
                                           
                                        </div>
 <script type="text/javascript">
     function filterData(val){
        var text=document.getElementById("dfrom").value;
        document.getElementById("dto").disabled=false;
        document.getElementById("test").innerHTML=text;
var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("filtered").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "returns_filter.php?from="+val, true);
  xhttp.send();

     }

     function filterdate2(vall){
        var from=document.getElementById("dfrom").value;
        var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("filtered").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "returns_filter2.php?from="+from+"&to="+vall, true);
  xhttp.send();
     }
 </script>                                       
                                        <div id="test">&nbsp</div>
<hr>
                                    <div id="toolbar">
                                        <div class="btn-group">
                                         <a href="#restock" data-toggle="modal" class="btn btn-warning"><i class="icon-plus icon-white"></i>&nbsp;Add spoilt stock</a> <span>&nbsp &nbsp &nbsp</span>
                                          <a href="stock.php"><button class="btn btn-info" style="margin-left: 10px;">Back to stock</button> </a>
                                       </div>
                                    </div>
                                      <div id="restock" class="modal hide">
                      <div class="modal-header" style="background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Record Spoilt stock</h3>
                      </div>
                      <div class="modal-body">


                        <form action="" method="POST" enctype="multipart/form-data">
                        <label>Product Name</label>
                        <div class="controls">
                          <select id=selectError name="mproduct" required style="width: 95%; height: 30px;">
                           <option>Select Product</option>
<?php  
$sqlstock = "SELECT product_name,(select product_name from products where id=stock.product_name && branch_id='$branch') as pd FROM stock where branch_id='$branch' ";
$resultstock = mysqli_query($conn, $sqlstock);

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

    while($rows = mysqli_fetch_assoc($resultstock)) {
      ?>
      <option value="<?php echo $rows['product_name']; ?>">
        <?php echo $rows['pd']; ?>
      </option>
       <?php
    }
    
} else {
    echo "0 results";
}
         ?>                                   
                                            </select>
                                          </div>
                        
                        <label>Quantity</label>
                        <input type="number" name="mquantity" class="input-xlarge focused" placeholder="Quantity" required="required" style="width: 95%; height: 30px;">
                      <label>Date Removed</label>
                        <input type="date" name="rdate" class="input-xlarge focused" required="required" style="width: 95%; height: 30px;">
                      <label>Reason</label>
                        <input type="text" name="reason" class="input-xlarge focused" required="required" style="width: 95%; height: 30px;">
                      
                      
                        
                        <div class="form-actions">
                                          <button type="submit" name="save" class="btn btn-primary">Save </button>
                                          
                                        </div>  
                        </form>
                        <?php

if(isset($_POST['save'])){
  include '../includes/connection.php';
$mproduct_name=mysqli_real_escape_string($con,$_POST['mproduct']);
$mquantity=mysqli_real_escape_string($con,$_POST['mquantity']);
$reason=mysqli_real_escape_string($con,ucwords($_POST['reason']));
$rdate=mysqli_real_escape_string($con,$_POST['rdate']);
$sqs = mysqli_fetch_array(mysqli_query($con,"select * from stock where id='$mproduct_name' && branch_id='$branch'"));
$quan=$sqs['quantity'];
$nquant=(int)$quan-(int)$mquantity;

$sqlst = mysqli_query($con,"update stock set quantity='$nquant' where product_name='$mproduct_name' ");
$sqlps = mysqli_query($con,"update products set quantity='$nquant' where id='$mproduct_name' ");

if ($sqlst) {
   $id_user=$_SESSION['id'];
  $ret1=mysqli_fetch_array(mysqli_query($con,"select * from stock where product_name='$mproduct_name' && branch_id='$branch'"));

  $retproduct=$ret1['product_name'];
  $retprice=$ret1['buying_price'];
  $return=mysqli_query($con,"INSERT INTO returns_out(product_id,quantity,price,branch_id,Reason,date,AddedBy) values('$retproduct','$mquantity','$retprice','$branch','$reason','$rdate','$id_user')");
 
  $ups1=mysqli_fetch_array(mysqli_query($con,"select * from products where id='$mproduct_name' && branch_id='$branch'"));
 $pname1=$ups1['product_name'];

 
  $activity="Removed  stock of  product ($mquantity units) ".$pname1;
 $log_sql1=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id) VALUES 
('$id_user','$activity','$branch')");

if ($log_sql1 && $return) {
//alert(' $mquantity units of $pname1 removed from stock succesfully');
    echo "<script>
    
    window.location='spoilt_stock.php';
    </script>";
} else {
    echo "Error: Failed to add log <br>" . mysqli_error($con);
}
  ?>


    <meta http-equiv="refresh" />  
    <?php
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

mysqli_close($con);
}

?>
                      
                      </div>
                    </div>
                                
                                        <table  id="table" data-toggle="table" data-pagination="false" data-search="true" data-show-columns="false" data-show-pagination-switch="false" data-show-refresh="true" data-key-events="true" data-show-toggle="false" 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"}'>
                                    <?php
include '../includes/connection.php';
                                   // Create connection


$sql= mysqli_query($con,"SELECT id,quantity,price,Reason,date,(select product_name from products where id=returns_out.product_id && branch_id='$branch')as name from returns_out where branch_id='$branch' order by date desc");
//$result = mysqli_query($con, $sql);


                                   ?>
                                        <thead>
                                            <tr>
                                                <th  width="10%">S/NO</th>
                                                
                                                   
                                                   
                                                    <th >Product</th>
                                                    <th >Quantity</th>
                                                    <th >Unit Price</th>
                                                     <th >Date</th>
                                                        <th >Reason</th>
                                                
                                            </tr>
                                        </thead>
                                        <tbody id="filtered">
                                        <?php
        if (mysqli_num_rows($sql) > 0) {
    // output data of each row
                                        $n=0;
              while($row = mysqli_fetch_assoc($sql)) {
                
                        $n++;
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $n; ?></td>
                                                
                                                <td><?php echo $row['name']; ?></td>
                                                <td><?php echo $row['quantity']; ?></td>
                                                <td><?php echo $row['price']; ?></td>
                                                <td><?php echo $row['date']; ?></td>
                                                <td><?php echo $row['Reason']; ?></td>
                                                
                                                
                                                
                                            </tr>
               <?php                             
    }
} else {
    echo "<td>No results</td><td></td><td></td><td></td><td></td><td></td>";
}

//mysqli_close($conn);
?>
                                            
                                        </tbody>
                                    </table>
                                
                                </div>
                                </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>