Finally populated the Flight Fares table

Anything and everything related to the airline
Post Reply
User avatar
jonbeckett
Site Admin
Posts: 56
Joined: Thu Jun 19, 2025 12:43 pm
Contact:

After a bit of head scratching this evening, I ran a database update to populate the flight fare table behind the airline. This closes a loophole that's existed forever in PHPVMS - where it charges a flat rate for seats on flights, regardless of their length.

The update implements independent fares for each flight, based on the anticipated duration of the flight, multiplied by a factor for each seating class (50 per hour for economy, 100 for business, 200 for first class).

Here's the SQL, if anybody is interested:

Code: Select all

INSERT INTO flight_fare (flight_id, fare_id, price, cost, capacity, created_at, updated_at)
SELECT 
    f.id as flight_id,
    fare.id as fare_id,
    CASE 
        WHEN fare.code = 'Y' THEN (f.flight_time / 60.0) * 50
        WHEN fare.code = 'B' THEN (f.flight_time / 60.0) * 100
        WHEN fare.code = 'F' THEN (f.flight_time / 60.0) * 200
    END as price,
    fare.cost,
    fare.capacity,
    NOW() as created_at,
    NOW() as updated_at
FROM flights f
CROSS JOIN fares fare
WHERE fare.code IN ('Y', 'B', 'F')
  AND fare.type = 0
  AND f.flight_time IS NOT NULL
  AND f.flight_time > 0
  AND f.flight_number >= 1000
  AND NOT EXISTS (
    SELECT 1 FROM flight_fare ff 
    WHERE ff.flight_id = f.id AND ff.fare_id = fare.id
  );
I have only implemented it for flights with a flight number > 1000 to avoid flights created by the "free flight" functionality.

The result of this? You'll actually make profit on long-haul flights.

Oh - and I multiplied the rates for any "Virtual Flight Business" flights (the business jets) to 10x the commercial rates - so it's 500 an hour economy for a seat on a Learjet, for example.

Code: Select all

UPDATE flight_fare ff
INNER JOIN flights f ON ff.flight_id = f.id
INNER JOIN airlines a ON f.airline_id = a.id
SET ff.price = ff.price * 10
WHERE a.name = 'VFO Business';
User avatar
jonbeckett
Site Admin
Posts: 56
Joined: Thu Jun 19, 2025 12:43 pm
Contact:

The best thing about the above? If we add new aircraft, we can just dump the flight_fares table content, and re-populate it by re-running the queries again.
Post Reply