Stammdaten: Leistungen mit Ordnern und Vorkalkulation, GAEB- und DATANORM-Import, Material mit Rabatten, Angebot mit Reitern Leistungen/Material und KI-Vorschlag (0.18.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
This commit is contained in:
191
src/domain/datanorm.ts
Normal file
191
src/domain/datanorm.ts
Normal file
@@ -0,0 +1,191 @@
|
||||
// DATANORM 4 und 5 lesen (Artikel- und Preisdateien der Händler): Satzarten mit Semikolon getrennt.
|
||||
// A = Artikel, B = Zusatzdaten (EAN, Warengruppe), P = Preisänderung, R = Rabattgruppe (.RAB), V = Vorlauf.
|
||||
// Preise stehen ohne Komma mit zwei Nachkommastellen, die Preiseinheit als Schlüssel 0–3 (1, 10, 100, 1000 Stück).
|
||||
// Gegen echte Dateien von Hennings Händlern noch zu prüfen; unbekannte Satzarten werden übersprungen.
|
||||
import { gaebEinheit } from "./gaeb.js";
|
||||
|
||||
export interface DnArtikel {
|
||||
artikelnummer: string;
|
||||
bezeichnung: string;
|
||||
langtext?: string | null;
|
||||
einheit: string | null;
|
||||
preiseinheit: 1 | 10 | 100 | 1000;
|
||||
preisart: "brutto" | "netto";
|
||||
/** Cent je Preiseinheit */
|
||||
listenpreis: number | null;
|
||||
rabattgruppe: string | null;
|
||||
warengruppe: string | null;
|
||||
ean: string | null;
|
||||
/** L im Satz: Artikel entfällt */
|
||||
loeschen?: boolean;
|
||||
}
|
||||
|
||||
export interface DnPreis {
|
||||
artikelnummer: string;
|
||||
preisart: "brutto" | "netto";
|
||||
/** Cent je Preiseinheit */
|
||||
preis: number;
|
||||
/** Rabatt aus dem P-Satz in % (falls angegeben) */
|
||||
rabatt: number | null;
|
||||
}
|
||||
|
||||
export interface DnRabatt {
|
||||
rabattgruppe: string;
|
||||
rabatt: number;
|
||||
bezeichnung: string | null;
|
||||
}
|
||||
|
||||
export interface Datanorm {
|
||||
version: string | null;
|
||||
/** Datum der Preisliste (JJJJ-MM-TT) */
|
||||
stand: string | null;
|
||||
/** Text aus dem Vorlaufsatz, meist der Händlername */
|
||||
absender: string | null;
|
||||
artikel: DnArtikel[];
|
||||
preise: DnPreis[];
|
||||
rabatte: DnRabatt[];
|
||||
/** übersprungene Zeilen (unbekannte Satzart oder unlesbar) */
|
||||
uebersprungen: number;
|
||||
}
|
||||
|
||||
const PREISEINHEIT: Record<string, 1 | 10 | 100 | 1000> = { "0": 1, "1": 10, "2": 100, "3": 1000, "10": 10, "100": 100, "1000": 1000 };
|
||||
|
||||
/** Betrag „12345“ = 123,45 €, „123,45“ oder „123.45“ als Euro; Ergebnis in Cent */
|
||||
export function dnBetrag(t: string | undefined): number | null {
|
||||
const s = (t ?? "").trim();
|
||||
if (!s) return null;
|
||||
if (/[.,]/.test(s)) {
|
||||
const n = Number(s.includes(",") ? s.replace(/\./g, "").replace(",", ".") : s);
|
||||
return Number.isFinite(n) ? Math.round(n * 100) : null;
|
||||
}
|
||||
const n = Number(s);
|
||||
return Number.isFinite(n) ? Math.round(n) : null;
|
||||
}
|
||||
|
||||
/** Rabatt „3500“ = 35 %, „35,00“ = 35 % */
|
||||
export function dnProzent(t: string | undefined): number | null {
|
||||
const s = (t ?? "").trim();
|
||||
if (!s) return null;
|
||||
const n = /[.,]/.test(s) ? Number(s.replace(",", ".")) : Number(s) / 100;
|
||||
return Number.isFinite(n) && n >= 0 && n <= 100 ? Math.round(n * 100) / 100 : null;
|
||||
}
|
||||
|
||||
function datum(t: string | undefined): string | null {
|
||||
const s = (t ?? "").trim();
|
||||
let m = /^(\d{2})(\d{2})(\d{2})$/.exec(s);
|
||||
if (m) return `20${m[3]}-${m[2]}-${m[1]}`;
|
||||
m = /^(\d{2})(\d{2})(\d{4})$/.exec(s);
|
||||
if (m) return `${m[3]}-${m[2]}-${m[1]}`;
|
||||
m = /^(\d{4})(\d{2})(\d{2})$/.exec(s);
|
||||
if (m && Number(m[2]) <= 12) return `${m[1]}-${m[2]}-${m[3]}`;
|
||||
m = /^(\d{2})\.(\d{2})\.(\d{2,4})$/.exec(s);
|
||||
if (m) return `${m[3]!.length === 2 ? `20${m[3]}` : m[3]}-${m[2]}-${m[1]}`;
|
||||
return null;
|
||||
}
|
||||
|
||||
export function dnEinheit(t: string | undefined): string | null {
|
||||
const s = (t ?? "").trim();
|
||||
if (!s) return null;
|
||||
const k = s.toLowerCase();
|
||||
if (["pak", "pck", "pkt", "paket"].includes(k)) return "Pck";
|
||||
if (["krt", "karton"].includes(k)) return "Krt";
|
||||
if (["rol", "rll", "rolle"].includes(k)) return "Rol";
|
||||
if (["sa", "sack"].includes(k)) return "Sack";
|
||||
if (["l", "ltr", "liter"].includes(k)) return "l";
|
||||
return gaebEinheit(s);
|
||||
}
|
||||
|
||||
/** Liest eine oder mehrere DATANORM-Dateien (Text schon dekodiert, siehe kodierung.ts) */
|
||||
export function leseDatanorm(texte: string[]): Datanorm {
|
||||
const aus: Datanorm = { version: null, stand: null, absender: null, artikel: [], preise: [], rabatte: [], uebersprungen: 0 };
|
||||
const nachNummer = new Map<string, DnArtikel>();
|
||||
let zeilenGesamt = 0, mitSemikolon = 0;
|
||||
for (const text of texte) {
|
||||
for (const roh of text.split(/\r?\n/)) {
|
||||
const zeile = roh.replace(/\s+$/, "");
|
||||
if (!zeile) continue;
|
||||
zeilenGesamt++;
|
||||
if (!zeile.includes(";")) { aus.uebersprungen++; continue; }
|
||||
mitSemikolon++;
|
||||
const f = zeile.split(";");
|
||||
const art = f[0]!.trim().toUpperCase();
|
||||
try {
|
||||
if (art === "V") {
|
||||
aus.version ??= (f[1] ?? "").trim() || null;
|
||||
for (const x of f.slice(1)) { const d = datum(x); if (d) { aus.stand ??= d; break; } }
|
||||
aus.absender ??= f.slice(2).map((x) => x.trim()).find((x) => x && !/^\d+$/.test(x) && !datum(x)) ?? null;
|
||||
} else if (art === "A") {
|
||||
const nr = (f[2] ?? "").trim();
|
||||
if (!nr) { aus.uebersprungen++; continue; }
|
||||
const a: DnArtikel = {
|
||||
artikelnummer: nr,
|
||||
bezeichnung: [f[4], f[5]].map((x) => (x ?? "").trim()).filter(Boolean).join(" ") || nr,
|
||||
einheit: dnEinheit(f[8]),
|
||||
preiseinheit: PREISEINHEIT[(f[7] ?? "").trim()] ?? 1,
|
||||
preisart: (f[6] ?? "").trim() === "2" ? "netto" : "brutto",
|
||||
listenpreis: dnBetrag(f[9]),
|
||||
rabattgruppe: (f[10] ?? "").trim() || null,
|
||||
warengruppe: (f[11] ?? "").trim() || null,
|
||||
ean: null,
|
||||
loeschen: (f[1] ?? "").trim().toUpperCase() === "L",
|
||||
};
|
||||
const alt = nachNummer.get(nr);
|
||||
if (alt) Object.assign(alt, { ...a, ean: alt.ean, warengruppe: a.warengruppe ?? alt.warengruppe });
|
||||
else { nachNummer.set(nr, a); aus.artikel.push(a); }
|
||||
} else if (art === "B") {
|
||||
const a = nachNummer.get((f[2] ?? "").trim());
|
||||
if (!a) continue;
|
||||
const ean = f.slice(4, 12).map((x) => x.trim()).find((x) => /^\d{8}$|^\d{12,14}$/.test(x));
|
||||
if (ean) a.ean = ean;
|
||||
const wg = (f[10] ?? "").trim();
|
||||
if (wg && !/^\d{8,}$/.test(wg)) a.warengruppe = wg;
|
||||
} else if (art === "D") {
|
||||
// Langtextzeilen: D;N;Artikelnummer;Zeile;Text;Zeile;Text
|
||||
const a = nachNummer.get((f[2] ?? "").trim());
|
||||
if (!a) continue;
|
||||
const text = [f[4], f[6]].map((x) => (x ?? "").trim()).filter(Boolean).join("\n");
|
||||
if (text) a.langtext = a.langtext ? `${a.langtext}\n${text}` : text;
|
||||
} else if (art === "P") {
|
||||
// bis zu drei Artikel je Satz: Artikelnummer; Preiskennzeichen; Preis; Rabattkennzeichen; Rabatt 1–3
|
||||
for (let i = 2; i + 2 < f.length; i += 7) {
|
||||
const nr = (f[i] ?? "").trim();
|
||||
const preis = dnBetrag(f[i + 2]);
|
||||
if (!nr || preis === null) continue;
|
||||
aus.preise.push({ artikelnummer: nr, preisart: (f[i + 1] ?? "").trim() === "2" ? "netto" : "brutto", preis, rabatt: (f[i + 3] ?? "").trim() === "1" ? dnProzent(f[i + 4]) : null });
|
||||
}
|
||||
} else if (art === "R") {
|
||||
// R;A;Rabattgruppe;Rabattkennzeichen;Rabatt;Bezeichnung
|
||||
const rg = (f[2] ?? "").trim();
|
||||
const kz = (f[3] ?? "").trim();
|
||||
let rabatt = dnProzent(f[4]);
|
||||
// Kennzeichen 2/3: Multiplikator (z. B. 0,65 = 35 % Rabatt)
|
||||
if ((kz === "2" || kz === "3") && rabatt !== null) {
|
||||
const m = /[.,]/.test(f[4]!) ? Number(f[4]!.replace(",", ".")) : Number(f[4]) / 1000;
|
||||
rabatt = Number.isFinite(m) && m > 0 && m <= 1 ? Math.round((1 - m) * 10000) / 100 : rabatt;
|
||||
}
|
||||
if (rg && rabatt !== null) aus.rabatte.push({ rabattgruppe: rg, rabatt, bezeichnung: (f[5] ?? "").trim() || null });
|
||||
} else aus.uebersprungen++;
|
||||
} catch {
|
||||
aus.uebersprungen++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (zeilenGesamt > 0 && mitSemikolon === 0) {
|
||||
throw new Error("Die Datei sieht nach DATANORM 3 (feste Spaltenbreite) aus. Bitte beim Händler DATANORM 4 oder 5 anfordern.");
|
||||
}
|
||||
if (!aus.artikel.length && !aus.preise.length && !aus.rabatte.length) throw new Error("In den Dateien stehen keine DATANORM-Artikel, Preise oder Rabattgruppen.");
|
||||
return aus;
|
||||
}
|
||||
|
||||
/** EK netto in Cent je Preiseinheit */
|
||||
export function ekNetto(listenpreis: number | null, preisart: "brutto" | "netto", rabatt: number | null | undefined): number | null {
|
||||
if (listenpreis === null) return null;
|
||||
if (preisart === "netto") return listenpreis;
|
||||
return Math.round(listenpreis * (1 - (rabatt ?? 0) / 100));
|
||||
}
|
||||
|
||||
/** EK in Cent je Einheit (für die Kalkulation), Preiseinheit umgerechnet */
|
||||
export function ekJeEinheit(a: { ek?: number | string | null; preiseinheit?: number | string | null }): number | null {
|
||||
if (a.ek === null || a.ek === undefined || a.ek === "") return null;
|
||||
return Math.round(Number(a.ek) / Number(a.preiseinheit || 1));
|
||||
}
|
||||
92
src/domain/gaeb.ts
Normal file
92
src/domain/gaeb.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
// GAEB DA XML (3.x) lesen: Leistungsverzeichnis mit Titeln (BoQCtgy) und Positionen (Item), z. B. .x81, .x83,
|
||||
// .x84, .x86. Aus den Titeln werden Ordner der Stammdaten, aus den Positionen Leistungen mit OZ, Kurztext,
|
||||
// Langtext, Einheit und, wenn vorhanden, Einheitspreis. GAEB 90 und GAEB 2000 (.d8x/.p8x) sind keine XML-Dateien.
|
||||
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" | "alternativ";
|
||||
}
|
||||
|
||||
export interface GaebLv {
|
||||
name: string;
|
||||
/** Datenaustauschphase, z. B. 81, 83, 84 */
|
||||
phase: string | null;
|
||||
positionen: GaebPosition[];
|
||||
titel: number;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
};
|
||||
|
||||
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", "Name")) || textVon(pfad(boq, "BoQInfo", "LblBoQ")) || textVon(pfad(gaeb, "PrjInfo", "LblPrj")) || textVon(pfad(gaeb, "PrjInfo", "NamePrj")) || "GAEB";
|
||||
const phase = textVon(kind(award, "DP")) || null;
|
||||
const positionen: GaebPosition[] = [];
|
||||
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);
|
||||
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 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 ? "eventual" : kind(it, "ALNSerNo") && textVon(kind(it, "ALNSerNo")) !== "0" ? "alternativ" : "normal",
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
koerper(kind(boq, "BoQBody"), [], []);
|
||||
if (!positionen.length) throw new Error("In der GAEB-Datei stehen keine Positionen.");
|
||||
return { name, phase, positionen, titel };
|
||||
}
|
||||
43
src/domain/ki.ts
Normal file
43
src/domain/ki.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
// KI-Vorschlag im Angebot (Henning, 21.10.2026): Aus einer Beschreibung der Arbeiten schlägt Claude Leistungen
|
||||
// vor, aber nur aus seinen Stammdaten, mit Menge. Er hakt an, was er übernehmen will.
|
||||
|
||||
export interface KiLeistung { id: string; kurztext: string; einheit: string | null; ordner: string; nummer?: string | null }
|
||||
export interface KiVorschlag { id: string; menge: number | null; grund: string }
|
||||
|
||||
/** Anfrage an Claude: Aufgabe, die erlaubten Leistungen und das Antwortformat */
|
||||
export function kiAnfrage(aufgabe: string, leistungen: KiLeistung[]): string {
|
||||
const liste = leistungen.map((l) => `${l.id} | ${l.ordner} | ${l.kurztext} | ${l.einheit ?? ""}`).join("\n");
|
||||
return `Du hilfst einem Zimmereibetrieb, ein Angebot zusammenzustellen. Wähle aus der Liste der Leistungen die passenden aus.
|
||||
Regeln:
|
||||
- Nur Leistungen aus der Liste, mit ihrer id genau wie angegeben. Erfinde keine Leistungen.
|
||||
- Schätze die Menge in der Einheit der Leistung aus der Beschreibung; wenn sie sich nicht ableiten lässt, menge null.
|
||||
- grund: ein kurzer deutscher Satz, warum die Leistung passt oder wie die Menge entsteht.
|
||||
- Höchstens 25 Vorschläge, in sinnvoller Reihenfolge des Bauablaufs.
|
||||
Antworte nur mit JSON: [{"id": "...", "menge": 12.5, "grund": "..."}]
|
||||
|
||||
Beschreibung der Arbeiten:
|
||||
${aufgabe.trim()}
|
||||
|
||||
Leistungen (id | Ordner | Kurztext | Einheit):
|
||||
${liste}`;
|
||||
}
|
||||
|
||||
/** Antwort prüfen: nur bekannte ids, Mengen als Zahl ≥ 0, doppelte weg */
|
||||
export function kiVorschlaege(antwort: unknown, erlaubt: Set<string>): KiVorschlag[] {
|
||||
let wert = antwort;
|
||||
if (typeof wert === "string") {
|
||||
const json = /\[[\s\S]*\]/.exec(wert)?.[0];
|
||||
try { wert = json ? JSON.parse(json) : []; } catch { wert = []; }
|
||||
}
|
||||
if (!Array.isArray(wert)) return [];
|
||||
const gesehen = new Set<string>();
|
||||
const aus: KiVorschlag[] = [];
|
||||
for (const x of wert as Record<string, unknown>[]) {
|
||||
const id = String(x?.id ?? "");
|
||||
if (!erlaubt.has(id) || gesehen.has(id)) continue;
|
||||
gesehen.add(id);
|
||||
const m = typeof x.menge === "number" ? x.menge : typeof x.menge === "string" ? Number(x.menge.replace(",", ".")) : NaN;
|
||||
aus.push({ id, menge: Number.isFinite(m) && m >= 0 ? Math.round(m * 1000) / 1000 : null, grund: String(x.grund ?? "").slice(0, 300) });
|
||||
}
|
||||
return aus.slice(0, 25);
|
||||
}
|
||||
38
src/domain/kodierung.ts
Normal file
38
src/domain/kodierung.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
// Zeichensatz von Importdateien erkennen: GAEB-XML nennt ihn in der ersten Zeile, DATANORM ist meist im alten
|
||||
// DOS-Zeichensatz (Codepage 850), manchmal Windows-1252 oder UTF-8. Codepage 850 kennt TextDecoder nicht.
|
||||
|
||||
const CP850 =
|
||||
"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø×ƒáíóúñѪº¿®¬½¼¡«»░▒▓│┤ÁÂÀ©╣║╗╝¢¥┐└┴┬├─┼ãÃ╚╔╩╦╠═╬¤ðÐÊËÈıÍÎÏ┘┌█▄¦Ì▀ÓßÔÒõÕµþÞÚÛÙýݯ´±‗¾¶§÷¸°¨·¹³²■ ";
|
||||
|
||||
export function cp850(bytes: Uint8Array): string {
|
||||
let s = "";
|
||||
for (let i = 0; i < bytes.length; i += 8192) {
|
||||
const teil = bytes.subarray(i, i + 8192);
|
||||
s += Array.from(teil, (b) => (b < 128 ? String.fromCharCode(b) : CP850[b - 128]!)).join("");
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
function utf8(bytes: Uint8Array): string | null {
|
||||
try { return new TextDecoder("utf-8", { fatal: true }).decode(bytes); } catch { return null; }
|
||||
}
|
||||
|
||||
/** Text einer Datei: UTF-8, sonst Codepage 850 oder Windows-1252, je nachdem, wo Umlaute plausibler sind */
|
||||
export function dateiText(bytes: Uint8Array): string {
|
||||
// GAEB-XML: Kodierung aus der XML-Deklaration
|
||||
const anfang = new TextDecoder("latin1").decode(bytes.subarray(0, 200));
|
||||
const dekl = /<\?xml[^>]*encoding=["']([^"']+)["']/i.exec(anfang);
|
||||
if (dekl) {
|
||||
const k = dekl[1]!.toLowerCase();
|
||||
if (k === "utf-8" || k === "utf8") return utf8(bytes) ?? new TextDecoder("windows-1252").decode(bytes);
|
||||
try { return new TextDecoder(k).decode(bytes); } catch { /* unbekannt: raten */ }
|
||||
}
|
||||
const u = utf8(bytes);
|
||||
if (u !== null) return u.replace(/^/, "");
|
||||
let dos = 0, win = 0;
|
||||
for (const b of bytes) {
|
||||
if (b === 0x81 || b === 0x84 || b === 0x94 || b === 0x8e || b === 0x99 || b === 0x9a || b === 0xe1) dos++;
|
||||
else if (b === 0xe4 || b === 0xf6 || b === 0xfc || b === 0xc4 || b === 0xd6 || b === 0xdc || b === 0xdf) win++;
|
||||
}
|
||||
return dos > win ? cp850(bytes) : new TextDecoder("windows-1252").decode(bytes);
|
||||
}
|
||||
@@ -19,6 +19,8 @@ export interface Ansatz {
|
||||
ek?: number | null;
|
||||
faktor?: number;
|
||||
aktiv?: boolean;
|
||||
/** Artikel aus dem Materialstamm: EK je Einheit und Einheit kommen von dort */
|
||||
artikel_id?: string | null;
|
||||
}
|
||||
|
||||
export interface Mengenvorlage {
|
||||
@@ -34,6 +36,13 @@ export interface Mengenvorlage {
|
||||
/** mitgeliefert, nicht änderbar */
|
||||
fest?: boolean;
|
||||
reihenfolge?: number;
|
||||
/** Leistungen (Stammdaten): OZ, Herkunft, „Preis festlegen auf“ in Cent, Importdatei */
|
||||
nummer?: string | null;
|
||||
herkunft?: "eigen" | "gaeb" | "plancraft";
|
||||
ep_fest?: number | string | null;
|
||||
quelle?: string | null;
|
||||
notiz?: string | null;
|
||||
geaendert_am?: string;
|
||||
}
|
||||
|
||||
const e = (kz: string, bez: string, einheit: string, formel: string): BlattWert => ({ kz, bez, einheit, formel, eingabe: true });
|
||||
|
||||
112
src/domain/xml.ts
Normal file
112
src/domain/xml.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
// Kleiner XML-Leser ohne Abhängigkeiten, für GAEB DA XML. Kennt Elemente, Attribute, Text, CDATA und die
|
||||
// üblichen Entitäten; Kommentare, Verarbeitungsanweisungen und DOCTYPE werden übersprungen.
|
||||
// Namensräume fallen weg: aus <g:Item> wird „Item“.
|
||||
|
||||
export interface XmlElement {
|
||||
name: string;
|
||||
attr: Record<string, string>;
|
||||
kinder: XmlKnoten[];
|
||||
}
|
||||
export type XmlKnoten = XmlElement | string;
|
||||
|
||||
const ENTITAETEN: Record<string, string> = { lt: "<", gt: ">", amp: "&", quot: '"', apos: "'", nbsp: " " };
|
||||
|
||||
export function entschluesseln(t: string): string {
|
||||
return t.replace(/&(#x[0-9a-fA-F]+|#\d+|[a-zA-Z]+);/g, (ganz, e: string) => {
|
||||
if (e[0] === "#") {
|
||||
const n = e[1] === "x" || e[1] === "X" ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
|
||||
return Number.isFinite(n) ? String.fromCodePoint(n) : ganz;
|
||||
}
|
||||
return ENTITAETEN[e] ?? ganz;
|
||||
});
|
||||
}
|
||||
|
||||
const ohnePraefix = (n: string) => n.slice(n.indexOf(":") + 1);
|
||||
|
||||
/** Liest ein XML-Dokument und gibt das Wurzelelement zurück */
|
||||
export function leseXml(text: string): XmlElement {
|
||||
const wurzel: XmlElement = { name: "#dokument", attr: {}, kinder: [] };
|
||||
const stapel: XmlElement[] = [wurzel];
|
||||
let i = 0;
|
||||
const n = text.length;
|
||||
while (i < n) {
|
||||
const lt = text.indexOf("<", i);
|
||||
if (lt < 0) { stapel[stapel.length - 1]!.kinder.push(entschluesseln(text.slice(i))); break; }
|
||||
if (lt > i) stapel[stapel.length - 1]!.kinder.push(entschluesseln(text.slice(i, lt)));
|
||||
if (text.startsWith("<!--", lt)) { const e = text.indexOf("-->", lt + 4); i = e < 0 ? n : e + 3; continue; }
|
||||
if (text.startsWith("<![CDATA[", lt)) {
|
||||
const e = text.indexOf("]]>", lt + 9);
|
||||
stapel[stapel.length - 1]!.kinder.push(text.slice(lt + 9, e < 0 ? n : e));
|
||||
i = e < 0 ? n : e + 3;
|
||||
continue;
|
||||
}
|
||||
if (text.startsWith("<?", lt)) { const e = text.indexOf("?>", lt + 2); i = e < 0 ? n : e + 2; continue; }
|
||||
if (text.startsWith("<!", lt)) {
|
||||
// DOCTYPE, ggf. mit [ … ]
|
||||
let tiefe = 0, j = lt + 2;
|
||||
for (; j < n; j++) { const c = text[j]; if (c === "[") tiefe++; else if (c === "]") tiefe--; else if (c === ">" && tiefe <= 0) break; }
|
||||
i = j + 1;
|
||||
continue;
|
||||
}
|
||||
// Ende des Tags finden, Anführungszeichen beachten
|
||||
let j = lt + 1, quote = "";
|
||||
for (; j < n; j++) {
|
||||
const c = text[j]!;
|
||||
if (quote) { if (c === quote) quote = ""; } else if (c === '"' || c === "'") quote = c; else if (c === ">") break;
|
||||
}
|
||||
if (j >= n) throw new Error("Die XML-Datei ist unvollständig (ein Tag endet nicht).");
|
||||
const tag = text.slice(lt + 1, j);
|
||||
i = j + 1;
|
||||
if (tag.startsWith("/")) {
|
||||
const name = ohnePraefix(tag.slice(1).trim());
|
||||
// bis zum passenden offenen Element schließen (verzeiht fehlerhafte Verschachtelung)
|
||||
for (let k = stapel.length - 1; k > 0; k--) if (stapel[k]!.name === name) { stapel.length = k; break; }
|
||||
continue;
|
||||
}
|
||||
const leer = tag.endsWith("/");
|
||||
const inhalt = leer ? tag.slice(0, -1) : tag;
|
||||
const m = /^\s*([^\s/>]+)/.exec(inhalt);
|
||||
if (!m) continue;
|
||||
const el: XmlElement = { name: ohnePraefix(m[1]!), attr: {}, kinder: [] };
|
||||
const re = /([^\s=]+)\s*=\s*("([^"]*)"|'([^']*)')/g;
|
||||
let a: RegExpExecArray | null;
|
||||
const rest = inhalt.slice(m[0].length);
|
||||
while ((a = re.exec(rest))) el.attr[ohnePraefix(a[1]!)] = entschluesseln(a[3] ?? a[4] ?? "");
|
||||
stapel[stapel.length - 1]!.kinder.push(el);
|
||||
if (!leer) stapel.push(el);
|
||||
}
|
||||
const erstes = wurzel.kinder.find((k): k is XmlElement => typeof k !== "string");
|
||||
if (!erstes) throw new Error("Die Datei enthält kein XML.");
|
||||
return erstes;
|
||||
}
|
||||
|
||||
export const elemente = (e: XmlElement, name?: string): XmlElement[] =>
|
||||
e.kinder.filter((k): k is XmlElement => typeof k !== "string" && (!name || k.name === name));
|
||||
export const kind = (e: XmlElement | undefined, name: string): XmlElement | undefined => (e ? elemente(e, name)[0] : undefined);
|
||||
/** Pfad aus Kindnamen, z. B. pfad(item, "Description", "CompleteText") */
|
||||
export const pfad = (e: XmlElement | undefined, ...namen: string[]): XmlElement | undefined => namen.reduce<XmlElement | undefined>((x, n) => kind(x, n), e);
|
||||
|
||||
/** reiner Text eines Elements, Leerraum zusammengefasst */
|
||||
export function textVon(e: XmlElement | undefined): string {
|
||||
if (!e) return "";
|
||||
const teile: string[] = [];
|
||||
const lauf = (x: XmlKnoten) => { if (typeof x === "string") teile.push(x); else x.kinder.forEach(lauf); };
|
||||
lauf(e);
|
||||
return teile.join("").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
/** Fließtext aus <p>, <br>, <span> (GAEB-Texte): Absätze als Zeilenumbruch */
|
||||
export function absatzText(e: XmlElement | undefined): string {
|
||||
if (!e) return "";
|
||||
let aus = "";
|
||||
const lauf = (x: XmlKnoten) => {
|
||||
if (typeof x === "string") { aus += x.replace(/\s+/g, " "); return; }
|
||||
if (x.name === "br") { aus += "\n"; return; }
|
||||
const block = x.name === "p" || x.name === "div" || x.name === "li";
|
||||
if (block && aus && !aus.endsWith("\n")) aus += "\n";
|
||||
x.kinder.forEach(lauf);
|
||||
if (block && !aus.endsWith("\n")) aus += "\n";
|
||||
};
|
||||
lauf(e);
|
||||
return aus.split("\n").map((z) => z.trim()).join("\n").replace(/\n{3,}/g, "\n\n").trim();
|
||||
}
|
||||
Reference in New Issue
Block a user