
<form action="" method="post" id="invoice">
       <div class="row col-md-12">
              
            <div class="col-md-10">
       
         		
       		</div>  
           
      </div>
        <div class="col-md-12 well">
        	<div class="col-md-3"></div>
            <div class="col-md-3"></div>
            <div class="col-md-2 pull-right"><input type="text" class="form-control" name="percentage" placeholder="Enter %"></div>
            <div class="col-md-3"><input type="button" name="btn" value="Generate Invoice" id="submitBtn" data-toggle="modal" data-target="#confirm-submit" class="btn btn-success btn-sm pull-right" /></div>
            
        </div>
       
<table  style="font-family:Montserrat; font-size:13px;" id="list_jobs" class="table table-responsive list_jobs table-hover table-bordered table-striped table-info" cellspacing="0" border="1">

        <thead>
        <tr>
        <td><div>
        
			
         </div>
         </td></tr>
            <tr class="btn-primary" style="padding:10px;">
                           <th>Job ID</th>
                           <th>Pickup Date</th>
                           <th>Pickup Time</th>
                           <th>Company</th>
                           <th>Passenger</th>
                           <th>Pickup From</th>
                           
                           <th>Drop To</th>
                            <th>Driver</th>
                           <th>Waiting Time</th>
                           <th style="text-align:right;">Waiting Charges</th>
                           <th style="text-align:right;">Parking</th>
                           <th style="text-align:right;">Fare</th>
                           <th style="text-align:right;">other</th>
                           <th style="text-align:right;">Total Fare</th>
                           <th><input type="checkbox" id="select_all" /> Select all</th>
                          
            </tr>

        </thead>

        <tfoot>

            <tr class="btn-primary">
                          <th>Job ID</th>
                           <th>Pickup Date</th>
                           <th>Pickup Time</th>
                           <th>Company</th>
                           <th>Passenger</th>
                           <th>Pickup From</th>
                           
                           <th>Drop To</th>
                            <th>Driver</th>
                            <th>Waiting Time</th>
                            <th style="text-align:right;">Waiting Charges</th>
                           <th style="text-align:right;">Parking</th>
                           <th style="text-align:right;">Fare</th>
                           <th style="text-align:right;">other</th>
                           <th style="text-align:right;">Total Fare</th>
                           <th><input type="checkbox" id="select_all" /> Select all</th>

        </tfoot>

        <tbody>
        

        <?php

                while($job=mysqli_fetch_assoc($res))

                  {
					  //echo $job['payment_type'];
					  //exit;
					 ?>
                           <td><input name="booking_id[]" type="hidden" value="<?php echo $job['booking_id'];?>"><label><?php echo $job['booking_id'];?></label></td>
                           <td><input name="booking_date[]" type="hidden" value="<?php echo date("d/m/y", strtotime($job['pickup_date']));?>"><label><?php echo date("d/m/y", strtotime($job['pickup_date']));?></label></td>
                           
                           <td>
                            <input name="hours[]" type="hidden" value="<?php echo $job['hours'];?>">
                              <input name="mins[]" type="hidden" value="<?php echo $job['mins'];?>">
                               <label><?php echo $job['hours'].":".$job['mins'];?></label>
                           </td>
                           
                           <td><input name="customer_name[]" type="hidden" value="<?php echo $job['customer_name'];?>"><label><?php echo $job['customer_name'];?></label></td>
                           <input type="hidden" name="customer_id" value="<?php echo $job['customer_id'];?>"/>
                           <td><input name="company_contact[]" type="hidden" value="<?php echo $job['company_contact'];?>"><label><?php echo $job['company_contact'];?></label></td>
                            <!-- Pickup Address -->
                           <td>
                             <input name="address1[]" type="hidden" value="<?php echo $job['address1'];?>">
                              <input name="city[]" type="hidden" value="<?php echo $job['city'];?>">
                               <input name="post_code[]" type="hidden" value="<?php echo $job['post_code'];?>">
                               
                              <label><?php echo $job['address1'].",".$job['city'].",".$job['post_code'];?></label>
                           </td>
                            <!-- Drop Address --> 
                           <td>
                             <input name="drop_address[]" type="hidden" value="<?php echo $job['drop_address'];?>">
                               <input name="drop_post_code[]" type="hidden" value="<?php echo $job['drop_post_code'];?>">
                               
                              <label><?php echo $job['drop_address'].",".$job['drop_post_code'];?></label>
                           </td>
                           <?php $booking_id=$job['booking_id'];?>
                          
                           <td><input name="driver" type="hidden" value="<?php echo $job['driver_id'];?>"><label><?php echo $job['driver'];?></label></td>
                        
                          <td><input name="waiting_time[]" type="hidden" value="<?php echo $job['waiting_time'];?>"><label><?php echo $job['waiting_time'];?></label></td>
                          
                          <td style="text-align:right;"><input name="waiting_charges[]" type="hidden" value="<?php echo $job['waiting_charges'];?>"><label><?php echo "£".$job['waiting_charges'];?></label></td>
                          <td style="text-align:right;"><input name="parking[]" type="hidden" value="<?php echo $job['parking'];?>"><label><?php echo "£".$job['parking'];?></label></td>
                          <td style="text-align:right;"><input name="fare[]" type="hidden" value="<?php echo $job['fare'];?>"><label><?php echo "£".$job['fare'];?></label></td>
                          <td style="text-align:right;"><input name="other[]" type="hidden" value="<?php echo $job['other'];?>"><label><?php echo "£".$job['other'];?></label></td>
                          <td style="text-align:right;"><input name="total_fare[<?php echo $booking_id;?>]" type="hidden" value="<?php echo $job['total_fare'];?>"><label><?php echo "£".$job['total_fare'];?></label></td>
                           <td style="text-align:right;"><input name="selected_booking[]" type="checkbox" class="checkbox selected_booking" value=<?php echo $job['booking_id'];?>></td>
                        
                                    <input type="hidden" id="selected_booking">
										</td>
       
                          
                 
              </tr>
   <?php } ?>
   		 </tbody>
			   </table>
 
 </form>
 <div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                Confirm Submit
            </div>
            <div class="modal-body">
                Are you sure you want to create invoice of these bookings. Once created, invoice can not be deleted. Are You Sure?
               
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">Check Again</button>
                <a href="#" id="submit" class="btn btn-success success">Generate Invoice</a>
            </div>
        </div>
    </div>
</div>


 <?php
 if(isset($_POST['selected_booking']))
			{
				
			$booking_id=$_POST['selected_booking'];
		/*	
			echo "<pre>";
  				 print_r($_POST);
			echo "</pre>";
		*/
		$driver_id=$con->real_escape_string($_POST['driver']);
		$percentage=$con->real_escape_string($_POST['percentage']);
		$today= date('Y-m-d');
		$create_invoice_sql="INSERT INTO driver_invoices 
		(status,date,driver_id) VALUES('2','$today','$driver_id');";
		
		
		if (!mysqli_query($con,$create_invoice_sql)) {
			  echo $create_invoice_sql;
			  die('INSERT 3 INTO Bookings -Error: ' . mysqli_error($con));
			  exit;
		  }
		   $invoice_no=mysqli_insert_id($con);
		   echo "Invoice Number is ".$invoice_no;
		  // exit;
			$count=count($_POST['selected_booking']);
			//echo $count;
			$total_fare='';
			$sum='';
			$nett='';
			$gross='';
			foreach($_POST['selected_booking'] as $booking_id) {
    $total_fare=$con->real_escape_string($_POST['total_fare'][$booking_id]);
	$sum +=  $total_fare;
	// Now update the bookings with invoice number
					  $update_booking="UPDATE driver_bookings SET
					  invoice_no=$invoice_no,
					  invoice_status=2
					  WHERE booking_id=$booking_id";
					 // echo $update_booking;
						if (!mysqli_query($con,$update_booking)) {
							echo $update_booking;
							die('INSERT 3 INTO Bookings -Error: ' . mysqli_error($con));
							exit;
						 }
  }

		// Now we got total of all selected values so now update update invoice
	    if (!empty($percentage)) {
		$percentage=number_format($percentage,2)/100;
		$nett=number_format($sum,2);
		$percentage=number_format($nett*($percentage),2);
		
		$gross=$nett-$percentage;
		}else{
		$gross=number_format($gross,2);
		}
		$update_invoice_sql="UPDATE driver_invoices 
		SET
		nett='$nett',
		percentage=$percentage,
		gross='$gross'		
		WHERE invoice_no=$invoice_no
		";
		//echo $update_invoice_sql;
		//exit;
		if (!mysqli_query($con,$update_invoice_sql)) {
			  echo $update_invoice_sql;
			  die('INSERT 3 INTO Bookings -Error: ' . mysqli_error($con));
			  exit;
		  }
		}
			?>
            
<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);
        }
    });
});
</script>

<script>
$('#submit').click(function(){
   // alert('submitting');
	
    $('#invoice').submit();
	
});

</script>