Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
192 lines
8.3 KiB
TypeScript
192 lines
8.3 KiB
TypeScript
// 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));
|
||
}
|