<?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 Company</h2></label>
			      <div class="well col-md-12" style="background-color:#e9e9e9; box-shadow: 5px 5px 5px;">
			       	 
                       <div class="form-group col-sm-5">
			       	   <select name="company" id="customer" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select from List </option>
                               <?php
							   
							        $customers_sql="SELECT * from customers 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']." ,Mobile:".$customer['mobile']." ,".$customer['address1']." ".$customer['post_code']; ?>
    </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-1">
                       	  <input type="submit" id="select_jobs" class="btn btn-warning" value="Find Invoices">
                       </div>
                       <!--
                       <div class="col-md-2">
                          <input type="button" name="btn" value="Print Invoices" id="filter_invoice" data-toggle="modal" data-target="#confirm-submit" class="btn btn-success btn-sm pull-right" />
                       </div>
			       	   -->
                    <!-- now retrieve the transactions of this customer -->
                   </div>
                 			
</div>
				  
			    
</form>
   <?php
$sql='';
$sql="SELECT invoices.invoice_no,invoices.date, invoices.customer_id, customers.name,invoices.nett, invoices.vat,invoices.gross, invoices.status status_id,
invoice_status.name as status,invoices.pay_date
FROM invoices
LEFT JOIN invoice_status ON invoices.status = invoice_status.id
LEFT JOIN customers ON invoices.customer_id = customers.id

";
$today= date('Y-m-d');
$from="";
$to="";
$company='';
if(isset($_POST['company'])){
	$company=$_POST['company'];
$sql.=" WHERE 
customers.id=$company 


";	
}	
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 invoices.invoice_no DESC
";

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

//echo $sql;


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

//echo $sql;

                
                $nett='';
				$vat='';
				$gross='';
				$company_name='';
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
								
								$company_name=$sum['name'];
				}
				;?>

</head>

<body> 	
                
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">	
                      <h2>All Invoices of <b>'.$company_name.'</b><h2>
					  <table>
					    <tr>
		                <td colspan="4"><h2 class="blue-background">Service Invoice From '.$company_name.'</h2></td>
		               </tr>
					  <tr>
						 <td class="bold-text"> 
						  <p class="blue-background"><b>Invoice From </b></p>
							 <p><b>Limo Chauffeurs Ltd.<b> </p>
							 <p>12 Lampton Park Road, </p>
							 <p>Hounslow,</p>
							 <p>TW3 4HS</p>
					      </td>
						   <td></td>
						   <td class="bold-text">  
						     <p><img src="images/logo.png" class="img-responsive"></p>
							 <p>E:info@limochaffaurs.com </p>
							 <p>Ph. +44(0)2082304594</p>
							 <p>VAT No: 1234569898</p>
							 <p>Website: www.limochauffeurs.com</p>
						   </td>
						</tr>            
				   </table>
				    <table border="1">
			    		<thead class="blue-background">
						 <tr>
                           <th>Invoice Number</th>
                           <th>Month</th>
                           <th>Status</th>
                          
                           <th>Nett</th>
                           <th>VAT</th>
                           <th>Gross</th>
                          
            </tr>

        </thead>

        <tbody style="font-size:18px;">
        ';?>
       
        <?php
				$total=0;
				$total_nett=0;
				$total_vat=0;
				$total_gross=0;
                while($job=mysqli_fetch_assoc($res))
				
                  {
					  //echo $job['payment_type'];
					  //exit;
					 $invoice_no=$job['invoice_no'];
					 $date=date("F", strtotime($job['date']));
					 $status=$job['status'];
					 $company=$job['name'];
					 $nett=$job['nett'];
					 $vat=$job['vat'];
					 $gross=$job['gross'];
					 $total_nett=$total_nett+$job['nett'];
					 $total_vat=$total_vat+$job['vat'];
					 $total_gross=$total_gross+$job['gross'];
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
                           <td><p>'.$invoice_no.'</p></td>
						   <td><p>'.$date.'</p></td>
						   <td><p>'.$status.'</p></td>
						   <td ><p>£ '.number_format($nett,2).'</p></td>
						   <td ><p>£ '.number_format($vat,2).'</p></td>
						   <td ><p>£ '.$gross.'</p></td>
						   
						   
					</tr>';
				 } ?>
                 <?php 
				
				 $invoice_html.='
				  <tr>
                     
						   <td  colspan="3"><p><b>Total Amounts: </b></td>
						   <td ><p><b>£ '.number_format($total_nett,2).'</p></td>
						   <td ><p><b>£ '.number_format($total_vat,2).'</p></td>
						   <td ><p><b>£ '.number_format($total_gross,2).'</p></td>
						   </b>
						   
					   </tr>
					   
					</tbody>
				 </table>
				
					 
               ';?>
			
				   <?php 
				   
				   echo 
				   '<div class="col-md-8" id="invoice_div" style="margin-left:150px;">'.$invoice_html.'</div>'
				  
				   ;?>
				   <script>
                   alert("Please right click in order to print this invoice.");
                   </script>