<?php
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/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  
   <?php
$sql='';
$sql="SELECT invoices_wings_courier.invoice_no,invoices_wings_courier.date, invoices_wings_courier.customer_id, customers.name,invoices_wings_courier.nett, invoices_wings_courier.vat,invoices_wings_courier.gross,
invoices_wings_courier.status status_id,invoice_status.name as status,invoices_wings_courier.pay_date
FROM invoices_wings_courier
LEFT JOIN invoice_status ON invoices_wings_courier.status = invoice_status.id
LEFT JOIN customers ON invoices_wings_courier.customer_id = customers.id

";
$today= date('Y-m-d');
$from="";
$to="";
if(isset($_POST['filter'])){
	$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));
	  $status=$_POST['status'];
if (!empty($_POST['from']) && !empty($_POST['to']))
	{
	  
	  $sql.=" WHERE invoices_wings_courier.pay_date BETWEEN '$from' AND '$to' AND invoices_wings_courier.status=$status";
	}elseif(!empty($_POST['status'])){
		
		$sql.=" WHERE invoices_wings_courier.status=$status";}
}



$sql.=" ORDER BY invoices_wings_courier.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;


?>
<form method="post" action="">
	<div class="container-fluid" style="margin-top:200x;">
	 
			      <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-2">
                        <select name="status" class="form-control">
                        <option value="1">Paid</option>
                        <option value="2">Raised</option>
                        
                        
                        </select>
             		 </div>
       
                       <div class="col-md-2">
                       	  <input type="submit" id="select_jobs" class="btn btn-warning" name="filter" 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 class="col-md-2">
                       <input type="button" class="btn btn-primary btn-sm" onclick="printDiv('invoice')" value="Print" /> 
                       
                       </div>
                   </div>
                 			
</div>
				  
			    
</form>



  
  						<?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"> 
					  <div id="invoice">
					  <center>
					  <h2>VAT Report<h2>	
                     </center>
                  <table border="1">

        <thead class="blue-background">
			
            <tr>
                            <th>Invoice Number</th>
                           <th>Month</th>
                           <th>Company</th>
                          
                           <th >Nett</th>
                           <th >VAT</th>
                           <th >Gross</th>
						   <th >Status</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']));
					 
					 $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'];
					 $status_id= $job['status_id'];
					 $status= $job['status'];
					 $invoice_pay_date=date("d/m/Y",strtotime($job['pay_date']));
					 if ($status_id==1){
						$invoice_status= 
                           $status.' on '.$invoice_pay_date ;
						   }else{
							   $invoice_status= 
                           $invoice_status=$status;
						   }
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
					 <div >
                           <td ><p>'.$invoice_no.'</p></td>
						   <td><p>'.$date.'</p></td>
						   <td><p>'.$company.'</td>
						   <td style="text-align:right;"><p>£ '.$nett.'</p></td>
						   <td style="text-align:right;"><p>£ '.$vat.'</p></td>
						   <td style="text-align:right;"><p>£ '.$gross.'</p></td>
						    <td ><p>'.$invoice_status.'</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>£ '.$total_nett.'</p></td>
						   <td style="text-align:right;"><p><b>£ '.$total_vat.'</p></td>
						   <td style="text-align:right;"><p><b>£ '.$total_gross.'</p></td>
						   <td></td>
						   </b>
					   </tr>
					   
					</tbody>
				 </table>
				</div>
					 
               ';?>
			
				   <?php 
				   
				   echo 
				   '<div class="col-md-8" id="invoice_div" style="margin-left:50px;">'.$invoice_html.'</div>'
				  
				   ;?>
				 
           <script>
 	$('#to').datepicker({
	
    	onSelect: function() {
			
                $( "#to" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });
	$('#from').datepicker({
	
    	onSelect: function() {
			
                $( "#from" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });	
 </script>		
 <script type="text/javascript">
   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>