<?php
error_reporting(0);
$customer=$_POST['customer'];
		
		  $fromdate=$_POST['fromdate'];
		  $todate=$_POST['todate'];
		  
		  $fdate=date_create($fromdate);
		  $tdate=date_create($todate);
?>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="datatable/data.css">
<script src="datatable/data.js" type="text/javascript"></script>
<script src="datatable/table.js" type="text/javascript"></script>
<script src="datatable/datatable.js"></script>
<link rel="stylesheet" href="datatable/data-boot.css">
<link href="javascript/select2.css" rel="stylesheet" >
<script src="javascript/selectt2.js"></script>
<link rel="stylesheet" href="calendar-15/calendar-15/fonts/icomoon/style.css">

<link rel="stylesheet" href="calendar-15/calendar-15/css/rome.css">
<link rel="stylesheet" href="calendar-15/calendar-15/css/style.css">
<!-- <script src = "ajax/loadoutreport1.js"></script>
<script src = "ajax/loadoutreport2.js"></script>
<script src = "ajax/loadoutreport3.js"></script> -->
<script src="js/loadoutreport.js"></script>
<style>
    
    #navbar
    {
        flex:15%;
        height:100%; 
        flex-direction:row;
        overflow:auto;
        
    }
    #content
    {
        flex:90%;
        overflow:auto;
        padding-left:5px;
        padding-right:5px;
    }
    #fullbody
    {
        display:flex;
        
         height:100%;         
    }
    label
    {
      font-size:20px;
    }
    .edit
    {
        font-size:18px;
        color:green;
        cursor:pointer;
    }
    .dele
    {
        font-size:18px;
        color:red;
        cursor:pointer;
    }
    .edit:hover
    {
        font-size:20px;
        
    }
    .dele:hover
    {
        font-size:20px;
        color:red;
        cursor:pointer;
        
    }
    td
    {
     
      padding:8px;
     border:1px solid rgba(24, 7, 173); 
      background-color:#FFFFE0;
      color:black;
    }
    th
    {
      background-color:rgba(24, 7, 173);
      text-align:center;
      padding:8px;
     border-bottom:1px solid #FFFFE0; 
     color:#FFFFE0;
    }
    li a 
    {
      color:black;
    }
    .page-item.active .page-link {
    
    background-color:yellow;
    border-color: #007bff;
}
.reportModal
{
  height:800px;
}
.searchfeilds input
{
height:30px;
}
.daterow
{
  margin-top:10px;
}
    @media (max-width: 800px) {
        #fullbody
    {
        display:flex;
        flex-wrap: wrap;
                 
    }
        #navbar,#content {
    flex: 100%;
  }
}

#table-wrapper {
  position:relative;
}
#table-scroll {
  height:auto;
  overflow:auto;  
  margin-top:20px;
}
#table-wrapper table {
  width:100%;

}

#table-wrapper table thead th .text {
  position:absolute;   
  top:-20px;
  z-index:2;
  height:20px;
  width:35%;
  border:1px solid red;
}
</style>
<script>
  $(function () {

// INITIALIZE DATEPICKER PLUGIN
$('.datepicker').datepicker({
    clearBtn: true,
    format: "dd/mm/yyyy"
});


// FOR DEMO PURPOSE
$('#reservationDate').on('change', function () {
    var pickedDate = $('input').val();
    $('#pickedDate').html(pickedDate);
});
});

</script>
<script>
$(document).ready( function () {
    $('#protable').DataTable();
} );
</script>

</head>
<body>
    
    <div ><?php include "navbar.php"?></div>
    <div id="fullbody"> 
<div id="content">

<h3 align="center">Customer Report</h3>

<form action="customer_report.php" method="post" autocomplete="off">

<div>&nbsp;</div>


<div class="row searchfeilds" >
  
  <div class="col-sm-1"></div>  
  
  <div class="col-sm-4">
<select class="selectpro" id="customer" name="customer" required>
    <option value="">--Select Customer--</option>
<?php 
$cus=mysqli_query($connect,"select * from customerdetails where status !='deleted'");
while($cusshow = mysqli_fetch_array($cus))
{
?>
<option value="<?php echo $cusshow['code'] ?>"><?php  echo $cusshow['name'] ?></option>
        <?php }?>         
</select>

</div>
     <div class="col-sm-3">
                <input type="text" class="form-control" id="input_from" name="fromdate" autocomplete="off" placeholder="Start Date" required></div>
                <div class="col-sm-3">
                            
                <input type="text" class="form-control" id="input_to" name="todate" autocomplete="off" placeholder="To Date" required>
              </div>             
            </div>
            <div>&nbsp;</div>
            
            <div class="row searchfeilds" >
  
  <div class="col-sm-1"></div>  
  <div class="col-sm-2"></div>  
  <div class="col-sm-1"><button type="submit" class="btn btn-primary" >Submit</button></div>  
  <div class="col-sm-1"><button type="button" class="btn btn-primary" onClick="tableToExcel('a', 'W3C Example Table')">Download</button></div>
  <div class="col-sm-1"><a href="print_customerreport.php?customer=<?php echo $customer;?>&&fromdate=<?php echo $fromdate;?>&&todate=<?php echo $todate;?>" class="btn btn-primary" target="_blank">Print</a></div>
  
  </div>
  </form>
   <div id="a">
  

  <?php
  
		?>
        <div class="row searchfeilds" >
        <div class="col-sm-3">&nbsp;</div>
        <div class="col-sm-3">
           <?php
  
  $qry_cus=mysqli_query($connect,"select * from customerdetails where code='".$customer."' and status=''");
		  $rw_cus=mysqli_fetch_array($qry_cus,MYSQLI_ASSOC);
		  
		  ?>
          
          <h3 >Customer :<?php echo $rw_cus['name'];?></h3>
          
          <h4 >Report Date:<?php echo  date_format($fdate,"d-m-Y");?>&nbsp;To&nbsp;<?php echo  date_format($tdate,"d-m-Y");
		  ?>
          </h4>
          </div>
          </div>
          <?php
		  $query=mysqli_query($connect,"select * from second_transaction where status='' and customer='$customer' and date between '$fromdate' and '$todate' group by productname");
		  while($row_product=mysqli_fetch_array($query))
		  {
		  $product=$row_product['product'];
  ?>
  
  
 
  <h4><?php echo $row_product['productname'];?></h4>
 
 
  <div id="table-wrapper">
  <div id="table-scroll">
  
  <table  class="table table-striped table-bordered " cellspacing="0" width="100%">
              <thead>
                 <th>Sno</th>
                 <th>Ticket</th>
                 <th>Invoice</th>
                 <th>TripNo</th>
                 <th>Customer</th>
                 <th>Product</th>
                 
                 <th>VehicleNo</th>
                <!-- <th>Chakka</th>-->
                 <th>Delivery</th>
                 
                 
                 <th>Date</th>
                 <th>Time</th>
                 <!--<th>EmptyWeight</th>
                 <th>LoadWeight</th>
                 <th>NetWeight</th>-->
                 <th>Capacity</th>
                 <th>NetQty</th>
                
                 <th>Price</th>
                 
                 <th>Totalamount</th>
               
                
                 <th>Mode</th>
                 <th>Remarks</th>
                 <th>User</th>
              
             </thead>
             <?php
			 
			  $sql_query="select sh.salescode,sh.ticket,sh.chakka,sh.vehicleno,sh.customer,sh.delivery,sh.productname,sh.price,sh.priceori,sh.date,sh.time,sh.emptyweight,sh.loadweight,sh.netweight,sh.netqty,sh.netqtyori,sh.cft,sh.totalamount,sh.modeof,sh.remark,sh.editby,cm.name,tik.tripno from second_transaction as sh inner join customerdetails as cm on cm.code=sh.customer inner join ticket as tik on tik.ticket=sh.ticket where tik.status='' and cm.status='' and sh.status=''  and sh.customer='$customer' and sh.date between '$fromdate' and '$todate' and sh.product='$product' order by sh.date,sh.id";
			 			$query_result=mysqli_query($connect, $sql_query) or die("brandgriddata.php: get brand Data step 1".$sql_query);
			$sno=0;
			$ntqty=0;
			$ntamount=0;
			while($row_query=mysqli_fetch_array($query_result))
{
			 ?>
             <tbody >
<tr>
<td><?php echo ++$sno;?></td>
<td><?php echo $row_query['ticket'];?></td>
<td><?php echo $row_query['salescode'];?></td>
<td><?php echo $row_query['tripno'];?></td>

<td><?php echo $row_query['name'];?></td>
<td><?php echo $row_query['productname'];?></td>
<td><?php echo $row_query['vehicleno'];?></td>
<!--<td><?php echo $row_query['chakka'];?></td>-->
<td><?php echo $row_query['delivery'];?></td>
<td><?php echo $row_query['date'];?></td>
<td><?php echo $row_query['time'];?></td>
<!--<td><?php echo $row_query['emptyweight'];?></td>
<td><?php echo $row_query['loadweight'];?></td>
<td><?php echo $row_query['netweight'];?></td>-->
<td><?php echo $row_query['cft'];?></td>
<td align="right"><?php echo number_format($row_query['netqty'],2,'.','');?></td>

<td><?php echo number_format($row_query['price'],2,'.','');?></td>

<td align="right"><?php echo number_format($row_query['totalamount'],2,'.','');?></td>
<td><?php echo $row_query['modeof'];?></td>
<td><?php echo $row_query['remark'];?></td>
<td><?php echo $row_query['editby'];?></td>

</tr>
             </tbody>
             <?php
			 $ntqty+=$row_query['netqty'];
			$ntamount+=$row_query['totalamount'];
			 }
			 ?>
             <tr>
             <td colspan="11" align="right"><strong>Total&nbsp;Qty</strong></td>
             <td align="right"><strong><?php echo number_format($ntqty,2,'.','');?></strong></td>
             <td align="right"><strong>Total&nbsp;Amount</strong></td>
             <td align="right"><strong><?php echo number_format($ntamount,2,'.','');?></strong></td>
             </tr>
           </table>
  </div></div>
  
  
           <?php
		   }
		   ?> 
            <table  class="table table-striped table-bordered " cellspacing="0" width="100%">
              <thead>
                 <th>Sno</th>
                 <th>Product</th>
                 <th>UOM</th>
                 <th>Qty</th>
                  <th>Amount</th>
                 </thead>
           <?php
            $query_sum=mysqli_query($connect,"select product,productname,sum(netqty) as netqty,sum(totalamount) as total from second_transaction where status='' and customer='$customer' and date between '$fromdate' and '$todate' group by productname");
			$sno2=0;
			$sumamt=0;
		  while($row_sum=mysqli_fetch_array($query_sum))
		  {
		  //echo "select * from productdetail where productcode='".$row_sum['product']."' and status=''";
		  $qry=mysqli_query($connect,"select * from productdetail where productcode='".$row_sum['product']."' and status<>'Deleted'");
		  $rw=mysqli_fetch_array($qry,MYSQLI_ASSOC);
           ?>
           <tbody>
           <tr>
           <td align="center"><?php echo ++$sno2;?></td>
           <td align="center"><?php echo $row_sum['productname'];?></td>
          <td align="center"><?php echo $rw['unit'];?></td>
           <td align="right"><?php echo $row_sum['netqty'];?></td>
           <td align="right"><?php echo number_format($row_sum['total'],2,'.','');?></td>
           </tr>
           </tbody>
           <?php
		   $sumamt+=$row_sum['total'];
		   }
		   ?>
           <tr>
           <td align="right" colspan="4"><b>Net&nbsp;Amount</b></td>
           <td align="right"><strong><?php echo number_format($sumamt,2,'.','');?></strong></td>
           </tr>
           </table>
         </div>  
  </div>



</div>


    <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>
    $('.selectpro').select2({ width: '100%',height:'420px;' });
    
</script>
<script src="calendar-15/calendar-15/js/rome.js"></script>

<script src="calendar-15/calendar-15/js/main.js"></script>
<script src="js/jsontoexcel.js"></script>
</body>
</html>