<?PHP

ob_start();

session_start();

include 'connect.php';

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

{

$user=$_SESSION['adminemail'];



$gtp=mysql_query("select * from admin where username='$user'");

$gtp1=mysql_fetch_array($gtp);



include 'top.php';

?>







			<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=mysql_query("select * from financial where user='$user' order by ID desc");

$aca1=mysql_fetch_array($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>



<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=mysql_query($z) or die(mysql_error());

while($z1=mysql_fetch_array($z))

{



$date=$z1['date'];



if($date!=$prevdate)

{



$sno=$sno+1;







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

{



$select=mysql_query("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(mysql_error());

$select2=mysql_fetch_array($select);



$fk=$select2['fillingkm'];



$sl=mysql_query("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=mysql_fetch_array($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=mysql_query("select * from fuel where vehicleno='$vehicleno' and date<'$z1[date]' order by date desc, ID desc limit 1");

$ssl1=mysql_fetch_array($ssl);



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

$ssk1=mysql_fetch_array($ssk);



$pc=$ssk1['closingkm'];

$pq=$ssl1['qty'];



}



else

{



$select=mysql_query("select * from fuel where date='$z1[date]' and vehicleno='".$vehicleno."'") or die(mysql_error());

$select1=mysql_fetch_array($select);



$fk=$select1['fillingkm'];



if($fk!='')

{





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

$sl1=mysql_fetch_array($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=mysql_query("select * from delivery where vehicleno='$vehicleno' and date='$z1[date]'");

$flmc1=mysql_fetch_array($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 name='vehicleno' style='width:220px;' id='required' title='*'>



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



$rgno=mysql_query("select * from transport order by ID asc");

while($rgno1=mysql_fetch_array($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 type='text' name='fromdate' class='fromdate' id='required' title='*' autocomplete='off' /></td></tr>



<tr><td>To Date <span class='red'>*</span></td><td style='padding:0 5px;'>:</td><td><input type='text' name='todate' class='todate' id='required' title='*' autocomplete='off' /></td></tr>



<tr><td colspan='3' style='text-align:center;'><button type='submit' class='btn btn-sm 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 -->





<?php

include 'bottom.php';

}

else

{



 header("location:login.php");

}



ob_flush();



?>