<?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/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="">
	<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="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="form-group col-md-4">
			       	   <select name="company" id="company" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select from List </option>
                               <?php
							   
							        $customers_sql="SELECT id,name from customers,invoices where invoices.customer_id=customers.id 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="submit" id="select_jobs" class="btn btn-warning" value="Find Invoices">
                       </div>
                       <div class="col-md-2">
                       <input type="button" class="btn btn-primary btn-sm" onClick="printDiv('invoice')" value="Print" /> 
                       </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 as company,invoices.nett, invoices.vat,invoices.gross, invoices.status status_id,
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='';
$header='';
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 
    invoices.date BETWEEN '$from' AND '$to'"; 
	$header='From '.date("d/m/Y",strtotime($from)).' To '.date("d/m/Y",strtotime($to));
	}
	
	if (!empty($_POST['company']))
	{
	$company=$con->real_escape_string($_POST['company']);
	$sql.=" WHERE customers.id=$company" ;
	}else{$sql.=" WHERE customers.id LIKE '%'";}
	
	



$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);
if(!$res){echo 'Error is '. mysqli_error($con);}
//echo $sql;

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

</head>

<body> 	
                
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">	
                     <div id="invoice">
					 <h2>List of Invoices for '.$company_name.'   <br>Total Invoices: '.$total_invoices.'</h2>
				    <table border="1">
			    		<thead class="blue-background">
						 <tr>
                           <th>Invoice Number</th>
						   <th>Company</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("M", strtotime($job['date']));
					 $status_id= $job['status_id'];
					 $status= $job['status'];
					 $company=$job['company'];
					 $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'];
					 $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>
                           <td><p>'.$invoice_no.'</p></td>
						   <td><p>'.$company.'</p></td>
						   <td><p>'.$date.'</p></td>
						   <td><p>'.$invoice_status.'</p></td>
						   <td align="right" ><p>£ '.number_format((float)$nett,2).'</p></td>
						   <td align="right"><p>£ '.number_format((float)$vat,2).'</p></td>
						   <td align="right"><p>£ '.number_format((float)$gross,2).'</p></td>
						   
						   
					</tr>';
				 } ?>
                 <?php 
				
				 $invoice_html.='
				  <tr align="right">
                     
						   <td  colspan="4"><p><b>Total Amounts: </b></td>
						   <td ><p><b>£ '.number_format((float)$total_nett,2).'</p></td>
						   <td ><p><b>£ '.number_format((float)$total_vat,2).'</p></td>
						   <td ><p><b>£ '.number_format((float)$total_gross,2).'</p></td>
						   </b>
						   
					   </tr>
					   
					</tbody>
				 </table>
				</div>
					 
               ';?>
			
				   <?php 
				   
				   echo 
				   '<div class="col-md-8" id="invoice_div" style="margin-left:20px;">'.$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">
				   $('#company').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>