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

$sql_customer = mysqli_query($conn,"SELECT sh.customer_id,cm.cname FROM saleheader as sh  join customer_master as cm on cm.id=sh.customer_id WHERE cm.isdelete='1' and sh.status='' GROUP BY sh.customer_id order by cm.cname asc");

$row_customer = '';




$tripfromDate=$_POST['fromDate'];
				$triptoDate=$_POST['fromDate'];
				//$triptoDate=$_POST['toDate'];
				
				$area=$_POST['area'];
				


?>
<!DOCTYPE html>
<html>
<head>
    <title>Area 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>

<script>
 
 function printDiv() {
    var divToPrint = document.getElementById('a');
    var htmlToPrint = '' +
        '<style type="text/css">' +
        'table th, table td {' +
        'border:1px solid #000;' +
        'padding;0.5em;' +
        '}' +
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
}
 
 
 </script>
</head>
<body>
    <?php require 'menu.php' ?>


    <div class="container text-center" id="saleStock">
  
  <form class="form-inline" method="post" action="product_sales_Report.php">
    
    <div class="form-group">
      <label for=""> Date:</label><br>
      <input type="date" class="form-control input-lg " id="fromDate" name="fromDate" autocomplete="off">
    </div>
  
    
    
    
    
    
     <div class="form-group"><br>

    <button type="submit" class="btn btn-primary btn-md datewise-submit-sale">Submit</button>
<button type="button" class="btn-success btn-lg btn-md datewise-submit-sale" onClick="printDiv();">Print</button>
    <!--<button type="button" class="btn btn-info btn-md datewise-submit-sale" onClick="tableToExcel('a', 'W3C Example Table')">Excel</button>-->
    </div>
  </form>
  
</div>


<div class="container col-sm-12">
&nbsp;
</div>
    



<div class="container col-sm-12">
<div id="a">

       
        
        
      
       
<h3>Date:<?php  $date=date_create($tripfromDate); echo date_format($date,"d-m-Y");?></h3>





    
    <div>&nbsp;</div>
    <h4 align="center">Total Products</h4>
    <table id="salestockDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
    <tr>
    <th>Product</th>
    <th>Product</th>
     <th>Opening Stock</th>
    <th>Sales Qty</th>
    <th>Remaining Stock</th>
    </tr>
    <?php
	
/*	 if($tripfromDate !='' && $triptoDate !='' && $area !='') {
$sumqry=mysqli_query($conn,"select id,area,area_tamil,customer_id from saleheader where status='' and sale_date between'".$tripfromDate."' AND  '".$triptoDate."' and area='".$area."' ");
}


while($sumprrow=mysqli_fetch_array($sumqry))
{
$hdrid=$sumprrow['id'];
*/
//echo $hdrid;
	
	$sql_sumpr=mysqli_query($conn,"select sh.id,sum(sd.sale_eqty) as sale_eqty,pm.productname,pm.product_tamil,pm.qty_type,sd.productID from saleheader as sh inner join saledetails as sd on sd.saleheader_id=sh.id inner join product_master as pm on pm.id=sd.productID where sd.status='' and sh.status='' and  sh.sale_date between'".$tripfromDate."' AND  '".$triptoDate."'  group by sd.productID");
	
	while($prrow=mysqli_fetch_array($sql_sumpr))
	{
	
	$pid=$prrow['productID'];
	
	$sql_purchase=mysqli_query($conn,"select ph.id,sum(pd.purchase_eqty) as purchase_eqty,pm.productname,pm.product_tamil,pm.qty_type from purchaseheader as ph inner join purchasedetails as pd on pd.purchaseheader_id=ph.id inner join product_master as pm on pm.id=pd.productID where pd.status='' and ph.status='' and ph.purchase_date <='".$tripfromDate."'  and pd.productID='$pid'");
	
		
	$row_purchase=mysqli_fetch_array($sql_purchase,MYSQLI_ASSOC);
	
	$pqty=$row_purchase['purchase_eqty'];
	
	$sql_sales_return=mysqli_query($conn,"select srh.id,sum(srd.sale_eqty) as sale_return_eqty,pm.productname,pm.product_tamil,pm.qty_type,srd.productID from sale_return_header as srh inner join sale_return_details as srd on srd.saleheader_id=srh.id inner join product_master as pm on pm.id=srd.productID where srd.status='' and srh.status='' and  srh.sale_date <='".$tripfromDate."' and srd.productID='$pid'");
	
		
	$row_return=mysqli_fetch_array($sql_sales_return,MYSQLI_ASSOC);
	$sales_rtn=$row_return['sale_return_eqty'];
	
	$sql_sales_bfr=mysqli_query($conn,"select sh.id,sum(sd.sale_eqty) as sale_eqty,pm.productname,pm.product_tamil,pm.qty_type,sd.productID from saleheader as sh inner join saledetails as sd on sd.saleheader_id=sh.id inner join product_master as pm on pm.id=sd.productID where sd.status='' and sh.status='' and  sh.sale_date <'".$tripfromDate."' and sd.productID='$pid'");
	
		
	$row_sales=mysqli_fetch_array($sql_sales_bfr,MYSQLI_ASSOC);
	
	$saleqty=$row_sales['sale_eqty'];
	//$available_qty=$pqty-$sqty;
	
	$sqty=$saleqty-$sales_rtn;
	
	$opening=$pqty-$sqty;
	
	$balance=$opening-$prrow['sale_eqty'];
	
	?>
    <tr>
    <td><?php echo $prrow['productname'];?></td>
   
    <td><?php echo $prrow['product_tamil'];?></td>
    <td><?php echo number_format($opening,2,'.','');?><?php echo $prrow['qty_type'];?></td>
    <td><?php echo $prrow['sale_eqty'];?>&nbsp;<?php echo $prrow['qty_type'];?></td>
     <td><?php echo number_format($balance,2,'.','');?><?php echo $prrow['qty_type'];?></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>