<?php 
error_reporting(0);
require_once("config/configDB.php"); 
//session_start();
require "userPermission.php";
require 'financial_year_calculation.php';

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


$internalprodate_temp   = new DateTime(stripslashes($_POST['fromDate']));
    $tripfromDate1 		= $internalprodate_temp->format('d-m-Y');
	
	$internalprodate_temp1   = new DateTime(stripslashes($_POST['toDate']));
    $triptoDate1 		= $internalprodate_temp1->format('d-m-Y');


$sqlVehicleQuery = mysqli_query($conn,"SELECT DISTINCT id, vehicleno  FROM vehicle_master WHERE isdelete=1 and  brandtype LIKE 'equipment' OR  brandtype LIKE 'transport' ");

$sqlVehicleNo = '';
//$prow = mysqli_fetch_array($sqlBrandQuery,MYSQLI_NUM))
while($prow = mysqli_fetch_array($sqlVehicleQuery)){

    $sqlVehicleNo =$sqlVehicleNo."<option value='".$prow['id']."'>".$prow['vehicleno']."</option>";
}



//$sqlVehicleQuery = mysqli_query($conn,"SELECT *  FROM supplier_master  ");

//$sname = '';
//$prow = mysqli_fetch_array($sqlBrandQuery,MYSQLI_NUM))
//while($prow = mysqli_fetch_array($sqlVehicleQuery)){

    //$sname =$sname."<option value='".$prow['sname']."'>".$prow['sname']."</option>";

//}


?>

<!DOCTYPE html>
<html>
<head>
    <?php 
        require 'meta.php';  
        require 'header.php'; 
    ?>
</head>


<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>

<body>
    <?php require 'menu.php' ?>
    <div class="col-sm-12 "> <h3 class="text-center"><strong>Diesel Mileage Report </strong></h3></div>
    <form method="post" action="reportmileage.php" >
<div class="container-fluid text-center col-sm-12 dashboard-add">  
<?php if($reportTrips_Add==1) {  ?>  
<!-- <a href="reportTripsAdd.php" class="btn btn-primary">Add</a> -->

<?php } ?>
<!-- <div class=" col-sm-2">
    <label>Category</label>
    <select class="form-control input-lg select-purchase-category" data-live-search="true" name="purchaseInvoice" id="trip_category" >
        <option value="transportation"  selected >Transportation Vehicle</option>
        <option value="equipment"   >Equipment Vehicle</option>
    </select> 
</div> -->
<div class=" col-sm-2">
    <label>From Date</label>
    <input type="text" class="form-control input-lg" id="fromDate" name="fromDate" autocomplete="off">
   
</div>
<div class=" col-sm-2">
    <label>To Date</label>
    <input type="text" class="form-control input-lg" id="toDate" name="toDate" autocomplete="off">
    
</div>

<div class=" col-sm-2">
    <label>Vehicle</label>
    <select class="form-control input-lg selectpicker select-trip-vehicle" id="selecttripvehicle" data-live-search="true" name="vehicleno">
        <option disabled selected>Please Select Vehicle</option>
        <?php   echo $sqlVehicleNo;   ?>
    </select> 
    
</div>




<div class=" col-sm-1" style="padding-top: 24px;">
<button type="submit" class="btn btn-primary btn-lg datewise-submit-sale">Apply</button>
</div>


<!--<div class=" col-sm-1" style="padding-top: 24px;">
<a href="printdiesel.php?fdate=<?php //echo $tripfromDate;?> && tdate=<?php //echo $triptoDate;?>&& vehicleno=<?php //echo $vehicleno;?>  " target="_blank"><input type="button" class="btn btn-primary btn-lg datewise-submit-sale" value="Print"></a>
</div>-->

<div class=" col-sm-1" style="padding-top: 24px;">
<button onClick="printDiv();" class="btn btn-primary btn-lg datewise-submit-sale">Print it</button>
</div>

</div>
</form>
<div class="container col-sm-12 ">
    <div class="datatable-header ">
        <div class="col-sm-12 box-title"><strong>Manage your vehicleno Purchase</strong></div>
<p><button class="btn btn-primary" onClick="tableToExcel('a', 'W3C Example Table')">Download</button></p>
<div id="a">

<h2>Diesel Mileage Report (From:<?php echo $tripfromDate1;?>-To:<?php echo $triptoDate1;?>)</h2>

        <table id="reportTripsTDashboard" class="table table-striped table-bordered dt-responsive wrap" cellspacing="0" style="width:100%" >
            <thead>
            
            
             
            
                <tr>				
                    
                <th>S.No</th>
                      
                    <th>Vehicle.No</th>
                   <!-- <th>Driver Name</th>-->
                    <th>Opening Hrs</th>
                    <th>Closing Hrs</th>
                    <th>Running Hrs</th> 
                    <th>Opening Km</th>
                    <th>Closing Km</th>
                    <th>Running KM</th>                   
                    <th>Consumed Ltrs</th>
                    <th>Hour Consumption</th>
                    <th>Standard Km</th>
                    <th>Present  Mileage</th>
                    
                </tr>
                </thead>
                <?php
				
				if($tripfromDate!='' && $triptoDate!='' && $vehicleno=='')
				{
				
				  $sql="select min(intakekm) as min_km,max(intakekm) as max_km,max(intakehrs) as max_hrs,min(intakehrs) as min_hrs,drivername,vehicleno,vehicleid  from fill_diesel  where dieseldate BETWEEN '".$tripfromDate."' and '".$triptoDate."'  group by vehicleid";


//echo $sql;




	 $query=mysqli_query($conn, $sql) or die("brandgriddata1.php: get brand Data step 1".$sql);
	 $cnt=0;
while($row=mysqli_fetch_array($query))
{

	$vno=$row['vehicleid'];
	$op=$row['min_km'];
	
	$sql1="select sum(litres) as ltr from total_litres where vehicleid='".$vno."' and  dieseldate BETWEEN '".$tripfromDate."' and '".$triptoDate."' and isdelete='1'";
	$query1=mysqli_query($conn, $sql1) or die("brandgriddata1.php: get brand Data step 1".$sql1);
	
	$svl="select companymileage from vehicle_master where id='".$vno."'";
	$vl=mysqli_query($conn, $svl) or die("brandgriddata1.php: get brand Data step 1".$svl);
	
	$mil="select litres as mltr from fill_diesel where intakekm='".$op."' and  vehicleid='".$vno."' and dieseldate BETWEEN '".$tripfromDate."' and '".$triptoDate."' group by vehicleid";
	$ml=mysqli_query($conn, $mil) or die("brandgriddata1.php: get brand Data step 1".$mil);

//echo $mil;
	 
while($vh=mysqli_fetch_array($vl))
{

while($row1=mysqli_fetch_array($query1))
{

while($milg=mysqli_fetch_array($ml))
{
	$minltr=number_format($milg['mltr'],2,'.','');
	
	
	$cmilieage=$vh['companymileage'];
				$opeingkm=$row['min_km'];
				
				$closingkm=$row['max_km'];
				$totltrs=number_format($row1['ltr'],2,'.','');
				
				$minusltr=($totltrs) - ($minltr);
				
				$runningkm=($closingkm) - ($opeingkm);
				
				$avgkm=($runningkm)/ ($minusltr);
				
				$opeinghr=$row['min_hrs'];
				
				$closinghr=$row['max_hrs'];
				$runninghr=($closinghr) - ($opeinghr);
				
				$avghr=($minusltr) / ($runninghr);

				
				
	?>
	<tr>
                 <td><?php echo ++$cnt;?></td>
                 <td><?php echo  $row['vehicleno'];?></td>
               <!-- <td><?php echo  $row['drivername'];?></td>-->
		<td align="right"><?php echo  $row['min_hrs'];?></td>
		<td align="right"><?php echo  $row['max_hrs'];?></td>
	
		<td align="right"><?php echo  number_format($runninghr,2,'.','');?></td>
        <td align="right"><?php echo  $row['min_km'];?></td>
                <td align="right"><?php echo  $row['max_km'];?></td>
		<td align="right"><?php echo  number_format($runningkm,2,'.','');?></td>
		<td align="right"><?php echo  number_format($minusltr,2,'.','');?></td>
        <td align="right"><?php echo  number_format($avghr,2,'.','');?></td>
        <td align="right"><?php echo  $cmilieage;?></td>
		<td align="right"><?php echo  number_format($avgkm,2,'.','');?></td>
        
		
                
                
                </tr>
				<?php
	
}
}
}
}
}


if($tripfromDate!='' && $triptoDate!='' && $vehicleno!='')
				{
				
				  $sql="select min(intakekm) as min_km,max(intakekm) as max_km,max(intakehrs) as max_hrs,min(intakehrs) as min_hrs,drivername,vehicleno,vehicleid  from fill_diesel  where dieseldate BETWEEN '".$tripfromDate."' and '".$triptoDate."' and vehicleid='".$vehicleno."'  group by vehicleid";


//echo $sql;




	 $query=mysqli_query($conn, $sql) or die("brandgriddata1.php: get brand Data step 1".$sql);
	 $cnt=0;
while($row=mysqli_fetch_array($query))
{

	$vno=$row['vehicleid'];
	$op=$row['min_km'];
	
	$sql1="select sum(litres) as ltr from total_litres where vehicleid='".$vno."' and  dieseldate BETWEEN '".$tripfromDate."' and '".$triptoDate."' and isdelete='1'";
	$query1=mysqli_query($conn, $sql1) or die("brandgriddata1.php: get brand Data step 1".$sql1);
	
	$svl="select companymileage from vehicle_master where id='".$vno."'";
	$vl=mysqli_query($conn, $svl) or die("brandgriddata1.php: get brand Data step 1".$svl);
	
	$mil="select litres as mltr from fill_diesel where intakekm='".$op."' and  vehicleid='".$vno."' and dieseldate BETWEEN '".$tripfromDate."' and '".$triptoDate."' group by vehicleid";
	$ml=mysqli_query($conn, $mil) or die("brandgriddata1.php: get brand Data step 1".$mil);

	 
while($vh=mysqli_fetch_array($vl))
{

while($row1=mysqli_fetch_array($query1))
{

while($milg=mysqli_fetch_array($ml))
{
	$minltr=number_format($milg['mltr'],2,'.','');
	
	
	$cmilieage=$vh['companymileage'];
				$opeingkm=$row['min_km'];
				
				$closingkm=$row['max_km'];
				$totltrs=number_format($row1['ltr'],2,'.','');
				
				$minusltr=($totltrs) - ($minltr);
				
				$runningkm=($closingkm) - ($opeingkm);
				
				$avgkm=($runningkm)/ ($minusltr);
				
				$opeinghr=$row['min_hrs'];
				
				$closinghr=$row['max_hrs'];
				$runninghr=($closinghr) - ($opeinghr);
				
				$avghr=($minusltr) / ($runninghr);

				
				
	?>
	<tr>
                 <td><?php echo ++$cnt;?></td>
                 <td><?php echo  $row['vehicleno'];?></td>
               <!-- <td><?php echo  $row['drivername'];?></td>-->
		<td align="right"><?php echo  $row['min_hrs'];?></td>
		<td align="right"><?php echo  $row['max_hrs'];?></td>
	
		<td align="right"><?php echo  number_format($runninghr,2,'.','');?></td>
        <td align="right"><?php echo  $row['min_km'];?></td>
                <td align="right"><?php echo  $row['max_km'];?></td>
		<td align="right"><?php echo  number_format($runningkm,2,'.','');?></td>
		<td align="right"><?php echo  number_format($minusltr,2,'.','');?></td>
        <td align="right"><?php echo  number_format($avghr,2,'.','');?></td>
        <td align="right"><?php echo  $cmilieage;?></td>
		<td align="right"><?php echo  number_format($avgkm,2,'.','');?></td>
        
		
                
                
                </tr>
				<?php
	
}
}
}
}
}




else if($tripfromDate=='' && $triptoDate=='' && $vehicleno=='')
				{
				
				  $sql="select max(intakekm) as max_km,min(intakekm) as min_km,max(intakehrs) as max_hrs,min(intakehrs) as min_hrs,drivername,vehicleno,vehicleid  from fill_diesel    group by vehicleid";







	 $query=mysqli_query($conn, $sql) or die("brandgriddata1.php: get brand Data step 1".$sql);
	 $cnt=0;
while($row=mysqli_fetch_array($query))
{

	$vno=$row['vehicleid'];
	$op=$row['min_km'];
	
	$sql1="select sum(litres) as ltr from total_litres where vehicleid='".$vno."' ";
	$query1=mysqli_query($conn, $sql1) or die("brandgriddata1.php: get brand Data step 1".$sql1);
	
	$svl="select companymileage from vehicle_master where id='".$vno."'";
	$vl=mysqli_query($conn, $svl) or die("brandgriddata1.php: get brand Data step 1".$svl);
	
	$mil="select litres as mltr from fill_diesel where intakekm='".$op."' and vehicleid='".$vno."' group by vehicleid";
	$ml=mysqli_query($conn, $mil) or die("brandgriddata1.php: get brand Data step 1".$mil);







while($vh=mysqli_fetch_array($vl))
{

while($row1=mysqli_fetch_array($query1))
{

while($milg=mysqli_fetch_array($ml))
{
	$minltr=number_format($milg['mltr'],2,'.','');
	
	
	$cmilieage=$vh['companymileage'];
				$opeingkm=$row['min_km'];
				
				$closingkm=$row['max_km'];
				$totltrs=number_format($row1['ltr'],2,'.','');
				
				$minusltr=($totltrs) - ($minltr);
				
				$runningkm=($closingkm) - ($opeingkm);
				
				$avgkm=($runningkm)/ ($minusltr);
				
				$opeinghr=$row['min_hrs'];
				
				$closinghr=$row['max_hrs'];
				$runninghr=($closinghr) - ($opeinghr);
				
				$avghr=($minusltr)/ ($runninghr);

				
				
	?>
	<tr>
                 <td><?php echo ++$cnt;?></td>
                 <td><?php echo  $row['vehicleno'];?></td>
              <!--  <td><?php echo  $row['drivername'];?></td>-->
		<td align="right"><?php echo  $row['min_hrs'];?></td>
		<td align="right"><?php echo  $row['max_hrs'];?></td>
	
		<td align="right"><?php echo  number_format($runninghr,2,'.','');?></td>
        <td align="right"><?php echo  $row['min_km'];?></td>
                <td align="right"><?php echo  $row['max_km'];?></td>
		<td align="right"><?php echo  $runningkm;?></td>
		<td align="right"><?php echo  number_format($minusltr,2,'.','');?></td>
        <td align="right"><?php echo  number_format($avghr,2,'.','');?></td>
        <td align="right"><?php echo  $cmilieage;?></td>
		<td align="right"><?php echo  number_format($avgkm,2,'.','');?></td>
        
		
                
                
                </tr>
				<?php
	
}
}
}
}
}


?>
</table>






            
           
          
           







            
           </div>
    </div>
    
</div>


<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>

    <?php require 'footer.php' ?>
    <script type="text/javascript">var base_url = "<?= $base_url ?>";</script>
    
    <script type="text/javascript" src="js/plugin/jquery-ui.min.js"></script>

    <script>
    
$(document).ready(function() {

   
                
    $("#fromDate").datepicker({
       
        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").datepicker({
        minDate: '1',
        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>