Personal: Stammdaten, Arbeitszeitmodell, Abwesenheiten, Urlaubskonto (0.8.0)
Stufe 2, Schritt 2 (M1, M8, M4, M10-Konto). Modelle mit Gültig-ab, Abwesenheiten ganztags, halbtags oder stundengenau, Verfügbarkeit je Tag als Grundlage für T15/T16. Lohnkosten je Stunde nur für den Inhaber (nurInhaber in TABELLEN). 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:
@@ -21,6 +21,7 @@ import { benutzerFuerSicherung, pruefeStand, speichereStand } from "./stand.js";
|
||||
import { VERSION } from "./version.js";
|
||||
import { type Anmeldeart, registriereAnmeldung, werName } from "./anmeldung.js";
|
||||
import { istEingeteilt, registriereIch } from "./ich.js";
|
||||
import { registrierePersonal } from "./personal.js";
|
||||
|
||||
export interface Optionen {
|
||||
pool: Pool;
|
||||
@@ -62,6 +63,7 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
||||
registriereAnmeldung(app, pool, o.anmeldung ?? "passwort", o.einrichtungsCode);
|
||||
|
||||
registriereIch(app, pool, ladeEinstellungen);
|
||||
registrierePersonal(app, pool, ladeEinstellungen);
|
||||
|
||||
app.get("/api/status", async () => {
|
||||
await pool.query("select 1");
|
||||
@@ -158,20 +160,32 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
||||
const projektDerZahlung = async (c: Client, tabelle: string, id: string) =>
|
||||
tabelle === "zahlung" ? (await c.query("select projekt_id from zahlung where id = $1", [id])).rows[0]?.projekt_id as string | undefined : undefined;
|
||||
|
||||
// U2: Felder wie der Stundenlohn gehen nur an den Inhaber; Zahlen-Arrays (Arbeitszeitmodell) als Zahlen
|
||||
const fuer = (req: FastifyRequest, t: (typeof TABELLEN)[number]) => (z: Record<string, unknown> | undefined) => {
|
||||
if (!z) return z;
|
||||
const aus: Record<string, unknown> = { ...z };
|
||||
if (req.wer?.rolle !== "inhaber") for (const f of t.nurInhaber ?? []) delete aus[f];
|
||||
for (const [k, v] of Object.entries(aus)) if (Array.isArray(v)) aus[k] = v.map((x) => (typeof x === "string" && /^-?\d+(\.\d+)?$/.test(x) ? Number(x) : x));
|
||||
return aus;
|
||||
};
|
||||
const verboten = (req: FastifyRequest, t: (typeof TABELLEN)[number], daten: Record<string, unknown>) =>
|
||||
req.wer?.rolle !== "inhaber" && (t.nurInhaber ?? []).some((f) => daten[f] !== undefined);
|
||||
|
||||
for (const t of TABELLEN) {
|
||||
const felder = Object.keys(t.schema.shape);
|
||||
const teil = t.schema.partial();
|
||||
|
||||
app.get(`/api/${t.pfad}`, async () => (await pool.query(`select * from ${t.name} order by ${t.sortierung}`)).rows);
|
||||
app.get(`/api/${t.pfad}`, async (req) => (await pool.query(`select * from ${t.name} order by ${t.sortierung}`)).rows.map(fuer(req, t)));
|
||||
|
||||
app.get(`/api/${t.pfad}/:id`, async (req, reply) => {
|
||||
const { id } = IdParam.parse(req.params);
|
||||
const r = await pool.query(`select * from ${t.name} where id = $1`, [id]);
|
||||
return r.rows[0] ?? reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
return fuer(req, t)(r.rows[0]) ?? reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
});
|
||||
|
||||
app.post(`/api/${t.pfad}`, async (req, reply) => {
|
||||
const daten = t.schema.parse(req.body) as Record<string, unknown>;
|
||||
if (verboten(req, t, daten)) return reply.status(403).send({ fehler: "Dafür fehlt dir die Berechtigung." });
|
||||
const spalten = felder.filter((f) => daten[f] !== undefined);
|
||||
const row = await transaktion(pool, benutzer(req), async (c) => {
|
||||
if (t.name === "vorgang_vorgaenger") await pruefeVerknuepfung(c, daten as { vorgang_id: string; vorgaenger_id: string });
|
||||
@@ -184,12 +198,13 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
||||
)).rows[0], [], t.name === "zahlung" ? daten.projekt_id as string : undefined);
|
||||
});
|
||||
if (t.name === "projekt") return reply.status(201).send(await mitOrdner(row, benutzer(req)));
|
||||
return reply.status(201).send(row);
|
||||
return reply.status(201).send(fuer(req, t)(row));
|
||||
});
|
||||
|
||||
app.patch(`/api/${t.pfad}/:id`, async (req, reply) => {
|
||||
const { id } = IdParam.parse(req.params);
|
||||
const daten = teil.parse(req.body) as Record<string, unknown>;
|
||||
if (verboten(req, t, daten)) return reply.status(403).send({ fehler: "Dafür fehlt dir die Berechtigung." });
|
||||
const spalten = felder.filter((f) => daten[f] !== undefined);
|
||||
if (spalten.length === 0) return reply.status(400).send({ fehler: "Keine Änderung angegeben" });
|
||||
const row = await transaktion(pool, benutzer(req), async (c) => {
|
||||
@@ -205,7 +220,7 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
||||
)).rows[0], t.name === "vorgang" && daten.start !== undefined ? [id] : [], zahlungVon);
|
||||
});
|
||||
if (row && t.name === "projekt" && (daten.nummer || daten.bezeichnung || daten.status)) return mitOrdner(row, benutzer(req));
|
||||
return row ?? reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
return fuer(req, t)(row) ?? reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
});
|
||||
|
||||
app.delete(`/api/${t.pfad}/:id`, async (req, reply) => {
|
||||
|
||||
71
src/server/migrations/008_personal.sql
Normal file
71
src/server/migrations/008_personal.sql
Normal file
@@ -0,0 +1,71 @@
|
||||
-- Stufe 2, Personal: Stammdaten je Mitarbeiter (M1), Arbeitszeitmodell mit Gültig-ab (M8),
|
||||
-- Abwesenheiten mit Urlaubsantrag (M4, M10) und Urlaubskonto je Jahr.
|
||||
|
||||
create table mitarbeiter (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
kontakt_id uuid not null unique references kontakt (id) on delete cascade,
|
||||
personalnummer text,
|
||||
eintritt date,
|
||||
austritt date,
|
||||
-- angestellt: Pausenregel M3 und Übergabe ans Steuerbüro; der Inhaber nicht
|
||||
angestellt boolean not null default true,
|
||||
-- Lohnkosten je Stunde in Cent, sieht nur der Inhaber (U2)
|
||||
lohn_cent bigint check (lohn_cent >= 0),
|
||||
urlaubstage numeric(4, 1) not null default 30 check (urlaubstage >= 0),
|
||||
angelegt_am timestamptz not null default now(),
|
||||
geaendert_am timestamptz not null default now(),
|
||||
geaendert_von text not null default 'system'
|
||||
);
|
||||
create trigger mitarbeiter_protokoll before insert or update or delete on mitarbeiter
|
||||
for each row execute function stempel_und_protokoll();
|
||||
|
||||
create table arbeitszeitmodell (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
kontakt_id uuid not null references kontakt (id) on delete cascade,
|
||||
gueltig_ab date not null,
|
||||
-- Stunden Mo … So
|
||||
stunden numeric(4, 2)[] not null check (array_length(stunden, 1) = 7 and 0 <= all (stunden) and 24 >= all (stunden)),
|
||||
angelegt_am timestamptz not null default now(),
|
||||
geaendert_am timestamptz not null default now(),
|
||||
geaendert_von text not null default 'system',
|
||||
unique (kontakt_id, gueltig_ab)
|
||||
);
|
||||
create trigger arbeitszeitmodell_protokoll before insert or update or delete on arbeitszeitmodell
|
||||
for each row execute function stempel_und_protokoll();
|
||||
|
||||
create table abwesenheit (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
kontakt_id uuid not null references kontakt (id) on delete cascade,
|
||||
art text not null check (art in ('urlaub', 'krank', 'schule', 'frei', 'sperre')),
|
||||
von date not null,
|
||||
bis date not null,
|
||||
halbtag boolean not null default false,
|
||||
stunden numeric(4, 2) check (stunden > 0),
|
||||
status text not null default 'genehmigt' check (status in ('beantragt', 'genehmigt', 'abgelehnt')),
|
||||
notiz text,
|
||||
-- Antwort auf einen Urlaubsantrag, sieht der Mitarbeiter
|
||||
antwort text,
|
||||
angelegt_am timestamptz not null default now(),
|
||||
geaendert_am timestamptz not null default now(),
|
||||
geaendert_von text not null default 'system',
|
||||
check (bis >= von),
|
||||
check (not halbtag or von = bis)
|
||||
);
|
||||
create index abwesenheit_kontakt on abwesenheit (kontakt_id, von);
|
||||
create trigger abwesenheit_protokoll before insert or update or delete on abwesenheit
|
||||
for each row execute function stempel_und_protokoll();
|
||||
|
||||
-- Übertrag aus dem Vorjahr und abweichender Anspruch je Jahr; ohne Zeile gilt mitarbeiter.urlaubstage
|
||||
create table urlaubskonto (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
kontakt_id uuid not null references kontakt (id) on delete cascade,
|
||||
jahr integer not null,
|
||||
anspruch numeric(4, 1),
|
||||
uebertrag numeric(4, 1) not null default 0,
|
||||
angelegt_am timestamptz not null default now(),
|
||||
geaendert_am timestamptz not null default now(),
|
||||
geaendert_von text not null default 'system',
|
||||
unique (kontakt_id, jahr)
|
||||
);
|
||||
create trigger urlaubskonto_protokoll before insert or update or delete on urlaubskonto
|
||||
for each row execute function stempel_und_protokoll();
|
||||
79
src/server/personal.ts
Normal file
79
src/server/personal.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
// Personal auf dem Server: Übersicht je Mitarbeiter mit Arbeitszeitmodell, Urlaubskonto und Verfügbarkeit.
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import type { Pool } from "./db.js";
|
||||
import type { Einstellungen } from "./einstellungen.js";
|
||||
import { type Abwesenheit, type Modell, standardStunden, urlaubskonto, verfuegbarkeit, modellAm } from "../domain/personal.js";
|
||||
import { plusTage } from "../domain/datum.js";
|
||||
|
||||
const Tag = z.string().regex(/^\d{4}-\d{2}-\d{2}$/);
|
||||
|
||||
export interface Personalstand {
|
||||
modelle: Modell[];
|
||||
abwesenheiten: (Abwesenheit & { id: string; notiz: string | null; antwort: string | null })[];
|
||||
stammdaten: Map<string, Record<string, any>>;
|
||||
konten: Map<string, Record<string, any>>;
|
||||
}
|
||||
|
||||
/** Modelle, Abwesenheiten, Stammdaten und Urlaubskonten; optional nur für einen Kontakt */
|
||||
export async function ladePersonal(pool: Pick<Pool, "query">, kontaktId?: string): Promise<Personalstand> {
|
||||
const wo = kontaktId ? "where kontakt_id = $1" : "";
|
||||
const p = kontaktId ? [kontaktId] : [];
|
||||
const [m, a, s, k] = await Promise.all([
|
||||
pool.query(`select kontakt_id, gueltig_ab, stunden from arbeitszeitmodell ${wo} order by gueltig_ab`, p),
|
||||
pool.query(`select * from abwesenheit ${wo} order by von`, p),
|
||||
pool.query(`select * from mitarbeiter ${wo}`, p),
|
||||
pool.query(`select * from urlaubskonto ${wo}`, p),
|
||||
]);
|
||||
return {
|
||||
modelle: m.rows.map((r) => ({ ...r, stunden: (r.stunden as unknown[]).map(Number) })),
|
||||
abwesenheiten: a.rows.map((r) => ({ ...r, stunden: r.stunden == null ? null : Number(r.stunden) })),
|
||||
stammdaten: new Map(s.rows.map((r) => [r.kontakt_id, r])),
|
||||
konten: new Map(k.rows.map((r) => [`${r.kontakt_id}/${r.jahr}`, r])),
|
||||
};
|
||||
}
|
||||
|
||||
export function kontoFuer(st: Personalstand, kontaktId: string, jahr: number, heute: string, stundenProTag: number) {
|
||||
const s = st.stammdaten.get(kontaktId);
|
||||
const k = st.konten.get(`${kontaktId}/${jahr}`);
|
||||
const anspruch = k?.anspruch != null ? Number(k.anspruch) : s ? Number(s.urlaubstage) : 30;
|
||||
return urlaubskonto(kontaktId, jahr, heute, anspruch, Number(k?.uebertrag ?? 0), st.abwesenheiten, st.modelle, stundenProTag);
|
||||
}
|
||||
|
||||
export function registrierePersonal(app: FastifyInstance, pool: Pool, einstellungen: () => Promise<Einstellungen>): void {
|
||||
// Liste für die Seite Personal: je Mitarbeiter Modell, Urlaub und was heute ist
|
||||
app.get("/api/personal", async (req) => {
|
||||
const q = z.object({ heute: Tag.optional() }).parse(req.query);
|
||||
const heute = q.heute ?? new Date().toISOString().slice(0, 10);
|
||||
const e = await einstellungen();
|
||||
const [kontakte, st] = await Promise.all([
|
||||
pool.query("select k.id, k.name, k.telefon, k.email, b.id as benutzer_id, b.anmeldename, b.rolle as benutzer_rolle from kontakt k left join benutzer b on b.kontakt_id = k.id where k.rolle = 'mitarbeiter' order by k.name"),
|
||||
ladePersonal(pool),
|
||||
]);
|
||||
const inhaber = req.wer?.rolle === "inhaber";
|
||||
return kontakte.rows.map((k) => {
|
||||
const s = st.stammdaten.get(k.id) ?? null;
|
||||
const [tag] = verfuegbarkeit(st.modelle, st.abwesenheiten, k.id, heute, heute, e.stundenProTag);
|
||||
return {
|
||||
...k,
|
||||
stammdaten: s && !inhaber ? { ...s, lohn_cent: undefined } : s,
|
||||
stunden: modellAm(st.modelle, k.id, heute)?.stunden ?? standardStunden(e.stundenProTag),
|
||||
eigenesModell: !!modellAm(st.modelle, k.id, heute),
|
||||
urlaub: kontoFuer(st, k.id, Number(heute.slice(0, 4)), heute, e.stundenProTag),
|
||||
heute: tag,
|
||||
antraege: st.abwesenheiten.filter((a) => a.kontakt_id === k.id && a.status === "beantragt").length,
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
// Verfügbare Stunden je Person und Tag (M8, Grundlage für T15/T16)
|
||||
app.get("/api/verfuegbarkeit", async (req) => {
|
||||
const q = z.object({ von: Tag, bis: Tag.optional(), kontakt_id: z.uuid().optional() }).parse(req.query);
|
||||
const bis = q.bis ?? plusTage(q.von, 27);
|
||||
if (bis < q.von || bis > plusTage(q.von, 400)) throw Object.assign(new Error("Zeitraum ungültig"), { statusCode: 400 });
|
||||
const e = await einstellungen();
|
||||
const kontakte = (await pool.query("select id, name from kontakt where rolle = 'mitarbeiter' and ($1::uuid is null or id = $1) order by name", [q.kontakt_id ?? null])).rows;
|
||||
const st = await ladePersonal(pool, q.kontakt_id);
|
||||
return kontakte.map((k) => ({ kontakt_id: k.id, name: k.name, tage: verfuegbarkeit(st.modelle, st.abwesenheiten, k.id, q.von, bis, e.stundenProTag) }));
|
||||
});
|
||||
}
|
||||
@@ -12,6 +12,8 @@ export interface Tabelle {
|
||||
pfad: string;
|
||||
schema: z.ZodObject;
|
||||
sortierung: string;
|
||||
/** Felder, die nur der Inhaber sieht und ändert (U2: Löhne) */
|
||||
nurInhaber?: string[];
|
||||
}
|
||||
|
||||
export const TABELLEN: Tabelle[] = [
|
||||
@@ -180,4 +182,57 @@ export const TABELLEN: Tabelle[] = [
|
||||
erledigt_am: z.iso.datetime({ offset: true }).nullable().optional(),
|
||||
}),
|
||||
},
|
||||
// Stufe 2: Personal (M1, M8, M4, M10)
|
||||
{
|
||||
name: "mitarbeiter",
|
||||
pfad: "mitarbeiter",
|
||||
sortierung: "angelegt_am",
|
||||
nurInhaber: ["lohn_cent"],
|
||||
schema: z.object({
|
||||
kontakt_id: id,
|
||||
personalnummer: optText,
|
||||
eintritt: datum.nullable().optional(),
|
||||
austritt: datum.nullable().optional(),
|
||||
angestellt: z.boolean().optional(),
|
||||
lohn_cent: cent.min(0).nullable().optional(),
|
||||
urlaubstage: z.number().min(0).max(60).optional(),
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "arbeitszeitmodell",
|
||||
pfad: "arbeitszeitmodelle",
|
||||
sortierung: "kontakt_id, gueltig_ab",
|
||||
schema: z.object({
|
||||
kontakt_id: id,
|
||||
gueltig_ab: datum,
|
||||
stunden: z.array(z.number().min(0).max(24)).length(7, "Stunden für Montag bis Sonntag"),
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "abwesenheit",
|
||||
pfad: "abwesenheiten",
|
||||
sortierung: "von, kontakt_id",
|
||||
schema: z.object({
|
||||
kontakt_id: id,
|
||||
art: z.enum(["urlaub", "krank", "schule", "frei", "sperre"]),
|
||||
von: datum,
|
||||
bis: datum,
|
||||
halbtag: z.boolean().optional(),
|
||||
stunden: z.number().positive().max(24).nullable().optional(),
|
||||
status: z.enum(["beantragt", "genehmigt", "abgelehnt"]).optional(),
|
||||
notiz: optText,
|
||||
antwort: optText,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "urlaubskonto",
|
||||
pfad: "urlaubskonten",
|
||||
sortierung: "kontakt_id, jahr",
|
||||
schema: z.object({
|
||||
kontakt_id: id,
|
||||
jahr: z.number().int().min(2000).max(2100),
|
||||
anspruch: z.number().min(0).max(60).nullable().optional(),
|
||||
uebertrag: z.number().min(-60).max(60).optional(),
|
||||
}),
|
||||
},
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user