0.20.0: Liquidität mit Lage, Kurve, Kommt rein / Geht raus und Mindestgrenze per Zahnrad

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:
Claude
2026-10-21 16:46:54 +00:00
parent feb19241c5
commit 176b1db832
13 changed files with 562 additions and 126 deletions

46
src/domain/lage.ts Normal file
View File

@@ -0,0 +1,46 @@
// Liquidität auf einen Blick (Henning, 21.10.2026): frei verfügbar heute, Ampel und ein Satz, wann es am knappsten
// wird und warum. Die Mindestgrenze ist vorerst ein Monat laufende Kosten oder ein fester Betrag; Töpfe kommen in Stufe 5.
import { type Datum, plusMonate } from "./datum.js";
import type { Cent } from "./geld.js";
import type { LaufendeKosten, Periode, Posten } from "./liquiditaet.js";
export type Ampel = "gruen" | "gelb" | "rot";
export interface Lage {
/** Kontostand heute minus Mindestgrenze */
frei: Cent;
ampel: Ampel;
/** Woche mit dem niedrigsten Kontostand am Ende */
tief: { von: Datum; bis: Datum; kontostand: Cent } | null;
/** Abstand des Tiefpunkts zur Mindestgrenze, negativ = darunter */
abstand: Cent;
/** die größten Ausgaben bis zum Tiefpunkt */
grund: Posten[];
/** die nächste größere Einnahme nach dem Tiefpunkt */
danach: Posten | null;
}
/** Laufende Kosten eines Monats: monatliche voll, vierteljährliche ein Drittel, jährliche ein Zwölftel; nur was heute läuft */
export function laufendJeMonat(laufend: LaufendeKosten[], heute: Datum): Cent {
let summe = 0;
for (const k of laufend) {
if (k.rhythmus === "einmalig" || k.start > plusMonate(heute, 1) || (k.ende && k.ende < heute)) continue;
summe += k.rhythmus === "monatlich" ? k.betrag : k.rhythmus === "vierteljaehrlich" ? k.betrag / 3 : k.betrag / 12;
}
return Math.round(summe);
}
export function lage(wochen: Periode[], kontostand: Cent, mindestgrenze: Cent): Lage {
const frei = kontostand - mindestgrenze;
if (!wochen.length) return { frei, ampel: kontostand < 0 ? "rot" : kontostand < mindestgrenze ? "gelb" : "gruen", tief: null, abstand: frei, grund: [], danach: null };
let ti = 0;
wochen.forEach((w, i) => { if (w.kontostand < wochen[ti]!.kontostand) ti = i; });
const t = wochen[ti]!;
const tiefster = Math.min(kontostand, t.kontostand);
const ampel: Ampel = tiefster < 0 ? "rot" : tiefster < mindestgrenze ? "gelb" : "gruen";
const bisTief = wochen.slice(0, ti + 1).flatMap((w) => w.posten);
const grund = bisTief.filter((p) => p.betrag < 0).sort((a, b) => a.betrag - b.betrag).slice(0, 2);
const danach = wochen.slice(ti + 1, ti + 5).flatMap((w) => w.posten).filter((p) => p.betrag > 0).sort((a, b) => b.betrag - a.betrag)[0] ?? null;
return { frei, ampel, tief: { von: t.von, bis: t.bis, kontostand: t.kontostand }, abstand: t.kontostand - mindestgrenze, grund, danach };
}