<?php
ob_start();
include "head.php";
include 'phpmailer/PHPMailerAutoload.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,customers.email,

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

                     <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> 
  
  
  					
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">
					  <div id="invoice">	
                      <table>
					  
					  <tr>
					  
						 <td width="30%"> 
						  <b>
							 <p style="font-size:20px">Limo Chauffeurs Ltd.</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="0" cellspacing="0" border="1">

        <thead>
			
            <tr>
                          <th>Sr.</th>
						  <th  width="2%">Ref</th>
                          <th width="10%">Date</th>
						  <th width="7%">Time</th>
                          <th width="10%">Passenger</th>
                          <th style="width:130px;">Pickup From</th>
                          <th style="width:130px;">Drop To</th>
                           <th>W/Time</th> 
						   <th  >W/Chrgs.</th>
                           <th >Price</th>
						   <th  >Parking</th>
						  
						   <th >Other</th>
                           <th >Total</th>
                         
            </tr>

        </thead>

        <tbody>
        ';?>
       
        <?php
				$total=0;
				$id=1;
                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']."<br>".$job['pickup_post_code'];
					 
					 $pickup_address=	ucfirst(strtolower($pickup_address));
					 $drop_address=$job['drop_address']."<br>".$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>
                            <td>'.$id.'</td>
						   <td>'.$booking_id.'</td>
						   <td>'.$date.'</td>
						   <td>'.$time.'</td>
						  
						   <td>'.$passenger.'</td>
						   <td><div style="width:220px;">'.$pickup_address.'</div></td>
						   <td><div style="width:220px;">'.$drop_address.'</div></td>
						   <td><p align="centre">'.$waiting_time.'</td>
						   <td align="right">£'.number_format($waiting_charges,2).'</td>
						   <td align="right">£'.number_format($fare,2).'</td>
						   <td align="right">£'.number_format($parking,2).'</td>
						   
						   <td align="right">£'.number_format($other,2).'</td>
						   <td style="text-align:right;">£'.number_format($total_fare,2).'</td>
						   					   
						   
					</tr>';
				 $id++;
				 } ?>
                 <?php 
				
				 $invoice_html.='
				<tr></tr>
				<b> 
				  <tr valign="middle">
                     
						   <td style="text-align:right;font-size:12px;" colspan="12"><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="12"><p><b>VAT(20%): </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="12"><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="13"><p><b>Bank Details: xxxxxxxxx   Sort Code: xxxxx</td>
					   </tr>
					  </b> 
					</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);
	 }
	 ?>  
  <?php 
	   if(isset($_POST['send_email'])){
	   //echo $invoice_html;
	   function send_driver_email($to,$message,$driver_name)
		 {
	   $driver_email=$to;
	   $driver_message=$message;
	   $driver_name='';
		$mail2 = new PHPMailer;
		$mail2->setFrom('info@limochauffeurs.com', 'Limo Chauffeurs Ltd.');
		$mail2->IsHTML(true);
		$mail2->addAddress($driver_email, $driver_name);
		$mail2->AddCC('jaspinder32@gmail.com', 'Rajesh');
		
		$mail2->Subject  = 'Your Invoice';
		$mail2->Body     = $driver_message;
		if(!$mail2->send()){
			echo '<script>alert("e-mail was not Sent");</script>';
			}else{
				echo '<script>alert("e-mail Sent");</script>';
		    }
		 }
	   send_driver_email('rajtakkar@yahoo.co.uk',$invoice_html,'Rajesh');
	   
	   }
	   
	   ?>
         <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) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

                   </script>