
<form action="" method="post" id="invoice">
       
        
       
<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 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>
                          
            </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>

        </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['start_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>
                           
                           <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>
                           <td><input name="driver[]" type="hidden" value="<?php echo $job['driver'];?>"><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_time[]" 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[]" type="hidden" value="<?php echo $job['total_fare'];?>"><label><?php echo "£".$job['total_fare'];?></label></td>
                           
										</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>";
		*/
		$customer_id=$con->real_escape_string($_POST['customer_id']);
		 
		
		
		$today= date('Y-m-d');
		$create_invoice_sql="INSERT INTO invoices 
		(status,date,customer_id) VALUES('Raised','$today','$customer_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_no;
		  // exit;
			$count=count($_POST['selected_booking']);
			//echo $count;
			$total_fare='';
			for($i = 0; $i<$count; $i++)
				{
				   //Get the total amount for all bookings 
					$total_fare=$con->real_escape_string($_POST['total_fare'][$i]);
					$booking_id=$con->real_escape_string($_POST['booking_id'][$i]);
					$total_fare+=$total_fare;
					// Now update the bookings with invoice number
					  $update_booking="UPDATE bookings SET
					  invoice_no=$invoice_no,
					  status='Raised'
					  WHERE id=$booking_id";
						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
	
		$nett=number_format($total_fare,2);
		$vat=number_format($nett*(0.20),2);
		$gross=$nett+$vat;
		$gross=number_format($gross,2);
		$update_invoice_sql="UPDATE invoices 
		SET
		nett=$nett,
		vat=$vat,
		gross=$gross		
		WHERE invoice_no=$invoice_no
		";
		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>