<?php
error_reporting(0);
ob_start();
include "head.php";
include "dbconfig.php";
include 'phpmailer/PHPMailerAutoload.php';		
date_default_timezone_set ("Europe/London");
$invoice_no=$_GET['invoice_no'];
?>

<?php
$sql='';
$from='';
$to='';

	$first_name='';
								$pco_number='';
								$pco_expiry='';
								$reg='';
								$make='';
								$driver_address='';
								$driver_post_code='';

	$invoice_no=$_GET['invoice_no'];
	$commission=0;//$_POST['commission'];
	$percentage=0;//$_POST['percentage'];
$sql="SELECT DISTINCT 
driver_invoices.invoice_no,driver_invoices.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,
driver_bookings.waiting_charges,driver_bookings.fare,driver_bookings.total_fare,driver_bookings.waiting_time,driver_bookings.other,driver_bookings.parking,driver_invoices.commission_text,driver_invoices.percentage_text,
customers.name as company,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,
driver_invoices.*
FROM `driver_invoice_details` 
INNER JOIN bookings on driver_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 driver_bookings on driver_bookings.booking_id=bookings.id
INNER JOIN driver_invoices on driver_invoice_details.invoice_no=driver_invoices.invoice_no
WHERE driver_invoices.invoice_no=".$invoice_no." ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC";

$today= date('Y-m-d');
if(!mysqli_query($con,$sql)){echo 'error'.mysqli_error($con);}
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);

?>
<style>


</style>			<form action="" method="post">
					<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-primary btn-sm" value="Send Email" /> 
                    </div>
                   </form>
</head>

<body> 
  <?php
if(!mysqli_query($con,$sql)){echo 'error'.mysqli_error($con);}
$invoice_sum=mysqli_query($con,$sql);
  
  					
  						$nett='';
								$commission='';
								$percentage='';
								$percentage_amount='';
								$gross='';
								$first_name='';
								$pco_number='';
								$pco_expiry='';
								$reg='';
								$make='';
								
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
								$first_name=$sum['first_name'];
								$date=$sum['date'];
								$reg=$sum['reg'];
								$driver_address=$sum['driver_address'];
								$driver_post_code=$sum['driver_post_code'];
								$percentage=$sum['percentage'];
								$percentage_amount=$sum['percentage_amount'];
								
								
								
				}
						?>
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">
					  <div id="invoice">
					  <h1>Sub Contractor Invoice</h1>	
                      <table>
					  
					  <tr>
						 <td> 
						  
							 <p>'.$first_name.'</p>
							 <p>'.$driver_address.',</p>
							 <p>'.$driver_post_code.'</p>
							
					      </td>
						  <td style="text-align:left;word-break:break-all;"> 
						  
					      </td>
						 
						 <td  style="text-align:left;"> 
						  
							<p>Invoice: '.$invoice_no.'</p>
							<p> Date: '.date("d/m/Y",strtotime($date)).'</p>
							 
					      </td>
						 
						  									   
						   <td class="bold-text">  
						     
							
						   </td>
						</tr>            
				   </table>
						        
                          
                  <table>

        <thead>
			
            <tr>
                           <th  width="2%">Ref</th>
                           <th width="3%">Date</th>
						    <th width="3%">Time</th>
						  
                           
                           <th width="30%">Pickup From</th>
                           
                           <th width="30%">Drop To</th>
                           <th width="7%">W/Time</th> 
						   <th  width="5%">W/Chrgs.</th>
                           <th width="7%">Price</th>
						   <th  width="7%">Car Park</th>
						   
						   <th  width="5%">Other</th>
                           <th  width="15%">Total</th>
                          
            </tr>

        </thead>

        <tbody style="text-align:left;">
        ';?>
       
        <?php
				$total=0;
				$total_nett=0;
				$total_commission=0;
				$total_gross=0;
				$percentage=0;
				$percentage_amount=0;
				$total_percentage_amount=0;
				$total_gross=0;
				$count=0;
                while($driver_data=mysqli_fetch_assoc($res))
				
                  {
					  	  
					 $job_id=$driver_data['booking_id'];
					 $pickup_date = date("d/m/Y",strtotime($driver_data['pickup_date']));
					 $time = $driver_data['hours'].":".$driver_data['mins'];
	 				 $passenger=$driver_data['passenger'];
					 $company=$driver_data['company'];
					 $pickup_address=$driver_data['pickup_address'];
					 $pickup_post_code=$driver_data['pickup_post_code'];
					 $drop_address=$driver_data['drop_address'];
					 $drop_post_code=$driver_data['drop_post_code'];
					 $waiting_time=$driver_data['waiting_time'];
					 $waiting_charges=$driver_data['waiting_charges'];
					 $parking=$driver_data['parking'];
					 $fare=$driver_data['fare'];
					 $other=$driver_data['other'];
					 $total_fare=$driver_data['total_fare']; 
					 $total_gross=$total_gross+$driver_data['total_fare'];
					 $driver_id = $driver_data['driver_id'];
					 $nett = $driver_data['nett'];
					 $percentage = $driver_data['percentage'];
					 $percentage_amount = $driver_data['percentage_amount'];
					 $commission = $driver_data['commission'];
					 $commission_text = $driver_data['commission_text'];
					 
					 $percentage_text = $driver_data['percentage_text'];
					 $gross = $driver_data['gross'];
					 $date = $driver_data['date'];
					 $vat = $driver_data['vat'];
					 
					 ?>
                     
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
                           <td><p>'.$job_id.'</p></td>
						   <td><p>'.$pickup_date.'</p></td>
						  <td><p>'.$time.'</p></td>
						  
						   <td><p>'.$pickup_address.'</p></td>
						   <td><p>'.$drop_address.'</p></td>
						   <td><p>'.$waiting_time.'</p></td>
						   <td align="right"><p>£'.number_format($waiting_charges,2).'</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($other,2).'</p></td>
						   <td style="text-align:right;"><p>£'.number_format($total_fare,2).'</p></td>
						   					   
						   
					</tr>';
				 } ?>
                 <?php 
				
				 $invoice_html.='
				
				  
				<tr align="right">
						   <td  align="right"  colspan="10"><p><b>Total Amounts: </b></td>
						   <td >£'.number_format($nett,2).'
						   
				</tr>
				<tr align="right">
				               
						   <td  align="right"  colspan="10"><p><b>'.$percentage_text.'('.$percentage.'%): </b></td>
						   <td >£'.number_format($percentage_amount,2).'</td>
				</tr>
				<tr align="right">
						   <td align="right"  colspan="10"><p><b>Balance: </b></td>
						   <td >£'.number_format($nett-$percentage_amount,2).'</td>
				</tr>
				';?>
                <?php
				
                    
				
					if($commission==''){
					
				}else{
					$invoice_html.='<tr align="right">
						   <td  align="right"  colspan="10"><p><b>'.$commission_text.': </b></td>
						   <td >£'.number_format($commission,2).'
						   </td>
						   </b>
						   
					   </tr>';
					}
				if($vat==''){
					
				}else{
					$invoice_html.='<tr align="right">
						   <td  align="right"  colspan="10"><p><b>VAT (20%): </b></td>
						   <td >£'.number_format($vat,2).'
						   </td>
						   </b>
						   
					   </tr>';
					}
				 ?>
				 <?php 
				
				 $invoice_html.=' 
				  <tr align="right">
						   <td  align="right"  colspan="10"><p><b>To Pay: </b></td>
						   <td >£'.number_format($gross,2).'
						   </td>
						   </b>
						   
					   </tr> 
					   
					</tbody>
				 </table>
				</div>
				</body>
				</html>	 
               ';?>
			
				   <?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('rajtakkar@yahoo.co.uk', 'Rajesh');
		
		$mail2->Subject  = 'Your Booking Reminder';
		$mail2->Body     = $driver_message;
		if(!$mail2->send()){
			echo '<h3>Email was not sent</h3>';
			}else{
				echo 'Mail was sent';
		    }
		 }
	   send_driver_email('jaspinder32@gmail.com',$invoice_html,'Jas');
	   
	   }
	   ?>
 
				   
                   <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>