<?php 
//error_reporting(0);
require_once("config/configDB.php");
require_once("config/constant.php");




?>
<!DOCTYPE html>
<html>
<head>
    <title>Stock Report</title>
    <?php require 'header.php' ?>
    <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
<script >
var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()

</script>
<style>
body
{
    counter-reset: Serial;           /* Set the Serial counter to 0 */
}

table
{
    border-collapse: separate;
}

tr td:first-child:before
{
  counter-increment: Serial;      /* Increment the Serial counter */
  content:  counter(Serial); /* Display the counter */
}
</style>
</head>
<body>
    <?php require 'menu.php' ?>




    
<button type="button" class="btn btn-info btn-md datewise-submit-sale" onClick="tableToExcel('a', 'W3C Example Table')">Excel</button>


<div class="container col-sm-12">
<div id="a">
<table id="salestockDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<caption> <strong>Available Product Stock</strong></caption>
        <thead>
            
                <th>S.No</th>
                <th>Product</th>
				<th>UOM</th>
				<th>Purchase Qty</th>
				<th>Sales Qty</th>
				
				<th>Avail Stock</th>
                
               
                <!-- <th>Action</th> -->
           
        </thead>
        
        <?php
				
		
				
				$sql = "SELECT * from purchase_product_master   where isdelete ='1' group by productname order by id asc ";
				
				
	  
	 
				?>
        
        <tbody>
            <?php
			$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1".$sql);
			$sno=0;
			while($row=mysqli_fetch_array($query))
				
{
	$pid=$row['id'];
				
				$purchase = mysqli_query($conn,"SELECT sum(loadqty) as pqty  FROM purchasedetails WHERE    productID='".$pid."'  and status=''  ");

while($prow = mysqli_fetch_array($purchase)){
	
	$pqty=$prow['pqty'];
	
		if($pqty=="")
	{
	    $pqty="0";
	}

}

$sales = mysqli_query($conn,"SELECT sale_eqty as sqty,conv_qty as prqty  FROM saledetails WHERE    purchase_productID='".$pid."'  and status=''  ");

$psale=0;
while($srow = mysqli_fetch_array($sales)){
	
	//$psale=$srow['sqty'];
	
	$sqty=$srow['sqty']*$srow['prqty'];
$psale+=$sqty;
	
		if($psale=="")
	{
	    $psale="0";
	}
	}
	$stk=$pqty - $psale;
	
				?>
			<tr>
            <td><?php echo ++$sno;?></td>
            
			<td><?php echo $row["productname"];?></td>
			<td><?php echo $row["qty_type"];?></td>
			<td><?php echo $pqty ;?></td>
			<td><?php echo  $psale;?></td>
            <td ><?php echo $stk;?></td>
            
			</tr>
             <?php	
}

				
	


?>
        
       
    </table>
    </div>
</div>



    <?php require 'footer.php' ?>

    <script type="text/javascript">var base_url = "<?= $base_url ?>";</script>
  
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js"></script>

    <script>



            $("#fromDate").datepicker({
        minDate: '-1Y-6M',
        maxDate: '+1Y+6M',
        dateFormat: 'yy-mm-dd',
        onSelect: function (dateStr) {
            var min = $(this).datepicker('getDate'); // Get selected date
            $("#toDate").datepicker('option', 'minDate', min || '0'); // Set other min, default to today
        }
    });

    //toDate

    $("#toDate").datepicker({
        minDate: '1',


        maxDate: '+1Y+6M',
        dateFormat: 'yy-mm-dd',
        onSelect: function (dateStr) {
            var max = $(this).datepicker('getDate'); // Get selected date

            $('#datepicker').datepicker('option', 'maxDate', max || '+1Y+6M'); // Set other max, default to +18 months

            
        }
    });

            


</script>
</body>
</html>