<?php
error_reporting(0);
ob_start();
include "head.php";
include "dbconfig.php";
?>
<form method="post" action="">
	<div class="container-fluid" style="margin-top:200x;">
	  <label><h2> Select a Driver</h2></label>
			      <div class="well col-md-12" style="background-color:#e9e9e9; box-shadow: 5px 5px 5px;">
			       	 
                       <div class="form-group col-sm-3">
			       	   <select name="driver_id" id="driver" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select from List </option>
                               <?php
							   
							        $customers_sql="SELECT * from employees order by first_name ASC";
									$t_result=mysqli_query($con,$customers_sql);
									while($customer=mysqli_fetch_assoc($t_result))
									{
									
									?>
	<option style="padding:20px; background-color:#2977C9; color:#F9F0F1; cursor:pointer;" value="<?php echo $customer['id']; ?>">
	   <?php echo $customer['first_name']; ?>
    </option>
						<?php
						
						}
						?>
			       	   </select>
			       	   </div>
                       <div class="col-md-2">
 						<input type="text" name="from" class="from form-control"  placeholder="Date From" id="from">
                     </div>
                       <div class="col-md-2">
                        <input type="text"  name="to" class="to form-control" placeholder="Date To" id="to">
             		 </div>
                     
			       	 <div class="col-md-2">
                       	  <input type="submit" id="select_jobs" class="btn btn-warning" value="See on Screen">
                       </div>
                      
                    <!-- now retrieve the transactions of this customer -->
                   </div>
                 			
</div>
				  
			    
</form>
<?php
$sql='';
$from='';
$to='';
$sql="SELECT DISTINCT bookings.id as job_id,bookings.pickup_date,bookings.pickup_address,company_contact.name as passenger,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,
customers.name as company,employees.first_name,
driver_bookings.waiting_charges,driver_bookings.fare,driver_bookings.total_fare,driver_bookings.waiting_time,driver_bookings.other,driver_bookings.parking,
vehicle_make.make,vehicles.reg,
employees.pco_number,employees.pco_expiry,employees.first_name as first_name
FROM bookings
LEFT JOIN customers ON bookings.customer_id=customers.id
LEFT JOIN company_contact on bookings.passenger_id=company_contact.id
LEFT JOIN invoices on bookings.invoice_no=invoices.invoice_no
LEFT JOIN employees on bookings.driver_id=employees.id
LEFT JOIN driver_bookings on driver_bookings.booking_id=bookings.id
LEFT JOIN vehicles on vehicles.driver_id=employees.id
LEFT JOIN vehicle_make on vehicle_make.make=vehicles.make
";
if (!empty($_POST['from']) && !empty($_POST['to']))
	{
	  $from=$con->real_escape_string($_POST['from']);
	  $from = str_replace("/","-",$from);
	  $from = date("Y-m-d", strtotime($from));
	  $to=$con->real_escape_string($_POST['to']);
	  $to = str_replace("/","-",$to);
	  $to = date("Y-m-d", strtotime($to));
	  $sql.=" AND bookings.pickup_date BETWEEN '$from' AND '$to'";
	}
	


if (!empty($_POST['driver_id'])) {
	$driver_id=$_POST['driver_id'];
	$commission=0;//$_POST['commission'];
	$percentage=0;//$_POST['percentage'];

	$sql.=" where employees.id=$driver_id"; 
}


$sql.=" order by employees.id DESC";

$today= date('Y-m-d');


$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);

?>
<?php
$invoice_sum=mysqli_query($con,$sql);
$total_driver_invoices=mysqli_num_rows($invoice_sum);
$res=mysqli_query($con,$sql);

//echo $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'];
								$pco_number=$sum['pco_number'];
								$pco_expiry=$sum['pco_expiry'];
								$reg=$sum['reg'];
								$make=$sum['make'];
								
								
				}
				;?>

</head>

<body> 	
                
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">	
					  <div id="invoice">
                      <h2>Sub Contractor <b>'.$first_name.' </b><h2>
					 <table cellpadding="10" style="border:none;font-size:12px;" border="0" bordercolor="#F4EEEE" width="100%">
					    <tr>
						  <td> 
						     <p>Rajesh Takkar.</p>
							 <p>12 Lampton Park Road,</p>
							 <p>Hounslow,</p>
							 <p>TW3 4HS</p>
							 
					      </td>
						  <td style="text-align:left;word-break:break-all;"> 
						  
							
					      </td>
						 
						 <td  style="text-align:left;"> 
						     <p><b>Details </b></p>
							 <p><b>Pay Scheme:  Percentage </b></p>
							 <p>Invoice No. AIL07525</p>
							 <p>Invoice. Date: '.date("d/m/Y").'</p>
					    </td>
						<td  style="text-align:left;"> 
						     <p><b>Driver % : 100.00</b></p>
							 <p><b>Company %:  0.00 </b></p>
							 
					    </td>
						 
						  									   
						   <td class="bold-text">  
						     
							
						   </td>
						</tr>            
				   </table>
                  <table border="1" style="font-size:12px;" class="table table-striped">

        <thead>

                        <tr>

                           <th>Job ID</th>
                           <th>Pickup Date</th>
                           <th>Company</th>
						   <th>Passenger</th>
						   <th>Pickup</th>
						   <th>Drop</th>
						   <th>Wt. Time</th>
                           <th>Wt. Chgs.</th>
                           <th>Prkng</th>
                           <th>Fare</th>
                           <th>other</th>
                           <th >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;
                while($driver_data=mysqli_fetch_assoc($res))
				
                  {
					  			  
					 $job_id = $driver_data['job_id'];
					 $pickup_date = date("d/m/Y",strtotime($driver_data['pickup_date']));
	 				 $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'];
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
                           
						   <td style="text-align:left;font-size:12px"><p>'.$job_id.'</p></td>
						   <td style="text-align:left;font-size:12px"><p>'.$pickup_date.'</p></td>
						   <td style="text-align:left;font-size:12px"><p>'.$company.'</p></td>
						   <td style="text-align:left;font-size:12px"><p>'.wordwrap($passenger,20,"<br>\n").'</p></td>
						   
						   <td style="text-align:left;font-size:12px"><p>'.$pickup_address.'<br>'.$pickup_post_code.'</p></td>
						   
						   <td style="text-align:left;font-size:12px"><p>'.$drop_address.'<br>'.$drop_post_code.'</p></td>
						    <td><p>'.$waiting_time.'</p></td>
						   <td style="text-align:right;"><p>£'.number_format($waiting_charges,2).'</p></td>
						   <td style="text-align:right;"><p>£'.number_format($parking,2).'</p></td>
						   <td style="text-align:right;"><p>£'.number_format($fare,2).'</p></td>
						   <td style="text-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.='
				  <form action="" method="post">
				<tr>
						   <td  align="right"  colspan="11"><p><b>Total Amounts: </b></td>
						   <td >
						   <input type="text" value="'.number_format($total_gross,2).'" name="nett" id="nett" class="form-control">
						   
				</tr>
				<tr>
						   <td align="right"  colspan="11"><p><b>%age: </b></td>
						   <td ><input value="" type="number" class="form-control" name="vat_p" id="vat_p" placeholder="%age" ></td>
				</tr>
				<tr>
						   <td align="right"  colspan="11"><p><b>Percentage Amount: </b></td>
						   <td ><input readonly value="" type="number" class="form-control" name="vat" id="vat" placeholder="VAT Amount"></td>
				</tr>
				<tr>
						   <td  align="right"  colspan="11"><p><b>Commission: </b></td>
						   <td ><input value="" type="number" class="form-control" name="permit" id="permit" placeholder="Comm.?"></td>
						   </b>
						   
					   </tr>
				  <tr>
						   <td  align="right"  colspan="11"><p><b>To Pay: </b></td>
						   <td ><input readonly value="" type="number" class="form-control" name="gross" id="gross" placeholder="Gross"></td>
						   </b>
						   
					   </tr> 
					    <tr>
						   <td  align="right"  colspan="11"><p><b></b></td>
						   <td ><input type="submit" name="btn-submit" class="btn btn-success" value="Save Invoice"></td>
						   </b>
					</form>	   
					
								<div class="input-group">
									
									<input readonly value="" type="hidden" class="form-control" name="nett" id="nett" placeholder="Sub Total">
								</div>
							</div> 
                            
							<input type="hidden" id="sub_total" value="'.$total_gross.'" name="sub_total"/>   
					   </tr> 
					</tbody>
				 </table>
				</div>
					 
               ';?>
			
				   <?php 
				   
				   echo 
				   '<div class="col-md-11" id="invoice_div" style="margin-left:40px;">'.$invoice_html.'</div>'
				  
				   ;?>
				   <script>
				
 function calculate_vat()  
{
	var vat_p=$('#vat_p').val();
	var sub_total=$('#sub_total').val();
	vat=(sub_total*vat_p)/100;
	vat = parseFloat(vat).toFixed(2);
	$('#vat').val(vat);
}
 
 $('#vat_p').keyup(function(){
	var vat_p=$('#vat_p').val();
	
	
	var sub_total=$('#sub_total').val();
	vat=(sub_total*vat_p)/100;
	vat = parseFloat(vat).toFixed(2);
	
	//alert(vat);
	var nett=sub_total ;
	nett=parseFloat(nett).toFixed(2);
	
	
	var permit=$('#permit').val();
	//alert(permit);
	if(permit==''){permit=0;}
	permit = parseFloat(permit).toFixed(2);
	$('#paid').val();
	
	var gross=$('#gross').val();
	gross = parseFloat(gross).toFixed(2);
	gross=nett-vat;
	
	gross=gross-permit;
	gross=parseFloat(gross).toFixed(2);
	$('#vat').val(vat);
	$('#nett').val(nett);
	$('#gross').val(gross);
 });
 
 $('#vat_p').blur(function(){
	
	var vat_p= $('#vat_p').val();
    vat_p=parseFloat(vat_p).toFixed(2);
	$('#vat_p').val(vat_p);
 });
 
 $('#permit').blur(function(){
	var permit= $('#permit').val();
    permit=parseFloat(permit).toFixed(2);
	if(permit==''){permit=0}
	permit=parseFloat(permit).toFixed(2);
	
	var vat= $('#vat').val();
    vat=parseFloat(vat).toFixed(2);
	
	
	
	var gross=$('#gross').val();
	gross = parseFloat(gross).toFixed(2);
	
	var nett=$('#nett').val();
	nett = parseFloat(nett).toFixed(2);
	
	gross=nett-vat;
	
	gross=gross-permit;
	gross=parseFloat(gross).toFixed(2);
	$('#gross').val(gross);
	$('#permit').val(permit);
	
 });
 
 $('#paid').blur(function(){
	var paid= $('#paid').val();
    paid=parseFloat(paid).toFixed(2);
	$('#paid').val(paid);
 });
 
 $('#permit').keyup(function(){
	var permit=$('#permit').val();
	permit = parseFloat(permit).toFixed(2);
	
	//alert(permit);
	
 });
                   
                   </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;
}
 