<?php
ob_start();
include "head.php";
include "dbconfig.php";
require_once('pdf_demo/fpdf.php');
//include 'phpmailer/PHPMailerAutoload.php';
error_reporting(E_ALL); 
session_start();
  $invoice_no =$_GET['invoice_no'];
  $sql="SELECT DISTINCT 
invoices_wings_courier.invoice_no,invoices_wings_courier.date as invoice_date,

bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.status as b_status,bookings.mins,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,
bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time,bookings.other,bookings.parking,bookings.transfer,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,customers.address1,customers.post_code,customers.email,customers.id as company_id,

company_contact.name as passenger,

employees.first_name AS driver,vehicles.reg as reg, vehicle_make.make as make,
invoices_wings_courier.*
FROM `invoice_details_wings_courier` 
INNER JOIN bookings on invoice_details_wings_courier.booking_id=bookings.id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees ON bookings.driver_id=employees.id
INNER JOIN vehicles ON vehicles.driver_id=employees.id
LEFT JOIN vehicle_make ON vehicle_make.id=bookings.vehicle_id
INNER JOIN invoices_wings_courier on invoice_details_wings_courier.invoice_no=invoices_wings_courier.invoice_no
WHERE invoices_wings_courier.invoice_no=$invoice_no

ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC
";

$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);
$client=mysqli_query($con,$sql);
$invoice=mysqli_fetch_assoc($client)
?>	
                          <?php
  						$customer_name=$invoice['company'];
                        $address1=$invoice['address1'];
                        $post_code=$invoice['post_code'];
						$invoice_no=$invoice['invoice_no'];
						$email=$invoice['email'];
                        $invoice_date=date("d/m/Y",strtotime($invoice['invoice_date']));
						$date='';
						 $vat1=$invoice['vat'];
						 
						?>
<style>
		/* these are just styles added for this demo page */
		#canvas{
	
			width:100%;
			height:100%;
			background:#FFFFFF;
			/*background-image: url(bg_img/bg.jpg);*/
			/*margin: 0 auto;*/
		}
		#invoice_div{
	
			/*width:100%;
			height:100%;*/
			background:#FFFFFF;
			/*padding:5%;*/
			/*background-image: url(bg_img/bg.jpg);*/
			/*margin: 0 auto;*/
		}
		.movable_div{
		margin-top:10px;
		width:80px;
		height:80px;
		padding:10px;
		
			color: white;
			font-family: Verdana;
			cursor: move;
			position: absolute;
		}
		#design{
			width: 510px;
			margin: 0 auto;
			background: purple;
			border: 1px solid black;
			color : white;
			padding: 30px;
		}
		
	</style>					
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
	<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
	
	<!-- this script helps us to capture any div --> 
	<script src="html2canvas.js"></script>

                     <form method="post" action="">
					<div class="col-md-2">
                       <input type="button" class="btn btn-primary btn-sm" onClick="printDiv('invoice')" value="Print" /> 
                    </div>
                    <div class="col-md-2">
                       <input type="submit" name="send_email" class="btn btn-danger btn-sm" value="Send Email" /> 
                    </div>
                   </form>
</head>

<body bgcolor="#FFFFFF"> 
  
  
  					
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  
					  <meta charset="utf-8">
					  <head><style>
h2 {
	
	 style="padding:10px; color:#000000; background-color:#68B0EC;"
	 }
*{
	font-size:14px;
	font-family:arial;
	color:#000000;}
table th {
	padding:2px;
    text-align: center;
    background-color: #4CAF50;
    color: white;
	font-size:13px;
}
table td
{
	align-content:center;
	margin-left:3px;

	padding:2px;
	font-size:12px;
	
	

	}
	table {
		width:100%;
		table-layout:auto;
		border:thin;
		border-color:#000000;}
		table tr:nth-child(even) {background-color:#E0DFDF;}

.navbar-inverse {
            background-color:#F3DB08;
            
			border-color: #f1f1f1;
         }
		 
</style></head>
					  <body>
					  <div id="invoice">';
					 // if($vat1!='0.00' && $vat1!='0' && $vat1!='')
					  {
					 $invoice_html.=' 	
                      <table>
					  
					  <tr>
					  
						 <td width="60%"> 
						<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:25px;">Wings Courier 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>
						  <b>
							 <!--p style="font-size:20px">Wings Chauffeurs Ltd.</p-->
							
							  <!--address>
							 <p>Office 2.25, 450 Bath Rd, </p>
							 <p>West Drayton,Middlesex,London,</p>
							 <p>UB7 0EB</p>
							  <p>E:info@wingschauffeurs.com </p>
							 <p>Ph. +44(0)208077925</p>
							 <p>M. +44(0)07792560326</p>
							
							 <p>Website: www.wingschauffeurs.com</p>
							 </address>
					      </td-->
						  <!--td style="text-align:left;word-break:break-all;"> 
						   <p><b>Invoice To : </b></p>
							<p>'.wordwrap($customer_name,15).'</p>
							
							 <p>'.wordwrap( $address1, 10, "\r\n", true ) .'</p>
							 <p>'.$post_code.'</p>
					      </td-->
						 
						 <td  style="text-align:right;"> 
						 <b>
						  <p>Invoice No. : '.$invoice_no.'</p>
						  <p>Invoice. Date: '.$invoice_date.'</p>
						  <p><b>Invoice To :</b>
						'.wordwrap($customer_name,15).'</p>
							
							 <p>'.wordwrap( $address1, 10, "\r\n", true ) .'</p>
							 <p>'.$post_code.'</p>
							
							
							 <!--p>VAT No. : 256574868</p-->
							 <!--p>Payment Terms: 14 days</p-->
					      </td>
						 
						  									   
						   <td class="bold-text">  
						     
							
						   </td>
						   </b>
						</tr>            
				   </table>';
					}
					
						        
                  $invoice_html.='     
                  <table cellpadding="0" cellspacing="0" border="1">

        <thead>
			
            <tr>
                            <th>Sr</th>
						  <th width="4%">JOB ID</th>
				           <th width="4%">STATUS</th>
                           <!--th width="10%">DRIVER</th-->
						   <th width="5%">PLATE</th>
                             <th width="10%">VEHICLE</th>
                           <th width="10%">CLIENT</th>
                         <th>Pickup From</th>
                           <th>Drop To</th>
                           <th width="4%">START</th>
                           
                           <th width="4%">FINISH</th>
						   <th width="4%">RATE</th>
						   
						   <th width="2%">DAYS</th>
						   <th width="4%">WAITING</th>
						   <th width="4%">PARKING</th>
						   
                           <th width="4%">TOTAL</th>
						   
						   <!--th width="10%">Code</th>
						   <th width="47%">Description</th>
						   <th  width="7%">Quantity</th>
						   
						   <th  width="7%">Rate</th>
						   
                           <th  width="7%">Amount</th-->
							
							            </tr>

        </thead>
		

        <tbody>
        ';?>
       
        <?php
				$total=0;
				$id=1;
				$total_gross=0;
				$vat=0;
				$gross=0;
				$nett=0;
				$count=0;
				$p=0;
                while($job=mysqli_fetch_assoc($res))
				
                  {
					  
					 $booking_id = $job['booking_id'];
					 $pickup_date = date("d/m/Y",strtotime($job['pickup_date']));
					 $time = $job['hours'].":".$job['mins'];
	 				 $passenger=$job['passenger'];
					 $company=$job['company'];
					 $reg=$job['reg'];
					  $status=$job['b_status'];
					 $make=$job['make'];
					  $waiting=$job['waiting_charges'];
					 $driver=$job['driver'];
					 $start_date=$job['start_date'];
					  $end_date=$job['end_date'];
					 $parking=$job['parking'];
					  $transfer=$job['transfer'];
					 $rate=$job['rate'];
					 $days=$job['days'];
					 $total_fare=$job['total_fare']; 
					 $total_gross=$total_gross+$job['total_fare'];
					 $company_id = $job['company_id'];
					 $total=$total+$job['nett'];
					 $nett=$job['nett'];
					 $vat=$job['vat'];
					 $gross=$job['gross'];
					 $count++;
					  $p++;
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
                           <input type="hidden" name="company_id" value="'.$company_id.'"/>
						   <input type="hidden" name="booking_id[]" value="'.$booking_id.'">
						   <td>'.$count.'</td>
						   <td>'.$booking_id.'</td>
						   <td>'.$status.'</td>
						   <!--td>'.ucfirst($driver).'</td-->
						   <td>'.$reg.'</td>
						    <td><p>'.$make.'</p></td>
						   <td>'.ucfirst($passenger).'</td>
						  <td><p>'.$job['pickup_address'].' '.$job['pickup_post_code'].'</p></td>
						    <td><p>'.$job['drop_address'].''.$job['drop_post_code'].'</p></td>
						   <td>'.date("d-M",strtotime($start_date)).'</td>
						   <td>'.date("d-M",strtotime($end_date)).'</td>
						   
						   <td>£'.number_format($rate,2).'</td>
						   <td>'.number_format($days,0).'</td>
						   
						    <td>£'.number_format($waiting,2).'</td>
						    <td>£'.number_format($parking,2).'</td> 
						  						  
						   <td style="text-align:right;">£'.number_format($total_fare,2).'</td>
						   				   
						   
					</tr>';
					
					
					  if($p==10)
						   {
						 echo '<input type="hidden" id="rows_lenght" name="rows_lenght" value="'.$p.'">';
						    $invoice_html.=
					 ' <!--p style="page-break-inside:avoid; page-break-after:always;">&nbsp;</p-->
					
					 <tr id="row_break1" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
					  <tr id="row_break2" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr> 
					  <tr id="row_break3" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
					   <tr id="row_break4" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
					    <tr id="row_break5" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
						 <tr id="row_break6" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
						  <tr id="row_break7" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
						   <tr id="row_break8" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
						    <tr id="row_break9" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							 <tr id="row_break10" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							  <tr id="row_break11" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							   <tr id="row_break12" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							   <tr id="row_break13" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							 <tr id="row_break14" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							  <tr id="row_break15" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							  <tr id="row_break16" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							  <tr id="row_break17" style="visibility: hidden; display:none;"><td>&nbsp;</td></tr>
							
					';
					
					$p=0;
						   }
				 $id++;
				 } ?>
                 
                 <?php 
				
				 $invoice_html.='
				
				<b> 
				  <tr valign="middle">
                     
						   <td style="text-align:right;font-size:12px;" colspan="14"><p><b>Sub Total: </b></td>
						   <td style="text-align:right;font-size:12px"><p>£'.number_format((float)$nett,2).'</p></td>
						   
					   </tr>';
					   if($vat1!='0.00' && $vat1!='0' && $vat1!='')
					  {
					 $invoice_html.=' 	
					   <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="14"><p><b>VAT(20%): </b></td>
						   <td style="text-align:right;font-size:12px"><p>£'.number_format((float)$vat,2).'</p></td>
						   
					   </tr>';
					   }
					    $invoice_html.=' 
					   <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="14"><p><b>Grand Total: </b></td>
						   <td style="text-align:right;font-size:12px"><p>£'.number_format((float)$gross,2).'</p></td>
						   
					   </tr>
					   <!--tr>
					   <td style="text-align:center;font-size:12px;" colspan="15"><p><b>Wings Chauffeurs Ltd., HSBC BANK, Account No: 42409593, Sort Code: 40-42-08, IBAN:GB09MIDL40420842409593, SWIFT: MIDLGB2109E, Company House No:09975905</td>
					   </tr-->';
					    
					  $invoice_html.=' 
					  </b> 
					</tbody>
				 </table>
				 <br>
				
				 <td width="30%"> 
						<br>
						  <b>
							 <!--p style="font-size:20px">Bank Details</p-->
							<p style="font-size:15px">Payment Details</p>
							  <address>
							 <!--p>Bank Name : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          HSBC</p>
							 <p>Branch Name : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       SLOUGH</p>
							 <p> Account No :  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      42409593</p>
							  <p>Sort Code : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;        40-42-08</p>
							 <p>SWIFT Code :  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       MIDLGB2109E</p>
							 <p>IBAN Code :  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;        GB09MIDL40420842409593</p>
							
							 <p>Company House No:  &nbsp;&nbsp;&nbsp;&nbsp;  09975905</p-->
							 
							 <p style="font-size:20px">Wings Courier Ltd</p>
							 <!--p>Account number 24127494</p>
							 <p>Sort Code 23-05-80</p>
							 <p>SWIFT Code MYMBGB2L</p>
							 <p>IBAN Code GB50MYMB23058024127494</p>
							 <p>Metro bank Address 8,Hayes Bridge Retail Park,Uxbridge Rd,Southall UB4 0RH</p-->
							
							 Account number 24127494<br>
							 Sort Code 23-05-80<br>
							 SWIFT Code MYMBGB2L<br>
							 IBAN Code GB50MYMB23058024127494<br>
							 Metro bank Address 8,Hayes Bridge Retail Park,Uxbridge Rd,Southall UB4 0RH
							 </address>
					      </td>
						  
						  
						  <table style="margin-bottom:0px;text-align: right;
height: 20px;
position:fixed;
margin:0px;
bottom:0px;">
<tr style=" border-bottom: 2px solid black;">
<td style="text-align:left;font-size:12px;" colspan="12"><p><b></b></p><td>
<td style="text-align:left;font-size:12px;" colspan="12"><p><b></b></p><td>
<td style="text-align:left;font-size:12px;" colspan="12"><p><b></b></p><td>
<td style="text-align:left;font-size:12px;" colspan="12"><p><b></b></p><td>
</tr>
<tr>
						  
					  <td style="text-align:left;font-size:12px;" colspan="12"><p><b> +44 20 36333706(UK) </b></p></td>
					   <td style="text-align:center;font-size:12px;" colspan="12"><p><b>  +1(888) 717-4502(USA) </b></p></td>
					    <td style="text-align:center;font-size:12px;" colspan="12"><p><b> info@wingschauffeurs.com </b></p></td>
					   <td style="text-align:right;font-size:12px;" colspan="12"><p><b>  www.wingschauffeurs.com</b></p></td>
					   </tr>
					   </table>
						  <!--td width="100%" > <p> +44 20 36333706(UK) &nbsp;&nbsp;&nbsp;&nbsp; +1(888) 717-4502(USA) &nbsp;&nbsp;&nbsp;&nbsp; info@wingschauffeurs.com &nbsp;&nbsp;&nbsp;&nbsp; www.wingschauffeurs.com</p></td--><br>
						  <br>';
						  $invoice_html.='
				  <!--a href="http://www.wingschauffeurs.com/wings/pay_courier.php?invoice_no='.$invoice_no.'&cop=courier&"  onClick="this.href+=new Date().getTime();return true;" id="b1"><b>&nbsp;<input type="button" class="btn btn-primary btn-sm" value="Pay with Card" /></b></a-->
				</div>
			  </body>
			</html> 
               ';?>
			
				   <?php 
				   echo '<div class="col-md-12" id="invoice_div" style="margin-left:2px;"><br>'.$invoice_html.'</div>';
				   ?>
  <script type="text/javascript">
		 /*
function printDiv() {
     var divToPrint = document.getElementById('invoice');
    var htmlToPrint = ''+
    '<style type="text/css">'+'th{border: 1px solid #7A7575;'+
'table td{align-content:left;padding-top:1px;font-size:12px;font-family:Arial;}'+
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
}
*/
function printDiv(divName) {
         
$('#b1').hide();
var p=$("#rows_lenght").val();
if(p=='10')
{
$("#row_break1").show();
$("#row_break2").show();
$("#row_break3").show();
$("#row_break4").show();
$("#row_break5").show();
$("#row_break6").show();
$("#row_break7").show();
$("#row_break8").show();
$("#row_break9").show();
$("#row_break10").show();
$("#row_break11").show();
$("#row_break12").show();
$("#row_break13").show();
$("#row_break14").show();
$("#row_break15").show();
$("#row_break16").show();
$("#row_break17").show();
} 
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
	 $('#b1').show();
	  $("#row_break1").hide();
	  $("#row_break2").hide();
	   $("#row_break3").hide();
	    $("#row_break4").hide();
		 $("#row_break5").hide();
		  $("#row_break6").hide();
		   $("#row_break7").hide();
		    $("#row_break8").hide();
			 $("#row_break9").hide();
			  $("#row_break10").hide();
			   $("#row_break11").hide();
			    $("#row_break12").hide();
				 $("#row_break13").hide();
				  $("#row_break14").hide();
				   $("#row_break15").hide();
				   $("#row_break16").hide();
				   $("#row_break17").hide();
}
function captureDiv() {

div_content = document.querySelector("#invoice_div")
				//make it as html5 canvas
				html2canvas(div_content).then(function(canvas) {
					//change the canvas to jpeg image
					data = canvas.toDataURL('image/jpeg');
					
					//then call a super hero php to save the image
					save_img(data);
				});
//alert('something wrong');

					
		}			

                   </script>
				   
  <script type="text/javascript">	
		$(function(){	
			
			//to make a div draggable
			$('.movable_div').draggable(
				{containment: "#canvas", scroll: false}
			);
			
			//to capture the entered text in the textbox 
			$('#textbox').keyup(function(){
				var text = $(this).val();
				$('.movable_div').text(text);
			});	
			
			//to change the background once the user select
			$('#background').change(function(){
				var background = $(this).val();
				$('#canvas').css('background', 'url(bg_img/'+background+')');
			});
			
			//font size handler here. 
			$('#slider').change(function(){
				fontSize = $(this).val();
				$('.movable_div').css('font-size', fontSize+'px');
			});		

			//here is the hero, after the capture button is clicked
			//he will take the screen shot of the div and save it as image.
			$('#capture').click(function(){
				//get the div content
				div_content = document.querySelector("#invoice_div")
				//make it as html5 canvas
				html2canvas(div_content).then(function(canvas) {
					//change the canvas to jpeg image
					data = canvas.toDataURL('image/jpeg');
					
					//then call a super hero php to save the image
					save_img(data);
				});
			});	
			
				
		});
		
		
		//to save the canvas image
		function save_img(data){
			//ajax method.
			
		//	var invoice_no=$('#invoice_no').val();
			//alert(invoice_no);
		//	var dataString = 'invoice_no='+ invoice_no + '&data='+ data;
           
	
			$.post('save_jpg.php', {data: data }, function(res){
				//if the file saved properly, trigger a popup to the user.
				if(res != ''){
				
					//yes = confirm('File saved in output folder, click ok to see it!');
					//if(yes){
						//location.href =document.URL+'output/'+res+'.jpg';
					//}
				}
				else{
					//alert('something wrong');
				}
			}); 
		}
    </script>				    
  
  <?php 
	   if(isset($_POST['send_email'])){
	  //echo $invoice_html;
	   echo '<script>captureDiv();</script>';
   // $passenger='jas';
   $subject="Your Invoice";
   $to=$email;//'jaspinder32@gmail.com';
  // $message=$invoice_html;
  
    $message="<h2><b>Dear ".wordwrap($customer_name,15)."</b></h2>
   <br/><br/>
   
   <h3>Please find attached is your invoice for your bookings.</h3>
  <br/><br/>
   <h3>Invoice Number : ".$invoice_no."</h3>
   <br/><br/>
  <h3> Total amount is £".number_format((float)$gross,2).".</h3>
   
   <br/><br/>
   <h3>You can pay online, Please click the button below to pay online now.
   <br/>
   We accept all major debit and credit cards.</h3>
   <br/><br/><br/>";
   $message.='<a  href="http://www.wingschauffeurs.com/wings/pay_courier.php?invoice_no='.$invoice_no.'&cop=courier&"  onClick="this.href+=new Date().getTime();return true;" id="b1"><b>&nbsp;<input type="button" class="btn btn-primary btn-sm" style="background:#00CC33; color:#FFFFFF; border:none; width:120px; height:40px; font-size:14px; text-align:center; padding:5px;" value="Pay By Card" /> </b></a>';
  $message.='<br/><br/><br/><br/><br/> <img src="http://www.wingschauffeurs.com/wings/creditcards.png"  style="max-width:500px;">';	
  
   $image="output/".$_SESSION['invoice_no'].".jpg";
  // echo $message;
///////////////////////////////////////////////////////*******************************************************////////////////////////////////////  
  /*
  	// require("fpdf.php");
class PDF extends FPDF {
    const DPI = 96;
    const MM_IN_INCH = 25.4;
    const A4_HEIGHT = 297;
    const A4_WIDTH = 210;
    // tweak these values (in pixels)
    const MAX_WIDTH = 1000;
    const MAX_HEIGHT = 500;
    function pixelsToMM($val) {
        return $val * self::MM_IN_INCH / self::DPI;
    }
    function resizeToFit($imgFilename) {
        list($width, $height) = getimagesize($imgFilename);
        $widthScale = self::MAX_WIDTH / $width;
        $heightScale = self::MAX_HEIGHT / $height;
        $scale = min($widthScale, $heightScale);
        return array(
            round($this->pixelsToMM($scale * $width)),
            round($this->pixelsToMM($scale * $height))
        );
    }
    function centreImage($img) {
        list($width, $height) = $this->resizeToFit($img);
        // you will probably want to swap the width/height
        // around depending on the page's orientation
        $this->Image(
            $img, (self::A4_HEIGHT - $width) / 2,
            (self::A4_WIDTH - $height) / 2,
            $width,
            $height
        );
    }
}
// usage:
$pdf = new PDF();
$pdf->AddPage("L");
$pdf->centreImage($image);
//$pdf->Output();
$name_plate='bookings/'.$booking_id.'_invoice.pdf';
		 $pdf->Output($name_plate,'F');		 
*/		 
 ///////////////////////******************************************************************************/////////////////////////////////////////// 
  
  // Define Header and Footer inside the class /////////////////////////////////////
class PDF extends FPDF
{
function Header()
{
/*
$client=mysqli_query($con,$sql);
$invoice=mysqli_fetch_assoc($client);
	
                         
  						$customer_name=$invoice['company'];
						//$email=$invoice['email'];
                        //$address1=$invoice['address1'];
                        //$post_code=$invoice['post_code'];
						$invoice_no=$invoice['invoice_no'];
						//$_SESSION['invoice_no']=$invoice_no."_chauffeurs";
						//$email=$invoice['email'];
                        $invoice_date=date("d/m/Y",strtotime($invoice['invoice_date']));*/
						
	$this->Image('wings_logo.png',5,5,-370,-500);
	$this->SetY(5);  
    $this->SetFont('Arial', 'B', 20); 
    $this->SetFillColor(255,255,255); 
    $this->SetTextColor(0); 
	
	$this->SetX(50);
	 $this->Cell(0, 10, "Wings Courier Ltd.", 0, 0, 'L', true); 
    $this->SetTextColor(0);
    $this->SetFont('Arial', '', 12); 
	$this->SetX(50);
	$this->Ln(10);
	$this->SetX(50);
	$this->Cell(100, 10, "Office 2.25, 450 Bath Rd,",0,1);
	$this->SetY(20);
	$this->SetX(50);
	$this->Cell(100, 10, "West Drayton,Middlesex,London,",0,1);
    $this->SetY(25);
	$this->SetX(50);
	$this->Cell(100, 10, "UB7 0EB",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(215,15);   
    $this->SetTextColor(0); 
 	$this->SetFont('Arial', '', 10); 
	$this->Cell(100, 10, $invoice_no,0,1);
	$this->Ln(8);
	$this->SetXY(215,20);   
	$this->Cell(100, 10, $invoice_date,0,1);
	$this->Ln(8);
	$this->SetXY(215,25);
	$this->Cell(100, 10, $customer_name,0,1);
/*	
	$this->Ln(3);
	$this->SetXY(215,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, "+44 20 36333706(UK)", 'T', 1, 'L');
	$this->SetXY(80,-15); 
	$this->Cell(0, 20, "+1(888) 717-4502(USA)", 'T', 1, 'L');
	$this->SetXY(160,-15); 
	$this->Cell(0, 20, "info@wingschauffeurs.com", 'T', 1, 'L');
	$this->SetXY(300,-15); 
	$this->Cell(0, 20, "www.wingschauffeurs.com", 'T', 1, 'R');
    
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); 
	
	//$this->Image('footer.JPG',0,750,620,50); 
}
}
	  $pdf=new PDF();
	  //Add first page



	  $pdf->AddPage('L');
	  $pdf->SetFont( 'Arial', '', 9 );
	  $pdf->SetAutoPageBreak(false);
	  
	  $pdf->SetXY(215,15);   
    $pdf->SetTextColor(0); 
 	$pdf->SetFont('Arial', '', 10); 
	$pdf->Cell(100, 10, 'Invoice No. : '.$invoice_no,0,1);
	$pdf->Ln(8);
	$pdf->SetXY(215,20);   
	$pdf->Cell(100, 10, 'Invoice. Date : '.$invoice_date,0,1);
	$pdf->Ln(8);
	$pdf->SetXY(215,25);
	$pdf->Cell(100, 10, 'Invoice To : '.$customer_name,0,1);
	  
	  $y_axis_initial = 50;
 
	  $pdf->SetX(5);
  $pdf->SetTextColor(255); 
$pdf->SetFont('Arial', 'B', 9);
$pdf->SetY($y_axis_initial);
$pdf->SetX(5);
     //   $pdf->SetFillColor(255,255,255); 
		 $pdf->SetFillColor(76,175,80); 
		
		
		
        $pdf->Cell(10, 6, 'Sr No. ', 1, 0, 'L', 1);
		$pdf->Cell(15, 6, 'JOB ID', 1, 0, 'L', 1);
        $pdf->Cell(20, 6, 'STATUS', 1, 0, 'L', 1);
		$pdf->Cell(40, 6, 'DRIVER', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'PLATE', 1, 0, 'L', 1);
		$pdf->Cell(50, 6, 'CLIENT', 1, 0, 'L', 1);
		$pdf->Cell(20, 6, 'START', 1, 0, 'L', 1);
		$pdf->Cell(20, 6, 'FINISH', 1, 0, 'L', 1);
		$pdf->Cell(15, 6, 'RATE', 1, 0, 'L', 1);
		$pdf->Cell(15, 6, 'DAYS', 1, 0, 'L', 1);
		$pdf->Cell(15, 6, 'WAITING', 1, 0, 'L', 1);
		$pdf->Cell(15, 6, 'PARKING', 1, 0, 'L', 1);
		$pdf->Cell(15, 6, 'TOTAL', 1, 0, 'L', 1);
		
		$pdf->SetFillColor(255,255,255); 
		 $pdf->SetTextColor(0); 
		
$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 = 20;

//Set Row Height
$count=0;
 $No = 0;
while($job = mysqli_fetch_array($report))
{
    $booking_id = $job['booking_id'];
					 $pickup_date = date("d/m/Y",strtotime($job['pickup_date']));
					 $time = $job['hours'].":".$job['mins'];
	 				 $passenger=$job['passenger'];
					 $company=$job['company'];
					 $reg=$job['reg'];
					 $status=$job['b_status'];
					 $make=$job['make'];
					 
					 $driver=$job['driver'];
					 $start_date=date("d-M",strtotime($job['start_date']));
					  $end_date=date("d-M",strtotime($job['end_date']));
					 $parking=$job['parking'];
					  $transfer=$job['transfer'];
					 $rate=$job['rate'];
					 $days=$job['days'];
					  $waiting=$job['waiting_charges'];
					 $total_fare=$job['total_fare']; 
					 $total_gross=$total_gross+$job['total_fare'];
					 $company_id = $job['company_id'];
					 $total=$total+$job['nett'];
					 $nett=$job['nett'];
					 $vat=$job['vat'];
					 $gross=$job['gross'];
					 $count++;
	//If the current row is the last one, create new page and print column title
    if ($i == $max)
    {
        $pdf->AddPage('L');
        $y_axis =50;
        //print column titles for the current page
        $pdf->SetY($y_axis);
        $pdf->SetX(5);
		
		 
		
    $pdf->Cell(10, 6, $count, 1, 0, 'L', 1);
	$pdf->Cell(15, 6, $booking_id, 1, 0, 'L', 1);
	$pdf->Cell(20, 6, $status, 1, 0, 'L', 1);
	$pdf->Cell(40, 6, $driver, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $reg, 1, 0, 'L', 1);
	$pdf->Cell(50, 6, $passenger, 1, 0, 'L', 1);
	$pdf->Cell(20, 6, $start_date, 1, 0, 'L', 1);
	$pdf->Cell(20, 6, $end_date, 1, 0, 'L', 1);
	$pdf->Cell(15, 6, chr(163).number_format($rate,2), 1, 0, 'L', 1);
	$pdf->Cell(15, 6, number_format($days,0), 1, 0, 'L', 1);
	$pdf->Cell(15, 6, chr(163).number_format($waiting,2), 1, 0, 'L', 1);
	$pdf->Cell(15, 6, chr(163).number_format($parking,2), 1, 0, 'L', 1);
	$pdf->Cell(15, 6, chr(163).number_format($total_fare,2), 1, 0, 'L', 1);
	
       
        //Go to next row
        $y_axis = $y_axis + $row_height;

        //Set $i variable to 0 (first row)
       $i = $i + 1;
    }
    $No = $No +1;
   
    

    $pdf->SetY($y_axis);
    $pdf->SetX(5);
	
   
	 $pdf->Cell(10, 6, $count, 1, 0, 'L', 1);
	$pdf->Cell(15, 6, $booking_id, 1, 0, 'L', 1);
	$pdf->Cell(20, 6, $status, 1, 0, 'L', 1);
	$pdf->Cell(40, 6, $driver, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $reg, 1, 0, 'L', 1);
	$pdf->Cell(50, 6, $passenger, 1, 0, 'L', 1);
	$pdf->Cell(20, 6, $start_date, 1, 0, 'L', 1);
	$pdf->Cell(20, 6, $end_date, 1, 0, 'L', 1);
	$pdf->Cell(15, 6, chr(163).number_format($rate,2), 1, 0, 'L', 1);
	$pdf->Cell(15, 6, number_format($days,0), 1, 0, 'L', 1);
	$pdf->Cell(15, 6, chr(163).number_format($waiting,2), 1, 0, 'L', 1);
	$pdf->Cell(15, 6, chr(163).number_format($parking,2), 1, 0, 'L', 1);
	$pdf->Cell(15, 6,chr(163).number_format($total_fare,2), 1, 0, 'L', 1);
	
    //Go to next row
   
	 $y_axis = $y_axis + $row_height;
    $i = $i + 1;
}

 //$y_axis = $y_axis + $row_height;
	$pdf->SetY($y_axis);
    $pdf->SetX(5);
	$pdf->Cell(265, 6,"Sub Total:", 1, 0, 'R', 1);
	$pdf->Cell(15, 6,chr(163).number_format($nett,2), 1, 0, 'L', 1);
	
	// $y_axis = $y_axis + $row_height;
	//$pdf->SetY($y_axis);
   // $pdf->SetX(5);
	//$pdf->Cell(265, 6,"VAT(20%):", 1, 0, 'R', 1);
	//$pdf->Cell(15, 6,chr(163).number_format($vat,2), 1, 0, 'L', 1);
	
	 $y_axis = $y_axis + $row_height;
	$pdf->SetY($y_axis);
    $pdf->SetX(5);
	$pdf->Cell(265, 6,"Grand Total:", 1, 0, 'R', 1);
	$pdf->Cell(15, 6,chr(163).number_format($gross,2), 1, 0, 'L', 1);
	

$pdf->SetFont('Arial', 'B', 11); 
 $pdf->SetY($y_axis + 10);
 $pdf->SetX(20); 
 /*
$pdf->Cell(0, 10, "Bank Details", 0, 1);
$pdf->SetFont('Arial', 'B', 9); 
$pdf->Cell(100, 5, "Bank Name : HSBC",0,1);
$pdf->Cell(100, 5, "Branch Name : SLOUGH",0,1);
$pdf->Cell(100, 5, "Account No : 42409593",0,1);
$pdf->Cell(100, 5, "Sort Code : 40-42-08",0,1);
$pdf->Cell(100, 5, "SWIFT Code : MIDLGB2109E",0,1);
$pdf->Cell(100, 5, "IBAN Code : GB09MIDL40420842409593",0,1);
$pdf->Cell(100, 5, "Company House No : 09975905",0,1);
//$pdf->Cell(100, 5, "Company VAT No :  256574868",0,1);
*/
$pdf->Cell(0, 10, "Payment Details", 0, 1);
$pdf->SetFont('Arial', 'B', 9); 
$pdf->Cell(100, 5, "Wings Courier Ltd",0,1);
//$pdf->Cell(100, 5, "Account number 24127494",0,1);
//$pdf->Cell(100, 5, "Sort Code 23-05-80",0,1);
//$pdf->Cell(100, 5, "SWIFT Code MYMBGB2L",0,1);
//$pdf->Cell(100, 5, "IBAN Code GB50MYMB23058024127494",0,1);
//$pdf->Cell(100, 5, "Metro bank Address 8,Hayes Bridge Retail Park,Uxbridge Rd,Southall UB4 0RH",0,1);




$pdf->Cell(100, 5, "Account number 24127494",0,1);
$pdf->Cell(100, 5, "Sort Code 23-05-80",0,1);
$pdf->Cell(100, 5, "SWIFT Code MYMBGB2L",0,1);
$pdf->Cell(100, 5, "IBAN Code GB50MYMB23058024127494",0,1);
$pdf->Cell(100, 5, "Metro bank Address 8,Hayes Bridge Retail Park,Uxbridge Rd,Southall UB4 0RH",0,1);
ob_clean();

// Now Print this pdf page 
//$pdf->Output('Driver_TFL_DATA.pdf','D');
		 
$name_plate='bookings/'.$booking_id.'_courier_invoice.pdf';
		 $pdf->Output($name_plate,'F');
  
  
 function send_passenger_email($name_plate,$to,$message,$passenger)
		 {
		$to=$to;
		$message=$message;   
		$passenger=$passenger;
		//$file_name=$file_name;
		$name_plate=$name_plate;
		
		/*$mail = new PHPMailer;
		$mail->SMTPAuth   = true;
		$mail->IsHTML(true);  
		$mail->setFrom('info@wingschauffeurs.com', 'Wings Chauffeurs Ltd.');
		$mail->addAddress($to, $passenger);
		$mail->AddCC('info@wingschauffeurs.com', 'Wings');
		$mail->Subject  = 'Your Bookings Invoice Details';
		$mail->Body     =$message;
		//$mail->addAttachment($file_name);
		$mail->addAttachment($name_plate);
		if(!$mail->send()) {
		  echo 'Message was not sent.';
		  echo 'Mailer error: ' . $mail->ErrorInfo;
		  }else{ echo '<script>alert("Invoice sent in email."); window.location.replace("list_invoice_courier.php");</script>';
		  }
		  */
		  
		  
		  $subject = 'Your Bookings Invoice Details';

 $filename = $name_plate;    
$file=$name_plate;

    
$content = file_get_contents($file);
    
$content = chunk_split(base64_encode($content));

    // a random hash will be necessary to send mixed content
    
$separator = md5(time());

    
    // carriage return type (RFC)
    
//$eol = "+AFw-r+AFw-n";
 $eol = "\r\n";

//$headers = "MIME-Version: 1.0" . "\r\n";
		//$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

		// More headers
		$headers = 'From: Wings Chauffeurs Ltd <info@wingschauffeurs.com> ' . $eol;	
 $headers .= 'Cc: Wings Chauffeurs Ltd <info@wingschauffeurs.com>' . $eol;

    
 $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    $headers .= "This is a MIME encoded message." . $eol;

    // message
    $body = "--" . $separator . $eol;
    //$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    //$body .= "Content-Transfer-Encoding: 8bit" . $eol;
	$body .= "Content-type:text/html;charset=UTF-8" . $eol;
    $body .= $message . $eol;

    // attachment
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment" . $eol;
    $body .= $content . $eol;
    $body .= "--" . $separator . "--";

 //SEND Mail
    
if (mail($to, $subject, $body, $headers)) {

echo '<script>alert("Invoice sent in email."); window.location.replace("list_invoice_courier.php");</script>';        
echo "mail send ... OK"; // or use booleans here
   
 } else {
       
 echo "mail send ... ERROR!";
        print_r( error_get_last() );
   
 }
		  
		  
		  
		}
	//	$to="dashraja65@gmail.com";
		send_passenger_email($name_plate,$to,$message,$passenger);
		
	   }
	   ?>
         <script type="text/javascript">
		 /*
function printDiv() {
     var divToPrint = document.getElementById('invoice');
    var htmlToPrint = ''+
    '<style type="text/css">'+'th{border: 1px solid #7A7575;'+
'table td{align-content:left;padding-top:1px;font-size:12px;font-family:Arial;}'+
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
}
*/


                   </script>