Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
301 lines
16 KiB
TypeScript
301 lines
16 KiB
TypeScript
// GAEB lesen: DA XML 3.x (.x81–.x86), GAEB 2000 (.p81–.p86) und GAEB 90 (.d81–.d86). Leistungsverzeichnis mit
|
||
// Titeln und Positionen (OZ, Kurztext, Langtext, Menge, Einheit, Einheitspreis, Positionsart), dazu Phase,
|
||
// Auftraggeber und Vortexte. Daraus werden Leistungen der Stammdaten oder ein Dokument im Projekt (Henning 22.10.2026).
|
||
import { type XmlElement, absatzText, elemente, kind, leseXml, pfad, textVon } from "./xml.js";
|
||
import { einheitRichtig } from "./kalkulation.js";
|
||
|
||
export interface GaebPosition {
|
||
oz: string;
|
||
/** Titelpfad, z. B. „03 Konstruktionen / 03.01 Dachstuhl“ */
|
||
ordner: string;
|
||
kurztext: string;
|
||
langtext: string;
|
||
einheit: string;
|
||
menge: number | null;
|
||
/** Einheitspreis in Cent (nur Dateien mit Preisen, z. B. .x84/.x86) */
|
||
ep: number | null;
|
||
art: "normal" | "eventual" | "eventual_summe" | "alternativ";
|
||
/** OZ des innersten Titels, zu dem die Position gehört */
|
||
titelOz: string | null;
|
||
}
|
||
|
||
export type GaebFormat = "GAEB DA XML" | "GAEB 2000" | "GAEB 90";
|
||
|
||
export interface GaebAdresse { name: string; strasse: string | null; plz: string | null; ort: string | null }
|
||
|
||
export interface GaebLv {
|
||
name: string;
|
||
/** Datenaustauschphase, z. B. 81, 83, 84 */
|
||
phase: string | null;
|
||
positionen: GaebPosition[];
|
||
titel: number;
|
||
format: GaebFormat;
|
||
version: string | null;
|
||
titelListe: { oz: string; bezeichnung: string }[];
|
||
auftraggeber: GaebAdresse | null;
|
||
/** Vorbemerkungen, z. B. ZTV, als Text */
|
||
vortexte: string;
|
||
/** Bauvorhaben laut Datei */
|
||
projekt: string | null;
|
||
}
|
||
|
||
export const GAEB_PHASE: Record<string, string> = {
|
||
"81": "Leistungsbeschreibung", "82": "Kostenansatz", "83": "Angebotsaufforderung", "84": "Angebotsabgabe", "85": "Nebenangebot", "86": "Auftragserteilung",
|
||
};
|
||
|
||
/** Einheiten aus GAEB (m2, Stck, lfm …) wie im Programm */
|
||
export function gaebEinheit(qu: string): string {
|
||
const t = qu.trim();
|
||
const klein = t.toLowerCase().replace(/\.$/, "");
|
||
const tabelle: Record<string, string> = {
|
||
m2: "m²", qm: "m²", "m²": "m²", m3: "m³", cbm: "m³", "m³": "m³", m: "m", lfm: "m", lm: "m",
|
||
st: "St", stk: "St", stck: "St", stück: "St", "stk.": "St", psch: "psch", pauschal: "psch", pausch: "psch",
|
||
h: "h", std: "h", kg: "kg", t: "t",
|
||
};
|
||
return tabelle[klein] ?? einheitRichtig(t) ?? t;
|
||
}
|
||
|
||
const zahl = (t: string | undefined) => {
|
||
if (!t?.trim()) return null;
|
||
const n = Number(t.trim().replace(",", "."));
|
||
return Number.isFinite(n) ? n : null;
|
||
};
|
||
|
||
/** Welches GAEB-Format: am Inhalt erkannt, die Endung hilft nur bei GAEB 90 */
|
||
export function gaebFormat(text: string, dateiname = ""): GaebFormat | null {
|
||
const anfang = text.slice(0, 5000);
|
||
if (/<\s*([\w-]+:)?GAEB[\s>]/.test(anfang)) return "GAEB DA XML";
|
||
if (/#begin\[GAEB\]/i.test(anfang)) return "GAEB 2000";
|
||
const zeilen = anfang.split(/\r?\n/).filter((z) => z.trim());
|
||
if (/\.d8\d$/i.test(dateiname) || (zeilen[0]?.startsWith("00") && zeilen.slice(0, 5).every((z) => /^[0-9T][0-9]/.test(z) && z.length >= 70 && z.length <= 82))) return "GAEB 90";
|
||
return null;
|
||
}
|
||
|
||
/** Jede GAEB-Datei: X8x, P8x oder D8x */
|
||
export function leseGaebDatei(text: string, dateiname = ""): GaebLv {
|
||
const f = gaebFormat(text, dateiname);
|
||
if (f === "GAEB 2000") return leseGaeb2000(text);
|
||
if (f === "GAEB 90") return leseGaeb90(text, dateiname);
|
||
if (f === "GAEB DA XML") return leseGaeb(text);
|
||
throw new Error("Das ist keine GAEB-Datei. Gelesen werden GAEB DA XML (.x81–.x86), GAEB 2000 (.p81–.p86) und GAEB 90 (.d81–.d86).");
|
||
}
|
||
|
||
/** Phase aus der Datei, sonst aus der Endung (.x83, .p84, .d86) */
|
||
const phaseVon = (inDatei: string | null | undefined, dateiname: string) => (inDatei?.match(/8[1-6]/)?.[0]) ?? /\.[xpd](8[1-6])$/i.exec(dateiname)?.[1] ?? null;
|
||
|
||
export function leseGaeb(text: string): GaebLv {
|
||
if (!/<\s*([\w-]+:)?GAEB[\s>]/.test(text.slice(0, 5000))) {
|
||
throw new Error("Das ist keine GAEB-XML-Datei. Bitte das Leistungsverzeichnis als GAEB DA XML exportieren (Endung .x81, .x83 oder .x84).");
|
||
}
|
||
const gaeb = leseXml(text);
|
||
const award = kind(gaeb, "Award");
|
||
const boq = kind(award, "BoQ");
|
||
if (!boq) throw new Error("In der GAEB-Datei steht kein Leistungsverzeichnis (BoQ).");
|
||
const name = textVon(pfad(boq, "BoQInfo", "LblBoQ")) || textVon(pfad(boq, "BoQInfo", "Name")) || textVon(pfad(gaeb, "PrjInfo", "LblPrj")) || textVon(pfad(gaeb, "PrjInfo", "NamePrj")) || "GAEB";
|
||
const phase = textVon(kind(award, "DP")) || null;
|
||
const positionen: GaebPosition[] = [];
|
||
const titelListe: GaebLv["titelListe"] = [];
|
||
let titel = 0;
|
||
|
||
const koerper = (body: XmlElement | undefined, oz: string[], ordner: string[]) => {
|
||
if (!body) return;
|
||
for (const c of elemente(body, "BoQCtgy")) {
|
||
titel++;
|
||
const nr = c.attr.RNoPart ?? "";
|
||
const bez = absatzText(kind(c, "LblTx")).split("\n")[0] || "Titel";
|
||
const ozNeu = [...oz, nr].filter(Boolean);
|
||
titelListe.push({ oz: ozNeu.join("."), bezeichnung: bez });
|
||
koerper(kind(c, "BoQBody"), ozNeu, [...ordner, [ozNeu.join("."), bez].filter(Boolean).join(" ")]);
|
||
}
|
||
for (const liste of elemente(body, "Itemlist")) {
|
||
for (const it of elemente(liste, "Item")) {
|
||
const ct = pfad(it, "Description", "CompleteText");
|
||
const kurz = absatzText(pfad(ct, "OutlineText", "OutlTxt", "TextOutlTxt")) || absatzText(pfad(ct, "OutlineText", "OutlTxt"));
|
||
const lang = absatzText(pfad(ct, "DetailTxt", "Text")) || absatzText(kind(ct, "DetailTxt"));
|
||
const provis = textVon(kind(it, "Provis"));
|
||
const titelOz = oz.length ? oz.join(".") : null;
|
||
const ozPos = [...oz, it.attr.RNoPart ?? ""].filter(Boolean).join(".");
|
||
const up = zahl(textVon(kind(it, "UP")));
|
||
positionen.push({
|
||
oz: ozPos,
|
||
ordner: ordner.join(" / "),
|
||
kurztext: (kurz || lang.split("\n")[0] || `Position ${ozPos}`).replace(/\n/g, " ").slice(0, 300),
|
||
langtext: lang,
|
||
einheit: gaebEinheit(textVon(kind(it, "QU"))),
|
||
menge: zahl(textVon(kind(it, "Qty"))),
|
||
ep: up === null ? null : Math.round(up * 100),
|
||
art: provis === "WithTotal" ? "eventual_summe" : provis ? "eventual" : kind(it, "ALNSerNo") && textVon(kind(it, "ALNSerNo")) !== "0" ? "alternativ" : "normal",
|
||
titelOz,
|
||
});
|
||
}
|
||
}
|
||
};
|
||
koerper(kind(boq, "BoQBody"), [], []);
|
||
if (!positionen.length) throw new Error("In der GAEB-Datei stehen keine Positionen.");
|
||
const adr = pfad(award, "OWN", "Address");
|
||
const auftraggeber = adr ? adresse([textVon(kind(adr, "Name1")), textVon(kind(adr, "Name2"))], textVon(kind(adr, "Street")), textVon(kind(adr, "PCode")), textVon(kind(adr, "City"))) : null;
|
||
const vortexte = [...(award ? elemente(award, "AddText") : []), ...elemente(boq, "AddText")]
|
||
.map((t) => [absatzText(kind(t, "OutlineAddText")), absatzText(kind(t, "DetailAddText"))].filter(Boolean).join("\n")).filter(Boolean).join("\n\n");
|
||
return { name, phase, positionen, titel, format: "GAEB DA XML", version: textVon(pfad(gaeb, "GAEBInfo", "Version")) || null, titelListe, auftraggeber, vortexte,
|
||
projekt: [textVon(pfad(gaeb, "PrjInfo", "LblPrj")), textVon(pfad(gaeb, "PrjInfo", "NamePrj"))].find((x) => x && !/^\d+$/.test(x)) || null };
|
||
}
|
||
|
||
function adresse(namen: (string | undefined)[], strasse?: string, plz?: string, ort?: string): GaebAdresse | null {
|
||
const name = namen.map((n) => n?.replace(/[\s,]+$/, "").trim()).filter(Boolean).join(" ");
|
||
if (!name) return null;
|
||
return { name, strasse: strasse?.trim() || null, plz: plz?.trim() || null, ort: ort?.trim() || null };
|
||
}
|
||
|
||
// ---------- GAEB 2000 (.p8x): #begin[Name] … #end[Name] mit [Feld]Wert[end] ----------
|
||
|
||
interface Knoten { name: string; kinder: Knoten[]; werte: [string, string][] }
|
||
|
||
function baum2000(text: string): Knoten {
|
||
const wurzel: Knoten = { name: "", kinder: [], werte: [] };
|
||
const stapel = [wurzel];
|
||
const re = /#begin\[([^\]]+)\]|#end\[([^\]]+)\]|\[([^\]\r\n]+)\]([\s\S]*?)\[end\]/gi;
|
||
for (const m of text.matchAll(re)) {
|
||
const oben = stapel.at(-1)!;
|
||
if (m[1]) { const k: Knoten = { name: m[1], kinder: [], werte: [] }; oben.kinder.push(k); stapel.push(k); }
|
||
else if (m[2]) { if (stapel.length > 1) stapel.pop(); }
|
||
else oben.werte.push([m[3]!, m[4]!.replace(/\r/g, "").replace(/\s*\n\s*/g, "\n").trim()]);
|
||
}
|
||
return wurzel;
|
||
}
|
||
const wert = (k: Knoten | undefined, ...namen: string[]) => k?.werte.find(([n]) => namen.some((x) => x.toLowerCase() === n.toLowerCase()))?.[1];
|
||
/** Wert auch in Unterknoten (z. B. Beschreibung → Kurztext) */
|
||
const wertTief = (k: Knoten | undefined, ...namen: string[]): string | undefined => {
|
||
if (!k) return undefined;
|
||
const hier = wert(k, ...namen);
|
||
if (hier !== undefined) return hier;
|
||
for (const x of k.kinder) { const w = wertTief(x, ...namen); if (w !== undefined) return w; }
|
||
return undefined;
|
||
};
|
||
const knoten = (k: Knoten | undefined, name: string) => k?.kinder.find((x) => x.name.toLowerCase() === name.toLowerCase());
|
||
/** alle Werte unter einem Knoten, als Absätze */
|
||
const textUnter = (k: Knoten | undefined): string => (k ? [...k.werte.map(([, v]) => v), ...k.kinder.map(textUnter)].filter(Boolean).join("\n") : "");
|
||
const suche = (k: Knoten, name: RegExp): Knoten[] => k.kinder.flatMap((x) => (name.test(x.name) ? [x] : suche(x, name)));
|
||
const ozTeile = (oz: string | undefined) => (oz ?? "").trim().split(/[\s.]+/).filter(Boolean);
|
||
const zahl2000 = (t: string | undefined) => (t ? zahl(t.includes(",") ? t.replace(/\./g, "") : t) : null);
|
||
|
||
export function leseGaeb2000(text: string): GaebLv {
|
||
const w = baum2000(text);
|
||
const gaeb = knoten(w, "GAEB");
|
||
if (!gaeb) throw new Error("Das ist keine GAEB-2000-Datei.");
|
||
const vergabe = knoten(gaeb, "Vergabe");
|
||
const lv = knoten(vergabe, "LV");
|
||
if (!lv) throw new Error("In der GAEB-Datei steht kein Leistungsverzeichnis (LV).");
|
||
const positionen: GaebPosition[] = [];
|
||
const titelListe: GaebLv["titelListe"] = [];
|
||
const bereich = (k: Knoten, oz: string[], ordner: string[]) => {
|
||
for (const c of k.kinder) {
|
||
if (/^LVBereich$/i.test(c.name)) {
|
||
const ozNeu = ozTeile(wert(c, "OZ"));
|
||
const bez = (wert(c, "BezTx", "Bez", "Bezeichnung") ?? textUnter(knoten(c, "Bez")) ?? "").split("\n")[0] || "Titel";
|
||
const z = ozNeu.length ? ozNeu : [...oz, String(titelListe.length + 1)];
|
||
titelListe.push({ oz: z.join("."), bezeichnung: bez });
|
||
bereich(c, z, [...ordner, `${z.join(".")} ${bez}`]);
|
||
} else if (/^Position$/i.test(c.name)) {
|
||
const ozPos = ozTeile(wert(c, "OZ")).join(".");
|
||
const kurzK = suche(c, /^(Kurztext|KurzTx|TextKurz)$/i)[0];
|
||
const langK = suche(c, /^(Langtext|LangTx|TextLang)$/i)[0];
|
||
const kurz = wertTief(c, "Kurztext", "KurzTx", "Stichwort") ?? textUnter(kurzK);
|
||
const lang = wertTief(c, "Langtext", "LangTx") ?? textUnter(langK);
|
||
const ep = zahl2000(wert(c, "EP"));
|
||
const bedarf = (wert(c, "Bedarf") ?? "").trim().toUpperCase();
|
||
positionen.push({
|
||
oz: ozPos || `${oz.join(".")}.${positionen.length + 1}`,
|
||
ordner: ordner.join(" / "),
|
||
kurztext: (kurz || lang.split("\n")[0] || `Position ${ozPos}`).replace(/\n/g, " ").slice(0, 300),
|
||
langtext: lang,
|
||
einheit: gaebEinheit(wert(c, "ME", "Einheit") ?? ""),
|
||
menge: zahl2000(wert(c, "Menge")),
|
||
ep: ep === null || (bedarf && ep === 0) ? null : Math.round(ep * 100),
|
||
art: bedarf === "M" ? "eventual_summe" : bedarf ? "eventual" : (wert(c, "Wahl", "Alternativ") ?? "").trim() && wert(c, "Wahl", "Alternativ") !== "0" ? "alternativ" : "normal",
|
||
titelOz: oz.length ? oz.join(".") : null,
|
||
});
|
||
}
|
||
}
|
||
};
|
||
bereich(lv, [], []);
|
||
if (!positionen.length) throw new Error("In der GAEB-Datei stehen keine Positionen.");
|
||
const ag = knoten(knoten(vergabe, "AG"), "Adresse");
|
||
const lvInfo = knoten(lv, "LVInfo");
|
||
return {
|
||
name: wert(lvInfo, "Name", "Bez") ?? wert(knoten(gaeb, "PrjInfo"), "Name") ?? "GAEB",
|
||
phase: phaseVon(wert(vergabe, "DP"), ""), positionen, titel: titelListe.length, format: "GAEB 2000",
|
||
version: wert(knoten(gaeb, "GAEBInfo"), "Version") ?? null, titelListe,
|
||
auftraggeber: ag ? adresse([wert(ag, "Name1"), wert(ag, "Name2")], wert(ag, "Strasse"), wert(ag, "PLZ"), wert(ag, "Ort")) : null,
|
||
vortexte: suche(gaeb, /^(Vortext|Vorbemerkung|ZusTexte?|AddText)$/i).map(textUnter).filter(Boolean).join("\n\n"),
|
||
projekt: wert(knoten(gaeb, "PrjInfo"), "Name", "Bez") ?? null,
|
||
};
|
||
}
|
||
|
||
// ---------- GAEB 90 (.d8x): Zeilen zu 80 Zeichen, Zeilenart in Spalte 1–2, Satznummer in 75–80 ----------
|
||
// Ohne Beispieldatei nach der Satzbeschreibung gelesen (OZ 9 Stellen, Menge 11 Stellen mit 3 Nachkommastellen,
|
||
// Einheit 4 Stellen, Kurztext 25, Langtext 26); Einheitspreise werden nicht übernommen, sie kalkulierst du selbst.
|
||
|
||
export function leseGaeb90(text: string, dateiname = ""): GaebLv {
|
||
const zeilen = text.split(/\r?\n/).map((z) => z.replace(/\r$/, "")).filter((z) => z.length >= 2);
|
||
const daten = (z: string) => z.slice(2, 74);
|
||
let maske = "", phase: string | null = null, name = "", ag = "";
|
||
const positionen: GaebPosition[] = [];
|
||
const titelListe: GaebLv["titelListe"] = [];
|
||
const offen: { oz: string; bezeichnung: string }[] = [];
|
||
let pos: GaebPosition | null = null;
|
||
const ozAus = (roh: string): string[] => {
|
||
if (!maske) return ozTeile(roh);
|
||
const teile: string[] = [];
|
||
let i = 0;
|
||
while (i < maske.length) {
|
||
let j = i;
|
||
while (j < maske.length && maske[j] === maske[i]) j++;
|
||
if (maske[i] !== "I") teile.push(roh.slice(i, j).trim());
|
||
i = j;
|
||
}
|
||
return teile.filter(Boolean);
|
||
};
|
||
const vortext: string[] = [];
|
||
for (const z of zeilen) {
|
||
const za = z.slice(0, 2);
|
||
const d = daten(z);
|
||
if (za === "00") {
|
||
maske = /([1-9]+P+I?)/.exec(d)?.[1]?.padEnd(9, "I") ?? "";
|
||
phase = phaseVon(/(?:^|\D)(8[1-6])(?:\D|$)/.exec(d)?.[1], dateiname);
|
||
} else if (za === "01") name ||= d.trim();
|
||
else if (za === "02") name ||= d.trim();
|
||
else if (za === "03") ag ||= d.trim();
|
||
else if (za === "T1") vortext.push(d.trim());
|
||
else if (za === "11") {
|
||
pos = null;
|
||
const t = { oz: ozAus(d.slice(0, 9)).join("."), bezeichnung: "" };
|
||
offen.push(t);
|
||
titelListe.push(t);
|
||
} else if (za === "12") {
|
||
const t = offen.at(-1);
|
||
if (t) t.bezeichnung = [t.bezeichnung, d.trim()].filter(Boolean).join(" ");
|
||
} else if (za === "31") { offen.pop(); pos = null; }
|
||
else if (za === "21") {
|
||
const oz = ozAus(d.slice(0, 9)).join(".");
|
||
const art1 = d[9] ?? "N", art2 = d[10] ?? "N";
|
||
let menge: number | null = null, einheit = "";
|
||
const fest = /^\d{11}$/.exec(z.slice(23, 34));
|
||
if (fest) { menge = Number(fest[0]) / 1000; einheit = z.slice(34, 38).trim(); }
|
||
else {
|
||
const m = /(\d{9,11})\s*([^\s\d]{1,4})?/.exec(d.slice(11));
|
||
if (m) { menge = Number(m[1]) / 1000; einheit = m[2] ?? ""; }
|
||
}
|
||
const titel = offen.at(-1);
|
||
pos = { oz, ordner: offen.map((t) => `${t.oz} ${t.bezeichnung}`.trim()).join(" / "), kurztext: "", langtext: "", einheit: gaebEinheit(einheit), menge, ep: null,
|
||
art: art2 === "M" ? "eventual_summe" : art2 === "E" ? "eventual" : art1 === "A" ? "alternativ" : "normal", titelOz: titel?.oz ?? null };
|
||
positionen.push(pos);
|
||
} else if (za === "25" && pos) pos.kurztext = [pos.kurztext, d.trim()].filter(Boolean).join(" ");
|
||
else if (za === "26" && pos) pos.langtext = [pos.langtext, d.trim()].filter(Boolean).join("\n");
|
||
}
|
||
for (const p of positionen) p.kurztext = (p.kurztext || p.langtext.split("\n")[0] || `Position ${p.oz}`).slice(0, 300);
|
||
for (const t of titelListe) t.bezeichnung ||= "Titel";
|
||
if (!positionen.length) throw new Error("In der GAEB-90-Datei stehen keine Positionen (Zeilenart 21).");
|
||
return { name: name || "GAEB", phase: phase ?? phaseVon(null, dateiname), positionen, titel: titelListe.length, format: "GAEB 90", version: null, titelListe,
|
||
auftraggeber: ag ? { name: ag, strasse: null, plz: null, ort: null } : null, vortexte: vortext.join("\n"), projekt: null };
|
||
}
|