<form action="" method="post">


			
            
            <div class="pull-right col-md-12">
                 <div class="col-md-6 pull-left">
			<?php
            $rowcount=mysqli_num_rows($res);

if($rowcount==0)
{
	echo '<label>Total '.$rowcount.' jobs found. Add some bookings.</label>';
}
//echo '<label>Total '.$rowcount.' jobs found. You can see other jobs by applying filter</label>';
echo '<label>Total '.$rowcount.' jobs found. You can see other jobs by applying filter</label>       Total Amount=£'.number_format($grand_total,2);


?>
</div>
<div class="col-md-2">
<input name="delete_booking" style="margin-bottom:10px;" type="submit" class="btn btn-sm btn-danger pull-right" value="Update All Jobs"/>

</div>

 <div class="form-group col-sm-3 pull-right">
  <div class="col-md-6">
			       	   <select name="invoice_id" id="invoice_id" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select Invoice </option>
                               <?php
							   
							        $customers_sqli="SELECT invoices.invoice_no,invoices.date, invoices.customer_id, invoices.vat,invoices.gross, invoices.pay_date
FROM invoices";
									$t_resulti=mysqli_query($con,$customers_sqli);
									while($inv=mysqli_fetch_assoc($t_resulti))
									{
									
									?>
	<option style="padding:20px; background-color:#2977C9; color:#F9F0F1; cursor:pointer;" value="<?php echo $inv['invoice_no']; ?>">
	   <?php echo $inv['invoice_no']; ?>
    </option>
						<?php
						
						}
						?>
			       	   </select>
					   </div>
					    <input  type="submit" name="allocate_invoice" class="btn btn-sm btn-success" value="Allocate Invoice"/>
   
			       	   </div>


</div>

<table id="jobs" class="table table list_jobs table-hover table-bordered table-striped table-info" cellspacing="0" border="1">

        <thead style="background-color:#07A8EC;">
            <tr class="btn-primary" style="padding:10px;">

                           <th>Job ID</th>
                           <th>Status</th>
                           <th>Driver</th>
                           <th>Plate</th>
                           <th>Client</th>
                           <th>Vehicle</th>
                           
                           <th>Start</th>
                            <th>Finish</th>
                           <th>Rate</th>
						  
                           <th>Days</th>
                           <th>Waiting</th>
						   <th>Parking</th>
                           <th>Transfer</th>
                           <th>Total</th>
                          
                           
                           <th><input type="checkbox" id="select_all" /></th>
                           
            </tr>

        </thead>

        <tfoot style="background-color:#07A8EC;">

            <tr class="btn-primary">
                          <th>Job ID</th>
                           <th>Status</th>
                           <th>Driver</th>
                           <th>Plate</th>
                           <th>Client</th>
                           <th>Vehicle</th>
                           
                           <th>Start</th>
                            <th>Finish</th>
                           <th>Rate</th>
						   
                           <th>Days</th>
						   <th>Waiting</th>
                           <th>Parking</th>
						   <th>Transfer</th>
                           <th>Total</th>
                        
                           <th></th>
                          
            </tr>

        </tfoot>

        <tbody>
        
        <?php
				
                while($job=mysqli_fetch_assoc($res))

                  {
					  
					  ?>      
                  <tr class="update_job" row-id="<?php echo $job['booking_id'];?>" >
                 
						   <?php
						  /*  $sqls="SELECT bookings.days AS days,bookings.start_date AS start_date,bookings.end_date AS end_date,bookings.flight_no,bookings.pickup_date,bookings.id AS booking_id,bookings.rate AS rate,bookings.supp_cost AS supp_cost,bookings.comments, bookings.parking,bookings.driver_id, bookings.drop_address,bookings.drop_post_code,bookings.pickup_address,bookings.pickup_post_code, bookings.job_type,bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time, bookings.hours,bookings.mins,bookings.other,bookings.job_status,bookings.passengers,bookings.bags, customers.id AS customer_id, customers.name AS customer_name,customers.address1,customers.post_code,customers.city, customers.phone as customer_phone, payment_type.name AS payment_type,  company_contact.name as company_contact,company_contact.email,company_contact.mobile as passenger_phone,company_contact.id as passenger_id, 
vehicle_make.make as make,vehicle_make.id as make_id
FROM bookings LEFT JOIN customers ON bookings.customer_id = customers.id 
LEFT JOIN vehicle_make on bookings.vehicle_id=vehicle_make.id
LEFT JOIN payment_type ON bookings.payment_type = payment_type.id  LEFT JOIN company_contact ON bookings.passenger_id = company_contact.id WHERE bookings.id=".$job['booking_id']." ORDER BY bookings.id DESC";
$ress=$con->query($sqls);

$booking_data= mysqli_fetch_assoc($ress);
*/
/////////////////////////////////////////////////////////////////////////////////////
$sqlb="SELECT bookings.start_date,bookings.id AS booking_id,bookings.comments,bookings.job_type,bookings.flight_no,bookings.parking,bookings.driver_id, bookings.drop_address,bookings.drop_post_code,bookings.pickup_address,bookings.pickup_post_code, bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time, bookings.hours,bookings.mins,bookings.other,bookings.job_status,bookings.passengers,bookings.bags,job_status.name,bookings.comments,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate AS rate,bookings.supp_cost AS supp_cost,bookings.substitute,
customers.id AS customer_id,bookings.client_reference, customers.name AS customer_name,customers.address1,customers.post_code,customers.city, customers.phone as customer_phone, 
employees.first_name AS driver,vehicles.color as color,vehicles.reg as reg, vehicle_make.make as make,
company_contact.name as company_contact,company_contact.email,company_contact.mobile as passenger_phone

FROM bookings 
LEFT JOIN customers ON bookings.customer_id = customers.id 
LEFT JOIN payment_type ON bookings.payment_type = payment_type.id 
LEFT JOIN employees ON bookings.driver_id = employees.id 
LEFT JOIN company_contact ON bookings.passenger_id = company_contact.id 
LEFT JOIN vehicle_make ON bookings.vehicle_id = vehicle_make.id 
LEFT JOIN vehicles on vehicles.driver_id=employees.id
LEFT JOIN job_status ON bookings.job_status = job_status.id 
WHERE bookings.id=".$job['booking_id']." 
";
$ressb=$con->query($sqlb);

$booking_data= mysqli_fetch_assoc($ressb);
?>
 <div class="details" id="detail<?php echo $job['booking_id'];?>" style="width:98%; float:left; position:fixed; top:60px; z-index:99; background:#FFFFFF; padding:10px; box-shadow: 2px 2px 2px 2px rgba(0,0,0,0.2);
border-radius: 15px; text-align:center; display:none;">
							 
							  
<div style="float:left; width:5%;">
<b style="text-decoration:underline;">JOB ID</b><br />  
<?php 
							 echo $booking_data['booking_id'];
							  ?>
</div>	
<div style="float:left; width:5%;">
<b style="text-decoration:underline;">Job Type</b><br />
<?php 
							 echo $booking_data['job_type'];
							  ?>
</div>	
<div style="float:left; width:5%;">
<b style="text-decoration:underline;">Pickup Time</b><br />
 <?php $mins=str_replace('&#39;','0',$booking_data['mins']);?>
                        <p><b style='font-size:13px'><?php echo date("d/m", strtotime($booking_data['start_date']))."</b> <b style='font-size:18px'>".$booking_data['hours'].":".$mins;?></b></p>
</div>	
<div style="float:left; width:20%;">
<b style="text-decoration:underline;">Vehicle/Booking Details</b><br />

<?php
  $bid=$job['booking_id']; 
							   //$sub_sql="select * from driver_substitute WHERE booking_id='$bid'";
							   $sub_sql="select * from driver_bookings WHERE booking_id='$bid'";
									$sub_result=mysqli_query($con,$sub_sql);
							  	while($sub_driver=mysqli_fetch_assoc($sub_result))
									{
									//$sid=$sub_driver['substitute_id'];
                                    $sid=$sub_driver['driver_id'];
							    $customers_sql1="select * from employees WHERE employees.id='$sid'";
									$t_result1=mysqli_query($con,$customers_sql1);
									$subname=mysqli_fetch_assoc($t_result1);
																
    echo "<p>".$subname['first_name']."<br/>".$booking_data["reg"]." ".$booking_data["make"]." ".$booking_data["color"]." <br/> <b>Start Date:</b> ".date("d/m/y", strtotime($sub_driver['start_date']))."<br/> <b>End Date:</b>  ".date("d/m/y", strtotime($sub_driver['end_date']))."</p>";
							  }
							  ?>
</div>	
<div style="float:left; width:10%;">
<b style="text-decoration:underline;">Client Name</b><br />

<?php echo $booking_data['company_contact']."<br>".$booking_data['customer_name']." ".$booking_data['passenger_phone'];
							if($booking_data['client_reference']!='')
							{
							echo "<br/><b style='color:#FF0000;'>";
							echo "Ref : ".$booking_data['client_reference'];
							echo "</b>"; 
							}
							?>

</div>	
<div style="float:left; width:25%;">
<b style="text-decoration:underline;">Pickup/Drop Address</b><br />

<b>Pickup:</b><?php echo $booking_data['pickup_address']." ".$booking_data['pickup_post_code'];?><!--/td>
                            <td style="min-width:150px;"--><br /><b>Drop:</b> <?php echo $booking_data['drop_address']." ".$booking_data['drop_post_code'];?><br />
							<b>Passengers:</b><?php echo $booking_data['passengers'];?>&nbsp;
							<b>Bags:</b><?php echo $booking_data['bags'];?><br />
							<b style='font-size:13px'>
							<?php if($booking_data['flight_no']!=""){echo "Flight No. : ";?><!--a href="https://uk.flightaware.com/live/flight/<?php //echo $job['flight_no'];?>" target="_blank"><?php //echo $job['flight_no']."  "; }?></a> </b-->
							<a href="https://www.google.co.in/search?gl=in&q=<?php echo $booking_data['flight_no'];?>&btnG=" target="_blank"><?php echo $booking_data['flight_no']."  "; }?></a></b>

</div>	
<div style="float:left; width:5%;">
<b style="text-decoration:underline;">Days</b><br />

 <?php echo "<p>". $booking_data['days']."</p>";?>

</div>	
<div style="float:left; width:5%;">
<b style="text-decoration:underline;">Rate</b><br />

  &pound;<?php echo $booking_data['rate'];?>

</div>
<div style="float:left; width:5%;">
<b style="text-decoration:underline;">Driver Rate</b><br />

 &pound;<?php echo $booking_data['supp_cost'];?>

</div>
<div style="float:left; width:13%;">
<b style="text-decoration:underline;">Comments</b><br />

<?php echo $booking_data['comments'];?>

</div>							 
<div style="float:left; width:2%;">
			 <span class="close" onclick="detail_hide(<?php echo $job['booking_id'];?>);">&times;</span>	
</div>			 	  
							  
							 </div>



<?php

						   if ($job['job_status']=='2'){?>
                           
							 <td style="background-color:#46E804; color:#0F0F0F;" onmouseover="detail_display(<?php echo $job['booking_id'];?>);">
                            
                                
						    <?php echo $job['booking_id']; 
						   }else{
								
								echo '<td style="background-color:#F1070B; color:#FFF8F8;">'.$job['booking_id'];
								}
							?>
                           
                            
                            </td>
                          
                          
                           <?php $id=$job['booking_id'];?>
						    <td>
                           <select name="status[]" data-id=<?php echo $id;?> id="status<?php echo $id;?>" >
						   <option <?php if($job['status'] == "OK"){ echo 'selected'; } ?> value="OK">OK</option>
						   <option <?php if($job['status'] == "Finished"){ echo 'selected'; } ?> value="Finished">Finished</option>
						   <option <?php if($job['status'] == "On Job"){ echo 'selected'; } ?> value="On Job">On Job</option>
						   </select>
						   </td>
                            <td><label><?php echo $job['driver'];?></label></td>
                            <td><?php echo $job['reg'];?></td>
                            <td><?php echo $job['company_contact'];?></td>
                            <td><?php echo $job['make'];?></td>
                            <td><input required name="start_date[]" type="text" data-id=<?php echo $id;?> id="start_date<?php echo $id;?>" class="form-control start_date" value=<?php echo $start = date("d/m/Y", strtotime($job['start_date'])); ?>  onkeypress="javascript:return isNumber(event)"></td>
                           <td><input required name="end_date[]" type="text" data-id=<?php echo $id;?> id="end_date<?php echo $id;?>" class="form-control end_date" value=<?php echo $start = date("d/m/Y", strtotime($job['end_date'])); ?>  onkeypress="javascript:return isNumber(event)"></td>
                           
                      <input name="id[]" type="hidden" id="id" value=<?php echo $job['booking_id']; ?>>
                        
                        
                        
               			
                        <td><input required name="rate[]" type="text" data-id=<?php echo $id;?> id="rate<?php echo $id;?>" class="form-control rate" value=<?php echo $job['rate']; ?>  onkeypress="javascript:return isNumber(event)"></td>
						 
                        <td><input required name="days[]" type="text" data-id=<?php echo $id;?> id="days<?php echo $id;?>" class="form-control days" value=<?php echo $job['days']; ?>  onkeypress="javascript:return isNumber(event)"></td>
                        
                        <td><input required name="waiting[]" type="text" data-id=<?php echo $id;?> id="waiting<?php echo $id;?>" class="form-control waiting" value=<?php echo $job['waiting_charges']; ?>  onkeypress="javascript:return isNumber(event)"></td>
						
						<td><input required name="parking[]" type="text" data-id=<?php echo $id;?> id="parking<?php echo $id;?>" class="form-control parking" value=<?php echo $job['parking']; ?>  onkeypress="javascript:return isNumber(event)"></td>
                        
                         <td><input required name="transfer[]" type="text" data-id=<?php echo $id;?> id="transfer<?php echo $id;?>" class="form-control transfer" value=<?php echo $job['transfer']; ?>  onkeypress="javascript:return isNumber(event)"></td>
                        
                        
                        <td><input readonly name="total_fare[]" type="text" data-id=<?php echo $id;?> id="total_fare<?php echo $id;?>" class="form-control total_fare" value=<?php echo $job['rate']*$job['days']+$job['parking']+$job['transfer']+$job['waiting_charges']; ?>></td>
                        
                       
						
                 <td style="text-align:right; font-size:20px;"><input name="selected_booking[]" type="checkbox" class="checkbox selected_booking" value=<?php echo $job['booking_id'];?>></td>
              </tr>
              
   <?php } ?>
   		 </tbody>
			   </table>
</form>
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Success</h4>
      </div>
      <div class="modal-body">
        <p>Records Updated Succesfully.</p>
      </div>
      <div class="modal-footer">
        <button id="close_me" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

<?php
if(isset($_POST['selected_booking']) && isset($_POST['delete_booking'])){
/*	
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;
			$status=$con->real_escape_string($_POST['status'][$index]);
			
		  $rate=$con->real_escape_string($_POST['rate'][$index]);
		  $days=$con->real_escape_string($_POST['days'][$index]);
		   $waiting=$con->real_escape_string($_POST['waiting'][$index]);
		   $parking=$con->real_escape_string($_POST['parking'][$index]);
		   $transfer=$con->real_escape_string($_POST['transfer'][$index]);
		  $total_fare=$con->real_escape_string($_POST['total_fare'][$index]);

		  $start_date=$con->real_escape_string($_POST['start_date'][$index]);
		  $start_date = str_replace('/', '-', $start_date);
		  $start_date = date("Y-m-d", strtotime($start_date));
		 
  		  $end_date=$con->real_escape_string($_POST['end_date'][$index]);
		  $end_date = str_replace('/', '-', $end_date);
		  $end_date = date("Y-m-d", strtotime($end_date));
		 
		  $booking_id=$con->real_escape_string($_POST['selected_booking'][$i]);
		  
$sql1="UPDATE bookings SET
rate='$rate', 
status='$status',
days='$days', 
total_fare='$total_fare', 
job_status='2',
start_date='$start_date',
end_date='$end_date',
waiting_charges='$waiting',
parking='$parking',
transfer='$transfer'

WHERE id='$booking_id'";
//echo $sql1;



  $sql2="UPDATE driver_bookings SET

total_fare='$total_fare', 
status='2',
waiting_charges='$waiting',
parking='$parking',
transfer='$transfer'

WHERE booking_id='$booking_id'";
//echo $sql1;

if (!mysqli_query($con,$sql1))
  {
  echo("Error description: " . mysqli_error($con));
  //exit;
  }

if (!mysqli_query($con,$sql2))
  {
  echo("Error description: " . mysqli_error($con));
 // exit;
  }
  
 $sql3="SELECT DISTINCT 
invoices.invoice_no,invoices.date as invoice_date,

bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.status as b_status,bookings.mins,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time,bookings.other,bookings.parking,bookings.transfer,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,customers.email as email,customers.address1,customers.post_code,customers.email,customers.id as company_id,

company_contact.name as passenger,

employees.first_name AS driver,vehicles.reg as reg, vehicle_make.make as make,
invoices.*
FROM `invoice_details` 
INNER JOIN bookings on invoice_details.booking_id=bookings.id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees ON bookings.driver_id=employees.id
INNER JOIN vehicles ON vehicles.driver_id=employees.id
LEFT JOIN vehicle_make ON vehicle_make.id=bookings.vehicle_id
INNER JOIN invoices on invoice_details.invoice_no=invoices.invoice_no
WHERE invoice_details.booking_id='$booking_id'

";
$res3=mysqli_query($con,$sql3);  
$invoice=mysqli_fetch_assoc($res3);

$invoice_no=$invoice['invoice_no'];  
$vat=$invoice['vat'];  
if($invoice_no=="" || $invoice_no=="0" || $invoice_no==0 || $invoice_no==" ")
{
 $sql3="
SELECT DISTINCT 
invoices_wings_courier.invoice_no,invoices_wings_courier.date as invoice_date,

bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.status as b_status,bookings.mins,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time,bookings.other,bookings.parking,bookings.transfer,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,customers.email as email,customers.address1,customers.post_code,customers.email,customers.id as company_id,

company_contact.name as passenger,

employees.first_name AS driver,vehicles.reg as reg, vehicle_make.make as make,
invoices_wings_courier.*
FROM `invoice_details_wings_courier` 
INNER JOIN bookings on invoice_details_wings_courier.booking_id=bookings.id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees ON bookings.driver_id=employees.id
INNER JOIN vehicles ON vehicles.driver_id=employees.id
LEFT JOIN vehicle_make ON vehicle_make.id=bookings.vehicle_id
INNER JOIN invoices_wings_courier on invoice_details_wings_courier.invoice_no=invoices_wings_courier.invoice_no
WHERE invoice_details_wings_courier.booking_id='$booking_id'
";

$res3=mysqli_query($con,$sql3);  
$invoice=mysqli_fetch_assoc($res3);

$invoice_no=$invoice['invoice_no'];  
$vat=$invoice['vat'];  

$sqlr="
SELECT DISTINCT 
invoices_wings_courier.invoice_no,invoices_wings_courier.date as invoice_date,

bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.status as b_status,bookings.mins,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time,bookings.other,bookings.parking,bookings.transfer,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,customers.email as email,customers.address1,customers.post_code,customers.email,customers.id as company_id,

company_contact.name as passenger,

employees.first_name AS driver,vehicles.reg as reg, vehicle_make.make as make,
invoices_wings_courier.*
FROM `invoice_details_wings_courier` 
INNER JOIN bookings on invoice_details_wings_courier.booking_id=bookings.id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees ON bookings.driver_id=employees.id
INNER JOIN vehicles ON vehicles.driver_id=employees.id
LEFT JOIN vehicle_make ON vehicle_make.id=bookings.vehicle_id
INNER JOIN invoices_wings_courier on invoice_details_wings_courier.invoice_no=invoices_wings_courier.invoice_no
WHERE invoices_wings_courier.invoice_no='$invoice_no'

ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC

";

$resr=mysqli_query($con,$sqlr);
$nett=0;
 while($job=mysqli_fetch_assoc($resr))
                  {
				  $nett=$nett+$job['total_fare']; 
				  }
//$vat=($nett*20)/100;				  
$gross=$nett;

$sql3u="UPDATE invoices_wings_courier SET
nett='$nett', 
gross='$gross'

WHERE invoice_no='$invoice_no'";  
mysqli_query($con,$sql3u);

}
else
{
 $sqlr="SELECT DISTINCT 
invoices.invoice_no,invoices.date as invoice_date,

bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.status as b_status,bookings.mins,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time,bookings.other,bookings.parking,bookings.transfer,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,customers.email as email,customers.address1,customers.post_code,customers.email,customers.id as company_id,

company_contact.name as passenger,

employees.first_name AS driver,vehicles.reg as reg, vehicle_make.make as make,
invoices.*
FROM `invoice_details` 
INNER JOIN bookings on invoice_details.booking_id=bookings.id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees ON bookings.driver_id=employees.id
INNER JOIN vehicles ON vehicles.driver_id=employees.id
LEFT JOIN vehicle_make ON vehicle_make.id=bookings.vehicle_id
INNER JOIN invoices on invoice_details.invoice_no=invoices.invoice_no
WHERE invoices.invoice_no='$invoice_no'

ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC
";

$resr=mysqli_query($con,$sqlr);
$nett=0;
 while($job=mysqli_fetch_assoc($resr))
                  {
				  $nett=$nett+$job['total_fare']; 
				  }
$vat=($nett*20)/100;				  
$gross=$nett+$vat;

$sql3u="UPDATE invoices SET
nett='$nett', 
gross='$gross',
vat='$vat'

WHERE invoice_no='$invoice_no'";  
mysqli_query($con,$sql3u);
  }
 echo '<script>alert("Bookings Updated Successfully"); window.location.replace("list_jobs.php")</script>'; 
    }
       }
    ?>
<?php
 if(!empty($_POST['allocate_invoice'])&&!empty($_POST['invoice_id']))
			{
			$invoice_id=$con->real_escape_string($_POST['invoice_id']);

 
   

		$bookingIds = $_POST;
		$count=count($_POST['selected_booking']);
		
		  $counter=0;
	$vat=0;
		$nett=0;
		$vat_p=0;
 $total_gross=0;
 $No = 0;

 
 for ($i=0;$i<$count;$i++){
			$counter=1+$i;
		//	$index = array_search($_POST['selected_booking'][$i] ,$bookingIds['booking_id']);
			//echo "index=".$index;
		 $index = array_search($_POST['selected_booking'][$i] ,$bookingIds['id']);
		
		$booking_id=$con->real_escape_string($_POST['selected_booking'][$i]);
		//$passengers=$con->real_escape_string($_POST['passengers'][$i]);
		
		
		// check if this booking already exist if yes then update otherwise insert record
		$sql="Select * from bookings where invoice_status=1 AND id=".$booking_id;
		//echo $sql;
		$res=mysqli_query($con,$sql);
		$rowcount=mysqli_num_rows($res);
		$update_booking='';
		if($rowcount>0){
			
		//echo "Booking Found";
			$update_booking="UPDATE bookings SET
					  invoice_status=2 WHERE id=$booking_id";
					//  echo $update_booking;
					  if (!mysqli_query($con,$update_booking)) {echo "Error. ".mysqli_error($con);}
					 // $update_booking="UPDATE bookings SET
					 // driver_id=$driver,job_status=2 WHERE id=$booking_id";
					 // if (!mysqli_query($con,$update_booking)) {echo "Error. ".mysqli_error($con);}
					  $update_invoice_booking="INSERT INTO invoice_details 
		(invoice_no,booking_id)
		VALUES ('$invoice_id','$booking_id')";
		// echo $update_invoice_booking;
		if (!mysqli_query($con,$update_invoice_booking)) {mysqli_error($con);}
		
		$sqlin="Select * from invoices where invoice_no=".$invoice_id;
		$resin=mysqli_query($con,$sqlin);
		$rowin=mysqli_fetch_assoc($resin);
		$vat=$rowin['vat'];
		$nett=$rowin['nett'];
		$vat_p=($vat / $nett)*100;
		
	$sqlind="Select * from invoice_details where invoice_no=".$invoice_id;
		$resind=mysqli_query($con,$sqlind);
		while($rowind=mysqli_fetch_assoc($resind))
		{
		$inv_bid=$rowind['booking_id'];
		$sqlindb="Select * from bookings where id=".$inv_bid;
		$resindb=mysqli_query($con,$sqlindb);
		$rowb=mysqli_fetch_assoc($resindb);
		 $total_gross=$total_gross+$rowb['total_fare'];
		
		}
		
		$nett=$total_gross;
		$vat=($total_gross * $vat_p)/100;
		$gross=$total_gross + $vat;
		
		
			$update_invoices="UPDATE invoices SET
					  nett='$nett',vat='$vat',gross='$gross' WHERE invoice_no='$invoice_id'";
					//  echo $update_booking;
					  if (!mysqli_query($con,$update_invoices)) {echo "Error. ".mysqli_error($con);}
		
		
		echo '<script>alert("Invoiced Done");</script>';
					
			}
			else
			{
			echo '<script>alert("You selected Job is already Invoiced");</script>';
			}
			
			
	}
		
}
			
?>			
<script>
function detail_display(id)
{

$(".details").hide();
$("#detail"+id).show();


} 
function detail_hide(id)
{

$("#detail"+id).hide();


} 

</script>	