<?php
ob_start();
include "head.php";?>
    <?php
	//include "dbfunction.php";
	include "dbconfig.php";
  $invoice_no =$_GET['invoice_no'];
  $sql="SELECT DISTINCT 
invoices.invoice_no,invoices.date as invoice_date,

bookings.pickup_date,bookings.id as booking_id,bookings.hours,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,

customers.name as company,customers.address1,customers.post_code,

company_contact.name as passenger,
employees.first_name,vehicles.reg,employees.id as driver_id,employees.first_name as first_name,employees.address as driver_address,employees.post_code as driver_post_code,
invoices.*
FROM `invoice_details` 
INNER JOIN bookings on invoice_details.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
INNER JOIN invoices on invoice_details.invoice_no=invoices.invoice_no
WHERE invoices.invoice_no=$invoice_no
GROUP BY bookings.id
";

$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);
$client=mysqli_query($con,$sql);
$invoice=mysqli_fetch_assoc($client)
?>
<style>
table th{
	padding:12px;
}
p {
	line-height:1;
}

</style>
<div class="col-md-2">
                       <input type="button" class="btn btn-primary btn-sm" onclick="printDiv('invoice')" value="Print" /> 
                       </div>
</head>

<body> 
  
  
  						<?php
  						$customer_name=$invoice['company'];
                        $address1=$invoice['address1'];
                        $post_code=$invoice['post_code'];
						$invoice_no=$invoice['invoice_no'];
                        $invoice_date=date("d/m/Y",strtotime($invoice['invoice_date']));
						$date='';
						?>
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">
					  <div id="invoice">	
                      <table cellpadding="10" style="border:none;font-size:12px;" border="0" bordercolor="#F4EEEE">
					  
					  <tr>
					  
						 <td> 
						  <b>
							 <p style="font-size:20px">Limo Chauffeurs Ltd.</p>
							 <p>12 Lampton Park Road, </p>
							 <p>Hounslow,</p>
							 <p>TW3 4HS</p>
							  <p>E:info@limochauffeurs.com </p>
							 <p>Ph. +44(0)2082304594</p>
							 <p>M. +44(0)7808939220</p>
							
							 <p>Website: www.limochauffeurs.com</p>
					      </td>
						  <td style="text-align:left;word-break:break-all;"> 
						  <b>
						  <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:left;"> 
						 <b>
						  <p><b>Details </b></p>
							 <p>Invoice No. '.$invoice_no.'</p>
							<p>Invoice. Date: '.$invoice_date.'</p>
							 <p>VAT No. 1234569898</p>
							 <p>Payemnt Terms: 14 days</p>
					      </td>
						 
						  									   
						   <td class="bold-text">  
						     
							
						   </td>
						   </b>
						</tr>            
				   </table>
						        
                          
                  <table cellpadding="5" border="1">

        <thead style="background-color:#020651;text-align:left;font-size:10px; color:#fff; padding:12px;">
			
            <tr>
                           <th  width="2%">Ref</th>
                           <th width="3%">Date</th>
						  <th width="2%">Time</th>
                           <th width="10%">Passenger</th>
                           <th width="30%">Pickup From</th>
                           
                           <th width="30%">Drop To</th>
                           
                           <th width="7%">Price</th>
						   <th  width="7%">Car Park</th>
						   <th  width="5%">Waiting</th>
						   <th  width="5%">Other</th>
                           <th  width="15%">Total</th>
                          
            </tr>

        </thead>

        <tbody style="text-align:left;font-size:12px;">
        ';?>
       
        <?php
				$total=0;
                while($job=mysqli_fetch_assoc($res))
				
                  {
					  
					 $booking_id=$job['booking_id'];
					 $date=date("d/m/y", strtotime($job['pickup_date']));
					 $time=$job['hours'].":".$job['mins'];
					 $passenger=$job['passenger'];
					 
					 $passenger=	ucfirst(strtolower($passenger));
					 $pickup_address=$job['pickup_address'].",".$job['pickup_post_code'];
					 
					 $pickup_address=	ucfirst(strtolower($pickup_address));
					 $drop_address=$job['drop_address'].",".$job['drop_post_code'];
					 
					 $drop_address=	ucfirst(strtolower($drop_address));
					 
					 
					 $waiting_time=$job['waiting_time'];
					 $waiting_charges=$job['waiting_charges'];
					 $parking=$job['parking'];
					 $fare=$job['fare'];
					 $other=$job['other'];
					 $total_fare=$job['total_fare'];
					
					 $total=$total+$job['nett'];
					 $nett=$job['nett'];
					 $vat=$job['vat'];
					 $gross=$job['gross'];
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr valign="middle">
                           <td><p>'.$booking_id.'</p></td>
						   <td>'.$date.'</td>
						   <td>'.$time.'</td>
						  
						   <td><p>'.$passenger.'</p></td>
						   <td><p>'.$pickup_address.'</p></td>
						   <td><p>'.$drop_address.'</p></td>
						   <td align="right"><p>£'.number_format($fare,2).'</p></td>
						   <td align="right"><p>£'.number_format($parking,2).'</p></td>
						   <td align="right"><p>£'.number_format($waiting_charges,2).'</p></td>
						   <td align="right"><p>£'.number_format($other,2).'</p></td>
						   <td style="text-align:right;"><p>£'.number_format($total_fare,2).'</p></td>
						   					   
						   
					</tr>';
				 } ?>
                 <?php 
				
				 $invoice_html.='
				  <tr valign="middle">
                     
						   <td style="text-align:right;font-size:12px;" colspan="10"><p><b>Sub Total: </b></td>
						   <td style="text-align:right;font-size:12px"><p>£'.number_format($nett,2).'</p></td>
						   
					   </tr>
					   <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="10"><p><b>VAT: </b></td>
						   <td style="text-align:right;font-size:12px"><p>£'.number_format($vat,2).'</p></td>
						   
					   </tr>
					   <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="10"><p><b>Grand Total: </b></td>
						   <td style="text-align:right;font-size:12px"><p>£'.number_format($gross,2).'</p></td>
						   
					   </tr>
					   <tr>
					   <td style="text-align:center;font-size:12px;" colspan="11"><p><b>Bank Details: xxxxxxxxx   Sort Code: xxxxx</td>
					   </tr>
					   
					</tbody>
				 </table>
				</div>
					 
               ';?>
			
				   <?php 
				   
				   echo 
				   '<div class="col-md-11" id="invoice_div" style="margin-left:20px;">'.$invoice_html.'</div>'
				  
				   ;?>
     <?php 
	 if(isset($_POST['make_invoice'])){
	 	echo "Invoice Created".number_format($gross,2);
	 }
	 ?>  
       
       
 
				   
                   <script type="text/javascript">
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}
                   </script>