<link rel="stylesheet" href="tablestyle/tablereportdisplay.css">
<script src="javascript/jquery.table2excel.js" type="text/javascript"></script>
<script src="javascript/jspdf.min.js"></script>
    <script src="javascript/jspdf.plugin.autotable.min.js"></script>
    <script src="javascript/tableHTMLExport.js"></script>
<button class="btn btn-warning" id="pdf" onclick="printDiv()" style="float:right;margin-right:20px;">Print</button>   <button class="btn btn-primary" id="ex" style="float:right;margin-right:20px;">Excel</button><br><br>
<script type="text/javascript">
$("#ex").click(function(){
    var a = $("#todate").val();
      var b = $("#fromdate").val();
  $("#fullreport").table2excel({
    
            exclude:".noExl",
    name:"Materialreport",
    filename:"Materialreport ",//do not include extension
    fileext:".xls" // file extension
  });
});

</script>
<script>function printDiv() 
{

  var divToPrint=document.getElementById('fulltable');

  var newWin=window.open('','Print-Window');

  newWin.document.open();

  newWin.document.write('<html><head><link rel="stylesheet" href="tablestyle/tablereport.css"></head><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
  newWin.document.close();
}</script>
<script type="text/javascript">
$("#pf").on("click", function () {
        $("#fullreport").tableHTMLExport({ type: "pdf", filename: "salesreport.pdf",
        
        });
      });
</script>
<?php 
include "../config.php";
//error_reporting(0);
//$product = $_POST['product'];
$fromd = $_POST['fromd'];
$tod = $_POST['tod'];
// echo $fromd;
// echo $tod;
    $datefr = date_create($fromd);
    $datefrom = date_format($datefr,"d/m/Y");
    $datet = date_create($tod);
    $dateto = date_format($datet,"d/m/Y");
    $proedit=mysqli_query($connect,"select * from partydetail order by id desc");
while($proshowedi = mysqli_fetch_array($proedit))
{
  $party=  $proshowedi['party'];
}
//echo $customer;
// echo $fromd;
// echo $tod;
echo 

'<div id="fulltable">

<table class="table table-striped">

   <thead style="">
   <tr><th colspan="19" style="text-align:center;">Material-Wise Customer Report-GST Between '.$datefrom.' And '.$dateto.'</th></tr>
   <tr><th colspan="19" style="text-align:center;">'.$party.'</th></tr>
   <tr>
   <th>S.no</th>
   <th>Customer</th>
   ';
   $repp="select *,sum(price) from second_transaction  where date
   between '$fromd' and '$tod' && second_transaction.status !='deleted'  group by product order by customer asc
   ";
   
   $rep =mysqli_query($connect,$repp);
   $pricefull=0;
   while($repshow = mysqli_fetch_array($rep))
   
   {
   echo'
   
<th>'.$repshow['productname'].'</th>
';
}




   echo'

   <th>Total</th>
   </tr>
   </thead>
   <tbody style="">
   <tr>
   
  ';
  $custo = mysqli_query($connect,"select *,sum(netqty)as netqt,(select customerdetails.name from customerdetails where customerdetails.code = second_transaction.customer && status!='deleted' )as cusname from second_transaction  where date
  between '$fromd' and '$tod' && second_transaction.status !='deleted'  group by customer order by customer asc  ");
//   loop to fetch customer
$sno=0;
  while($custoshow = mysqli_fetch_array($custo))
  { $sno +=1;

 echo '
 <td>'.$sno.'</td>
 <td>'.$custoshow['cusname'].'</td>';
 
 //  loop inside customer to product
 $customer = $custoshow['customer'];
 $procount =mysqli_query($connect,"select *,sum(price) from second_transaction  where date
 between '$fromd' and '$tod' && second_transaction.status !='deleted'  group by product order by customer asc
 ");
// loop start final product
 while ($proshow =mysqli_fetch_array($procount)  )
 {
     $product = $proshow['product'];
 $cuspric = mysqli_query($connect,"select *,ifnull(sum(netqty),0)as netqt,(select customerdetails.name from customerdetails where customerdetails.code = second_transaction.customer && status!='deleted' )as cusname from second_transaction  where date
 between '$fromd' and '$tod' && customer ='$customer' && product='$product' && second_transaction.status !='deleted' group by customer order by customer asc  ");
 if(mysqli_num_rows($cuspric) > 0)
 {
     $fullprice = 0 ;
 while($cusprishow = mysqli_fetch_array($cuspric))
 {
 
 
    echo '<td>'.number_format($cusprishow['netqt'],2,'.','').'</td>';
    $fullprice +=number_format($cusprishow['netqt'],2,'.','');
}
//  loop close  to product final
 }
else
{
    echo '<td>-</td>';
}

}
// loop close customer to product
 echo'
 <td>'.$custoshow['netqt'].'</td>
   </tr>
 
 ';
  }
//   loop close fetch customer
   echo'


   
   </tbody>
   
   </table>
   </div>';
?>
