<?php
		  	
			  include "../dbconfig.php";
			  
			  $x=0;
			  $booking_id=0;
			  $count=0;
			    $nett=$con->real_escape_string($_POST['nett']);
				$invoice_no=$con->real_escape_string($_POST['invoice_no']);
				$date=$con->real_escape_string($_POST['date']);
				$date = str_replace("/","-",$date);
	  $date = date("Y-m-d", strtotime($date));
				$percentage=$con->real_escape_string($_POST['percentage']);
				$percentage_amount=$con->real_escape_string($_POST['percentage_amount']);
				$commission=$con->real_escape_string($_POST['commission']);
				$gross=$con->real_escape_string($_POST['gross']);
				$driver_id=$con->real_escape_string($_POST['driver_id']);
				$commission_text=$con->real_escape_string($_POST['commission_text']);
				$percentage_text=$con->real_escape_string($_POST['percentage_text']);
				$vat_p=$con->real_escape_string($_POST['vat_p']);
				$vat_amount=$con->real_escape_string($_POST['vat_amount']);
				$vat_text=$con->real_escape_string($_POST['vat_text']);
				
			 $sql="INSERT INTO driver_invoices (invoice_no,date,driver_id,nett,percentage,percentage_amount,commission,gross,status,commission_text,percentage_text,vat_p,vat_amount,vat_text) VALUES('".$invoice_no."','".$date."','".$driver_id."','".$nett."','".$percentage."','".$percentage_amount."','".$commission."','".$gross."','2','".$commission_text."','".$percentage_text."','".$vat_p."','".$vat_amount."','".$vat_text."')"; 
			if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}
			//$invoice_no = $con->insert_id;
			
			$sql='';
			    $count=count($_POST['booking_id']);
			    for ($x = 0; $x < $count; $x++) {
				$booking_id=$con->real_escape_string($_POST['booking_id'][$x]);
				
				$date=date("Y-m-d");
				
				$sql="INSERT INTO driver_invoice_details (booking_id,invoice_no) VALUES(".$booking_id.",".$invoice_no.")";
			    if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}
				
				$sql="UPDATE driver_bookings SET invoice_status=2 WHERE booking_id='$booking_id' AND driver_id='$driver_id'";
			    if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}
				
				}//for loop",
				
				$sql="SELECT * FROM employees WHERE id=".$driver_id;
				$p_res=mysqli_query($con,$sql);
				$d_sql=mysqli_fetch_assoc($p_res);
				$driver_name=$d_sql['first_name'];
				$driver_phone=$d_sql['phone'];
				$driver_email=$d_sql['email'];
				$driver_address=$d_sql['address'];
				
			
				
				$sqld="SELECT * FROM debtors WHERE source_id=".$driver_id;
				$p_resd=mysqli_query($con,$sqld);
				$d_sqld=mysqli_fetch_assoc($p_resd);
				$d_balance=$d_sqld['balance'];
				$gross=$gross+$d_balance;
				$sql="UPDATE debtors SET balance='$gross' WHERE source_id=".$driver_id;
			    if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}
				
				
			 	echo 'success';
			 
		  ?>
