
<?php  
require_once("config/configDB.php"); 
require "userPermission.php";
error_reporting(0);
if(isset($_POST["query"]))  
 {  
	if($_POST["query"]!=""){
      $output = '';  
	  $vehicleid = $_POST["query"];
	  
	  
      $query = "SELECT max(id) as a FROM internalpro_diesel WHERE vehicleid='".$vehicleid."'";
      $exec = mysqli_query($conn,$query) or die ("Error in Query".mysqli_error());
      $row = mysqli_fetch_array($exec);
	  $id = $row['a'];
	  
	  $queryv = "SELECT * FROM vehicle_master WHERE id='".$vehicleid."'";
      $execv = mysqli_query($conn,$queryv) or die ("Error in Query".mysqli_error());
      $rowv = mysqli_fetch_array($execv);
	  $vno = $rowv['openingkm'];
	  $ohs = $rowv['openinghrs'];
	 

$query1 = mysqli_query($conn,"select max(intakekm) as max_km,
min(intakekm) as min_km,
max(intakehrs) as max_hrs,
min(intakehrs) as min_hrs,
sum(litres) as ltr,vehicleno,vehicleid 
from (
select vehicleno,intakekm,intakehrs,litres,vehicleid
from 
(select a.vehicleno,a.intakekm,a.intakehrs,a.litres,a.vehicleid from internalpro_diesel a

UNION
select c.vehicleno,c.intakekm,c.intakehrs,c.litres,c.vehicleid from externalfill_diesel c

)e
)fg
where fg.vehicleid='".$vehicleid."'
group by fg.vehicleno;");

while($rown=mysqli_fetch_array($query1))
{
	
	$closingkm=$rown['max_km'];
	$closinghr=$rown['max_hrs'];
}
	 
	 $queryl = "SELECT litres FROM total_litres WHERE vehicleid='".$vehicleid."' and intakekm='".$closingkm."'";
      $execl = mysqli_query($conn,$queryl) or die ("Error in Query".mysqli_error());
      $rowl = mysqli_fetch_array($execl);
	  $litres1 = $rowl['litres'];
	  
	 
	 
	if($closingkm=='')
	 {
		 $intakekm=$vno;
	 }
	 else
	 {
		 $intakekm=$closingkm;
	 }
	 
	 if($closinghr=='')
	 {
		 $intakehrs=$ohs;
	 }
	 else
	 {
		 $intakehrs=$closinghr;
	 }
	 
	 if($litres1=='')
	 {
		 $litres=0;
	 }
	 else
	 {
		 $litres=$litres1;
	 }
   		
		
		
      $output = $intakekm.'||'.$intakehrs.'||'.$litres;
	  
	  //echo $output; 
	  //echo $vno;
	}  
}
echo $output;  
 ?>  

 