<?php

ob_start();
include "head.php";
include "dbconfig.php";
?>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  
   <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 Driver & Dates From / To </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-sm-3">
			       	   <select name="driver_id" id="driver_id" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select from List </option>
                               <?php
							   
							        $customers_sql="SELECT * from employees order by first_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['first_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>
                    <!-- now retrieve the transactions of this customer -->
                   </div>
                 			
</div>
		    
  </form>

   <?php
   $today= date('Y-m-d');
$from="";
$to="";
$company='';
$header='';
$sql='';
$sql="SELECT driver_invoices.invoice_no,driver_invoices.date,driver_invoices.commission, driver_invoices.driver_id,driver_invoices.nett,driver_invoices.gross,driver_invoices.vat_p,driver_invoices.vat_amount, driver_invoices.status status_id,driver_invoices.percentage_amount,driver_invoices.percentage,driver_invoices.percentage_amount,
employees.first_name,
invoice_status.name as status,driver_invoices.pay_date
FROM driver_invoices
LEFT JOIN invoice_status ON driver_invoices.status = invoice_status.id
LEFT JOIN employees ON driver_invoices.driver_id = employees.id";

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 driver_invoices.date BETWEEN '$from' AND '$to'";
	  $header='From '.date("d/m/Y",strtotime($from)).' To '.date("d/m/Y",strtotime($to)); 
	}
	if (!empty($_POST['driver_id']))
	{
	$driver_id=$con->real_escape_string($_POST['driver_id']);
	$sql.=" AND driver_invoices.driver_id=$driver_id" ;
	}
	$sql.=" ORDER BY driver_invoices.invoice_no DESC
    ";
    $res=mysqli_query($con,$sql);
    if(!$res){echo 'Error is '. mysqli_error($con);}
    $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))
				{
								
								$first_name=$sum['first_name'];
				}
				;?>

</head>

<body> 	
              <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">	
                     <div id="invoice">
					  
                      <h2>Driver Invoices Report '.$header.'</b><h2>
					  
                  <table border="1">

        <thead>
			
            <tr>
                            <th>Invoice Number</th>
							<th>Name</th>
                           <th>Month</th>
                           
                           <th>Paid</th>
						   
                          
                          
            </tr>

        </thead>

        <tbody>
        ';?>
       
        <?php
				$total=0;
				$total_nett=0;
				$total_commission=0;
				$total_gross=0;
				$percentage=0;
				$percentage_amount=0;
				$total_percentage_amount=0;
				$total_vat=0;
				$vat=0;
                while($job=mysqli_fetch_assoc($res))
				
                  {
					  //echo $job['payment_type'];
					  //exit;
					 $invoice_no=$job['invoice_no'];
					 $date=date("F", strtotime($job['date']));
					            $nett=$job['nett'];
								$driver_name=$job['first_name'];
								$gross=$job['gross'];
							    $commission=$job['commission'];
								$percentage=$job['percentage'];
								$percentage_amount=$job['percentage_amount'];
//								$vat=$job['vat'];
								
					 $total_nett=$total_nett+$job['nett'];
					 $total_commission=$total_commission+$job['commission'];
					 $total_gross=$total_gross+$job['gross'];
	//				 $total_vat=$total_vat+$job['vat'];
					 $total_percentage_amount=$total_percentage_amount+$job['percentage_amount'];
					 
					
					 
					 $total=$total+$job['gross'];
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					 <tr>
                           <td><p>'.$invoice_no.'</p></td>
						   <td><p>'.$driver_name.'</p></td>
						   <td><p>'.$date.'</p></td>
						   
						   <td style="text-align:right;font-size:12px"><p>£ '.number_format((float)$gross,2).'</p></td>
						   
						   
					</tr>';
				 } ?>
                 <?php 
				 
				 $invoice_html.='
				  <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="3"><p><b>Total Amounts: </b></td>
						   
						   <td style="text-align:right;font-size:12px"><p><b>£ '.number_format((float)$total_gross,2).'</p></td>
						   </b>
					   </tr>
				 
					</tbody>
				 </table>
				
					 
               ';?>
			
				   <?php 
				   
				   echo 
				   '<div class="col-md-8" id="invoice_div" style="margin-left:40px;">'.$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">
				   $('#driver_id').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>