<?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="form-group col-sm-3">
			       	   <select name="company_id" id="company_id" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select from List </option>
                               <?php
							   
							        $customers_sql="SELECT customers.name,customers.id from customers,bookings
where bookings.customer_id=customers.id AND bookings.job_status=2 group by customers.id order by 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['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='';
								$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.id AS booking_id,bookings.comments, bookings.parking,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,

customers.id AS company_id, customers.name AS company,customers.address1,customers.post_code,customers.city, customers.mobile, 

employees.first_name AS driver, 

vehicles.make as vehicle, company_contact.name as company_contact,
company_contact.name as passenger

FROM 
bookings

LEFT JOIN customers ON bookings.customer_id = customers.id 
LEFT JOIN employees ON bookings.driver_id = employees.id 
LEFT JOIN vehicles ON bookings.vehicle_id = vehicles.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";
	}
	else{
		$sql.=" WHERE customers.id LIKE '%' AND bookings.job_status=2 bookings.invoice_status=1";
		}
	
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'"; 
	}
	
$sql.=" ORDER BY bookings.pickup_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;}
?>
</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.'"/>
				<table cellpadding="10" style="border:none;font-size:12px;" border="0" bordercolor="#F4EEEE">
					  
					  <tr>
						 <td> 
						  
							 <p>Limo Chauffeurs Ltd.</p>
							 <p>12 Lampton Park Road, </p>
							 <p>Hounslow,</p>
							 <p>TW3 4HS</p>
							  <p>E:info@limochauffeurs.com </p>
							 <p>Ph. +44(0)2082304594</p>
							 <p>M. +44(0)7808939220</p>
							
							 <p>Website: www.limochauffeurs.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. 1234569898</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%">Ref</th>
                           <th width="3%">Date</th>
						   <th width="3%">Time</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>
                        </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'];
					 $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'];
					 $fare=$driver_data['fare'];
					 $other=$driver_data['other'];
					 $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>'.$booking_id.'</p></td>
						   <td>'.$pickup_date.'</td>
						   <td>'.$time.'</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>';
				 $count++;
				 } ?>
                 <?php 
				 
				 $invoice_html.='
				
				<tr>
						   <td  align="right"  colspan="10"><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="10"><p><b>VAT %: </b></td>
						   <td ><input style="width:90px;" value="" required type="text" class="form-control" required name="vat_p" id="vat_p" placeholder="20" ></td>
				</tr>
				<tr>
						   <td align="right"  colspan="10"><p><b>VAT Amount: </b></td>
						   <td ><input  style="width:90px;" readonly value="" type="number" class="form-control" name="vat" id="vat" placeholder="VAT"></td>
				</tr>
				
				  <tr>
						   <td  align="right"  colspan="10"><p><b>Grand Total: </b></td>
						   <td ><input  style="width:90px;" readonly value="" type="number" class="form-control" name="gross" id="gross" placeholder="Gross"></td>
						   </b>
						   
					   </tr> 
					    <tr>
						   <td  align="right"  colspan="10"><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-11" id="invoice_div" style="margin-left:40px;">'.$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 = 'new_customer_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>
 	$('#to').datepicker({
	
    	onSelect: function() {
			
                $( "#to" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });
	$('#from').datepicker({
	
    	onSelect: function() {
			
                $( "#from" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
		
        });
		$('#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>