Stundensätze im Personalmodul, Übergabe ans Angebot (0.26.0)
Rechentabellen je Lohnart wie Hennings Excel (Zeit, Kosten, Vollkostensatz), Mittellohn mit Schaltern, Versionen mit gilt ab. Excel-Fassung centgenau (80,38 / 107,81 EUR), Fassung nach S2 mit Akquise/Verwaltung als Kosten. Angebote schreiben die geltenden Sätze fest, Lohnzuschlag 0 %, Nachfrage bei neuer Version. 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:
140
src/server/stundensaetze.ts
Normal file
140
src/server/stundensaetze.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
// Stundensätze im Personalmodul (M15–M19, Henning 22.10.2026). Nur der Inhaber sieht Löhne und Sätze (U2);
|
||||
// die Namen der Lohnarten (/api/lohnarten) braucht auch das Büro für Personal und Kalkulation.
|
||||
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { type Client, type Pool, transaktion } from "./db.js";
|
||||
import { werName } from "./anmeldung.js";
|
||||
import { type Block, KOSTENARTEN_S, MITTELLOHN, type StundensatzDaten, type Umfeld, laufendImJahr, rechneStundensaetze, saetzeFuerAngebot } from "../domain/stundensatz.js";
|
||||
import { stundensaetzeAusExcel, stundensaetzeNachS2 } from "../domain/stundensatz-excel.js";
|
||||
import type { Cent } from "../domain/geld.js";
|
||||
|
||||
const schluessel = z.string().regex(/^[a-z0-9-]{1,40}$/).refine((s) => s !== MITTELLOHN, "„mittellohn“ ist vergeben");
|
||||
const kurz = z.string().trim().max(120);
|
||||
const zahl = z.number().min(0).max(1e7);
|
||||
const cent = z.number().int().min(0).max(10_000_000_000);
|
||||
const kleineId = z.string().min(1).max(40);
|
||||
|
||||
export const DatenSchema = z.object({
|
||||
tabellen: z.array(z.object({
|
||||
id: schluessel,
|
||||
name: kurz.min(1),
|
||||
hinweis: z.string().max(500).optional(),
|
||||
zeit: z.object({
|
||||
stundenTag: z.number().min(0).max(24), tageJahr: z.number().min(0).max(366), feiertage: z.number().min(0).max(366),
|
||||
ausfall: z.array(z.object({ id: kleineId, name: kurz, tage: z.number().min(0).max(366) })).max(30),
|
||||
unproduktiv: z.array(z.object({ id: kleineId, name: kurz, stunden: z.number().min(0).max(744), je: z.enum(["monat", "tag"]), als: z.enum(["zeit", "kosten"]) })).max(30),
|
||||
}),
|
||||
kosten: z.array(z.object({
|
||||
id: kleineId, block: z.number().int().min(1).max(5).transform((b) => b as Block),
|
||||
name: kurz, art: z.enum(KOSTENARTEN_S), betrag: cent.optional(), faktor: z.number().min(0.01).max(10).optional(), prozent: z.number().min(0).max(1000).optional(),
|
||||
stunden: z.number().min(0).max(744).optional(), laufend: z.array(z.uuid()).max(200).optional(), koepfe: z.boolean().optional(),
|
||||
anteil: z.number().min(0).max(1000).optional(), notiz: z.string().max(300).optional(),
|
||||
})).max(100),
|
||||
gewinn: z.number().min(0).max(100), lohnanteil: z.number().min(1).max(100), wagnis: z.number().min(0).max(50),
|
||||
})).min(1).max(30).refine((t) => new Set(t.map((x) => x.id)).size === t.length, "Jede Tabelle braucht einen eigenen Schlüssel"),
|
||||
koepfe: zahl.min(1).max(500).nullable(),
|
||||
bewertung: z.string().max(40),
|
||||
mittellohn: z.record(z.string().max(40), z.object({ an: z.boolean(), personen: z.number().min(0).max(500).nullable() })),
|
||||
});
|
||||
|
||||
const Datum = z.string().regex(/^\d{4}-\d{2}-\d{2}$/);
|
||||
const IdParam = z.object({ id: z.uuid() });
|
||||
|
||||
type Abfrage = Pick<Pool | Client, "query">;
|
||||
|
||||
/** Köpfe und Personen je Lohnart aus dem Personal (heute beschäftigt): der Inhaber ohne Rolle zählt als Chef,
|
||||
* Angestellte ohne Rolle als Geselle. Laufende Kosten als Jahressumme im angegebenen Jahr. */
|
||||
export async function umfeldLaden(c: Abfrage, jahr: number, heute: string): Promise<Umfeld & { liste: { id: string; bezeichnung: string; kategorie: string | null; jahr: Cent }[] }> {
|
||||
const leute = (await c.query(`select coalesce(funktion, case when angestellt then 'geselle' else 'chef' end) as lohnart, count(*)::int as n from mitarbeiter
|
||||
where (eintritt is null or eintritt <= $1) and (austritt is null or austritt >= $1) group by 1`, [heute])).rows;
|
||||
const personen = Object.fromEntries(leute.map((r) => [r.lohnart as string, r.n as number]));
|
||||
const lk = (await c.query("select id, bezeichnung, kategorie, betrag, rhythmus, start, ende from laufende_kosten order by kategorie nulls last, bezeichnung")).rows;
|
||||
const liste = lk.map((l) => ({ id: l.id as string, bezeichnung: l.bezeichnung as string, kategorie: (l.kategorie ?? null) as string | null, jahr: laufendImJahr({ ...l, betrag: Number(l.betrag) }, jahr) }));
|
||||
return { koepfe: Math.max(1, leute.reduce((s, r) => s + (r.n as number), 0)), personen, laufend: Object.fromEntries(liste.map((l) => [l.id, l.jahr])), liste };
|
||||
}
|
||||
|
||||
/** Version, die an diesem Tag gilt */
|
||||
async function geltende(c: Abfrage, datum: string) {
|
||||
return (await c.query("select id, name, gilt_ab, daten from stundensatz_version where gilt_ab <= $1 order by gilt_ab desc limit 1", [datum])).rows[0] as
|
||||
{ id: string; name: string; gilt_ab: string; daten: StundensatzDaten } | undefined;
|
||||
}
|
||||
|
||||
export interface AngebotsSaetze { stundensaetze: Record<string, Cent>; lohnarten: { id: string; name: string }[]; stundensatz_version: { id: string; name: string; gilt_ab: string } }
|
||||
|
||||
/** Sätze für ein neues Angebot (A36) aus der Version, die am Datum gilt; ohne Version null (dann die Einstellungen) */
|
||||
export async function aktuelleSaetze(c: Abfrage, datum: string): Promise<AngebotsSaetze | null> {
|
||||
const v = await geltende(c, datum);
|
||||
if (!v) return null;
|
||||
const u = await umfeldLaden(c, Number(v.gilt_ab.slice(0, 4)), datum);
|
||||
return { ...saetzeFuerAngebot(rechneStundensaetze(v.daten, u)), stundensatz_version: { id: v.id, name: v.name, gilt_ab: v.gilt_ab } };
|
||||
}
|
||||
|
||||
export function registriereStundensaetze(app: FastifyInstance, pool: Pool): void {
|
||||
const wer = (req: FastifyRequest) => werName(req);
|
||||
const heute = () => new Date().toISOString().slice(0, 10);
|
||||
const zeile = (r: Record<string, any>) => ({ id: r.id, name: r.name, gilt_ab: r.gilt_ab, daten: r.daten as StundensatzDaten, geaendert_am: r.geaendert_am });
|
||||
|
||||
app.get("/api/stundensaetze", async () => {
|
||||
const versionen = (await pool.query("select * from stundensatz_version order by gilt_ab")).rows.map(zeile);
|
||||
const gilt = await geltende(pool, heute());
|
||||
const jahre = [...new Set(versionen.map((v) => Number(v.gilt_ab.slice(0, 4))).concat(Number(heute().slice(0, 4))))];
|
||||
const umfeld: Record<number, Awaited<ReturnType<typeof umfeldLaden>>> = {};
|
||||
for (const j of jahre) umfeld[j] = await umfeldLaden(pool, j, heute());
|
||||
const personal = (await pool.query(`select k.name, m.funktion, m.angestellt from mitarbeiter m join kontakt k on k.id = m.kontakt_id
|
||||
where (m.eintritt is null or m.eintritt <= $1) and (m.austritt is null or m.austritt >= $1) order by k.name`, [heute()])).rows;
|
||||
return { versionen, gilt: gilt?.id ?? null, umfeld, personal, heute: heute() };
|
||||
});
|
||||
|
||||
// Lohnarten für Rolle im Personal und Auswahl in der Kalkulation: Tabellen der heute geltenden Version
|
||||
app.get("/api/lohnarten", async () => {
|
||||
const v = await geltende(pool, heute());
|
||||
const tabellen = v?.daten.tabellen.map((t) => ({ id: t.id, name: t.name })) ?? [];
|
||||
return tabellen.length ? tabellen : [{ id: "planer", name: "Planer" }, { id: "geselle", name: "Geselle" }, { id: "helfer", name: "Helfer" }];
|
||||
});
|
||||
|
||||
const Neu = z.object({ name: kurz.min(1), gilt_ab: Datum, daten: DatenSchema });
|
||||
app.post("/api/stundensaetze", async (req, reply) => {
|
||||
const q = Neu.parse(req.body);
|
||||
const r = await transaktion(pool, wer(req), async (c) => {
|
||||
if ((await c.query("select 1 from stundensatz_version where gilt_ab = $1", [q.gilt_ab])).rowCount) return null;
|
||||
return (await c.query("insert into stundensatz_version (name, gilt_ab, daten) values ($1, $2, $3) returning *", [q.name, q.gilt_ab, q.daten])).rows[0];
|
||||
});
|
||||
if (!r) return reply.status(409).send({ fehler: "An diesem Tag beginnt schon eine Version. Bitte ein anderes Datum wählen." });
|
||||
return reply.status(201).send(zeile(r));
|
||||
});
|
||||
|
||||
// M19: erste Fassung aus Hennings Excel (gilt ab 26.05.2026) und die Fassung nach S2 (gilt ab heute)
|
||||
app.post("/api/stundensaetze/excel", async (req, reply) => {
|
||||
const r = await transaktion(pool, wer(req), async (c) => {
|
||||
if ((await c.query("select 1 from stundensatz_version limit 1")).rowCount) return null;
|
||||
const excel = stundensaetzeAusExcel();
|
||||
await c.query("insert into stundensatz_version (name, gilt_ab, daten) values ($1, $2, $3)", ["Excel 26.05.2026", "2026-05-26", excel]);
|
||||
const ab = heute() > "2026-05-26" ? heute() : "2026-05-27";
|
||||
return (await c.query("insert into stundensatz_version (name, gilt_ab, daten) values ($1, $2, $3) returning *",
|
||||
["Akquise und Verwaltung als Kosten", ab, stundensaetzeNachS2(excel)])).rows[0];
|
||||
});
|
||||
if (!r) return reply.status(409).send({ fehler: "Es gibt schon Stundensätze." });
|
||||
return reply.status(201).send(zeile(r));
|
||||
});
|
||||
|
||||
const Aenderung = z.object({ name: kurz.min(1).optional(), gilt_ab: Datum.optional(), daten: DatenSchema.optional() });
|
||||
app.patch("/api/stundensaetze/:id", async (req, reply) => {
|
||||
const { id } = IdParam.parse(req.params);
|
||||
const q = Aenderung.parse(req.body);
|
||||
const r = await transaktion(pool, wer(req), async (c) => {
|
||||
if (q.gilt_ab && (await c.query("select 1 from stundensatz_version where gilt_ab = $1 and id <> $2", [q.gilt_ab, id])).rowCount) return "doppelt" as const;
|
||||
return (await c.query(`update stundensatz_version set name = coalesce($2, name), gilt_ab = coalesce($3, gilt_ab), daten = coalesce($4, daten)
|
||||
where id = $1 returning *`, [id, q.name ?? null, q.gilt_ab ?? null, q.daten ?? null])).rows[0];
|
||||
});
|
||||
if (r === "doppelt") return reply.status(409).send({ fehler: "An diesem Tag beginnt schon eine Version. Bitte ein anderes Datum wählen." });
|
||||
if (!r) return reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
return zeile(r);
|
||||
});
|
||||
|
||||
app.delete("/api/stundensaetze/:id", async (req, reply) => {
|
||||
const { id } = IdParam.parse(req.params);
|
||||
const r = await transaktion(pool, wer(req), (c) => c.query("delete from stundensatz_version where id = $1", [id]));
|
||||
if (!r.rowCount) return reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user