<?php
		  	
			  include "../dbconfig.php";
			  
			  $x=0;
			  $booking_id=0;
			  $count=0;
			  $invoice_no=$con->real_escape_string($_POST['invoice_no']);
			   $invoice_no1=$con->real_escape_string($_POST['invoice_no1']);
			    $nett=$con->real_escape_string($_POST['nett']);
				$date=$con->real_escape_string($_POST['date']);
				$vat_p=$con->real_escape_string($_POST['vat_p']);
				$vat=$con->real_escape_string($_POST['vat']);
				
				$date=$con->real_escape_string($_POST['date']);
				$date = str_replace("/","-",$date);
	  $date = date("Y-m-d", strtotime($date));
				
				$gross=$con->real_escape_string($_POST['gross']);
				$company_id=$con->real_escape_string($_POST['company_id']);
				if($vat!='0.00' && $vat!='0' && $vat!='')
					  {
			 $sql="INSERT INTO invoices (invoice_no,date,customer_id,nett,vat,gross,status) VALUES('".$invoice_no."','".$date."','".$company_id."','".$nett."','".$vat."','".$gross."','2')"; 
			//echo $sql;
			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 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 bookings SET invoice_status=2 WHERE id=".$booking_id;
			    if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}
				
				}//for loop",
				}
				else
				{
				
				
				$sql="INSERT INTO invoices_wings_courier (invoice_no,date,customer_id,nett,vat,gross,status) VALUES('".$invoice_no1."','".$date."','".$company_id."','".$nett."','".$vat."','".$gross."','2')"; 
			//echo $sql;
			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 invoice_details_wings_courier (booking_id,invoice_no) VALUES(".$booking_id.",".$invoice_no1.")";
			    if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}
				
				$sql="UPDATE bookings SET invoice_status=2 WHERE id=".$booking_id;
			    if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}
				
				}//for loop",
				
				}
				
				
				
				
				
				
				$sqld="SELECT * FROM creditors WHERE source_id=".$company_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 creditors SET balance='$gross' WHERE source_id=".$company_id;
			    if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}
				
			 	echo 'success';
			 
		  ?>
