<?PHP

ob_start();

session_start();

include 'connect.php';

if(isset($_SESSION['adminemail']))

{

$user=$_SESSION['adminemail'];



$gtp=mysqli_query($conn,"select * from admin where username='$user'");

$gtp1=mysqli_fetch_assoc($gtp);



include 'top.php';

?>






<div id="page-wrapper" >
          <div id="page-inner">
			<div class="main-content">

				<div class="main-content-inner">

					<div class="breadcrumbs ace-save-state" id="breadcrumbs">

						<ul class="breadcrumb">

							<li>

								<i class="ace-icon fa fa-home home-icon"></i>

								<a href="#">Home</a>

							</li>

							<li class="active">Fuel Report</li>

						</ul><!-- /.breadcrumb -->

					</div>



					<div class="page-content">

			



						<div class="page-header">

                        

<?php



$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");

$aca1=mysqli_fetch_assoc($aca);



$financial=$aca1['financial'];



?>

                        

                        <table style="width:100%;">

                        

                        <tr><td style="width:50%;">

                        

							<h1>

								Fuel Report

							</h1>

                            

                            </td>

                            

                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">

                            

                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>

                            

                            </td></tr></table>

                            

						</div><!-- /.page-header -->



						<div class="row">

							<div class="col-xs-12">

                            

								<!-- PAGE CONTENT BEGINS -->





<script src="assets/js/jquery.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
<script>
  $(function() {
  // Initialize the datepicker for the "From Date" input
  $("#from-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });

  // Initialize the datepicker for the "To Date" input
  $("#to-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });
});
</script>

<script>

	

jQuery(function($){

		   

	// simple jQuery validation script

	$('#course').submit(function(){

		

		var valid = true;

		var errormsg = 'This field is required!';

		var errorcn = 'error';

		

		$('.' + errorcn, this).remove();			

		

		$('#required', this).each(function(){

			var parent = $(this).parent();

			if( $(this).val() == '' ){

				var msg = $(this).attr('title');

				msg = (msg != '') ? msg : errormsg;

				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')

					.appendTo(parent)

					.fadeIn('fast')

					.click(function(){ $(this).remove(); });

				valid = false;

			};

		});

		

		return valid;

	});

	

});	



</script>   

								

										

                                        

<?php



if(isset($_POST['submit']))

{

	$vehicleno=$_POST['vehicleno'];

	$fromdate=$_POST['fromdate'];

	$todate=$_POST['todate'];

	

	

echo"<br /><p style='padding-left:5%;'>Vechicle Regno: <b>".$vehicleno."</b></p>

<table class='table table-striped table-bordered table-hover' style='width:95%; margin:auto;'>

<tr height='30'><th style='width:10%;'>Date</th><th style='width:15%;'>Filling KM / Hour</th>

<th style='width:15%;'>Diesel Qty</th><th style='width:15%;'>Closing Km / Hour</th>

<th style='width:15%;'>AVG</th><th style='width:15%;'>Balance Fuel</th></tr>";



$fromdate=date('Y-m-d',strtotime($fromdate));

$todate=date('Y-m-d',strtotime($todate));



$sno=0;

$milage=0;

$pq=0;

$tf=0;

$pc=0;



$z="select date as date, endkm as closingkm, null as fillingkm, null as qty from delivery where  date between '$fromdate' and '$todate'and vehicleno='$vehicleno'

UNION

select date as date, null as closingkm, fillingkm as fillingkm, qty as qty from fuel where date between '$fromdate' and '$todate' and vehicleno='$vehicleno'

order by date asc";



$z=mysqli_query($conn,$z) or die(mysqli_error($conn));

while($z1=mysqli_fetch_assoc($z))

{



$date=$z1['date'];



if($date!=$prevdate)

{



$sno=$sno+1;







if(date('d',strtotime($z1['date']))==$fromdate)

{



$select=mysqli_query($conn,"select * from fuel where fillingkm!='$fk' and topup='0' and vehicleno='".$vehicleno."' and date<'$date' order by date desc, ID desc limit 1") or die(mysqli_error($conn));

$select2=mysqli_fetch_assoc($select);



$fk=$select2['fillingkm'];



$sl=mysqli_query($conn,"select * from fuel where fillingkm!='$fk' and topup='0' and regno='".$vehicleno."' and date<'$select2[date]' order by date desc, ID desc limit 1");

$sl2=mysqli_fetch_assoc($sl);



if($select2['topup']==1)

{



$tf=$tf+$select2['qty'];



}



else

{



$tf=$tf+$select2['qty'];



$avg=($fk-$sl2['fillingkm'])/$tf;

$avg=number_format($avg,2,'.','');



$tf=0;

}



$ssl=mysqli_query($conn,"select * from fuel where vehicleno='$vehicleno' and date<'$z1[date]' order by date desc, ID desc limit 1");

$ssl1=mysqli_fetch_assoc($ssl);



$ssk=mysqli_query($conn,"select * from delivery where vehicleno='$vehicleno' and date='$ssl1[date]' order by ID desc limit 1");

$ssk1=mysqli_fetch_assoc($ssk);



$pc=$ssk1['closingkm'];

$pq=$ssl1['qty'];



}



else

{



$select=mysqli_query($conn,"select * from fuel where date='$z1[date]' and vehicleno='".$vehicleno."'") or die(mysqli_error($conn));

$select1=mysqli_fetch_assoc($select);



$fk=$select1['fillingkm'];



if($fk!='')

{





$sl=mysqli_query($conn,"select * from fuel where fillingkm!='$fk' and topup='0' and vehicleno='".$vehicleno."' and date<'$date' order by date desc, ID desc limit 1");

$sl1=mysqli_fetch_assoc($sl);



if($select1['topup']==1)

{



$tf=$tf+$select1['qty'];



}



else

{



$tf=$tf+$select1['qty'];



$avg=($fk-$sl1['fillingkm'])/$tf;

$avg=number_format($avg,2,'.','');



$tf=0;

}



}



}





//closing in loop date

$flmc=mysqli_query($conn,"select * from delivery where vehicleno='$vehicleno' and date='$z1[date]'");

$flmc1=mysqli_fetch_assoc($flmc);



$cc=$flmc1['endkm'];



if($avg!=0)

{



$bf=$cc-$pc;



$bf=$bf/$avg;



$tq=$pq+$select1['qty'];



$bal=$tq-$bf;



//echo"$tq & $bf<br />";



$bal=number_format($bal,2,'.','');



}



echo"<tr><td>".date('d-m-Y',strtotime($z1['date']))."</td>

<td style='text-align:right;'>".$select1['fillingkm']."</td>

<td style='text-align:right;'>".$select1['qty']."</td>

<td style='text-align:right;'>".$flmc1['endkm']."</td><td style='text-align:right;'>";



if($sl1['fillingkm']!='' || $sl2['fillingkm']!='')

{

echo"".$avg."";

}



echo"</td><td style='text-align:right;'>";



if($sl1['fillingkm']!='' || $sl2['fillingkm']!='')

{

echo"".$bal."";

}



echo"</td></tr>";











if($select1['fillingkm']!=''&&$select1['qty']!='')

{

$pq=$select1['qty'];

$pc=$flmc1['endkm'];

}





$prevdate=$z1['date'];



}



}

echo"</table>";	

	

	

}

else

{



echo"<form action='fuel_report.php' method='post' id='course'>



<table style='margin:auto; line-height:35px;'>



<tr><td>Vehicle Reg No. <span class='red'></span></td><td style='padding:0 5px;'></td><td><select class='form-control' style='width: 30ch; margin-bottom:10px' name='vehicleno' style='width:220px;' id='required' title='*'>



<option value=''>Select Vehicle No.</option>";



$rgno=mysqli_query($conn,"select * from transport order by ID asc");

while($rgno1=mysqli_fetch_assoc($rgno))

{

	echo"<option value='".$rgno1['vehicleno']."'>".$rgno1['vehicleno']."</option>";

}



echo"</select></td></tr>



<tr><td>From Date <span class='red'></span></td><td style='padding:0 5px;'></td><td><input class='form-control' style='width: 30ch; margin-bottom:10px'  type='text' name='fromdate' class='fromdate' id='from-datepicker' title='*' autocomplete='off' /required></td></tr>



<tr><td>To Date <span class='red'></span></td><td style='padding:0 5px;'></td><td><input class='form-control' style='width: 30ch; margin-bottom:10px' type='text' name='todate' class='todate' id='to-datepicker' title='*' autocomplete='off' /required></td></tr>



<tr><td colspan='3' style='text-align:center;'><button type='submit' style='margin-left: 70px;' class='btn btn btn-primary' name='submit' value='Submit'>Submit</button></td></tr>



</table>



</form>";	

	

}



?>



                                            

                                            							



										<!-- PAGE CONTENT ENDS -->

									</div><!-- /.col -->

								</div><!-- /.row -->

							</div><!-- /.page-content -->

						</div>

					</div><!-- /.main-content -->
					</div><!-- /.main-content -->
					</div><!-- /.main-content -->





<?php

include 'bottom.php';

}

else

{



 header("location:login.php");

}



ob_flush();



?>