
<meta charset="utf-8">

<title>VEHICLE TFL DATA</title>
<!doctype html>

<html>

<?php 

include "head.php";
include "dbconfig.php";

?>
  
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

   <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
   <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.css">
  
<!--script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" charset="utf8" src="hhttps://cdn.datatables.net/buttons/1.5.1/js/buttons.flash.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script>

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js"></script--->

<!--script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script-->
<script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.colVis.min.js"></script>


<script>
/*
$(document).ready( function () {
    $('#invoices').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    } );
} );
*/
function trhide(i)
{
//alert(i);
//$("#"+i).classList.add("noExport");
document.getElementById(i).classList.add("noExport");
}
$(document).ready(function() {
    $('#invoices').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'copyHtml5',
                exportOptions: {
                    columns: [ 0, ':visible' ]
                }
            },
            {
                extend: 'excelHtml5',
                exportOptions: {
                   // columns: ':visible'
					//columns: "thead th:not(.noExport)"
					rows: "tr:not(.noExport)"
                }
            },
			{
                extend: 'csvHtml5',
                exportOptions: {
                    //columns: ':visible'
					//visible:not(.not-exported)
					//columns: "thead th:not(.noExport)"
					rows: "tr:not(.noExport)"
                }
            },
            {
                extend: 'pdfHtml5',
                exportOptions: {
                    //columns: ':visible'
					//columns: "thead th:not(.noExport)"
					rows: "tr:not(.noExport)"
                }
            },
            'colvis'
        ]
    } );
} );


</script>

<style>

.dt-buttons {
    float: left;
}

button.dt-button {
    padding: 6px 20px;
}
.dt-buttons {
    float: left;
    padding-left: 20px;
}
 
</style>

<body style="font-family:Montserrat; font-size:13px;"> 

<?php
$sql='';
/*$sql="SELECT driver_invoices.invoice_no,driver_invoices.driver_id,driver_invoices.date, employees.first_name,driver_invoices.nett, driver_invoices.gross FROM driver_invoices 
LEFT JOIN invoice_status ON driver_invoices.status = invoice_status.id 
LEFT JOIN employees ON driver_invoices.driver_id = employees.id 

";*/
$sql="SELECT DISTINCT employees.id as driver_id,employees.first_name,employees.phone,employees.reg_plate,employees.tfl_expiry,employees.pco_number,employees.pco_expiry,employees.driver_photo,employees.address,employees.post_code,employees.badge_expiry,employees.badge_number,
vehicles.mot_date,vehicles.reg,vehicle_make.make,vehicles.insurance_date,bookings.start_date,bookings.end_date
FROM employees
JOIN vehicles on vehicles.driver_id=employees.id
JOIN bookings ON bookings.driver_id = employees.id 
JOIN vehicle_make ON vehicle_make.id = vehicles.make 
";
$today= date('Y-m-d');
$from="";
$to="";
	
if (!empty($_POST['from']) && !empty($_POST['to']))
	{
	  $from=$con->real_escape_string($_POST['from']);
	  $from = str_replace("/","-",$from);
	  $from = date("Y-m-d", strtotime($from));
	
	  $to=$con->real_escape_string($_POST['to']);
	  $to = str_replace("/","-",$to);
	  $to = date("Y-m-d", strtotime($to));
	//$sql.=" WHERE  bookings.start_date BETWEEN '$from' AND '$to'"; 
	$sql.=" WHERE bookings.end_date >= '$from' AND bookings.start_date <= '$to'"; 
	}	



//$sql.=" GROUP BY employees.id ORDER BY bookings.start_date DESC ";
$sql.="  ORDER BY bookings.start_date DESC";
//echo $sql;
// $customer_id=$_GET['company_id'];
//WHERE customers.id=$customer_id GROUP BY bookings.id
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);

if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}


?>
	  <form action="" method="post">
	<div align="center" class="container-fluid" style="margin-top:200x;">
	 
			      <div align="center" class="well col-md-12" style="background-color:#e9e9e9; box-shadow: 5px 5px 5px;">
			       	  
                         
                       <div class="col-md-2">
 						<input type="text" name="from" class="from form-control" placeholder="Date From" id="from">
                     </div>
                       <div class="col-md-2">
                        <input type="text"  name="to" class="to form-control" placeholder="Date To" id="to">
             		 </div>
        
                       <div class="col-md-1">
                       	  <input type="submit" id="select_jobs" class="btn btn-warning" value="Find">
                       </div>
					 
					 <div class="col-md-1">
                     <input type="button"  class="btn btn-primary btn-sm" onClick="printDiv('invoice')" value="Print" /> 
                   </div>
                    <!-- now retrieve the transactions of this customer -->
                   </div>
                 			
</div>
				  
			    
</form>
<?php
$invoice_sum=mysqli_query($con,$sql);
$total_invoices=mysqli_num_rows($invoice_sum);
$res=mysqli_query($con,$sql);

//echo $sql;

                
                $nett='';
				$vat='';
				$gross='';
				
				;?>
                
   <form action="" name="print_invoice" method="post">
   
         
       
       
      <div id="invoice">
	  <h3 align="left" style=" float:left;"><img src="wings_logo.png" style="margin-left:0%; width:122px; height:80px;"></h3> 
							 <h3 align="left" style=" float:left;"><font face="Times New Roman, Times, serif"><b  style="font-size:30px;">Wings Chauffeurs Ltd.</b></font>
							 <b style="font-size:12px;"><br>Office 2.25, 450 Bath Rd,<br>
							 West Drayton,Middlesex,London,<br>
							 UB7 0EB<br></b></h3>
         <table style="font-family:Montserrat; font-size:13px; cursor:pointer;" id="invoices" class="table invoices table-striped table-bordered table-hover bordered" cellspacing="0">

        <thead>

            <tr class="btn-primary">
			
              <!--th>Date</th-->
                          <!--th>Operator Licence Number</th>
                           <th>Operator Name</th-->
                          
                       
                           <th>VRM</th>
                           <th>Vehicle Make</th>                          
                           <th>Vehicle Licence Number</th>
                           
            </tr>

        </thead>

        <!--tfoot>

            <tr class="btn-primary">
                           <th>Date</th>
                           <th>Operator Licence Number</th>
                           <th>Operator Name</th>
                          
                       
                           <th>PHV Licence Number</th>
                          
                           <th>Name</th>
                           
            </tr>

        </tfoot-->

        <tbody >
				
        <?php
                $i=0;
				while($invoice=mysqli_fetch_assoc($res))
                {
				$i=$i+1;
				$operator_licence_no="0094150101";
				$operator_name="WINGS CHAUFFEUR";
				
				?>
				<tr id="<?php echo $i;?>">
				
				<!--td align="center">
				<?php //echo date("d/m/Y", strtotime($invoice['start_date']))."<br> to <br>".date("d/m/Y", strtotime($invoice['end_date']));?>
                     <?php //echo date("d/m/Y", strtotime($invoice['start_date']))."<br> to <br>".date("d/m/Y", strtotime($invoice['end_date']));?>
				<?php if (!empty($_POST['from']) && !empty($_POST['to']))
				{ 
				
				 $from=$con->real_escape_string($_POST['from']);
	  $from = str_replace("/","-",$from);
	  $from = date("Y-m-d", strtotime($from));
	
	  $to=$con->real_escape_string($_POST['to']);
	  $to = str_replace("/","-",$to);
	  $to = date("Y-m-d", strtotime($to));
	  
	  
	                           $created=date("Y-m-d", strtotime($invoice['start_date']));
                                $t=$from;
                                $date1=date_create($created);
                                $date2=date_create($t);
                                $diff=date_diff($date1,$date2);
                                $d=$diff->format("%R%a");
                               // $days=abs($d);
								 $days=($d);
								 
								 
								 $created1=date("Y-m-d", strtotime($invoice['end_date']));
                                $t1=$to;
                                $date11=date_create($created1);
                                $date21=date_create($t1);
                                $diff1=date_diff($date11,$date21);
                                $d1=$diff1->format("%R%a");
                              //  $days1=abs($d1);
								 $days1=($d1);
				if($days >= 0)
				   {
					
				   echo date("d/m/Y", strtotime($from))."<br> to <br>";
				   }
				   else
				   {
					//  echo date("d/m/Y", strtotime($invoice['start_date']))."<br> to <br>";
				    echo date("d/m/Y", strtotime($from))."<br> to <br>";
				   }
				   
				   
				    if($days1 <= 0)
				   {
					// echo date("d/m/Y", strtotime($to));
				   echo date("d/m/Y", strtotime($to));
				   }
				   else
				   {
					      echo date("d/m/Y", strtotime($to));
					//echo 3;
				   // date("d/m/Y", strtotime($invoice['end_date']));
				   //echo date("d/m/Y", strtotime($to));
				   }
				///echo date("d/m/Y", strtotime($from))."<br> to <br>".date("d/m/Y", strtotime($to));
				}else{
	 echo date("d/m/Y", strtotime($invoice['start_date']))."<br> to  <br>".date("d/m/Y", strtotime($invoice['end_date']));
	 }		?>
	 </td-->
	 
					      <!--td align="center">
						   <?php echo $operator_licence_no; ?>
						   </td>
                            <td align="center"><?php echo $operator_name; ?></td-->
                           
                        
                            <td align="center"><?php echo ucwords($invoice['reg']);?></td>
							<td align="center"><?php echo $invoice['make'];?></td>
                            <td align="center"><a onClick="trhide(<?php echo $i;?>)"><?php echo $invoice['reg_plate'];?></a></td>						  
							 <!--td align="center"><?php //echo $invoice['tfl_expiry'];?></td>
							 <td align="center"><?php //echo $invoice['pco_number'];?></td>
							 <td align="center"><?php //echo $invoice['pco_expiry'];?></td-->
											  
              
              
            </tr>
      			<?php } ?>
            </tbody>
        </table>
      </div>
	   </div>
      </div>
<div class="modal fade bd-example-modal-lg custom-class" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" id="jobs_modal">

</div><!-- /.modal -->
</form>

<?php

 if(isset($_POST['selected_invoice']))
			{
		
		/*	
			echo "<pre>";
  				 print_r($_POST);
			echo "</pre>";
		*/	
		$bookingIds = $_POST;
		
		$count=count($_POST['selected_invoice']);
		
		for ($i=0;$i<$count;$i++){
			$counter=1+$i;
			$index = array_search($_POST['selected_invoice'][$i] ,$bookingIds['invoice_no']);
			//echo "index=".$index;
		$status=$con->real_escape_string($_POST['status']);
		$date=$con->real_escape_string($_POST['date']);
		$invoice_no=$bookingIds['invoice_no'][$index];
		
		$update_booking="UPDATE invoices SET
					  status=$status,
					  pay_date='$date'
				  
					  WHERE invoice_no=$invoice_no";
					  //echo $update_booking;
					  if (!mysqli_query($con,$update_booking)) {
							echo $update_booking;
							die('update 3 INTO Bookings -Error: ' . mysqli_error($con));
							exit;
						 }
		}
	}
?>
<?php
if(isset($_POST['print_invoices']))
{
$sql="SELECT invoices.invoice_no,invoices.date, invoices.customer_id, customers.name,invoices.nett, invoices.vat,invoices.gross, invoices.status status_id,
invoice_status.name as status,invoices.pay_date
FROM invoices
LEFT JOIN invoice_status ON invoices.status = invoice_status.id
LEFT JOIN customers ON invoices.customer_id = customers.id 
WHERE invoices.date BETWEEN '$from' AND '$to'";
if(!empty($filter_invoice_no)){
	
	$sql.=" AND invoice_no=$filter_invoice_no";
	}
$sql.=" order by invoices.invoice_no DESC";

include "pdf_demo/fpdf.php";
class PDF extends FPDF
{
function Header()
{
	$this->SetY(5);  
    $this->SetFont('Arial', 'B', 20); 
    $this->SetFillColor(0,0,125); 
    $this->SetTextColor(225); 
    $this->Cell(0, 10, "List of Invoices", 0, 0, 'L', true); 
	
    $this->SetTextColor(0);
    $this->SetFont('Arial', '', 12); 
	$this->Ln(10);
	$this->Cell(100, 10, "Wings Chauffers Ltd.,",0,1);
	$this->SetY(20);
	$this->Cell(100, 10, "12 Lampton Park Road,",0,1);
    $this->SetY(25);;
	$this->Cell(100, 10, "Hounslow,",0,1);
    $this->SetY(30);
	$this->Cell(100, 10, "Middlesex",0,1);
	$this->SetY(35);
	$this->Cell(100, 10, "TW3 4HS ",0,1);
	 $this->SetY(40);
	$this->Cell(100, 10, "United Kingdom ",0,1); 

	$this->SetXY(115,15);   
    $this->SetTextColor(0); 
 	$this->SetFont('Arial', '', 10); 
	$this->Cell(100, 10, "Phone: (0)2082304594",0,1);
	$this->Ln(8);
	$this->SetXY(115,20);   
	$this->Cell(100, 10, "Mobile:(0)7808939220,",0,1);
	$this->Ln(8);
	$this->SetXY(115,25);
	$this->Cell(100, 10, "Email: info@wingschauffeurs.com",0,1);
	$this->Ln(3);
	$this->SetXY(115,30);
	
	$this->Cell(100, 10, "Website: www.limochauffeurs.com");
	$this->SetFont('Arial', 'B', 11); 
	$this->Ln(3);
	$this->SetXY(115,35);
	
	$this->Cell(100, 10, "VAT No: 1234569898");
	// Set up the Invoice To Data
}
 //footer
 function Footer() { 
    $this->SetFont('Arial', '', 10); 
    $this->SetTextColor(0); 
    $this->SetY(-15);
	$this->Cell(0, 20, "Wings Chauffeurs Ltd", 'T', 1, 'C');
    
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); 
	
	//$this->Image('footer.JPG',0,750,620,50); 
}
}
	  
	  $pdf=new PDF();
	  define('£',chr(163));
	  //Add first page
	  $pdf->AddPage('L');
	  $pdf->SetFont( 'Arial', '', 20 );
	 $y_axis_initial = 50;

$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(10);
		$pdf->Cell(20, 6, 'Sr.No.', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Date', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Invoice No.', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Status', 1, 0, 'R', 1);
        
        $pdf->Cell(60, 6, 'Company', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Nett', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'VAT', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Gross', 1, 0, 'L', 1);
        

$row_height = 6;
$y_axis = 50;
$y_axis = $y_axis + $row_height;
//echo $sql;
//exit;
$report=mysqli_query($con,$sql);

//initialize counter
$i = 0;

//Set maximum rows per page
$max = 25;

//Set Row Height

 $No = 0;
while($row = mysqli_fetch_array($report))
{
    
	//If the current row is the last one, create new page and print column title
    if ($i == $max)
    {
        $pdf->AddPage('L');

        //print column titles for the current page
        $pdf->SetY($y_axis_initial);
        $pdf->SetX(25);
		$pdf->Cell(20, 6, 'Sr.No.', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Date', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Invoice No.', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Status', 1, 0, 'R', 1);
		
        $pdf->Cell(60, 6, 'Company', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Nett', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'VAT', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Gross', 1, 0, 'L', 1);
        

        //Go to next row
        $y_axis = $y_axis + $row_height;

        //Set $i variable to 0 (first row)
        $i = 0;
    }
     $No = $No +1;
    $invoice_no = $row['invoice_no'];
	$date = $row['date'];
	$date =date("d/m/Y",strtotime($date));
	$company = $row['name'];
    $nett = number_format($row['nett'],2);
	$vat = number_format($row['vat'],2);
	
	$gross = number_format($row['gross'],2);
    $status = $row['status'];

    $pdf->SetY($y_axis);
    $pdf->SetX(10);
	$pdf->Cell(20, 6, $No, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $date, 1, 0, 'L', 1);
    $pdf->Cell(30, 6, $invoice_no, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $status, 1, 0, 'R', 1);
	
	$pdf->Cell(60, 6, $company, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, £.$nett, 1, 0, 'R', 1);
	$pdf->Cell(30, 6, £.$vat, 1, 0, 'R', 1);
    $pdf->Cell(30, 6, £.$gross, 1, 0, 'R', 1);
    

    //Go to next row
    $y_axis = $y_axis + $row_height;
    $i = $i + 1;
	$total_net =number_format($total_net+$nett,2);
	$total_vat =number_format($total_vat+$vat,2);
	$total_gross =number_format($total_gross+$gross,2);
}
$pdf->SetFillColor(0,0,255);
$pdf->SetTextColor(255,255,255);
$pdf->SetFont( 'Arial', '', 15);
$pdf->SetXY(150,$y_axis);
$pdf->Cell(30, 6, "Totals", 1, 0, 'R', 1);
$pdf->SetXY(180,$y_axis);
$pdf->Cell(30, 6, £.$total_net, 1, 0, 'R', 1);
$pdf->SetXY(210,$y_axis);
$pdf->Cell(30, 6, £.$total_vat, 1, 0, 'R', 1);
$pdf->SetXY(240,$y_axis);
$pdf->Cell(30, 6, £.$total_gross, 1, 0, 'R', 1);

//Create file
$file_name="Invoice-Report-on ".date("d-m-Y").".pdf";
ob_clean();
$pdf->Output($file_name,'D');

}
?>
<script>
$(document).ready(function() { 
        $('#driver_id').select2({ width: 'resolve' });
	});

</script>


<script type="text/jscript">

$('td a').click(function() {
    $(this).parent().parent().hide();
});

$('.date').datepicker({
	
    	onSelect: function() {
			
                $( ".date" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });
$('.from').datepicker({
	
    	onSelect: function() {
			
                $( ".from" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });
$('.to').datepicker({
	
    	onSelect: function() {
			
                $( ".to" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });
		
		
$(".invoices").find('td[data-id]').on('click', function () {
	
	////alert("Demo");
	var selected_col=$(this).attr('col-id');
	
	if(selected_col=='6')
	{
			var invoice_no=$(this).attr('data-id');
			var dataString = 'invoice_no='+invoice_no;
			alert(dataString);
			var url11 = 'ajax/list_invoice_jobs.php?'+new Date().getTime();
			  $.ajax({
				  type: "POST",
			  url: url11,
			  data: dataString,
			  success: function(data) {
				  //alert(data);
				  $('#jobs_modal').html(data);
				  
				  $('#jobs_modal').modal('show');
				  }
			  });
	}
});
</script>
<script type="text/javascript">
$('#driver_id').select2({width: '100%'});


function printDiv(divName) {
$('.donotprint').hide();
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
$('.donotprint').show();		 
}
                   </script>
</body>
</html>