<?php
error_reporting(0);
ob_start();
include "head.php";
include "dbconfig.php";
?>
<form method="post" action="report_invoice_by_driver.php">
	<div class="container-fluid" style="margin-top:200x;">
	  <label><h2> Select a Driver</h2></label>
			      <div class="well col-md-12" style="background-color:#e9e9e9; box-shadow: 5px 5px 5px;">
			       	 
                       <div class="form-group col-sm-3">
			       	   <select name="driver_id" id="driver" 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="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">
                        <input type="text"  name="commission" class="to form-control" value="" required placeholder="Car Commission" id="to">
             		 </div>
                     <div class="col-md-2">
                        <input type="text"  name="percentage" class="to form-control" required placeholder="Enter %age eg. 20" id="to">
             		 </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>
                       -->
			       	 <div class="col-md-1">
                       	  <input type="submit" id="select_jobs" class="btn btn-warning" value="Print Invoices">
                       </div>
                    <!-- now retrieve the transactions of this customer -->
                   </div>
                 			
</div>
				  
			    
</form>
<?php
if(isset($_POST['driver_id'])){
	
	$driver_id=$_POST['driver_id'];
	$commission=0;//$_POST['commission'];
	$percentage=0;//$_POST['percentage'];
$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, 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
WHERE employees.id=$driver_id 
";
$today= date('Y-m-d');
$from="";
$to="";

	

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 driver_invoices.date DESC
";
}else{
	$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, 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 ORDER BY driver_invoices.date DESC";
	
	}
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);

//echo $sql;


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

//echo $sql;

                
                $nett='';
				$commission='';
				$percentage='';
				$percentage_amount='';
				$gross='';
				$first_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">	
					  
                      <h2>All Invoices of <b>'.$first_name.'</b><h2>
					  
                  <table border="1" class="table table-striped">

        <thead class="blue-background">
			
            <tr>
                            <th>Invoice Number</th>
							<th>Driver Name</th>
                           <th>Month</th>
                          
                           <th>Gross</th>
						   <th>Print</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'];
								$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>'.$first_name.'</p></td>
						   <td><p>'.$date.'</p></td>
						   
						    
						  
						   <td style="text-align:right;font-size:12px"><p>£ '.$gross.'</p></td>
						   <td><a href="print_driver_invoice.php?invoice_no="'.$job['invoice_no'].'"<i class="fa fa-print"></i><b>&nbsp;Print</b></a></td>
						   
					</tr>';
				 } ?>
                 <?php 
				 
				 $invoice_html.='
				  <tr>
                     
						   <td style="text-align:right;font-size:12px;" colspan="2"><p><b>Total Amounts: </b></td>
						  
						   <td style="text-align:right;font-size:12px"><p><b>£ '.$total_gross.'</p></td>
						   </b>
					   </tr>
				 
					</tbody>
				 </table>
				
					 
               ';?>
			
				   <?php 
				   
				   echo 
				   '<div class="col-md-8" id="invoice_div" style="margin-left:40px;">'.$invoice_html.'</div>'
				  
				   ;?>
				   <script>
                   alert("Please right click in order to print this invoice.");
                   </script>