Files
Betriebsf-hrungssoftware/src/domain/lage.ts
Claude e348fd040c
Some checks failed
Prüfung / tests (push) Has been cancelled
Prüfung / container (push) Has been cancelled
0.34.0: Übersicht mit Projektfarben, zweizeiligen Aufgaben und Geldfluss ohne Kontostand
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
2026-10-23 06:55:07 +00:00

63 lines
3.4 KiB
TypeScript

// 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;
}
/** Läuft die Position heute? Die erste Zahlung darf höchstens einen Rhythmus entfernt sein (jährliche also bis 12 Monate) */
export function laeuftHeute(k: Pick<LaufendeKosten, "rhythmus" | "start" | "ende">, heute: Datum): boolean {
if (k.rhythmus === "einmalig" || (k.ende && k.ende < heute)) return false;
return k.start <= plusMonate(heute, k.rhythmus === "jaehrlich" ? 12 : k.rhythmus === "vierteljaehrlich" ? 3 : 1);
}
/** 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 (!laeuftHeute(k, heute)) continue;
summe += k.rhythmus === "monatlich" ? k.betrag : k.rhythmus === "vierteljaehrlich" ? k.betrag / 3 : k.betrag / 12;
}
return Math.round(summe);
}
/** Geldfluss einer Woche ohne Kontostand (Henning, 23.10.2026); Kosten positiv, Umsatzsteuer: Zahllast positiv, Erstattung negativ */
export interface Fluss { von: Datum; bis: Datum; einnahmen: Cent; projektkosten: Cent; laufend: Cent; ust: Cent }
export function flussJeWoche(wochen: Periode[]): Fluss[] {
return wochen.map((w) => {
const summe = (g: Posten["gruppe"]) => w.posten.filter((p) => p.gruppe === g).reduce((s, p) => s + p.betrag, 0);
return { von: w.von, bis: w.bis, einnahmen: summe("Projekteinnahmen"), projektkosten: -summe("Projektkosten"), laufend: -summe("Laufende Kosten"), ust: -summe("Umsatzsteuer") };
});
}
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 };
}