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

<?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 
bookings.id as job_id,
bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.mins,bookings.substitute,driver_bookings.end_date,driver_bookings.start_date,driver_bookings.days,bookings.rate,driver_bookings.supp_cost,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,
driver_bookings.waiting_charges,driver_bookings.supp_cost,driver_bookings.total_fare,driver_bookings.waiting_time,driver_bookings.other,driver_bookings.parking,driver_bookings.transfer,
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,employees.email,
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.start_date ASC, bookings.hours ASC, bookings.mins ASC";*/

/*$sql="SELECT DISTINCT 
bookings.id as job_id,
bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.mins,bookings.substitute,driver_bookings.end_date,driver_bookings.start_date,driver_bookings.days,bookings.rate,driver_bookings.supp_cost,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,
driver_bookings.waiting_charges,driver_bookings.supp_cost,driver_bookings.total_fare,driver_bookings.waiting_time,driver_bookings.other,driver_bookings.parking,driver_bookings.transfer,
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,employees.email,
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.start_date ASC, bookings.hours ASC, bookings.mins ASC";

$sql="SELECT driver_invoices.invoice_no,driver_invoices.date, employees.first_name,driver_invoices.nett, driver_invoices.gross FROM driver_invoices 
LEFT JOIN invoice_status ON driver_invoices.status = invoice_status.id 
LEFT JOIN employees ON driver_invoices.driver_id = employees.id 
";*/
$sql="SELECT * FROM driver_invoice_details WHERE invoice_no='$invoice_no'";



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

//$driver_id=$d_sub['driver_id'];

//$customers_sql1="select * from driver_substitute WHERE substitute_id='$driver_id' AND booking_id= ORDER BY start_date ASC";
	//								$t_result1=mysqli_query($con,$customers_sql1);
		//							$sub=mysqli_fetch_assoc($t_result1);
			//						$bid=$sub['booking_id'];
							  // $driver_id;
							   // date("d/m/y", strtotime($sub['start_date']));
$customers_sql2="select * from employees WHERE employees.id='$driver_id'";
									$t_result2=mysqli_query($con,$customers_sql2);
									$sub2=mysqli_fetch_assoc($t_result2);
$sql3="select * from driver_invoices WHERE invoice_no='$invoice_no'";
									//$t_result3=mysqli_query($con,$customers_sql3);
									//$sub3=mysqli_fetch_assoc($t_result3);									
									

?>
<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,$sql3);
  
  					
  						$nett='';
								$commission='';
								$percentage='';
								$percentage_amount='';
								$gross='';
								$first_name='';
								$pco_number='';
								$pco_expiry='';
								$reg='';
								$make='';
								$c=0;
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
				$c++;	
								//$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'];
								
								
			
				 $first_name=$sub2['first_name'];
					 
							//	$pco_number=$sub2['pco_number'];
							//	$pco_expiry=$sub2['pco_expiry'];
								//$reg=$sub2['reg'];
								//$make=$sub2['make'];
								$email=$sub2['email'];
								$driver_address=$sub2['address'];
								$driver_post_code=$sub2['post_code'];
					}	
						?>
                       <?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 border="1">

        <thead>
			
            <tr>
                            <th  width="2%">Ref</th>
                           <th width="20%">Client</th>
						   <th width="7%">Start date</th>
                           <th width="7%">End date</th>
                           
                           <th width="7%">Days</th>
                           
                           <th width="7%">Rate</th>
						   <th  width="7%">Parking</th>
						   <th  width="7%">Transfer</th>
						   
						  
                           <th  width="15%">Total</th>
                          
            </tr>

        </thead>

        <tbody>
        ';?>
       
        <?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;
				$tdays=0;
                while($driver_data=mysqli_fetch_assoc($res))
				
                  {
					  	  
						  
					  $job_id=$driver_data['booking_id']; 
					 // $customers_sql1="select * from driver_substitute WHERE substitute_id='$driver_id' AND booking_id='$job_id' ORDER BY start_date ASC";
						//			$t_result1=mysqli_query($con,$customers_sql1);
							//		$sub=mysqli_fetch_assoc($t_result1);
									//$bid=$sub['booking_id'];
				
					 
					 $customers_sql11="select * from driver_bookings where driver_id='$driver_id' AND booking_id='$job_id' AND invoice_status='2'";
									$t_result11=mysqli_query($con,$customers_sql11);
									$sub11=mysqli_fetch_assoc($t_result11);
									
									 $start_date = date("d/m/Y",strtotime($sub11['start_date']));
					   $end_date = date("d/m/Y",strtotime($sub11['end_date']));
					   $tdays=$tdays+$sub11['days'];
					    $days=$sub11['days'];
						$rate=$sub11['supp_cost'];
						$total_fare=$sub11['total_fare']; 
						
						 $parking=$sub11['parking'];
					$transfer=$sub11['transfer'];
							  
						  
					
					// $driver_id = $driver_data['driver_id'];
					 //$date = $driver_data['date'];
					 //$pickup_date = date("d/m/Y",strtotime($driver_data['pickup_date']));
					 //$time = $driver_data['hours'].":".$driver_data['mins'];
					  $sql_p="select * from bookings where id='$job_id'";
									$p_result=mysqli_query($con,$sql_p);
									$pass=mysqli_fetch_assoc($p_result);
									$pid=$pass['passenger_id'];
					$sql_pass="select * from company_contact where id='$pid'";
									$pass_result=mysqli_query($con,$sql_pass);
									$passenger_data=mysqli_fetch_assoc($pass_result);
									//$pid=$passenger_data['name'];				
	 				 $passenger=ucfirst(strtolower($passenger_data['name']));
					// $company=ucfirst(strtolower($driver_data['company']));
					// $pickup_address=ucfirst(strtolower($driver_data['pickup_address']));
					// $pickup_post_code=$driver_data['pickup_post_code'];
					// $drop_address=ucfirst(strtolower($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'];
					// $transfer=$driver_data['transfer'];
					 //$fare=$driver_data['fare'];
					// $other=$driver_data['other'];
					// $total_fare=$driver_data['total_fare']; 
					
					
					//$job_id = $driver_data['job_id'];
					// $pickup_date = date("d/m/Y",strtotime($driver_data['pickup_date']));
					 // $start_date = date("d/m/Y",strtotime($driver_data['start_date']));
					 //  $end_date = date("d/m/Y",strtotime($driver_data['end_date']));
					//   $days=$driver_data['days'];
					 //  $rate=$driver_data['supp_cost'];
					// $time = $driver_data['hours'].":".$driver_data['mins'];
	 				// $passenger=$driver_data['passenger'];
					// $company=$driver_data['company'];
					// $pickup_address=$driver_data['pickup_address']."<br><b>".$driver_data['pickup_post_code'].'</b>';
					
					
					
					 
					 
					 $sql4="select * from driver_invoices WHERE invoice_no='$invoice_no'";
									$t_result4=mysqli_query($con,$sql4);
									$sub4=mysqli_fetch_assoc($t_result4);	
					 $nett = $sub4['nett'];
					 $percentage_text = $sub4['percentage_text'];
					 $percentage = $sub4['percentage'];
					 $percentage_amount = $sub4['percentage_amount'];
					 $commission_text = $sub4['commission_text'];
					 $commission = $sub4['commission'];
					 
					 $vat_text = $sub4['vat_text'];
					 $vat_p = $sub4['vat_p']; 
					 $vat_amount = $sub4['vat_amount'];
					 
					 $total_gross=$total_gross+$total_fare;
					 $gross = $sub4['gross'];
					
					 
					// $email = $driver_data['email'];
					 
							
									
									
					 
					 ?>
                     
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
                           <td>'.$job_id.'</p></td>
						  <td><p>'.$passenger.'</p></td>
						   <td>'.$start_date.'</td>
						  
						   <td><p>'.$end_date.'</p></td>
						   <td><p>'.$days.'</p></td>
						   <td align="right"><p>£'.number_format($rate,2).'</p></td>
						   <td align="right"><p>£'.number_format($parking,2).'</p></td>
						   <td align="right"><p>£'.number_format($transfer,2).'</p></td>
						   <td style="text-align:right;">£'.number_format((float)$total_fare,2).'</td>
						   					   
						   
					</tr>';
				 } ?>
                 
				 <?php 
				$invoice_html.=' 
				  <tr align="right">
						   <td  align="right"  colspan="8"><p><b>Sub Total: </b></td>
						   <td >£'.number_format((float)$total_gross,2).'
						   </td>
						   </b>
						   
					   </tr> ';
					   if(!empty($percentage_text) || !empty($percentage))
					   {
						   
					   $invoice_html.=' 
				  <tr align="right">
						   <td  align="right" colspan="8"><p><b>'.$percentage_text.'('.$percentage.'%) </b></td>
						   <td >£'.number_format((float)$percentage_amount,2).'
						   </td>
						   </b>
						   
					   </tr> ';
					   }
					   if(!empty($commission_text) || !empty($commission))
					   {
					  // <td  align="right"  colspan="8"><p><b>'.$commission_text.'</b></td>
					   $invoice_html.=' 
				  <tr align="right">
				   <td  align="right"  colspan="8"><p><b>commission</b></td>
						   
						   <td >£'.number_format((float)$commission,2).'
						   </td>
						   </b>
						   
					   </tr> ';
					   }
					   if(!empty($vat_amount) && !empty($vat_p))
					   {
					   $invoice_html.=' 
				  <tr align="right">
						   <td  align="right"  colspan="8"><p><b>'.$vat_text.'('.$vat_p.'%)</b></td>
						   <td >£'.number_format((float)$vat_amount,2).'
						   </td>
						   </b>
						   
					   </tr> ';
					   }
				 $invoice_html.=' 
				  <tr align="right">
						   <td  align="right"  colspan="8"><p><b>To Pay: </b></td>
						   <td >£'.number_format((float)$gross,2).'
						   </td>
						   </b>
						   
					   </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['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@wingschauffeurs.com', 'Wings Chauffeurs Ltd.');
		$mail2->IsHTML(true);
		$mail2->addAddress($driver_email, $driver_name);
		$mail2->AddCC('info@wingschauffeurs.com', 'wings');
		
		$mail2->Subject  = 'Your Bookings Invoice Details';
		$mail2->Body     = $driver_message;
		if(!$mail2->send()){
			echo '<h3>Email was not sent</h3>';
			}else{
				echo 'Mail was sent';
		    }
		 }
	   send_driver_email('info@wingschauffeurs.com',$invoice_html,'wings');
	   
	   }
	   ?>
    
	 <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>