<?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['sname'];
				//$intake_place=$_POST['intake_place'];


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


$sqlVehicleQuery = mysqli_query($conn,"SELECT * from supplier_master where stype='Diesel' ");

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

    $sname =$sname."<option value='".$prow['sname']."'>".$prow['sname']."</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 Report </strong></h3></div>
    <form method="post" action="reportDieselpurchase.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>Supplier</label>
    <select class="form-control input-lg selectpicker select-trip-vehicle" id="selecttripvehicle" data-live-search="true" name="sname">
        <option disabled selected>Please Supplier</option>
        <?php   echo $sname;   ?>
    </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 " style="border:none;">
        <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 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>Date</th>
                    <th>Vehicle.No</th>
                   
                   
                    
                    <th>Buyer Name</th> 
                    <th>Starting Hr</th>
                    <th>Closing Hr</th>
                      
                     <th>Running Hr</th> 
                     <th>Closing Km</th>
                     <th>Starting Km</th> 
                     <th>Running Km</th>                 
                    <th>Net. Litres</th>
                    <th>Rate/Lit. (Rs.)</th>
                    <!--<th>Average Km</th>-->
                    <th>Amount</th>
                </tr>
                </thead>
                <?php
				
				
				$sql = "SELECT dieseldate,sname,vehicleno,previouskms,previoushrs,intakekm,intakehrs,litres,rate,amount,mileage from  externalfill_diesel WHERE isdelete=1  ";

 
if($tripfromDate !=''  && $triptoDate !='' && $vehicleno =='') {
	 	$sql.=" and dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  order by dieseldate ";

	 }   else if($tripfromDate !=''  && $triptoDate !=''  && $vehicleno !='') {
		$sql.=" and   dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."' and sname='".$vehicleno."'  order by dieseldate   ";

   	 }  else if($tripfromDate ==''  && $triptoDate ==''  && $vehicleno !='' )  {
		$sql.="  and sname='".$vehicleno."'   order by dieseldate";

	} else if($tripfromDate ==''  && $triptoDate ==''  && $vehicleno !='' )  {
		$sql.="  and sname='".$vehicleno."'  order by dieseldate  ";

	}

	$query1=mysqli_query($conn, $sql) or die("brandgriddata2b.php: get brand Data step 1".$sql1);
	
while($row1=mysqli_fetch_array($query1))
{
	
	$closinghr= $row1["previoushrs"];
	$intakehr=$row1["intakehrs"];
	$runninghr=($intakehr) - ($closinghr);
	
	$dieseldate4=$row1["dieseldate"];
$date4=date_create($dieseldate4);
$dieseldate4=date_format($date4,"d-m-Y");
	
	
				
				?>
                <tr>
                 <td><?php echo ++$cnt1;?></td>
                <td><?php echo  $dieseldate4;?></td>
                <td><?php echo  $row1["vehicleno"];?></td>
		
		<td><?php echo  $row1["sname"];?></td>
		<td align="right"><?php echo  number_format($row1["previoushrs"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row1["intakehrs"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($runninghr,2,'.','');?></td>
        
        <td align="right"><?php echo  $row1["intakekm"];?></td>
        <td align="right"><?php echo  $row1["previouskms"];?></td>
        <td align="right"><?php echo  number_format($row1["intakekm"]-$row1["previouskms"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row1["litres"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row1["rate"],2,'.','');?></td>
       <!-- <td><?php //echo  $row1["mileage"];?></td>-->
        <td align="right"><?php echo  number_format($row1["amount"],2,'.','');?></td>
                
                
                </tr>
                
                <?php
}


?>


              <?php
				
				
				$sql1 = "SELECT dieseldate,sname,vehicleno,intakekm,intakehrs,litres,rate,amount from  externalpro_diesel WHERE isdelete=1 ";

 
if($tripfromDate !=''  && $triptoDate !='' && $vehicleno =='') {
	 	$sql1.=" and dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  order by dieseldate ";

	 }   else if($tripfromDate !=''  && $triptoDate !=''  && $vehicleno !='') {
		$sql1.=" and   dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."' and sname='".$vehicleno."'  order by dieseldate  ";

   	 }  else if($tripfromDate ==''  && $triptoDate ==''  && $vehicleno !='' )  {
		$sql1.="  and sname='".$vehicleno."'  order by dieseldate ";

	} else if($tripfromDate ==''  && $triptoDate ==''  && $vehicleno !='' )  {
		$sql1.="  and sname='".$vehicleno."'  order by dieseldate   ";

	}

	$query11=mysqli_query($conn, $sql1) or die("brandgriddata2j.php: get brand Data step 1".$sql1);
	
while($row11=mysqli_fetch_array($query11))
{
	
	$dieseldate5=$row11["dieseldate"];
$date5=date_create($dieseldate5);
$dieseldate5=date_format($date5,"d-m-Y");
				
				?>
                <tr>
                 <td><?php echo ++$cnt1;?></td>
                <td><?php echo  $dieseldate5;?></td>
                <td><?php echo  $row11["vehicleno"];?></td>
		
		<td><?php echo  $row11["sname"];?></td>
		<td><?php echo  "";?></td>
        <td align="right"><?php echo  number_format($row11["intakehrs"],2,'.','');?></td>
        <td><?php echo  "";?></td>
        
        
        <td><?php echo  "";?></td>
        <td align="right"><?php echo  $row11["intakekm"];?></td>
        <td><?php echo  "";?></td>
        <td align="right"><?php echo  number_format($row11["litres"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row11["rate"],2,'.','');?></td>
        <!--<td><?php //echo  $row11["mileage"];?></td>-->
        <td align="right"><?php echo  number_format($row11["amount"],2,'.','');?></td>
                
                
                </tr>
                
                <?php
}


?>

 <?php
				
				
				$sql2 = "SELECT dieseldate,sname,litres,rate1,amount from  purchase_dieselinvoice  WHERE isdelete=1 ";

 
if($tripfromDate !=''  && $triptoDate !='' && $vehicleno =='') {
	 	$sql2.=" and dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  order by dieseldate ";

	 }   else if($tripfromDate !=''  && $triptoDate !=''  && $vehicleno !='') {
		$sql2.=" and   dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."' and sname='".$vehicleno."'  order by dieseldate  ";

   	 }  else if($tripfromDate ==''  && $triptoDate ==''  && $vehicleno !='' )  {
		$sql2.="  and sname='".$vehicleno."'  order by dieseldate";

	} else if($tripfromDate ==''  && $triptoDate ==''  && $vehicleno !='' )  {
		$sql2.="  and sname='".$vehicleno."'  order by dieseldate  ";

	}

	$query2=mysqli_query($conn, $sql2) or die("brandgriddata2.php: get brand Data step 1".$sql2);
	
while($row2=mysqli_fetch_array($query2))
{
	
				
				?>
                <tr>
                 <td><?php echo ++$cnt1;?></td>
                <td><?php echo  $row2['dieseldate'];?></td>
                <td>CAN</td>
		
		<td><?php echo  $row2['sname'];?></td>
		<td><?php echo  "";?></td>
        <td><?php echo  "";?></td>
        <td><?php echo  "";?></td>
        
        
        <td><?php echo  "";?></td>
        <td><?php echo  "";?></td>
        <td><?php echo  "";?></td>
        <td align="right"><?php echo  number_format($row2["litres"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row2["rate1"],2,'.','');?></td>
        
        <td align="right"><?php echo  number_format($row2["amount"],2,'.','');?></td>
                
                
                </tr>
                
                <?php
}


?>



 
</table>

<table border="1" align="right" >
<?php
				
				
				
if($tripfromDate ==''  && $triptoDate =='' && $vehicleno =='') {
	 	$sql3=" select sname,sum(amount) as tot
from 
(
SELECT a.supplierid,a.sname, a.amount,a.dieseldate FROM `externalfill_diesel` a
union 
select b.supplierid,b.sname,b.amount,b.dieseldate from externalpro_diesel b
union 
select c.supplierid,c.sname,c.amount,c.dieseldate from purchase_dieselinvoice c
)d group by sname ";

	 }  
	 
	 else if($tripfromDate !=''  && $triptoDate !='' && $vehicleno =='') {
		 
		 $sql3="select sname,sum(amount) as tot
from 
(
SELECT a.supplierid,a.sname, a.amount,a.dieseldate FROM `externalfill_diesel` a
where (a.dieseldate  BETWEEN '".$tripfromDate."' AND '".$triptoDate."')
union all
select b.supplierid,b.sname,b.amount,b.dieseldate from externalpro_diesel b
where (b.dieseldate  BETWEEN '".$tripfromDate."' AND '".$triptoDate."')
union all
select c.supplierid,c.sname,c.amount,c.dieseldate from purchase_dieselinvoice c
where (c.dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."')
)d
group by d.sname ";
		 
	 }
	 
	 else if($tripfromDate !=''  && $triptoDate !='' && $vehicleno !='') {
		 
		 $sql3="select sname,sum(amount) as tot
from 
(
SELECT a.supplierid,a.sname, a.amount,a.dieseldate FROM `externalfill_diesel` a
where (a.dieseldate  BETWEEN '".$tripfromDate."' AND '".$triptoDate."'  and a.isdelete='1')
union all
select b.supplierid,b.sname,b.amount,b.dieseldate from externalpro_diesel b
where (b.dieseldate  BETWEEN '".$tripfromDate."' AND '".$triptoDate."' and b.isdelete='1')
union all
select c.supplierid,c.sname,c.amount,c.dieseldate from purchase_dieselinvoice c
where (c.dieseldate BETWEEN '".$tripfromDate."' AND '".$triptoDate."' and c.isdelete='1')
)d where d.sname='".$vehicleno."'
group by d.sname ";
		 
	 }
	 
	 
	 else if($tripfromDate ==''  && $triptoDate =='' && $vehicleno !='') {
		 
		 $sql3="select sname,sum(amount) as tot
from 
(
SELECT a.supplierid,a.sname, a.amount,a.dieseldate FROM `externalfill_diesel` a
union all
select b.supplierid,b.sname,b.amount,b.dieseldate from externalpro_diesel b
union all
select c.supplierid,c.sname,c.amount,c.dieseldate from purchase_dieselinvoice c
)d where d.sname='".$vehicleno."'
group by d.sname ";
		 
	 }
	 

	$query3=mysqli_query($conn, $sql3) or die("brandgriddata3.php: get brand Data step 1".$sql3);
	
while($row3=mysqli_fetch_array($query3))
{
	
				
				?>
                <tr>
                 
                
		
		<td ><?php echo  $row3['sname'];?></td>
		<td>&nbsp;</td>
        <td align="right" ><?php echo  number_format($row3["tot"],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>