<?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.=" WHERE invoices.date BETWEEN '$from' AND '$to'";
	}
	



$sql.=" ORDER BY invoices.invoice_no DESC
";
if (!mysqli_query($con,$sql))
  {
	  echo $sql;
  echo("Error description: " . mysqli_error($con));
  }
 
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);

//echo $sql;


?>


</head>

<body> 
  


  
  						<?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))
				{
							
								$company_name=$sum['name'];
				}
				;?>
                
                       <?php $invoice_html=
					 ' 
					 
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">
					  <h2>Sales Report From<h2>	
                      
                  <table border="1">

        <thead class="blue-background">
			
            <tr>
                            <th>Invoice Number</th>
                           <th>Month</th>
                           <th>Company</th>
                          
                           <th style="text-align:right;">Nett</th>
                           <th style="text-align:right;">VAT</th>
                           <th style="text-align:right;">Gross</th>
                          
            </tr>

        </thead calss="blue-background">

        <tbody>
        ';?>
       
        <?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']));
					 $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>
					 <div >
                           <td ><p>'.$invoice_no.'</p></td>
						   <td><p>'.$date.'</p></td>
						   <td><p>Time '.$company.'</td>
						   <td style="text-align:right;"><p>£ '.number_format($nett,2).'</p></td>
						   <td style="text-align:right;"><p>£ '.number_format($vat,2).'</p></td>
						   <td style="text-align:right;"><p>£ '.$gross.'</p></td>
					</div>	   
						   
					</tr>';
				 } ?>
                 <?php 
				 
				 $invoice_html.='
				  <tr class="money">
                     	   	 
						   <td style="text-align:right;" colspan="3"><p><b>Total Amounts: </b></td>
						   <td style="text-align:right;"><p><b>£ '.number_format($total_nett,2).'</p></td>
						   <td style="text-align:right;"><p><b>£ '.number_format($total_vat,2).'</p></td>
						   <td style="text-align:right;"><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:50px;">'.$invoice_html.'</div>'
				  
				   ;?>
				   <script>
                   alert("Please right click in order to print this invoice.");
                   </script>