<?php
error_reporting(E_ALL);
ob_start();
include "head.php";
include "dbconfig.php";

?>  
 <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
 <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

  
   <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
   <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<form method="post" action="" name="filter_data">
	<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="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-3" id="jobs_data" style="display:none;">
			            
                       </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='';

	$first_name='';
								$pco_number='';
								$pco_expiry='';
								$reg='';
								$make='';
								$driver_address='';
								$driver_post_code='';
								$driver_id1="0";
if (isset($_POST['driver_id'])) {
	
	$driver_id=$_POST['driver_id'];
	$commission=0;//$_POST['commission'];
	$percentage=0;//$_POST['percentage'];
	
$sql="SELECT DISTINCT bookings.id as job_id,bookings.substitute,bookings.pickup_date,driver_bookings.end_date,driver_bookings.start_date,driver_bookings.days,bookings.rate,bookings.supp_cost,bookings.hours,bookings.mins,bookings.pickup_address,company_contact.name as passenger,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,
customers.name as company,
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,
vehicle_make.make,vehicles.reg,employees.id as driver_id,employees.pco_number,employees.pco_expiry,employees.first_name as first_name,employees.address as driver_address,employees.post_code as driver_post_code
FROM bookings
LEFT JOIN customers ON bookings.customer_id=customers.id
LEFT JOIN company_contact on bookings.passenger_id=company_contact.id
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['driver_id']))
	{
	  $driver_id=$con->real_escape_string($_POST['driver_id']);
	$sql.=" WHERE driver_bookings.driver_id=".$driver_id ;
	}else{$sql.=" WHERE driver_bookings.driver_id LIKE '%'";}
	
if (!empty($_POST['from']) && !empty($_POST['to']))
	{
	  $from=$con->real_escape_string($_POST['from']);
	 // echo $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.start_date BETWEEN '$from' AND '$to'"; 
	}
	$sql.=" AND driver_bookings.invoice_status!='2' AND bookings.job_type!='Cash' ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";	
//echo $sql;	

if(!mysqli_query($con,$sql)){echo 'error'.mysqli_error($con);}
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);

//if($rowcount==0)
{
if (!empty($_POST['from']) && !empty($_POST['to']))
	{
	  $from=$con->real_escape_string($_POST['from']);
	 // echo $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));

 $customers_sql1="select * from driver_substitute WHERE substitute_id='$driver_id' AND start_date BETWEEN '$from' AND '$to' 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);
									




}

	


}

?>
<?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='';
								$subs='';
								$c=0;
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
							$c++;	
								
								$first_name=$sub2['first_name'];
								
								$pco_number=$sum['pco_number'];
								$pco_expiry=$sum['pco_expiry'];
								$reg=$sum['reg'];
								$make=$sum['make'];
								$driver_address=$sub2['address'];
								$driver_post_code=$sub2['post_code'];
								
							
				}
						
				?>

<?php 
	$sql="SELECT MAX(invoice_no) as invoice_no from driver_invoices";
	$i_res=$con->query($sql);
	$invoice_res=mysqli_fetch_assoc($i_res);
	$invoice_no=$invoice_res['invoice_no'];
	$invoice_no=$invoice_no+1;
	if($invoice_no==''){$invoice_no=1;}
?>
</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="0" cellspacing="0">
            <tr bgcolor="#ffffff">
                <td colspan="2">
			<form action="" method="post" id="driver_invoice_form">
			<input type="hidden" name="invoice_no" value="'.$invoice_no.'"/>
                    <table>
                        <tr>
                            <td >
                               <h1>Sub Contractor Invoice</h1>
                            </td>
                            
                            <td>
                                Invoice #:'.$invoice_no.'<br>
                                Created: <input required type="text" name="date" id="date" style="width:150px;" class="form-control"/><br>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            
            <tr class="information">
                <td colspan="2">
                    <table>
                        <tr>
                            <td>
                                
                                '.$first_name.'<br>
                                '.$driver_address.',<br>
										'.$driver_post_code.'
								
                            </td>
                            
                            
                        </tr>
                    </table>
                </td>
            </tr>
           
          
        </table>
		
                  <table border="1" style="font-size:12px;" class="table table-striped">

        <thead>

                      <tr>
				           <!--th  width="2%">Ref</th>
                           <th width="3%">Date</th>
						   <th width="10%">Passenger</th>
                           <th width="35%">Pickup From</th>
                           
                           <th width="35%">Drop To</th>
                           
                           <th width="7%">Price</th>
						   <th  width="7%">Car Park</th>
						   
						   <th  width="5%">Waiting</th>
						   <th  width="3%">Other</th>
                           <th  width="15%">Total</th-->
						   
						    <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_gross1=0;
				$count=0;
				$tdays=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']));
					  $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>';
					 
					 $drop_address=$driver_data['drop_address']."<br><b>".$driver_data['drop_post_code'].'</b>';
					 $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'];  
					 $total_gross=$total_gross+$driver_data['total_fare'];
					 $driver_id = $driver_data['driver_id'];
					 
					
					
                      
                    
					/* 
					 $invoice_html.=
					 '
					<tr>
                           <input type="hidden" name="driver_id" value="'.$driver_id.'"/>
						   <input type="hidden" name="booking_id[]" value="'.$job_id.'">
						   <td><p>'.$job_id.'</p></td>
						   <td>'.$pickup_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>';
					*/
					 $invoice_html.=
					 '
					<tr>
                           <input type="hidden" name="driver_id" value="'.$driver_id.'"/>
						   
						   <input type="hidden" name="booking_id[]" value="'.$job_id.'">
						   <td><p>'.$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;"><p>£'.number_format($total_fare,2).'</p></td>
					</tr>';
				 $count++;
				 }
				// if($count==0)
				/* {
				 
				 //if($driver_data['substitute']!="")
					 {
					 $ds=$driver_data['driver_id'];
					 // $customers_sql1="select * from driver_substitute ORDER BY start_date ASC"; 
									//$t_result1=mysqli_query($con,$customers_sql1);
							
					 if(!$t_result1=mysqli_query($con,$customers_sql1)){echo 'error'.mysqli_error($con);}
									else
									{
									//$t_result1=mysqli_query($con,$customers_sql1);
								//	$sub=mysqli_fetch_assoc($t_result1);
									  while($sub=mysqli_fetch_assoc($t_result1))
									  {
									  
									//  echo $sub['id'];
								$job_id=$sub['booking_id'];
								$customers_sql3="select * from bookings WHERE id='$job_id'";
									$t_result3=mysqli_query($con,$customers_sql3);
									$sub3=mysqli_fetch_assoc($t_result3);	  
									$pid=$sub3['passenger_id'];


                                $customers_sql4="select * from company_contact WHERE id='$pid'";
									$t_result4=mysqli_query($con,$customers_sql4);
									$sub4=mysqli_fetch_assoc($t_result4);	  
									$passenger=$sub4['name'];
														 //$pickup_date = date("d/m/Y",strtotime($sub['start_date']));
					  $start_date = date("d/m/Y",strtotime($sub['start_date']));
					   $end_date = date("d/m/Y",strtotime($sub['end_date']));
					    $days=$sub['days'];
						 $parking=$sub['parking'];
						 $transfer=$sub['transfer'];
						  $rate=$sub['supp_cost'];
						$total_fare=$sub['supp_cost']*$days+$sub['parking']+$sub['transfer'];  
						 $total_gross=$total_gross+$total_fare;
					//echo	 $total_gross=$total_gross1;
						 
						 
						  $invoice_html.=
					 '
					<tr>
                           <input type="hidden" name="driver_id" value="'.$driver_id.'"/>
						   <input type="hidden" name="booking_id[]" value="'.$job_id.'">
						   <td><p>'.$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;"><p>£'.number_format($total_fare,2).'</p></td>
					</tr>';
									}
									}
							}
						
				 
				 }*/
				 
				  ?>
                 <?php 
				/* 
				 $invoice_html.='
				 
				  
				<tr>
						   <td  align="right"  colspan="9"><p><b>Total Amounts: </b></td>
						   <td >
						   <input type="text" readonly value="'.$total_gross.'" name="nett" id="nett" class="form-control">
						   
				</tr>
				<tr>
						   <td align="right"  colspan="9"><input value="" type="text" class="form-control" name="percentage_text" placeholder="Percentage"></td>
						   <td ><input value="" required type="text" class="form-control" name="percentage" id="percentage"  placeholder="%age" ></td>
				</tr>
				<tr>
						   <td align="right"  colspan="9">% Amount</td>
						   <td ><input readonly value="" type="text" class="form-control" name="percentage_amount" id="percentage_amount"  placeholder=""></td>
				</tr>
				<tr>
						   <td align="right"  colspan="9"><input value="" type="text" class="form-control" name="commission_text" placeholder="Enter Text Here"></td>
						   <td ><input value="" required type="text" class="form-control" name="commission" id="commission" placeholder="Comm.?"></td>
						   </b>
						   
					   </tr>
					   <tr>
						   <td align="right"  colspan="9"><input type="text" class="form-control" name="vat_text" id="vat_text" placeholder="Enter VAT Here"></td>
						   <td ><input  value=""  required type="text" class="form-control" name="vat_p" id="vat_p" placeholder="VAT"></td>
				</tr>
				<tr><td align="right"  colspan="9"><input value="" readonly type="text" class="form-control"></td><td><input readonly value="" type="number" class="form-control" name="vat_amount" id="vat_amount" placeholder="VAT Amount"></td></tr>
				  <tr>
						   <td  align="right"  colspan="9"><p><b>To Pay: </b></td>
	 <td ><input readonly type="text" class="form-control" name="gross" id="gross" value="'.$total_gross.'"></td>
						   </b>
						   
					   </tr> 
					    <tr>
						   <td  align="right"  colspan="9"><p><b></b></td>
						   <td ><input type="submit" name="make_invoice" 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>
					 
               ';  */
			   
			    $invoice_html.='
				 
				  
				<tr>
						   <td  align="right"  colspan="8"><p><b>Total Amounts: </b></td>
						   <td >
						   <input type="text" readonly value="'.$total_gross.'" name="nett" id="nett" class="form-control">
						   
				</tr>
				<tr>
						   <td align="right"  colspan="8"><input value="" type="text" on class="form-control" name="percentage_text" id="percentage_text"  placeholder="Percentage"></td>
						   <td ><input value="" required type="text" class="form-control"  name="percentage" id="percentage"  placeholder="%age" ></td>
				</tr>
				<tr>
						   <td align="right"  colspan="8">% Amount</td>
						   <td ><input readonly value="" type="text" class="form-control" name="percentage_amount" id="percentage_amount"  placeholder=""></td>
				</tr>
				<tr>
						   <td align="right"  colspan="8"><input value="" type="text" class="form-control comm" name="commission_text" id="commission_text" placeholder="Enter Text Here"></td>
						   <td ><input value="" required type="text" class="form-control comm" name="commission" id="commission" placeholder="Comm.?"></td>
						   </b>
						   
					   </tr>
					   <tr>
						   <td align="right"  colspan="8"><input type="text" class="form-control" name="vat_text" id="vat_text" placeholder="Enter VAT Here"></td>
						   <td ><input  value=""  required type="text" class="form-control" name="vat_p" id="vat_p" placeholder="VAT"></td>
				</tr>
				<tr><td align="right"  colspan="8"><input value="" readonly type="text" name="vat_amount_text" id="vat_amount_text" class="form-control"></td><td><input readonly value="" type="number" class="form-control" name="vat_amount" id="vat_amount" placeholder="VAT Amount"></td></tr>
				  <tr>
						   <td  align="right"  colspan="8"><p><b>To Pay: </b></td>
	 <td ><input readonly type="text" class="form-control" name="gross" id="gross" value="'.$total_gross.'"></td>
						   </b>
						   
					   </tr> 
					    <tr>
						   <td  align="right"  colspan="8"><p><b></b></td>
						   <td ><input type="submit" name="make_invoice" 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>
					 
               ';
			   
			   echo 
				   '<div class="col-md-11" id="invoice_div" style="margin-left:40px;">'.$invoice_html.'</div>';}
				   ?>
			
				  
				   <script>
$(document).ready(function() { 
        $('#driver_id').select2({ width: 'resolve' });
	});				
 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);
	
	//if(percentage=="" || percentage=="NaN")
	//{
	//$('#percentage_amount').val(0);
	//$('#percentage').val(0);
	//}
 });
 $('#percentage').blur(function(){
	
	var percentage= $('#percentage').val();
    percentage=parseFloat(percentage).toFixed(2);
	$('#percentage').val(percentage);
	if(percentage=="" || percentage=="NaN")
	{
	$('#percentage_amount').val(0);
	$('#percentage').val(0);
	
	}
 });
 
 
 
 
 
 
 $('#commission').keyup(function(){
	var commission= $('#commission').val();
	if(commission=='' || commission=="NaN"){commission=0;}
	commission=parseFloat(commission).toFixed(2);
	
	var percentage_amount= $('#percentage_amount').val();
	if(percentage_amount=='' || percentage_amount=='NaN'){percentage_amount=0;}
	
    percentage_amount=parseFloat(percentage_amount).toFixed(2);
	
	var sub_total=$('#sub_total').val();
	var nett=sub_total ;
	nett=parseFloat(nett).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);
	
	
});	

 $('#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();
	if(percentage_amount==''){percentage_amount=0;}
    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);
	if(commission=="" || commission=="NaN")
	{
	$('#commission').val(0);
	//$('#gross').val(0);
	}
	
	
 });
 
  $('#vat_p').keyup(function(){
  var vat_p=$('#vat_p').val();
if(vat_p=="" || vat_p=="NaN" || vat_p==0)
	{
	vat_p=0;
	
	var sub_total=$('#sub_total').val();
	var percentage_amount= $('#percentage_amount').val();
	if(percentage_amount=='' || percentage_amount=='NaN'){percentage_amount=0;}
    percentage_amount=parseFloat(percentage_amount).toFixed(2);
	var commission= $('#commission').val();
    commission=parseFloat(commission).toFixed(2);
	if(commission==''){commission=0;}
	commission=parseFloat(commission).toFixed(2);
	gross=sub_total-percentage_amount;
	gross=gross-commission;
	
	$('#gross').val(gross);	
	}
	else
	{
	 vat_p=parseFloat(vat_p).toFixed(2);
	var commission=$('#commission').val();
	//alert(commission);
	if(commission=='' || commission=="NaN"){commission=0;}
	commission = parseFloat(commission).toFixed(2);
	
	//vat_amount=(commission*vat_p)/100;
	//vat_amount = parseFloat(vat_amount).toFixed(2);
	
	
	var gross=$('#gross').val();
	gross = parseFloat(gross).toFixed(2);
	gross=gross-commission;
	vat_amount=(gross*vat_p)/100;
	vat_amount = parseFloat(vat_amount).toFixed(2);
	
	gross=gross-vat_amount;
	
	//alert(gross);
	gross=parseFloat(gross).toFixed(2);
	$('#vat_amount').val(vat_amount);
	
	$('#gross').val(gross);	
	}
 });
 
 
 $('#vat_p').blur(function(){
	var vat_p=$('#vat_p').val();
	
	$('#vat_p').val(vat_p);
	if(vat_p=="" || vat_p=="NaN" || vat_p=='0')
	{
	$('#vat_p').val(0);
	$('#vat_amount').val(0);
	}
	
 });
 
 
 

 
 $('#paid').blur(function(){
	var paid= $('#paid').val();
    paid=parseFloat(paid).toFixed(2);
	$('#paid').val(paid);
 });
 
 /*
 $('#vat').blur(function(){
	
	var commission= $('#commission').val();
    commission=parseFloat(commission).toFixed(2);
	if(commission==''){commission=0}
	commission=parseFloat(commission).toFixed(2);
	alert(commission);
	// calcculate vat on commission
	var vat=commission*.20;
	vat = parseFloat(vat).toFixed(2);
	$('#vat').val(vat);
	alert(vat);
	var gross=$('#gross').val();
	gross = parseFloat(gross).toFixed(2);
	
	
	gross= parseFloat(gross-vat).toFixed(2);
	alert(gross);
	$('#gross').val(gross);
	
 });              
        
		*/           </script>
            <script>
			// Make Driver Invoice
   $("#driver_invoice_form").on('submit',(function(e) {
	
		e.preventDefault();
		$('#loader').show();
		var url11 = 'ajax/create_driver_invoice.php?'+new Date().getTime();
		$.ajax({
			
        	url: url11,
			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");
		var url1 = 'list_driver_invoice.php?'+new Date().getTime();
		window.location.replace(url1);
			}else{
				alert("Booking Was Not Added");
				$('#loader').delay(500).fadeOut("slow");
				}
					
				
			//$("#gallery").html(data);
			
				
				
		    },
		  	error: function() 
	    	{
	    	} 	        
	   });
	}));
  </script>        
 <script>
 	// this is code to fire Ajax request to fetch only selected companies in dropdown
$('.from').datepicker({
	
    	onSelect: function() {
			
                $( ".from" ).datepicker( "option", "dateFormat","dd/mm/yy");
		var from=$('#from').val();
		
        }
        });
$('.to').datepicker({
	
    	onSelect: function() {
		$( ".from" ).datepicker( "option", "dateFormat","dd/mm/yy");
		var from=$('#from').val();	
	    $( ".to" ).datepicker( "option", "dateFormat","dd/mm/yy");
		var to=$(this).val();
		
        if(from>to){//alert("Date From is Greater than Date To");return false;
		
		}        
		var date='from='+from+'&to='+to;
		var url11 = 'ajax/find_invoice_drivers.php?'+new Date().getTime();
		$.ajax
        ({
              type: "Post",
              url: url11,
              data: date,
              success: function(data)
              {
                 $('#jobs_data').html(data);
				 $('#jobs_data').show();
              }
		});
			
    }
  
  });
	$('#date').datepicker({
	
    	onSelect: function() {
			
                $( "#date" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
		
        });		
 </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>