<?php
error_reporting(0);
ob_start();
include "head.php";
include "dbconfig.php";
$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,
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;

$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);

?>
<?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'];
								
								
								
				}
				;?>

<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"> <a class="btn btn-info" href="list_driver_invoice.php"><i class="fa fa-user"></i><b>&nbsp;Back to Driver Invoice List</b></a></a> </div>
                       
<style>
#driver {border-collapse : collapse; border : 1px solid orange;}

#driver td {border: none;}
#driver th{
	font-size:12px;}
</style>
</head>

<body> 	
                       <?php $invoice_html=
					 '   
					 
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">	 
					 
				 <div id="invoice">	   
				 <h1>Sub Contractor Invoice</h1></td>
			 <table cellpadding="10" style="border:none;font-size:12px;" border="0" bordercolor="#F4EEEE">
					  
					  <tr> 
					  <td>
                                <b>Car Reg: </b>'.$reg.'<br>
                                <b>Driver: </b>'.$first_name.'<br>
                                <address>'.$driver_address.',<br>
										'.$driver_post_code.'
								</address>
                            </td>
						  
					  <div style="float:right;">
						  
                                Invoice #:'.$invoice_no.'<br>
                                Created:.'.date("d/m/Y",strtotime($date)).'<br>
                            </div>
							
							
						</tr>            
				   </table>
		
                  <table border="1" style="font-size:12px;" class="table table-striped">

        <thead>

                        <tr>
				<th  width="2%">Ref</th>
                           <th width="12%">Date</th>
						  
                           <th width="20%">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>
        ';?>
       
        <?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'];
					 $gross = $driver_data['gross'];
					 $date = $driver_data['date'];
					 
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr valign="middle">
                           <td><p>'.$job_id.'</p></td>
						   <td>'.$date.'</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 align="right">
						   <td  align="right"  colspan="9"><p><b>Total Amounts: </b></td>
						   <td >£'.number_format($nett,2).'
						   
				</tr>
				<tr align="right">
						   <td align="right"  colspan="9"><p><b>Percentage Amount('.number_format($percentage,2).'%): </b></td>
						   <td >£'.number_format($percentage_amount,2).'</td>
				</tr>
				<tr align="right">
						   <td align="right"  colspan="9"><p><b>Balance: </b></td>
						   <td >£'.number_format($nett-$percentage_amount,2).'</td>
				</tr>
				<tr align="right">
						   <td  align="right"  colspan="9"><p><b>Commission: </b></td>
						   <td >£'.number_format($commission,2).'
						   </td>
						   </b>
						   
					   </tr>
				  <tr align="right">
						   <td  align="right"  colspan="9"><p><b>To Pay: </b></td>
						   <td >£'.number_format($gross,2).'
						   </td>
						   </b>
						   
					   </tr> 
				 
					</tbody>
				 </table>
				</div>
					 
               ';
			   echo 
				   '<div class="col-md-11" id="invoice_div" style="margin-left:20px;">'.$invoice_html.'</div>';
				   ?>
			
				  
				   <script>
				
 function calculate_percentage_amount()  
{
	var percentage=$('#percentage').val();
	var sub_total=$('#sub_total').val();
	percentage_amount=(sub_total*percentage)/100;
	percentage_amount = parseFloat(percentage_amount).toFixed(2);
	$('#percentage_amount').val(percentage_amount);
}
 
 $('#percentage').keyup(function(){
	var percentage=$('#percentage').val();
	
	
	var sub_total=$('#sub_total').val();
	percentage_amount=(sub_total*percentage)/100;
	percentage_amount = parseFloat(percentage_amount).toFixed(2);
	
	//alert(percentage_amount);
	var nett=sub_total ;
	nett=parseFloat(nett).toFixed(2);
	
	
	var commission=$('#commission').val();
	//alert(commission);
	if(commission==''){commission=0;}
	commission = parseFloat(commission).toFixed(2);
	$('#paid').val();
	
	var gross=$('#gross').val();
	gross = parseFloat(gross).toFixed(2);
	gross=nett-percentage_amount;
	
	gross=gross-commission;
	gross=parseFloat(gross).toFixed(2);
	$('#percentage_amount').val(percentage_amount);
	$('#nett').val(nett);
	$('#gross').val(gross);
 });
 
 $('#percentage').blur(function(){
	
	var percentage= $('#percentage').val();
    percentage=parseFloat(percentage).toFixed(2);
	$('#percentage').val(percentage);
 });
 
 $('#commission').blur(function(){
	var commission= $('#commission').val();
    commission=parseFloat(commission).toFixed(2);
	if(commission==''){commission=0}
	commission=parseFloat(commission).toFixed(2);
	
	var percentage_amount= $('#percentage_amount').val();
    percentage_amount=parseFloat(percentage_amount).toFixed(2);
	
	
	
	var gross=$('#gross').val();
	gross = parseFloat(gross).toFixed(2);
	
	var nett=$('#nett').val();
	nett = parseFloat(nett).toFixed(2);
	
	gross=nett-percentage_amount;
	
	gross=gross-commission;
	gross=parseFloat(gross).toFixed(2);
	$('#gross').val(gross);
	$('#commission').val(commission);
	
 });
 
 $('#paid').blur(function(){
	var paid= $('#paid').val();
    paid=parseFloat(paid).toFixed(2);
	$('#paid').val(paid);
 });
 
 $('#commission').keyup(function(){
	var commission=$('#commission').val();
	commission = parseFloat(commission).toFixed(2);
	
	//alert(commission);
	
 });
                   
                   </script>
            <script>
			// Make Driver Invoice
   $("#driver_invoice_form").on('submit',(function(e) {
	
		e.preventDefault();
		$('#loader').show();
		
		$.ajax({
			
        	url: "ajax/create_driver_invoice.php",
			type: "POST",
			data:  new FormData(this),
			contentType: false,
    	    cache: false,
			processData:false,
			success: function(data)
		    { 
			//alert(data);
			if(data=='success'){
		alert("A New Invoice has been Added");
		$('#loader').delay(500).fadeOut("slow");
		window.location.replace("list_bookings.php");
			}else{
				alert("Booking Was Not Added");
				$('#loader').delay(500).fadeOut("slow");
				}
					
				
			//$("#gallery").html(data);
			
				
				
		    },
		  	error: function() 
	    	{
	    	} 	        
	   });
	}));
  </script>        
 <script>
 	$('#to').datepicker({
	
    	onSelect: function() {
			
                $( "#to" ).datepicker( "option", "dateFormat","yy-mm-dd");
		
        }
        });
	$('#from').datepicker({
	
    	onSelect: function() {
			
                $( "#from" ).datepicker( "option", "dateFormat","yy-mm-dd");
		
        }
        });	
 </script>
 <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>