
<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>' ;
		}
?>

</div>

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

<div class="col-md-4 pull-right"><select name="paid_status" class="form-control"><option value="1" selected>Paid</option>
     <option value="2">Not Paid</option>
       </select></div>

</div>

       
</div>

<table  style="font-family:Montserrat; font-size:13px;" id="jobs" class="table table 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>Status</th>
						    <th>Company</th>
                           <th>Client</th>
                           <th>Start Date</th>
                          <th>End Date</th>
                          
						   
                           <th>Rate</th>
                           <th>Days</th>
						   <th>Parking</th>
                           <th>Transfer</th>
                           <!--th>Customer Fare</th>
                           <th>Other</th-->
                           <th>Total</th>
                           
                          
                          
                           <th><input type="checkbox" id="select_all" /></th>
                          
                           
            </tr>

        </thead>

        <tfoot>

            <tr class="btn-primary">
                           <th>Job ID</th>
						   <th>Status</th>
						    <th>Company</th>
                           <th>Client</th>
                           <th>Start Date</th>
                          <th>End Date</th>
                          
						   
                           <th>Rate</th>
                           <th>Days</th>
						   <th>Parking</th>
                           <th>Transfer</th>
                           <!--th>Customer Fare</th>
                           <th>Other</th-->
                           <th>Total</th>
                           
                           <th></th>
                           
                          
            </tr>

        </tfoot>

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

if($rowcount==0)
{
	//echo '<tr style="cursor:pointer; background-color:#F80307; color:#F3EBEB;"><td></td><td></td><td></td><td><p>You have no Jobs, Add Some Jobs.</p></td></tr>';
}
				

//echo '<p style="text-align:center;background-color:light gray; color:#000000; padding:15px; font-size:18px;">Total '.$rowcount.' jobs found. You can see other jobs by applying filter. Total Amount=£'.number_format($grand_total,2).'</p>';

?>
</div>
        <?php
$id=0;
$i=0;
$tdays=0;
                while($job=mysqli_fetch_assoc($res))

                  {?>      
				  <?php $id=$job['id'];?>
                  <tr class="update_job" row-id="<?php echo $job['id'];?>">
                 
						   <?php
						   
						   $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; float:left; 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>
							 <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>
						   <option <?php if($job['status'] == "Clear"){ echo 'selected'; } ?> value="Clear">Clear</option>
						   </select>
						   </td>
                           <td><?php echo $job['customer_name'];?></td>
						    <td><?php echo $job['company_contact'];?></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 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 date("d/m/Y", strtotime($job['end_date'])); ?>  onkeypress="javascript:return isNumber(event)">
					 	   
				  
						   </td>
                           
                           <input name="substitute_id[]" type="hidden" id="substitue_id" value=<?php echo $job['driver_id']; ?>>
                           <input name="driver_id[]" type="hidden" id="driver_id" value=<?php echo $job['driver_id']; ?>>
						   <input name="customer_id[]" type="hidden" id="customer_id" value=<?php echo $job['customer_id']; ?>>
						   <input name="b_id[]" type="hidden" id="b_id" value=<?php echo $job['booking_id']; ?>>
                          <input name="id[]" type="hidden" id="id" value=<?php echo $job['id']; ?>>
                        <?php $id=$job['id'];?>
                         <input name="parking_id[]" type="hidden" id="parking_id" value=<?php echo $job['parking']; ?>>
                       <input name="transfer_id[]" type="hidden" id="transfer_id" value=<?php echo $job['transfer']; ?>>
					    <input name="rate_id[]" type="hidden" id="rate_id" value=<?php echo $job['rate']; ?>>
                       <input name="job_type[]" type="hidden" id="job_type" value=<?php echo $job['job_type']; ?>>
                        
                        
						 <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['supp_cost']; ?>  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="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['supp_cost']*$job['days']+$job['parking']+$job['transfer']; ?>></td>
						 
                 <td style="text-align:right; font-size:20px;"><input name="selected_booking[]" type="checkbox" class="checkbox selected_booking" value=<?php echo $job['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'])){
/*	
echo "<pre>";
   print_r($_POST);
echo "</pre>";
 */
        $cp=0;
		$cpp=0;
		$ct=0; 
        $ctt=0;
		
        $bookingIds = $_POST;
		$count=count($_POST['selected_booking']);
			$paid_status=$con->real_escape_string($_POST['paid_status']);
		for ($i=0;$i<$count;$i++){
        $index = array_search($_POST['selected_booking'][$i] ,$bookingIds['id']);
	    //echo "index=".$index;
		//$substitute_id=$con->real_escape_string($_POST['substitute_id'][$index]);
		$status=$con->real_escape_string($_POST['status'][$index]);
			
			$driver_id=$con->real_escape_string($_POST['driver_id'][$index]);
			$customer_id=$con->real_escape_string($_POST['customer_id'][$index]);
			$b_id=$con->real_escape_string($_POST['b_id'][$index]);
			
			 $rate=$con->real_escape_string($_POST['rate'][$index]);
		  $days=$con->real_escape_string($_POST['days'][$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));
		  
		  $parking_id=$con->real_escape_string($_POST['parking_id'][$index]);
		  $transfer_id=$con->real_escape_string($_POST['transfer_id'][$index]);
		  $rate_id=$con->real_escape_string($_POST['rate_id'][$index]);
		//  $waiting_time=$con->real_escape_string($_POST['waiting_time'][$index]);
		 // $waiting_charges=$con->real_escape_string($_POST['waiting'][$index]);
		 // $parking=$con->real_escape_string($_POST['parking'][$index]);
		 // $fare=$con->real_escape_string($_POST['fare'][$index]);
		  // $other=$con->real_escape_string($_POST['other'][$index]);
		 // $total_fare=$con->real_escape_string($_POST['total_fare'][$index]);
		  $job_type=$con->real_escape_string($_POST['job_type'][$i]); 
		  $booking_id=$con->real_escape_string($_POST['selected_booking'][$i]);
		  
		 // echo '<script>alert("'.$booking_id.'"); <script>';
		 //echo '<script>alert("'.$driver_id.'"); <script>';
		 // echo '<script>alert("'.$substitute_id.'");<script>';
/*		  
$sql1="UPDATE driver_bookings SET
waiting_time='$waiting_time', 
waiting_charges='$waiting_charges', 
parking='$parking', 
fare='$fare', 
total_fare='$total_fare', 
other='$other',
status='2',
invoice_status=1
WHERE booking_id='$booking_id'";
*/


//$sqlbupdate="UPDATE bookings SET paid_status='$paid_status',status='$status' WHERE id=".(int)$bid;
//mysqli_query($con,$sqlbupdate);


//echo $sql1;

$sql11="SELECT * FROM driver_bookings WHERE id='$booking_id'";
$sql11result=mysqli_query($con,$sql11);
$sql11driver=mysqli_num_rows($sql11result);

 //echo '<script>alert("'.$sql11driver.'"); <script>';
if($sql11driver>0)
{

$sql1="UPDATE driver_bookings SET

start_date='$start_date',
end_date='$end_date',
days='$days', 
parking='$parking', 
transfer='$transfer',
total_fare='$total_fare', 
supp_cost='$rate',
status='2',
invoice_status=1
WHERE id='$booking_id'";
if (!mysqli_query($con,$sql1))
  {
  echo("Error description: " . mysqli_error($con));
  exit;
  }
  

$sql1s="UPDATE `bookings` SET
`paid_status`='$paid_status', 
`status`='$status'

WHERE `id`='$b_id'";
if (!mysqli_query($con,$sql1s))
  {
  echo("Error description: " . mysqli_error($con));
  exit;
  }
 
 
 
//echo $sql1;
//exit;
$ctotal= $rate_id*$days+$parking+$transfer; 
/*$sql2="UPDATE bookings SET
supp_cost='$rate', 

days='$days', 
total_fare='$ctotal', 
job_status='2',
start_date='$start_date',
end_date='$end_date',
parking='$parking',
transfer='$transfer'

WHERE id='$booking_id'";
*/
if($job_type=="Cash")
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//if($payment_type=="cr")
			{
				       $source_id=$customer_id;
					   $amount=$ctotal;
					   $transaction_date=date("Y-m-d",strtotime(date("Y-m-d")));
	
	$payment_type='cr';//$_POST['payment_type'];
	$mode_of_payment="Cash";//$_POST['mode_of_payment'];
	$details="*Booking Id: ".$b_id."*";//$_POST['details'];
					   
					   $pass_sql = "SELECT current_bal FROM passbook WHERE company_id=4";
			$pass=mysqli_query($con,$pass_sql);
			$passbook=mysqli_fetch_assoc($pass);
			$bank_bal=$passbook['current_bal'];
			// now we will update the passbook table to update the balance of this client.

			$client_sql = "SELECT current_bal FROM passbook WHERE company_id=20";
			$pass_sql=mysqli_query($con,$client_sql);
			$passbook=mysqli_fetch_assoc($pass_sql);
			$client_bal=$passbook['current_bal'];
					   
					   
					  $cr_sql = "SELECT  name,balance FROM creditors WHERE source_id=".$source_id;
					 // echo $cr_sql;
					  //exit;
					  $cr=mysqli_query($con,$cr_sql);
					  $cr_balance=mysqli_fetch_assoc($cr);
					  $cr_bal=$cr_balance['balance'];
					  $c_name=$cr_balance['name'];
					 // echo $cr_bal;
					 // exit;
					  if($amount>$cr_bal)
					{
					echo '<script language="javascript">';
				//	echo 'alert("You are recieving more than balance.")';
					echo '</script>';
						
					//header("Refresh: 1; url=../new_transaction.php");
					// echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
				//	exit;			
					}	
					
					  //Update the balance of this creditor
					  $new_bal=$cr_bal-$amount;							
					  $upd_cr = "UPDATE creditors SET ". 
					  "balance =".$new_bal."". 
					  " WHERE source_id=".$source_id;
					  //echo "Query is <br> " .$upd_cr. "<br>";
					  mysqli_query($con,$upd_cr);
					  
					  //Now update Passbook
					  $passbook_bal=$bank_bal+$amount;
					  $upd = "UPDATE passbook SET ". 
					  "current_bal =".$passbook_bal."". 
					  " WHERE company_id=4";
					  mysqli_query($con,$upd);


                        $inv_sql = "SELECT  * FROM invoices WHERE customer_id=".$source_id;
					 // echo $cr_sql;
					  //exit;
					  $inv=mysqli_query($con,$inv_sql);
					  $inv_balance=mysqli_fetch_assoc($inv);
					  $inv_bal=$inv_balance['amount_paid'];
					  $inv_bal1=$inv_bal+$amount;
					                          
					  $upd_inv = "UPDATE invoices SET ". 
					  "amount_paid =".$inv_bal1."". 
					  " WHERE customer_id=".$source_id;
					  mysqli_query($con,$upd_inv);
					  
					  
					   $inv_sql1 = "SELECT  * FROM invoices_wings_courier WHERE customer_id=".$source_id;
					 // echo $cr_sql;
					  //exit;
					  $inv1=mysqli_query($con,$inv_sql1);
					  $inv_balance1=mysqli_fetch_assoc($inv1);
					  $inv_bal1=$inv_balance1['amount_paid'];
					  $inv_bal11=$inv_bal1+$amount;
					                          
					  $upd_inv1 = "UPDATE invoices_wings_courier SET ". 
					  "amount_paid =".$inv_bal11."". 
					  " WHERE customer_id=".$source_id;
					  mysqli_query($con,$upd_inv1);

					  
					  $current_bal=$bank_bal+$amount;
							  //Make a transaction
						$sql = "INSERT INTO transactions". 
						//type database fields exactly here
						" (
					  transaction_date,
					  source_id,
					  name,
					  transaction_type,
					  mode_of_payment,
					  amount,
					  details,
					  current_bal	
						)" . 
						
						//type names of form variables exactly here
						"VALUES 
						(
						'$transaction_date',
						'$source_id',
						'$c_name',
						'$payment_type',
						'$mode_of_payment',
						'$amount',
						'$details',
						'$current_bal'
						) ";
						
						//echo $sql;
						mysqli_query($con,$sql);
						$last_insert_id=mysqli_insert_id($con);	
						echo '<script language="javascript">';
				//	echo 'alert("Transaction Successful")';
					echo '</script>';
						//header("Refresh: 1; url=../new_transaction.php");
					//	 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
					//	exit;
			}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//if($payment_type=="dr")
			{
				
			 $source_id=$driver_id;
					   $amount=$total_fare;
					   $transaction_date=date("Y-m-d",strtotime(date("Y-m-d")));
	
	$payment_type='dr';//$_POST['payment_type'];
	$mode_of_payment="Cash";//$_POST['mode_of_payment'];
	$details="*Booking Id: ".$b_id."*";//$_POST['details'];
					   
					   $pass_sql = "SELECT current_bal FROM passbook WHERE company_id=4";
			$pass=mysqli_query($con,$pass_sql);
			$passbook=mysqli_fetch_assoc($pass);
			$bank_bal=$passbook['current_bal'];
			// now we will update the passbook table to update the balance of this client.

			$client_sql = "SELECT current_bal FROM passbook WHERE company_id=20";
			$pass_sql=mysqli_query($con,$client_sql);
			$passbook=mysqli_fetch_assoc($pass_sql);
			$client_bal=$passbook['current_bal'];	
				
				
			$debt_sql = "SELECT balance FROM debtors WHERE source_id=".$source_id;
			$debt=mysqli_query($con,$debt_sql);
			$d_balance=mysqli_fetch_assoc($debt);
			$d_bal=$d_balance['balance'];
			//echo $d_bal;
					if($d_bal==0)
					{
			/*		 echo "<script>var confirm = confirm('There is nothing to pay, you still want to pay ?');
          if(confirm)
		  {
		//  alert('dfsdThere is nothing to pay, you still want to pay ?');
		  }
		  else
		  {
		  // window.location='../new_transaction.php';
		 }  
		  </script>";
		  
		 */
		
		  
				/*	echo '<script language="javascript">';
					echo 'alert("There is nothing to pay, you still want to pay ?")';
					echo '</script>';
					//header("Refresh: 1; url=../new_transaction.php");
					 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
					exit;		*/			
					}	
			//just check if you are paying him more money
					if($amount>$d_bal)
					{
					/* echo "<script>var confirm = confirm('You are paying more than balance, you still want to pay ?');
          if(confirm)
		  {
		 // alert('dfsdThere is nothing to pay, you still want to pay ?');
		  }
		  else
		  {
		 //  window.location='../new_transaction.php';
		 }  
		  </script>";
			*/
				/*	echo '<script language="javascript">';
					echo 'alert("You are paying more than balance.")';
					echo '</script>'; */
					//header("Refresh: 1; url=../new_transaction.php");
					//exit;					
					}	
			
					if($bank_bal<$amount)
					{
				
					
					echo '<script language="javascript">';
				//	echo 'alert("There are not enough funds in your account.")';
					echo '</script>';
					//header("Refresh: 1; url=../new_transaction.php");
				//	 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
				//	exit;					
					}		
					
				  	  $dr_sql = "SELECT name,balance FROM debtors WHERE source_id=".$source_id;
					  $dr=mysqli_query($con,$dr_sql);
					  $dr_balance=mysqli_fetch_assoc($dr);
					  $dr_bal=$dr_balance['balance'];
					  $d_name=$dr_balance['name'];
					  
					  //Update the balance of this debtor
					  $new_bal=$dr_bal-$amount;							
					  $upd_dr = "UPDATE debtors SET ". 
					  "balance =".$new_bal."". 
					  " WHERE source_id=".$source_id;
					 // echo "Query is <br> " .$upd_dr. "<br>";
					  mysqli_query($con,$upd_dr);
					  
					  //Now update Passbook
					  $client_passbook_bal=$bank_bal-$amount;
					  $upd = "UPDATE passbook SET ". 
					  "current_bal =".$client_passbook_bal."". 
					  " WHERE company_id=4";
					  mysqli_query($con,$upd);
					  //Now update Passbook
					  $passbook_bal=$client_bal-$amount;
					  $upd = "UPDATE passbook SET ". 
					  "current_bal =".$passbook_bal."". 
					  " WHERE company_id=20";
					  mysqli_query($con,$upd);
					  //Make a transaction
					  $current_bal=$bank_bal-$amount;
							  
					  $sql = "INSERT INTO transactions". 
					  //type database fields exactly here
					  " (
					  transaction_date,
					  source_id,
					  name,
					  transaction_type,
					  mode_of_payment,
					  amount,
					  details,
					  current_bal	
					  )" . 
					  
					  //type names of form variables exactly here
					  "VALUES 
					  (
					  '$transaction_date',
					  '$source_id',
					  '$d_name',
					  '$payment_type',
					  '$mode_of_payment',
					  '$amount',
					  '$details',
					  '$current_bal'
					  ) ";
						
						//echo $sql;
						mysqli_query($con,$sql);
						$last_insert_id=mysqli_insert_id($con);	
						echo '<script language="javascript">';
					//echo 'alert("Transaction Successful")';
					echo '</script>';
						//header("Refresh: 1; url=../new_transaction.php");
					//	 echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../new_transaction.php">';
						//exit;
			}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}



}


 
         }
		  echo '<script>alert("Bookings Updated Successfully"); window.location.replace("list_driver_jobs.php");</script>';
       }
    ?>
<script>
function detail_display(id)
{

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


} 
function detail_hide(id)
{

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


} 

</script>	