diff --git a/firebase-rules-template.json b/firebase-rules-template.json index a56afb67..61f5a134 100644 --- a/firebase-rules-template.json +++ b/firebase-rules-template.json @@ -170,6 +170,9 @@ "feeTotalGross": { ".validate": "newData.isNumber()" }, + "aircraftDataSource": { + ".validate": "newData.val() === 'manual' || newData.val() === 'registry'" + }, "lastname": { ".validate": "newData.isString() && newData.val().length <= 100 && newData.val().length > 0" }, diff --git a/functions/fees/computeArrivalFees.spec.js b/functions/fees/computeArrivalFees.spec.js index 1f4073bf..9b375893 100644 --- a/functions/fees/computeArrivalFees.spec.js +++ b/functions/fees/computeArrivalFees.spec.js @@ -119,6 +119,32 @@ describe('functions/fees/computeArrivalFees', () => { expect(update.feeVat).toBe(1.87); }); + it('prices an LSZE arrival recorded without fees (e.g. through the API)', async () => { + mockAdmin.getDatabase.mockReturnValue(makeDb({ + '/settings/landingFeesStrategy': 'lsze', + '/aircrafts/HBPIV': { mtow: 1157, category: 'Flugzeug' } + })); + const event = makeEvent({ + immatriculation: 'HBPIV', mtow: 1157, flightType: 'private', + aircraftCategory: 'Flugzeug', landingCount: 1 + }); + await _test.recomputeArrivalFees(event); + + expect(event._update).toHaveBeenCalledWith({ + aircraftDataSource: 'registry', + landingFeeSingle: 27.75, + landingFeeCode: null, + landingFeeTotal: 27.75, + goAroundFeeSingle: null, + goAroundFeeCode: null, + goAroundFeeTotal: null, + feeTotalNet: 27.75, + feeVat: 2.25, + feeRoundingDifference: 0, + feeTotalGross: 30, + }); + }); + it('prices a known registration from the registry MTOW, not the submitted one', async () => { // Submitted MTOW (500) would price at 18.50; the registry MTOW (1001) // prices at 23.13. The registry must win, and the record's declared diff --git a/functions/fees/fees.spec.js b/functions/fees/fees.spec.js index 3050096f..50a443a4 100644 --- a/functions/fees/fees.spec.js +++ b/functions/fees/fees.spec.js @@ -168,3 +168,95 @@ describe('functions/fees (lspl strategy — client parity)', () => { }); }); }); + +// Copied verbatim from src/util/landingFeeStrategies/lsze.spec.ts: the parity +// contract for LSZE (fees declared including VAT, net fee rounded to cents). +const LSZE_LANDING_FEE_CASES = [ + [0, 'Flugzeug', 18.5], + [0, 'Hubschrauber', 18.5], + [1000, 'Flugzeug', 18.5], + [1000, 'Hubschrauber', 18.5], + [1001, 'Flugzeug', 27.75], + [1001, 'Hubschrauber', 27.75], + [2000, 'Flugzeug', 27.75], + [2000, 'Hubschrauber', 27.75], + [2001, 'Flugzeug', 37], + [2001, 'Hubschrauber', 55.5], + [3000, 'Flugzeug', 37], + [3000, 'Hubschrauber', 55.5], + // > 3000 kg: CHF 20 per ton (incl. VAT) + [3001, 'Flugzeug', 74.01], + [3001, 'Hubschrauber', 74.01], + [4000, 'Flugzeug', 74.01], + [4000, 'Hubschrauber', 74.01], + [4001, 'Flugzeug', 92.51], + [4001, 'Hubschrauber', 92.51], + [5000, 'Flugzeug', 92.51], + [5000, 'Hubschrauber', 92.51], + [50000, 'Flugzeug', 925.07], + [50000, 'Hubschrauber', 925.07], +]; + +describe('functions/fees (lsze strategy — client parity)', () => { + const lsze = require('./landingFeeStrategies/lsze'); + + it.each(LSZE_LANDING_FEE_CASES)('getLandingFee(%i, %s) === %s', (mtow, aircraftCategory, expected) => { + expect(lsze.getLandingFee(mtow, undefined, undefined, aircraftCategory).fee).toBe(expected); + }); + + it('applies the same tariff to every flight type and aircraft origin', () => { + ['private', 'instruction', 'aerotow', 'glider_private_self'].forEach(flightType => { + Object.values(AircraftOrigin).forEach(origin => { + expect(lsze.getLandingFee(1157, flightType, origin, 'Flugzeug')).toEqual({ fee: 27.75 }); + }); + }); + }); + + it('has no go-around fee and 8.1% VAT', () => { + expect(lsze.getGoAroundFee(1000, 'private', AircraftOrigin.OTHER, 'Flugzeug')).toBeUndefined(); + expect(lsze.getVatRate(undefined, undefined, 'Flugzeug')).toBe(8.1); + }); + + it('computeFees: 1157 kg plane, 1 landing is CHF 30.00 gross', () => { + expect(computeFees({ + strategy: 'lsze', + mtow: 1157, + flightType: 'private', + aircraftOrigin: AircraftOrigin.OTHER, + aircraftCategory: 'Flugzeug', + landingCount: 1, + })).toEqual({ + landingFeeSingle: 27.75, + landingFeeTotal: 27.75, + feeTotalNet: 27.75, + feeVat: 2.25, // round(27.75 * 0.081, 1c) + feeRoundingDifference: 0, + feeTotalGross: 30, + }); + }); + + it('computeFees: 3 landings with a go-around', () => { + const fees = computeFees({ + strategy: 'lsze', + mtow: 900, + flightType: 'instruction', + aircraftOrigin: AircraftOrigin.CLUB, + aircraftCategory: 'Flugzeug', + landingCount: 3, + goAroundCount: 1, + }); + expect(fees.landingFeeTotal).toBe(55.5); + expect(fees.goAroundFeeTotal).toBeUndefined(); + expect(fees.feeVat).toBe(4.5); // round(55.5 * 0.081 = 4.4955, 1c) + expect(fees.feeTotalGross).toBe(60); + }); +}); + +describe('functions/fees: tariff data identical to the client', () => { + const path = require('path'); + const clientData = name => require(path.join(__dirname, '../../src/util/landingFeeStrategies', `${name}_data.json`)); + + it.each(['lspl', 'lsze'])('%s_data.json', name => { + expect(require(`./landingFeeStrategies/${name}_data.json`)).toEqual(clientData(name)); + }); +}); diff --git a/functions/fees/index.js b/functions/fees/index.js index 54daa721..098d67b9 100644 --- a/functions/fees/index.js +++ b/functions/fees/index.js @@ -6,6 +6,7 @@ // server-side; a project whose strategy is not present must not be switched to // server-owned fees. const lspl = require('./landingFeeStrategies/lspl'); +const lsze = require('./landingFeeStrategies/lsze'); const AircraftOrigin = Object.freeze({ CLUB: 'club', @@ -15,6 +16,7 @@ const AircraftOrigin = Object.freeze({ const strategies = { lspl, + lsze, }; const roundToFiveCents = (val) => Math.round(val * 20) / 20; diff --git a/functions/fees/landingFeeStrategies/lsze.js b/functions/fees/landingFeeStrategies/lsze.js new file mode 100644 index 00000000..46d0e11a --- /dev/null +++ b/functions/fees/landingFeeStrategies/lsze.js @@ -0,0 +1,39 @@ +'use strict'; + +// Server-side port of src/util/landingFeeStrategies/lsze.ts. The client keeps +// its copy for live display in the wizard; this is the authoritative version. +// Parity between the two is enforced by fees.spec.js (mirrors lsze.spec.ts). +// CAUTION: the fees in lsze_data.json are INCLUDING VAT. +const data = require('./lsze_data.json'); +const { getMtowFee } = require('./utils'); + +const VAT_RATE = 8.1; + +// Mirrors src/util/aircraftCategories.ts isHelicopter. +const HELICOPTER_CATEGORIES = ['Hubschrauber', 'Eigenbauhubschrauber']; +const isHelicopter = (aircraftCategory) => HELICOPTER_CATEGORIES.includes(aircraftCategory); + +// Over 3000 kg: CHF 20 per started ton. +const getMtowFeeHeavy = (mtow) => Math.ceil(mtow / 1000) * 20; + +const getFee = (mtow, flightType, aircraftOrigin, aircraftCategory) => { + if (mtow > 3000) { + return getMtowFeeHeavy(mtow); + } + return getMtowFee(isHelicopter(aircraftCategory) ? data.fees.helicopter : data.fees.plane, mtow); +}; + +const getVatRate = () => VAT_RATE; + +const getLandingFee = (mtow, flightType, aircraftOrigin, aircraftCategory) => { + const fee = getFee(mtow, flightType, aircraftOrigin, aircraftCategory); + if (fee) { + const netFee = fee / (1 + getVatRate(flightType, aircraftOrigin, aircraftCategory) / 100); + return { fee: Math.round(netFee * 100) / 100 }; + } + return undefined; +}; + +const getGoAroundFee = () => undefined; + +module.exports = { getLandingFee, getGoAroundFee, getVatRate }; diff --git a/functions/fees/landingFeeStrategies/lsze_data.json b/functions/fees/landingFeeStrategies/lsze_data.json new file mode 100644 index 00000000..fa6913bf --- /dev/null +++ b/functions/fees/landingFeeStrategies/lsze_data.json @@ -0,0 +1,14 @@ +{ + "fees": { + "plane": [ + { "max_weight": 1000, "fee": 20 }, + { "max_weight": 2000, "fee": 30 }, + { "max_weight": 3000, "fee": 40 } + ], + "helicopter": [ + { "max_weight": 1000, "fee": 20 }, + { "max_weight": 2000, "fee": 30 }, + { "max_weight": 3000, "fee": 60 } + ] + } +} diff --git a/functions/movements/domain/testing/movementCases.js b/functions/movements/domain/testing/movementCases.js index 9b5aeccd..a3ebfa2f 100644 --- a/functions/movements/domain/testing/movementCases.js +++ b/functions/movements/domain/testing/movementCases.js @@ -356,6 +356,8 @@ function departureOnlyCases(policy) { invalid(['paymentMethod', REASONS.UNKNOWN_FIELD])); create('landingCount is not a departure field', { landingCount: 1 }, invalid(['landingCount', REASONS.UNKNOWN_FIELD])); + create('aircraftDataSource is not a departure field', { aircraftDataSource: 'registry' }, + invalid(['aircraftDataSource', REASONS.UNKNOWN_FIELD])); return cases; } @@ -394,6 +396,13 @@ function arrivalOnlyCases(policy) { create('duration is not an arrival field', { duration: '01:00' }, invalid(['duration', REASONS.UNKNOWN_FIELD])); create('route is not an arrival field', { route: 'via VOR' }, invalid(['route', REASONS.UNKNOWN_FIELD])); + // Written by the server-side fee computation; the app sends it back. + create('aircraftDataSource registry', { aircraftDataSource: 'registry' }, accepted()); + create('aircraftDataSource manual', { aircraftDataSource: 'manual' }, accepted()); + create('aircraftDataSource unknown value', { aircraftDataSource: 'guessed' }, + invalid(['aircraftDataSource', REASONS.NOT_ALLOWED])); + create('aircraftDataSource as a number', { aircraftDataSource: 1 }, + invalid(['aircraftDataSource', REASONS.NOT_ALLOWED])); return cases; } @@ -434,6 +443,14 @@ function updateCases(type, policy) { { remarks: 'x', negativeTimestamp: -1, createdBy: 'pilot@example.com', createdBy_orderKey: 'forged' }, accepted()); update('with an invalid field', {}, { mtow: 0 }, invalid(['mtow', REASONS.MUST_BE_POSITIVE])); update('with an unknown field', {}, { foo: 1 }, invalid(['foo', REASONS.UNKNOWN_FIELD])); + if (type === 'arrival') { + // An arrival priced by the server, edited in the app: the whole movement + // comes back, with the server's fee fields and data source. + const priced = { aircraftDataSource: 'registry', landingFeeSingle: 27.75, landingFeeTotal: 27.75, + feeTotalNet: 27.75, feeVat: 2.25, feeRoundingDifference: 0, feeTotalGross: 30 }; + update('of an arrival priced by the server, as the app sends it', priced, + { ...inputFor(type, policy, { remarks: 'x' }), ...priced }, accepted()); + } update('with an own __proto__ key', {}, withOwnProto({ remarks: 'x' }, type === 'departure' ? { carriageVoucher: 'garbage' } : { paymentMethod: { status: 'pending' } }), diff --git a/functions/movements/domain/validateMovement.js b/functions/movements/domain/validateMovement.js index 32264531..84353c61 100644 --- a/functions/movements/domain/validateMovement.js +++ b/functions/movements/domain/validateMovement.js @@ -23,6 +23,9 @@ const PAYMENT_METHODS = Object.freeze([ const CIRCUITS = 'circuits'; +// Where the server-side fee computation took MTOW and category from. +const AIRCRAFT_DATA_SOURCES = Object.freeze(['manual', 'registry']); + // Realtime Database keys: non-empty, at most 768 UTF-8 bytes, none of // . $ # [ ] / and no ASCII control characters. The SDKs refuse such keys // before the rules run; the Admin SDK writer must not get them either. @@ -220,6 +223,9 @@ const SCHEMAS = { feeVat: anyNumber, feeRoundingDifference: anyNumber, feeTotalGross: anyNumber, + // Set by the server-side fee computation (functions/fees); the app + // sends it back unchanged when an arrival is edited. + aircraftDataSource: oneOf(() => AIRCRAFT_DATA_SOURCES), paymentMethod, }, }, diff --git a/projects/Configuration.md b/projects/Configuration.md index 2715de02..bff1d019 100644 --- a/projects/Configuration.md +++ b/projects/Configuration.md @@ -182,6 +182,18 @@ Configuration: Same structure for the go arounds as for the landings. If no go around fees are defined, the pilots won't be charged for go arounds. +#### `landingFeesStrategy` + +The tariff code in `src/util/landingFeeStrategies/.ts` (e.g. `lsze`), used instead of `landingFees` and +`goAroundFees`. The app computes the fees while the pilot records an arrival and saves them with it. + +Arrivals recorded without fees (e.g. through the movement API by the Open Digital MCP hub) are priced by the server +when the database holds the strategy under `/settings/landingFeesStrategy` (set it in the Firebase console). The +function `computeArrivalFeesOnWrite` then recomputes the fee fields of every arrival that is written, from the app or +the API, with MTOW and category from the aircraft register where the registration is known, and records where they came +from in `aircraftDataSource` (`registry` or `manual`). Only strategies ported to `functions/fees/landingFeeStrategies` +can be used this way: `lspl` and `lsze`. Arrivals stored before are priced at their next change. + ### `environments` Description of the environments. Each environment description consists of the following properties: