mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
23b7f6d71c
A rail, bus or ferry ticket with changes arrived as one booking per segment, each with a linked cost for the full fare, so the trip total was the fare times the number of segments. Import: the KItinerary mapper now groups consecutive train segments under one booking reference that connect (same station by name or within 500 m, no return, within a day) into one train booking with stops and metadata.legs, the way connecting flights already were. The connection rule is shared between flights and ground transport, and a stop carries the onward departure for flights as for trains, as the planner form and the MCP tools write it. The three copied ground mappers are folded into one. Within one such booking a fare repeated on every segment is booked once, and fares that differ per segment are added in whole cents. That summing now applies to connecting flights too, which used to keep the first leg's price only. Segments of one order that do not join into one booking (an outward and a return trip, a gap, a change of station named two ways without coordinates, bus and boat tickets, Rail&Fly) are compared across the document: when every priced transport booking of a reference carries the same amount in the same currency, the first to depart keeps it and the others lose it. Amounts that differ stay. This runs on what KItinerary extracted only. AI import: the repeated-total safety net from #2651 now covers train, bus and ferry tickets and any mix of them with flights, still only when the document prints that amount on a total line. MCP and assistant: create_transport says that a ticket with several segments is one booking with endpoints[] and legs[], and the price fields say they hold the whole booking's cost. When a new booking repeats the price an earlier booking with the same confirmation number already carries in the trip's currency (its own currency, or the trip's when it names none), create_transport and create_reservation still save it and return a warning naming the earlier booking. REST is unchanged. Existing duplicated costs are left alone: they cannot be told apart from two tickets that really cost the same, and deleting expenses automatically would change settled balances. They can be removed in the Costs tab. Closes #2895
87 lines
3.8 KiB
TypeScript
87 lines
3.8 KiB
TypeScript
import { parseAmount } from './clients/nuextract';
|
|
import { toMinor } from '@trek/shared';
|
|
|
|
type Node = Record<string, unknown>;
|
|
|
|
/** Lines that state a sum rather than a fare per leg or a seat surcharge. */
|
|
const TOTAL_LINE = /total|gesamt|summe|betrag|amount|paid|bezahlt/i;
|
|
/** One number as printed: digits with their grouping and decimal marks, never two numbers glued together. */
|
|
const NUMBER = /\d[\d.,]*/g;
|
|
|
|
/**
|
|
* How many lines of the document print this amount (in hundredths), and whether
|
|
* one of them is a total line. Printed once per leg it is a fare per leg, even
|
|
* on lines labelled "Amount", not one total the model repeated.
|
|
*/
|
|
function amountLines(text: string, minor: number): { lines: number; onTotal: boolean } {
|
|
let lines = 0;
|
|
let onTotal = false;
|
|
for (const line of text.split('\n')) {
|
|
const printed = (line.match(NUMBER) ?? []).some((token) => {
|
|
const amount = parseAmount(token);
|
|
return amount != null && toMinor(amount) === minor;
|
|
});
|
|
if (!printed) continue;
|
|
lines += 1;
|
|
if (TOTAL_LINE.test(line)) onTotal = true;
|
|
}
|
|
return { lines, onTotal };
|
|
}
|
|
|
|
const reference = (node: Node) =>
|
|
String(node.reservationNumber ?? '')
|
|
.trim()
|
|
.toUpperCase();
|
|
|
|
const departure = (node: Node) => {
|
|
const time = (node.reservationFor as { departureTime?: unknown } | undefined)?.departureTime;
|
|
return typeof time === 'string' ? time : '';
|
|
};
|
|
|
|
const priceOf = (node: Node): number | null => {
|
|
const amount = parseAmount(node.price);
|
|
return amount != null && amount > 0 ? toMinor(amount) : null;
|
|
};
|
|
|
|
/** The reservations a ticket with several segments is read as, one node per segment. */
|
|
const SEGMENT_TYPES = new Set(['FlightReservation', 'TrainReservation', 'BusReservation', 'BoatReservation']);
|
|
|
|
/**
|
|
* A model reading a round trip with one total for the whole booking puts that
|
|
* total on every flight, and saving both legs then books the purchase twice
|
|
* (#2651). A rail, bus or ferry ticket with changes reads the same way, one
|
|
* node per segment with the ticket's total on each (#2895). When the segments
|
|
* plainly share it, the price stays on the first segment to depart and leaves
|
|
* the others.
|
|
*
|
|
* Plainly means: transport segments only, of any mix of kinds (Rail&Fly, a
|
|
* train with a replacement bus), all on one booking reference, at least two of
|
|
* them priced, every price the same amount in the same currency, and the
|
|
* document printing that amount on a total line, on fewer lines than there are
|
|
* priced segments. Fares that differ, separate references, mixed currencies,
|
|
* other booking types, or no document text to check against (a PDF read
|
|
* natively, a photo) leave the nodes as they are.
|
|
*/
|
|
export function collapseRepeatedBookingTotal(nodes: Node[], documentText: string | undefined): Node[] {
|
|
if (!documentText || nodes.length < 2) return nodes;
|
|
if (!nodes.every((node) => typeof node['@type'] === 'string' && SEGMENT_TYPES.has(node['@type']))) return nodes;
|
|
const ref = reference(nodes[0]);
|
|
if (!ref || !nodes.every((node) => reference(node) === ref)) return nodes;
|
|
|
|
const priced = nodes.filter((node) => priceOf(node) != null);
|
|
const minor = priced.length >= 2 ? priceOf(priced[0]) : null;
|
|
if (minor == null) return nodes;
|
|
const currency = priced[0].priceCurrency;
|
|
if (!priced.every((node) => priceOf(node) === minor && node.priceCurrency === currency)) return nodes;
|
|
const printed = amountLines(documentText, minor);
|
|
if (!printed.onTotal || printed.lines >= priced.length) return nodes;
|
|
|
|
// The first to depart keeps it; a tie keeps the order the model gave.
|
|
const keeper = priced.reduce((first, node) => (departure(node) < departure(first) ? node : first), priced[0]);
|
|
return nodes.map((node) => {
|
|
if (node === keeper || priceOf(node) == null) return node;
|
|
const { price: _price, priceCurrency: _currency, ...rest } = node;
|
|
return rest;
|
|
});
|
|
}
|