<?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'];


$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 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>Internal Diesel Report </strong></h3></div>
    <form method="post" action="reportInDieselDashboard.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 Internal Diesel Entry</strong></div>
<p><button class="btn btn-primary" onClick="tableToExcel('a', 'W3C Example Table')">Download</button></p>
<div id="a">

<h2>Diesel Internal Filling 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>
            
            <caption>Internal Filling</caption>
             
            
                <tr>				
                    
                <th>S.No</th>
                      <th>Date</th>
                    <th>Vehicle.No</th>
                    <th>Driver Name</th>
                    <th>Incharge Name</th>
                    <th>Previous KM</th>
                    <th>Previous Hrs</th>
                    <th>Previous Filling</th>
                    <th>Intake Km</th>                    
                    <th>Intake Hrs</th>
                    <th>Intake Qty</th>
                </tr>
                </thead>
                <?php
				
				
				$sql = "SELECT ifd.dieseldate,ifd.drivername,ifd.inchargename,ifd.previouskms,ifd.previoushrs,ifd.preiousqty,ifd.intakekm,ifd.intakehrs,ifd.litres,ifd.vehicleid,vm.vehicleno from internalpro_diesel as ifd inner join vehicle_master as vm on vm.id=ifd.vehicleid WHERE ifd.isdelete=1 ";

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

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

   	 }  else if($tripfromDate ==''  && $triptoDate ==''  && $vehicleno !='' )  {
		$sql.="  and vehicleid='".$vehicleno."' ";
}
	


	$query=mysqli_query($conn, $sql) or die("brandgriddata1.php: get brand Data step 1".$sql);
	 $tmp=0;
	 $cnt=0;
while($row=mysqli_fetch_array($query))
{
	$tmp+=$row['litres'];
				
				$dieseldate=$row["dieseldate"];
$date=date_create($dieseldate);
$dieseldate1=date_format($date,"d-m-Y");

$vid=$row["vehicleid"];

//echo "select *  FROM vehicle_master WHERE isdelete=1 and id='$vid'";




				
				?>
                <tr>
                 <td><?php echo ++$cnt;?></td>
                <td><?php echo  $dieseldate1;?></td>
                <td><?php echo  $row["vehicleno"];?></td>
		<td><?php echo  $row["drivername"];?></td>
		<td><?php echo  $row["inchargename"];?></td>
		<td align="right"><?php echo  $row["previouskms"];?></td>
	
		<td align="right"><?php echo  number_format($row["previoushrs"],2,'.','');?></td>
		<td align="right"><?php echo  number_format($row["preiousqty"],2,'.','');?></td>
		<td align="right"><?php echo  $row["intakekm"];?></td>
		<td align="right"><?php echo  number_format($row["intakehrs"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row["litres"],2,'.','');?></td>
                
                
                </tr>
                
                <?php
				
				
}

$sqlAStockQuery = mysqli_query($conn,"SELECT sum(litres) as astock   FROM purchase_dieselinvoice WHERE isdelete=1 ");
$AStockrow = mysqli_fetch_array($sqlAStockQuery,MYSQLI_ASSOC);

$AStock =($AStockrow['astock']-$tmp);
//echo $tmp;
?>
</table>


<table id="reportTripsTDashboard" class="table table-striped table-bordered dt-responsive wrap" cellspacing="0" style="width:100%" >
            <thead>
            
            <caption>Internal Unfilling</caption>
             
            
                <tr>				
                    
                <th>S.No</th>
                      <th>Date</th>
                    <th>Vehicle.No</th>
                   <th>Driver Name</th>
                   
                    
                    <th>Intake Km</th>                    
                    <th>Intake Hrs</th>
                    <th>Intake Qty</th>
                </tr>
                </thead>
                <?php
				
				
				$sql1 = "SELECT iuf.dieseldate,vm.vehicleno,iuf.drivername,iuf.intakekm,iuf.intakehrs,iuf.litres,iuf.vehicleid from internalunfillpro_diesel as iuf inner join vehicle_master as vm on iuf.vehicleid=vm.id  WHERE iuf.isdelete=1 ";

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

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

   	 }  else if($tripfromDate ==''  && $triptoDate ==''  && $vehicleno !='' )  {
		$sql1.="  and vehicleid='".$vehicleno."' ";

	}


	$query1=mysqli_query($conn, $sql1) or die("brandgriddata2.php: get brand Data step 1".$sql1);
	 $tmp1=0;
	 $cnt1=0;
while($row1=mysqli_fetch_array($query1))
{
	$tmp1+=$row1['litres'];
	
	$dieseldateuf=$row1["dieseldate"];
$date1=date_create($dieseldateuf);
$dieseldate2=date_format($date1,"d-m-Y");
				
				?>
                <tr>
                 <td><?php echo ++$cnt1;?></td>
                <td><?php echo  $dieseldate2;?></td>
                <td><?php echo  $row1["vehicleno"];?></td>
                 <td><?php echo  $row1["drivername"];?></td>
		
		
		
		<td align="right"><?php echo  $row1["intakekm"];?></td>
		<td align="right"><?php echo  number_format($row1["intakehrs"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row1["litres"],2,'.','');?></td>
                
                
                </tr>
                
                <?php
}

$sqlAStockQuery = mysqli_query($conn,"SELECT sum(litres) as astock   FROM purchase_dieselinvoice WHERE isdelete=1 and status='' ");
$AStockrow = mysqli_fetch_array($sqlAStockQuery,MYSQLI_ASSOC);
$b=$tmp+$tmp1;
//echo $b;
$AStock =($AStockrow['astock']-$b);
?>
</table>



            <table align="right">
            <tr>
            <td>Total Consumed Litres:</td>
            <td><strong><?php echo $tmp;?></strong></td>
            </tr>
            
            <tr>
            <td align="right">Current Stock:</td>
            <td><strong><?php echo $AStock;?></strong></td>
            </tr>
            
           </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>