<?php
ob_start();
include "head.php";
include "../dbconfig.php";
?>
   <?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="";
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
";

// $customer_id=$_GET['company_id'];
//WHERE customers.id=$customer_id GROUP BY bookings.id
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);

//echo $sql;


?>

<style>table{ table-layout:fixed; } table td{ word-wrap:break-word; width:30px; }

thead th {
  padding: 12px;
  border: 1px solid black;
  font-size:11px;
  font-weight:300;
}
tbody td {
  padding: 2px;
  
  border: 1px solid black;
}
table {
  border: solid black;
  border-width:  2px;
  width: 100%;
  
  font-style:normal;
  font-weight:300;
  
  table-layout: fixed;
  border-spacing: 4px;
  margin-top: 1px; /* same value as border-spacing */
}
tbody {
  border: solid black;
  border-width:  2px;
  width: 100%;
  font-size:10px;
  table-layout: fixed;
  border-spacing: 4px;
  margin-top: 1px; /* same value as border-spacing */
}

</style>
</head>

<body> 
  
<h2>Sales Report<h2>

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

//echo $sql;

                
                $nett='';
				$vat='';
				$gross='';
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
								$nett=number_format($nett+$sum['nett'],2);
								$vat=number_format($vat+$sum['vat'],2);
								$gross=number_format($gross+$sum['gross'],2);
								$company_name=$sum['name'];
				}
				;?>
                
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">	
                      <table border="1" width="100%">
						 <thead border="1">
								  
								  <tr>
								     <th width="25%">Invoice From</th>
										<th width="25%">Invoice To</th>
										
								  </tr>
							  </thead>
								  <tbody border="1">
									  <td> <p> Limo Chauffeurs Ltd. </p>
										   <p>12 Lampton Park Road, </p>
										   <p>Hounslow,</p>
										   <p>TW3 4HS</p>
										    <p>Ph. +44(0)2082304594</p>
										   
									   </td>
									   
									   <td>
									       <p>Comapny. '.$company_name.'</p>
										   <p>Total Invoices: '.$total_invoices.'</p>
										 </td>           
									  </tr>
								  </tbody>
								 </table>             
                          
                  <table border="1">

        <thead style="background-color:#32A8E9; color:#fff; padding:15px;">
			
            <tr>
                            <th>Invoice Number</th>
                           <th>Month</th>
                           <th>Company</th>
                          
                           <th>Nett</th>
                           <th>VAT</th>
                           <th>Gross</th>
                          
            </tr>

        </thead>

        <tbody>
        ';?>
       
        <?php
				$total=0;
                while($job=mysqli_fetch_assoc($res))
				
                  {
					  //echo $job['payment_type'];
					  //exit;
					 $invoice_no=$job['invoice_no'];
					 $date=date("F", strtotime($job['date']));
					 $company=$job['name'];
					 $nett=$job['nett'];
					 $vat=$job['vat'];
					 
					 $total=$total+$job['gross'];
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
                           <td><p>'.$invoice_no.'</p></td>
						   <td><p>'.$date.'</p></td>
						   <td><p>Time '.$company.'</td>
						   <td style="text-align:right;font-size:12px;"><p>£ '.number_format($nett,2).'</p></td>
						   <td style="text-align:right;font-size:12px;"><p>£ '.number_format($vat,2).'</p></td>
						   <td style="text-align:right;font-size:12px;"><p>£ '.number_format($gross,2).'</p></td>
						   
						   
					</tr>';
				 } ?>
                 <?php 
				 $vat=$total*.2;
				   $gross=$total+$vat;
				 $invoice_html.='
				  <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="5"><p><b>Nett: </b></td>
						   <td style="text-align:right;font-size:12px"><p>£ '.number_format($total,2).'</p></td>
						   
					   </tr>
					   <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="5"><p><b>VAT: </b></td>
						   <td style="text-align:right;font-size:12px"><p>£ '.number_format($vat,2).'</p></td>
						   
					   </tr>
					   <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="5"><p><b>Gross: </b></td>
						   <td style="text-align:right;font-size:12px"><p>£ '.number_format($gross,2).'</p></td>
						   
					   </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>