<?php
ob_start();
include "head.php";
include 'phpmailer/PHPMailerAutoload.php';
error_reporting(E_ALL); 
?>
    <?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,bookings.transfer,

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

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>
					  
					  <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">	
                      <table>
					  
					  <tr>
					  
						 <td width="30%"> 
						  <b>
							 <p style="font-size:20px">Limo Chauffeurs Ltd.</p>
							
							  <address>
							12 Lampton Park Road,<br>
							Hounslow, TW3 4HS<br>
							Ph. +44(0)2082304594<br>
							M. +44(0)7808939220<br>
							E:info@limochauffeurs.com<br>
							www.limochauffeurs.com
							 </address>
					      </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'];
					 $parking=$job['transfer'];
					 $fare=$job['fare'];
					 $other=$job['other'];
					
					 $total_fare=$job['total_fare'];
					
					 $total=$total+$job['total_fare'];
					 
					 $nett=$total;
					 $vat=$nett*.20;
					 $gross=$nett+$vat;
					 ?>
                      
                     <?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;font-size:12px;">'.$pickup_address.'</div></td>
						   <td><div style="width:220px;font-size:12px;">'.$drop_address.'</div></td>
						   <td>'.$waiting_time.'</td>
						   <td align="right">£'.number_format((float)$waiting_charges,2).'</td>
						   <td align="right">£'.number_format((float)$fare,2).'</td>
						   <td align="right">£'.number_format((float)$parking,2).'</td>
						   <td align="right">£'.number_format((float)$other,2).'</td>
						   <td style="text-align:right;">£'.number_format((float)$total_fare,2).'</td>
						   					   
						   
					</tr>';
				 $id++;
				 } ?>
                 
                 <?php 
				
				 $invoice_html.='
				
				<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((float)$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((float)$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((float)$gross,2).'</p></td>
						   
					   </tr>
					   <tr>
					   <td style="text-align:center;font-size:12px;" colspan="13"><p><b>Limo Chauffeurs Ltd., METRO BANK, Account No: 10880769  Sort Code: 23-05-80, IBAN:GB24MYMB23058010880769, SWIFT: MYMBGB2L</td>
					   </tr>
					  </b> 
					</tbody>
				 </table>
				</div>
			  </body>
			</html> 
               ';?>
			
				   <?php 
				  //echo '<div class="col-md-11" id="invoice_div" style="margin-left:20px;">'.$invoice_html.'</div>'
				  echo '<script>alert("Invoice Updated Successfully");var url11 = "list_invoice.php?"+new Date().getTime();window.location.replace(url11);</script>';
				  ;?>
    <?php
/*	$sql = "  UPDATE`invoices` SET
		  nett='$nett',
		  vat='$vat',
		  gross='$gross', 
		  
		  WHERE invoice_no=$invoice_no 
";
//echo "UPDATE sql ".$sql."<br>";
$result=$con->query($sql);
*/
    // Now update the invoice
    ?>
  <?php 
	   if(isset($_POST['send_email'])){
	  //echo $invoice_html;
	  
   $passenger='jas';
   $subject="Your Invoice";
   $to='jaspinder32@gmail.com';
   $message=$invoice_html;
  // echo $message;
  function send_passenger_email($to,$message,$passenger)
		 {
		$to=$to;
		$message=$message;   
		$passenger=$passenger;
		
		$mail = new PHPMailer;
		$mail->SMTPAuth   = true;
		$mail->IsHTML(true);  
		$mail->setFrom('info@limochauffeurs.com', 'Limo Chauffeurs Ltd.');
		$mail->addAddress($to, $passenger);
		$mail->AddCC('rajtakkar@yahoo.co.uk', 'Rajesh');
		$mail->Subject  = 'Your Bookings Invoice Details';
		$mail->Body     =$message;
		
		if(!$mail->send()) {
		  echo 'Message was not sent.';
		  echo 'Mailer error: ' . $mail->ErrorInfo;
		  }else{echo '<script>alert("Invoice sent in email.");var url11 = "list_invoice.php?"+new Date().getTime(); window.location.replace(url11);</script>';}
		}
		send_passenger_email($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();
}
*/
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>