<?php
  //require_once('./config.php');
require_once('config_courier.php');
include "dbconfig.php";
error_reporting(0); 
 date_default_timezone_set ("Europe/London");

  $token  = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$stripe_amount = $_POST['stripe_amount'];
$stripe_amount = $stripe_amount * 100;

  $customer = \Stripe\Customer::create(array(
      'email' => $email,
      'source'  => $token
  ));

  $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $stripe_amount,
      'currency' => 'gbp'
  ));

$cop=$_POST['cop'];
$invoice_no=$_POST['invoice_no'];


if($cop=="wings")
  {
  $sql="SELECT DISTINCT 
invoices.invoice_no,invoices.date as invoice_date,

bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.mins,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,
bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time,bookings.other,bookings.parking,bookings.transfer,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,customers.email as email,customers.address1,customers.post_code,customers.email,customers.id as company_id,

company_contact.name as passenger,

employees.first_name AS driver,vehicles.reg as reg, vehicle_make.make as make,
invoices.*
FROM `invoice_details` 
INNER JOIN bookings on invoice_details.booking_id=bookings.id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees ON bookings.driver_id=employees.id
INNER JOIN vehicles ON vehicles.driver_id=employees.id
LEFT JOIN vehicle_make ON vehicle_make.id=bookings.vehicle_id
INNER JOIN invoices on invoice_details.invoice_no=invoices.invoice_no
WHERE invoices.invoice_no='$invoice_no'

ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC
";
}
elseif($cop=="courier")
{

 $sql="SELECT DISTINCT 
invoices_wings_courier.invoice_no,invoices_wings_courier.date as invoice_date,

bookings.pickup_date,bookings.id as booking_id,bookings.hours,bookings.mins,bookings.pickup_address,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,
bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time,bookings.other,bookings.parking,bookings.transfer,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate,
customers.name as company,customers.address1,customers.post_code,customers.email,customers.id as company_id,

company_contact.name as passenger,

employees.first_name AS driver,vehicles.reg as reg, vehicle_make.make as make,
invoices_wings_courier.*
FROM `invoice_details_wings_courier` 
INNER JOIN bookings on invoice_details_wings_courier.booking_id=bookings.id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees ON bookings.driver_id=employees.id
INNER JOIN vehicles ON vehicles.driver_id=employees.id
LEFT JOIN vehicle_make ON vehicle_make.id=bookings.vehicle_id
INNER JOIN invoices_wings_courier on invoice_details_wings_courier.invoice_no=invoices_wings_courier.invoice_no
WHERE invoices_wings_courier.invoice_no=$invoice_no

ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC
";

}
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);
$client=mysqli_query($con,$sql);
$invoice=mysqli_fetch_assoc($client);
//echo $invoice['gross'];
$total=0;
				$id=1;
				$total_gross=0;
				$vat=0;
				$gross=0;
				$nett=0;
				$count=0;
				$company_id=0;
                while($job=mysqli_fetch_assoc($res))
				
                  {
					 
					 $booking_id = $job['booking_id'];
					 $pickup_date = date("d/m/Y",strtotime($job['pickup_date']));
					 $time = $job['hours'].":".$job['mins'];
	 				 $passenger=$job['passenger'];
					 $company=$job['company'];
					 $reg=$job['reg'];
					 
					 $make=$job['make'];
					 
					 $driver=$job['driver'];
					 $start_date=$job['start_date'];
					  $end_date=$job['end_date'];
					 $parking=$job['parking'];
					  $transfer=$job['transfer'];
					 $rate=$job['rate'];
			    	 $days=$job['days'];
					 $total_fare=$job['total_fare']; 
					 $total_gross=$total_gross+$job['total_fare'];
					 $company_id = $job['company_id'];
					 $total=$total+$job['nett'];
					 $nett=$job['nett'];
					 $vat=$job['vat'];
					 $gross=$job['gross'];
					 $count++;

               

                  }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$amount=$gross;//$_POST['amount'];
	
	$source_id=$company_id;//$_POST['source_id'];
	
	//$transaction_date=$_POST['transaction_date']; 
	$transaction_date=date("Y-m-d",strtotime(date("Y-m-d")));
	
	$payment_type='cr';//$_POST['payment_type'];
	$mode_of_payment="Card";//$_POST['mode_of_payment'];
	$details="";//$_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;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   echo '<body bgcolor="#f9f9f9"><br><br><br><br><br><br><br><br>
 <div align="center" style="width:60%; margin-left:20%; padding:2%; background:#FFFFFF;   box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);">
    <h1 align="center"  style="color:#666666; font-family:Arial, Helvetica, sans-serif;"><img src="check-mark-1292787_960_720.png" width="60"> Your Transaction has been placed ! </h1><br>
	<h1 align="center"  style="color:#666666; font-family:Arial, Helvetica, sans-serif;">Thank you !</h1><br><br>';
	 echo '<h1 align="center" style=" color:#009900; font-family:Arial, Helvetica, sans-serif;">Successfully charged &pound;'.$gross.'!</h1></div></body>';
?>