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

?>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  
   <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> First Select a Company and Select The Booking Date From & TO</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='';
if (isset($_POST['company_id'])) {
	$company_id=$_POST['company_id'];
	
	$vat_p=0;//$_POST['vat_p'];
$sql="SELECT 

bookings.pickup_date,bookings.status,bookings.id AS booking_id,bookings.comments, bookings.parking,bookings.transfer,bookings.customer_id,bookings.pickup_address,bookings.pickup_post_code, bookings.drop_address,bookings.drop_post_code, 

bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time, bookings.hours,bookings.mins,bookings.other,bookings.total_fare as total_fare,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.id AS company_id, customers.name AS company,customers.address1,customers.post_code,customers.city, customers.mobile, 

employees.first_name AS driver, company_contact.name as passenger,
vehicles.reg as reg, vehicle_make.make as make
FROM 
bookings

LEFT JOIN customers ON bookings.customer_id = customers.id 
LEFT JOIN employees ON bookings.driver_id = employees.id 
LEFT JOIN vehicles ON vehicles.driver_id=bookings.driver_id
LEFT JOIN vehicle_make ON vehicle_make.id=bookings.vehicle_id
LEFT JOIN company_contact ON bookings.passenger_id = company_contact.id ";

if (!empty($_POST['company_id']))
	{
	  $company_id=$con->real_escape_string($_POST['company_id']);
	//$sql.=" WHERE customers.id=".$company_id."  AND bookings.job_status=2 AND bookings.invoice_status=1";
	$sql.=" WHERE customers.id=".$company_id." AND bookings.job_status=2 AND bookings.invoice_status=1 AND bookings.job_type!='Cash'";
	}
	else{
		$sql.=" WHERE customers.id LIKE '%' AND bookings.job_status=2 AND bookings.invoice_status=1 AND bookings.job_type!='Cash'";
		}
	
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.start_date BETWEEN '$from' AND '$to'"; 
	}
	
$sql.=" 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);

?>
<?php
if(!mysqli_query($con,$sql)){echo 'error'.mysqli_error($con);}
$invoice_sum=mysqli_query($con,$sql);
               
								$nett='';
								$commission='';
								$vat_p='';
								$vat='';
								$gross='';
								$first_name='';
								$pco_number='';
								$pco_expiry='';
								$reg='';
								$make='';
								$company='';
								$address1='';
								$post_code='';
								
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
								$company=$sum['company'];
								$address1=$sum['address1'];
								$post_code=$sum['post_code'];
								
								
								
				}
				;?>

<?php 
	$sql="SELECT MAX(invoice_no) as invoice_no from 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;}
	
	
	$sql1="SELECT MAX(invoice_no) as invoice_no from invoices_wings_courier";
	$i_res1=$con->query($sql1);
	$invoice_res1=mysqli_fetch_assoc($i_res1);
	$invoice_no1=$invoice_res1['invoice_no'];
	
	$invoice_no1=$invoice_no1+1;
	if($invoice_no1==''){$invoice_no1=1;}
?>
</head>

<body> 	
                
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">	
					  <div id="invoice">
                      <h2>Sales Invoice <b></b><h2>
					  <form action="" method="post" id="customer_invoice_form">
					  <input type="hidden" name="invoice_no" value="'.$invoice_no.'"/>
					  <input type="hidden" name="invoice_no1" value="'.$invoice_no1.'"/>
				<table cellpadding="10" style="border:none;font-size:12px;" border="0" bordercolor="#F4EEEE">
					  
					  <tr>
						 <td> 
						  
							 <p>Wings Chauffeurs Ltd.</p>
							 <p>Office 2.25, 450 Bath Rd, </p>
							 <p>West Drayton,Middlesex,London,</p>
							 <p>UB7 0EB</p>
							  <p>E:info@wingschauffeurs.com </p>
							 <p>Ph. +44(0)208077925</p>
							 <p>M. +44(0)07792560326</p>
							
							 <p>Website: www.wingschauffeurs.com</p>
					      </td>
						  <td bordercolor="#000000" style="text-align:left;word-break:break-all;"> 
						  <p><b>Invoice To </b></p>
							<p>'.wordwrap($company,15).'</p>
							
							 <p>'.wordwrap( $address1, 10, "\r\n", true ) .'</p>
							 <p>'.$post_code.'</p>
					      </td>
						 
						 <td  style="text-align:left;"> 
						  <p><b>Details </b></p>
						   
							 <p>Invoice No. '.$invoice_no.'</p>
							 
							 <p>VAT No. 256574868</p>
							 <p><b>Invoice Date</b></p>
							 <p><input required type="text" name="date" style="width:100px;" placeholder="dd/mm/yy" id="date" class="form-control col-md-3"/></p>
					      </td>
						 
						  									   
						   <td class="bold-text">  
						     
							
						   </td>
						</tr>            
				   </table>
		
                  <table border="1" style="font-size:12px;" class="table table-striped">

        <thead>

                      <tr>
				           <th  width="2%">STATUS</th>
                           <th width="25%">DRIVER</th>
						   <th width="7%">PLATE</th>
                           
                           <th width="35%">CLIENT</th>
                           <th width="15%">VEHICLE</th>
                           
                           <th width="7%">START</th>
                           
                           <th width="7%">FINISH</th>
						   <th  width="4%">RATE</th>
						   
						   <th  width="5%">DAYS</th>
						   <th  width="3%">WAITING</th>
						   <th  width="3%">PARKING</th>
						   <th  width="3%">TRANSFER</th>
                           <th  width="15%">TOTAL</th>
                        </tr>
					
                     </thead>


        <tbody>
        ';?>
       
        <?php
				$total=0;
				$total_nett=0;
				$total_commission=0;
				$total_gross=0;
				$vat_p=0;
				$vat=0;
				$other=0;
				$total_vat=0;
				$total_gross=0;
				$count=0;
                while($driver_data=mysqli_fetch_assoc($res))
				
                  {
					  	  
					 $booking_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'];
					 $reg=$driver_data['reg'];
					 $status=$driver_data['status'];
					 $make=$driver_data['make'];
					 $waiting=$driver_data['waiting_charges'];
					 $driver=$driver_data['driver'];
					 $start_date=$driver_data['start_date'];
					  $end_date=$driver_data['end_date'];
					 $parking=$driver_data['parking'];
					 $transfer=$driver_data['transfer'];
					 $rate=$driver_data['rate'];
					 $days=$driver_data['days'];
					 $total_fare=$driver_data['total_fare']; 
					 $total_gross=$total_gross+$driver_data['total_fare'];
					 $company_id = $driver_data['company_id'];
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
                           <input type="hidden" name="company_id" value="'.$company_id.'"/>
						   <input type="hidden" name="booking_id[]" value="'.$booking_id.'">
						   <td><p>'.$status.'</p></td>
						   <td>'.ucfirst($driver).'</td>
						   <td>'.$reg.'</td>
						   <td><p>'.ucfirst($passenger).'</p></td>
						   <td><p>'.$make.'</p></td>
						   <td><p>'.date("d-M",strtotime($start_date)).'</p></td>
						   <td><p>'.date("d-M",strtotime($end_date)).'</p></td>
						   
						   <td align="right"><p>£'.number_format($rate,2).'</p></td> 
						   <td align="right"><p>'.number_format($days,0).'</p></td>
						   <td align="right"><p>'.number_format($waiting,0).'</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++;
				 } ?>
                 <?php 
				 
				 $invoice_html.='
				
				<tr>
						   <td  align="right"  colspan="12"><p><b>Sub Total: </b></td>
						   <td >
						   <input  style="width:90px; align="right" type="text" readonly value="'.$total_gross.'" name="nett" id="nett" class="form-control">
						   
				</tr>
				<tr>
						   <td align="right"  colspan="12"><p><b>VAT %: </b></td>
						   <td ><input style="width:90px;" value="0.00" required type="text" class="form-control" required name="vat_p" id="vat_p" placeholder="20" ></td>
				</tr>
				<tr>
						   <td align="right"  colspan="12"><p><b>VAT Amount: </b></td>
						   <td ><input  style="width:90px;" readonly value="0.00" type="number" class="form-control" name="vat" id="vat" placeholder="VAT"></td>
				</tr>
				
				  <tr>
						   <td  align="right"  colspan="12"><p><b>Grand Total: </b></td>
						   <td ><input  style="width:90px;" readonly value="'.$total_gross.'" type="number" class="form-control" name="gross" id="gross" placeholder="Gross"></td>
						   </b>
						   
					   </tr> 
					    <tr>
						   <td  align="right"  colspan="12"><p><b></b></td>
						   <td ><input  style="width:90px;" type="submit" name="make_invoice" class="btn btn-success" value="Save"></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-12" id="invoice_div" style="margin-left:2px;">'.$invoice_html.'</div>';}
				   ?>
			
				  
				   <script>
	$(document).ready(function() { 
        $('#company_id').select2({ width: 'resolve' });
		
	});

	
 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);
	
	
	
	$('#paid').val();
	
	var gross=$('#gross').val();
	gross = parseFloat(gross).toFixed(2);
	gross=+nett+ +vat;
	
	
	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);
 });
 

 
 $('#paid').blur(function(){
	var paid= $('#paid').val();
    paid=parseFloat(paid).toFixed(2);
	$('#paid').val(paid);
 });
 

                   </script>
            <script>
			// Make Driver Invoice
   $("#customer_invoice_form").on('submit',(function(e) {
	
		e.preventDefault();
		$('#loader').show();
		var url11 = 'ajax/create_customer_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_invoice.php?'+new Date().getTime();
		window.location.replace(url1);
			}else{
				alert("iNVOICE 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_jobs.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">
 $('#company_id').select2({width: '100%'});
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>