<?php
error_reporting(0);
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="" name="filter_data">
	<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="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 employees.id,employees.phone,employees.first_name,vehicle_make.make,vehicles.color,vehicles.reg
from vehicles
left join employees on vehicles.driver_id=employees.id
left join vehicle_make on vehicles.make=vehicle_make.id WHERE employees.first_name IS NOT NULL ORDER BY first_name ";
									$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['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="See on Screen">
                       </div>
                      
                    <!-- now retrieve the transactions of this customer -->
                   
                 			

				 <div class="col-md-1">
                       <input type="button" class="btn btn-primary btn-sm" onClick="printDiv('invoice')" value="Print" /> 
                       </div>  
					   
					<div class="col-md-2">
<input name="update_booking_paid" style="margin-bottom:10px;" type="submit" class="btn btn-sm btn-danger pull-right" value="Paid"/>

</div>   
              </div>
	</div>		    

<?php
$sql='';
$from='';
$to='';

	$first_name='';
								$pco_number='';
								$pco_expiry='';
								$reg='';
								$make='';
								$driver_address='';
								$driver_post_code='';
if (!empty($_POST['from']) && !empty($_POST['to']) && !empty($_POST['driver_id']))
	{
	
	echo $driver_id=$_POST['driver_id'];
	$commission=0;//$_POST['commission'];
	$percentage=0;//$_POST['percentage'];
	
$sql="SELECT DISTINCT bookings.id as job_id,bookings.status,bookings.job_type,bookings.paid_status,bookings.pickup_date,bookings.hours,bookings.mins,bookings.pickup_address,bookings.driver_paid,company_contact.name as passenger,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,
driver_bookings.waiting_charges,driver_bookings.supp_cost,bookings.total_fare,driver_bookings.waiting_time,driver_bookings.other,driver_bookings.parking,
vehicle_make.make,vehicles.reg,employees.id as driver_id,employees.pco_number,employees.pco_expiry,employees.first_name as first_name,employees.address as driver_address,employees.post_code as driver_post_code
FROM bookings
LEFT JOIN customers ON bookings.customer_id=customers.id
LEFT JOIN company_contact on bookings.passenger_id=company_contact.id
LEFT JOIN employees on bookings.driver_id=employees.id
LEFT JOIN driver_bookings on driver_bookings.booking_id=bookings.id
LEFT JOIN vehicles on vehicles.driver_id=employees.id
LEFT JOIN vehicle_make on vehicle_make.make=vehicles.make";

	  $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));
	  $driver_id=$con->real_escape_string($_POST['driver_id']);
	  
	$sql.="  WHERE bookings.job_type='Account' AND driver_bookings.driver_id=$driver_id AND 
    bookings.pickup_date BETWEEN '$from' AND '$to'"; 
	}else{

$sql="SELECT DISTINCT bookings.id as job_id,bookings.pickup_date,bookings.status,bookings.job_type,bookings.paid_status,bookings.hours,bookings.mins,bookings.pickup_address,bookings.driver_paid,company_contact.name as passenger,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,
driver_bookings.waiting_charges,driver_bookings.supp_cost,bookings.total_fare,driver_bookings.waiting_time,driver_bookings.other,driver_bookings.parking,
vehicle_make.make,vehicles.reg,employees.id as driver_id,employees.pco_number,employees.pco_expiry,employees.first_name as first_name,employees.address as driver_address,employees.post_code as driver_post_code
FROM bookings
LEFT JOIN customers ON bookings.customer_id=customers.id
LEFT JOIN company_contact on bookings.passenger_id=company_contact.id
LEFT JOIN employees on bookings.driver_id=employees.id
LEFT JOIN driver_bookings on driver_bookings.booking_id=bookings.id
LEFT JOIN vehicles on vehicles.driver_id=employees.id
LEFT JOIN vehicle_make on vehicle_make.make=vehicles.make
WHERE bookings.job_type='Account' 
";
}
		
$sql.=" ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC";
//echo $sql;

if(!mysqli_query($con,$sql)){echo 'error'.mysqli_error($con);}
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);
if($rowcount==0){echo "<h2>                      No Booking Found - Please make sure you select Driver Name and Dates Correctly</h2";}
?>

<?php
if(!mysqli_query($con,$sql)){echo 'error'.mysqli_error($con);}
$invoice_sum=mysqli_query($con,$sql);
               
								$nett='';
								$commission='';
								$percentage='';
								$percentage_amount='';
								$gross='';
								$first_name='';
								$pco_number='';
								$pco_expiry='';
								$reg='';
								$make='';
								$cash=0;
								$account=0;
								$account_paid=0;
								$account_unpaid=0;
								$cheque=0;
								$grand=0;
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
								$first_name=$sum['first_name'];
								$pco_number=$sum['pco_number'];
								$pco_expiry=$sum['pco_expiry'];
								$reg=$sum['reg'];
								$make=$sum['make'];
								$driver_address=$sum['driver_address'];
								$driver_post_code=$sum['driver_post_code'];
								$driver_paid=$sum['driver_paid'];
							if($sum['driver_paid']=="0")
							{
								if($sum['job_type']=="Cash")
								{
								$cash=$cash+$sum['total_fare'];
								}
								if($sum['job_type']=="Account")
								{
								$account_unpaid=$account_unpaid+$sum['total_fare'];
								}
								if($sum['job_type']=="Cheque")
								{
								$cheque=$cheque+$sum['total_fare'];
								}
								//$grand_unpaid=$grand+$sum['total_fare'];
							}
							elseif($sum['driver_paid']=="1")
							{
								if($sum['job_type']=="Cash")
								{
								$cash=$cash+$sum['total_fare'];
								}
								if($sum['job_type']=="Account")
								{
								$account_paid=$account_paid+$sum['total_fare'];
								}
								if($sum['job_type']=="Cheque")
								{
								$cheque=$cheque+$sum['total_fare'];
								}
								//$grand_paid=$grand_paid+$sum['total_fare'];
							}
							$grand=$grand+$sum['total_fare'];
							
				}
				
				;?>

<?php 
	$sql="SELECT MAX(invoice_no) as invoice_no from driver_invoices";
	$i_res=$con->query($sql);
	$invoice_res=mysqli_fetch_assoc($i_res);
	$invoice_no=$invoice_res['invoice_no'];
	if($invoice_no==''){$invoice_no=1;}
?>
</head>

<body> 	
               
                       <?php $invoice_html=
					 '   
					 <!doctype html>
					  <html>
					  <head>
					  <meta charset="utf-8">	
					  <div id="invoice">
                      <!--h2>List of Bookings for '.$first_name.'</b><h2-->
				<h2>Total Paid &pound;'.$account_paid.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Total Unpaid &pound;'.$account_unpaid.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Grand Total &pound;'.$grand.'</b><h2>
		
                  <table border="1">

        <thead>

                      <tr>
				           <th  width="2%">Ref</th>
                           <th width="7%">START</th>
						   <th width="7%">FINISH</th>
						   <th width="10%">DAYS</th>
                           <th width="35%">CLIENT</th>
						   <th>DRIVER</th>
						   <th>Amount</th>
						   <!--th>Booking Status</th>
<th>Payment Status</th>
<th>Job type</th-->
<th style="max-width:10px; text-align:right;"><input type="checkbox" id="select_all" /></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_gross=0;
				$count=0;
                while($driver_data=mysqli_fetch_assoc($res))
				
                  {
					 $first_name= $driver_data['first_name'];	  
					 $job_id = $driver_data['job_id'];
					 $start_date = date("d/m/Y",strtotime($driver_data['start_date']));
					 $end_date = date("d/m/Y",strtotime($driver_data['end_date']));
					 $days = $driver_data['days'];
	 				 $passenger=$driver_data['passenger'];
					 $amount='&pound;'.$driver_data['total_fare'];
					 $driver_id = $driver_data['driver_id'];
					 $status = $driver_data['status'];
					 $driver_paid=$driver_data['driver_paid'];
					 if($driver_data['paid_status']=="1")
					 {
					 $paid_status = "+Paid";
					 }
					 else
					 {
					 $paid_status = "Not Paid";
					 }
					 $job_type = $driver_data['job_type'];
					 ?>
                      
                     <?php
					 
					 $invoice_html.=
					 '
					<tr>
                           <!--input type="hidden" name="driver_id" value="'.$driver_id.'"/-->
						   <input type="hidden" name="booking_id[]" value="'.$job_id.'">
						    <input name="id[]" type="hidden" id="id" value="'.$job_id.'">
							
						   <td>'.$job_id.'</td>
						   <td>'.$start_date.'</td>
						   <td>'.$end_date.'</td>
						   <td>'.$days.'</td>
						   <td>'.$passenger.'</td>
						    <td>'.$first_name.'</td>
						   <td>'.$amount.'</td>
						   <!--td>'.$status.'</td>
						   <td>'.$paid_status.'</td>
						   <td>'.$job_type.'</td-->
						    <td style="text-align:right;">'; if($driver_paid==0){
							$invoice_html.=
					 '<input style="float:right;" name="selected_booking[]" type="checkbox" class="checkbox selected_booking" value="'.$job_id.'">';
					 }else{ $invoice_html.='Paid'; }
					 $invoice_html.=
					 '
					 </td>
						  
					</tr>';
					$count++;
					} ?>
				 <?php
				  $invoice_html.=' 
					</tbody>
				 </table>
				</div>
				'; 
              
			   echo 
				   '<div class="col-md-11" id="invoice_div" style="margin-left:40px;">'.$invoice_html.'</div>';
				   ?>
			
	</form>		
			
			
<?php
if(isset($_POST['selected_booking']) && isset($_POST['update_booking_paid'])){
/*
echo "<pre>";
   print_r($_POST);
echo "</pre>";
 //exit;
*/


      $bookingIds = $_POST;
		$count=count($_POST['selected_booking']);
			
		for ($i=0;$i<$count;$i++){
          $index = array_search($_POST['selected_booking'][$i] ,$bookingIds['id']);
			//echo "index=".$index;
			
		 
		  $booking_id=$con->real_escape_string($_POST['selected_booking'][$i]);
		  
$sql1="UPDATE bookings SET
driver_paid='1'
WHERE id='$booking_id'";

mysqli_query($con,$sql1);
  
}

echo '<script>alert("Updated Successfully"); window.location.replace("driver_statements_paid.php")</script>'; 

}
?>			
			
				   <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">
 $(document).ready(function(){

    $('#select_all').on('click',function(){
        if(this.checked){
            $('.checkbox').each(function(){
                this.checked = true;
            });
        }else{
             $('.checkbox').each(function(){
                this.checked = false;
            });
        }
    });
    
    $('.checkbox').on('click',function(){
        if($('.checkbox:checked').length == $('.checkbox').length){
            $('#select_all').prop('checked',true);
        }else{
            $('#select_all').prop('checked',false);
        }
    });
});
 
  $('#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>