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


$internalprodate_temp   = new DateTime(stripslashes($_POST['fromDate']));
    $tripfromDate1 		= $internalprodate_temp->format('d-m-Y');
	
	$internalprodate_temp1   = new DateTime(stripslashes($_POST['toDate']));
    $triptoDate1 		= $internalprodate_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>External Diesel Report </strong></h3></div>
    <form method="post" action="reportExDieselDashboard.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 External Diesel Purchase</strong></div>
<p><button class="btn btn-primary" onClick="tableToExcel('a', 'W3C Example Table')">Download</button></p>
<div id="a">

<h2>External Diesel 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>External  Filling</caption>
            
                <tr>				
                    
                <th>S.No</th>
                      <th>Date</th>
                      <th>Supplier</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>
                    
                    <th>Rate</th>                    
                    <th>Amount</th>
                    
                </tr>
                </thead>
                <?php
				
				
				$sql2 = "SELECT ef.dieseldate,ef.sname,vm.vehicleno,ef.drivername,ef.inchargename,ef.previouskms,ef.previoushrs,ef.preiousqty,ef.intakekm,ef.intakehrs,ef.litres,ef.rate,ef.amount,ef.vehicleid from externalfill_diesel as ef inner join vehicle_master as vm on vm.id=ef.vehicleid WHERE ef.isdelete=1 ";

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

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

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

	}


	$query2=mysqli_query($conn, $sql2) or die("brandgriddata3.php: get brand Data step 1".$sql2);
	 $tmp2=0;
	 $cnt2=0;
	 $tamt1=0;
while($row2=mysqli_fetch_array($query2))
{
	$tmp2+=$row2['litres'];
	
	$dieseldate=$row2["dieseldate"];
$date=date_create($dieseldate);
$dieseldate1=date_format($date,"d-m-Y");
				
				?>
                <tr>
                 <td><?php echo ++$cnt2;?></td>
                <td><?php echo  $dieseldate1;?></td>
                 <td><?php echo  $row2["sname"];?></td>
                <td><?php echo  $row2["vehicleno"];?></td>
		<td><?php echo  $row2["drivername"];?></td>
		<!--<td><?php //echo  $row2["inchargename"];?></td>-->
		<td align="right"><?php echo  $row2["previouskms"];?></td>
	
		<td align="right"><?php echo  number_format($row2["previoushrs"],2,'.','');?></td>
		<td align="right"><?php echo  number_format($row2["preiousqty"],2,'.','');?></td>
		<td align="right"><?php echo  $row2["intakekm"];?></td>
		<td align="right"><?php echo  number_format($row2["intakehrs"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row2["litres"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row2["rate"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row2["amount"],2,'.','');?></td>
                
                
                </tr>
                
                <?php
				
				$tamt1+=$row2["amount"];
}


?>
<tr>
<td align="right" colspan="12">Total Amount</td>
<td align="right"><strong><?php echo number_format($tamt1,2,'.','');?></strong></td>
</tr>
</table>


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

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

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

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

	} 


	$query3=mysqli_query($conn, $sql3) or die("brandgriddata4.php: get brand Data step 2".$sql3);
	 $tmp3=0;
	 $cnt3=0;
	 $tamt2=0;
while($row3=mysqli_fetch_array($query3))
{
	$tmp3+=$row3['litres'];
	
	$dieseldate4=$row3["dieseldate"];
$date4=date_create($dieseldate4);
$dieseldate4=date_format($date4,"d-m-Y");
				
				?>
                <tr>
                 <td><?php echo ++$cnt3;?></td>
                <td><?php echo  $dieseldate4;?></td>
                 <td><?php echo  $row3["sname"];?></td>
                <td><?php echo  $row3["vehicleno"];?></td>
                <td><?php echo  $row3["drivername"];?></td>
		
		
		
		<td align="right"><?php echo  $row3["intakekm"];?></td>
		<td align="right"><?php echo  number_format($row3["intakehrs"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row3["litres"],2,'.','');?></td>
        
        <td align="right"><?php echo  number_format($row3["rate"],2,'.','');?></td>
        <td align="right"><?php echo  number_format($row3["amount"],2,'.','');?></td>
                
                
                </tr>
                <?php
               $tamt2+=$row3["amount"];
}


?>
<tr>
<td align="right" colspan="9">Total Amount</td>
<td align="right"><strong><?php echo number_format($tamt2,2,'.','');?></strong></td>
</tr>

<?php
$c=$tmp2+$tmp3;
$AStock3 =($c);
?>
</table>




            <table align="right">
            <tr>
            <td>Total Consumed Litres:</td>
            <td><strong><?php echo $AStock3;?></strong></td>
            </tr>
            
            <tr>
            <td>Net.Amount:</td>
            <td><strong><?php echo number_format(($tamt2+$tamt1),2,'.','');?></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>