From ae4918fc6de33844d41fb703e76686896e0beec3 Mon Sep 17 00:00:00 2001 From: Andrei Laas Date: Tue, 20 Oct 2026 16:08:02 +0200 Subject: [PATCH] Berichte: Kostenstelle als Filter und als Gruppierung Im Bestand laesst sich nach Kostenstelle filtern ("Alle Kostenstellen") und gruppieren ("Kostenstelle", Planstellen ohne Kontierung als eigene Gruppe). Gemeint ist die Kontierung der Planstelle, auf der jemand am Stichtag sitzt. Der Filter gilt auch fuer die Exporte dieser Seite. Geprueft: lint, tsc, 950 Tests, Uebergabe-Waechter --- app/(app)/reports/page.tsx | 13 ++++++++++--- app/api/export/report/route.ts | 1 + components/reports/ReportsPageClient.tsx | 19 ++++++++++++++++++- lib/export-auswahl.ts | 8 +++++++- lib/reports-data.ts | 11 ++++++++++- lib/reports.ts | 12 ++++++++++++ tests/unit/reports.test.ts | 12 ++++++++++++ 7 files changed, 70 insertions(+), 6 deletions(-) diff --git a/app/(app)/reports/page.tsx b/app/(app)/reports/page.tsx index d4be9fc..abc47d2 100644 --- a/app/(app)/reports/page.tsx +++ b/app/(app)/reports/page.tsx @@ -31,6 +31,7 @@ type SearchParams = Record & { split?: string; division?: string; location?: string; + kostenstelle?: string; status?: string; asOf?: string; eventType?: string; @@ -66,8 +67,8 @@ export default async function ReportsPage({ searchParams }: { searchParams: Prom // Alles in einer Transaktion — dort gilt der Sitzungskontext, und der // Lesestand ist über alle Abfragen hinweg derselbe. - const { lookups, units, locations, events, employees, savedReports } = await withUser(userId, async (tx) => { - const [{ lookups, units, locations }, events, employees, savedReports] = await Promise.all([ + const { lookups, units, locations, events, employees, savedReports, kostenstellen } = await withUser(userId, async (tx) => { + const [{ lookups, units, locations }, events, employees, savedReports, kostenstellen] = await Promise.all([ loadOrgLookups(tx), mode === "events" ? loadEventHistory(tx, { @@ -83,6 +84,7 @@ export default async function ReportsPage({ searchParams }: { searchParams: Prom ? loadSnapshotEmployees(tx, { division: params.division, location: params.location, + kostenstelle: params.kostenstelle, status: params.status, criteria, asOf, @@ -96,8 +98,11 @@ export default async function ReportsPage({ searchParams }: { searchParams: Prom .orderBy("created_at", "desc") .execute() : Promise.resolve([]), + // Für den Filter: alle Kostenstellen, auch beendete — ein Stichtag in + // der Vergangenheit kann eine meinen, die es heute nicht mehr gibt. + tx.selectFrom("cost_centers").select(["id", "code", "name"]).orderBy("code").execute(), ]); - return { lookups, units, locations, events, employees, savedReports }; + return { lookups, units, locations, events, employees, savedReports, kostenstellen }; }); if (mode === "events") { @@ -136,6 +141,7 @@ export default async function ReportsPage({ searchParams }: { searchParams: Prom filters={{ division: params.division ?? "", location: params.location ?? "", + kostenstelle: params.kostenstelle ?? "", status: params.status ?? "", }} criteria={criteria} @@ -144,6 +150,7 @@ export default async function ReportsPage({ searchParams }: { searchParams: Prom recordCount={employees.length} units={units} locations={locations} + kostenstellen={kostenstellen} savedReports={savedReports} /> diff --git a/app/api/export/report/route.ts b/app/api/export/report/route.ts index bb07773..4b794d4 100644 --- a/app/api/export/report/route.ts +++ b/app/api/export/report/route.ts @@ -73,6 +73,7 @@ export async function GET(request: NextRequest) { const employees = await loadSnapshotEmployees(tx, { division: params.get("division") ?? undefined, location: params.get("location") ?? undefined, + kostenstelle: params.get("kostenstelle") ?? undefined, status: params.get("status") ?? undefined, criteria: parseCriteria((k) => params.get(k)), asOf, diff --git a/components/reports/ReportsPageClient.tsx b/components/reports/ReportsPageClient.tsx index c15e9aa..96d7701 100644 --- a/components/reports/ReportsPageClient.tsx +++ b/components/reports/ReportsPageClient.tsx @@ -93,7 +93,9 @@ type SnapshotProps = CommonProps & { group: GroupDimension; split: GroupDimension | ""; asOf: string; - filters: { division: string; location: string; status: string }; + filters: { division: string; location: string; kostenstelle: string; status: string }; + /** Für den Filter nach Kostenstelle — die Kontierung der Planstelle zum Stichtag. */ + kostenstellen: { id: string; code: string; name: string }[]; criteria: Criteria; }; @@ -638,6 +640,21 @@ export function ReportsPageClient(props: ReportsPageClientProps) { ))} + {mode === "snapshot" && ( + + )} {mode === "snapshot" && ( <>
diff --git a/lib/export-auswahl.ts b/lib/export-auswahl.ts index a354db4..dd32367 100644 --- a/lib/export-auswahl.ts +++ b/lib/export-auswahl.ts @@ -1,3 +1,4 @@ +import { loadKontierungen } from "./cost-centers"; import type { Tx } from "./db"; import { derivedStatusFilter } from "./employee-status-filter"; import { fmtName, todayIso } from "./format"; @@ -38,6 +39,7 @@ export function exportParameter(params: URLSearchParams): ExportParameter { filters: { division: params.get("division") ?? undefined, location: params.get("location") ?? undefined, + kostenstelle: params.get("kostenstelle") ?? undefined, status: params.get("status") ?? undefined, criteria: parseCriteria((k) => params.get(k)), }, @@ -86,7 +88,7 @@ export async function ladeExportMitarbeiter( return applyCriteria(q, criteria); } - const [employees, lookupResult, allEmployees, dependentsCounts, placements, lines, hrKonten, kontakte] = await Promise.all([ + const [employees, lookupResult, allEmployees, dependentsCounts, placements, lines, hrKonten, kontakte, kontierung] = await Promise.all([ employeeQuery().execute(), loadOrgLookups(tx), // Absichtlich schmal: die Liste umfasst **alle** Personen, nicht nur die @@ -103,6 +105,9 @@ export async function ladeExportMitarbeiter( .orderBy("employee_id") .orderBy("created_at") .execute(), + // Nur für den Filter nach Kostenstelle — wie im Bericht die Kontierung + // der Planstelle zum Stichtag. + filters.kostenstelle ? loadKontierungen(tx, { asOf: stichtag }) : Promise.resolve(null), ]); const notfallkontakte = new Map(); for (const k of kontakte) { @@ -121,6 +126,7 @@ export async function ladeExportMitarbeiter( const orgUnitId = placement?.current ? placement.orgUnitId : null; if (allowedUnits && (!orgUnitId || !allowedUnits.has(orgUnitId))) return []; if (!passtImSpeicher({ work_days: e.work_days, dependentsCount: dependentsCounts.get(e.id) ?? 0 }, criteria)) return []; + if (kontierung && (!placement?.current || kontierung.get(placement.positionId)?.id !== filters.kostenstelle)) return []; return [ { ...e, diff --git a/lib/reports-data.ts b/lib/reports-data.ts index 3a589ca..3f203d5 100644 --- a/lib/reports-data.ts +++ b/lib/reports-data.ts @@ -2,6 +2,7 @@ import type { SelectQueryBuilder } from "kysely"; import type { Schema } from "./db/schema"; import type { Tx } from "./db"; import { ancestorsOf, loadOrgMaps, subtreeOf, type OrgMaps } from "./org"; +import { loadKontierungen } from "./cost-centers"; import { loadPlacements } from "./placement"; import type { Austrittsart } from "./types"; import { LEERE_CRITERIA, passtImSpeicher, type Criteria } from "./report-criteria"; @@ -40,6 +41,8 @@ export type ReportFilters = { /** Id einer Organisationseinheit; wirkt auf die Einheit *und alles darunter*. */ division?: string; location?: string; + /** Id einer Kostenstelle; gemeint ist die Kontierung der Planstelle zum Stichtag. */ + kostenstelle?: string; status?: string; /** Alles Weitere — siehe lib/report-criteria.ts. */ criteria?: Criteria; @@ -235,12 +238,15 @@ export async function loadSnapshotEmployees(tx: Tx, filters: SnapshotFilters): P return applyCriteria(q, filters.criteria ?? LEERE_CRITERIA); } - const [data, dependentsCounts, placements, orgMaps, hrNamen] = await Promise.all([ + const [data, dependentsCounts, placements, orgMaps, hrNamen, kontierung] = await Promise.all([ snapshotQuery().execute(), loadDependentsCounts(tx), loadPlacements(tx, { asOf }), filters.division ? loadOrgMaps(tx) : Promise.resolve(null), loadHrNamen(tx), + // Die Kostenstelle hängt an der Planstelle, nicht an der Person: gelesen + // wird die Kontierung der Planstelle, auf der jemand am Stichtag sitzt. + loadKontierungen(tx, { asOf }), ]); // Der Bereichsfilter meint den ganzen Teilbaum: „Produktion" schliesst @@ -257,8 +263,11 @@ export async function loadSnapshotEmployees(tx: Tx, filters: SnapshotFilters): P const orgUnitId = placement?.current ? placement.orgUnitId : null; if (allowedUnits && (!orgUnitId || !allowedUnits.has(orgUnitId))) continue; if (!passtImSpeicher({ work_days: e.work_days, dependentsCount: dependentsCounts.get(e.id) ?? 0 }, criteria)) continue; + const kst = placement?.current ? (kontierung.get(placement.positionId) ?? null) : null; + if (filters.kostenstelle && kst?.id !== filters.kostenstelle) continue; withDerivedStatus.push({ + kostenstelle: kst ? `${kst.code} · ${kst.name}` : null, id: e.id, first_name: e.first_name, last_name: e.last_name, diff --git a/lib/reports.ts b/lib/reports.ts index bc6ec02..fba555f 100644 --- a/lib/reports.ts +++ b/lib/reports.ts @@ -32,6 +32,7 @@ export type GroupDimension = | "c_level" | "has_dependents" | "teilzeit_art" + | "kostenstelle" | "weekday"; /** @@ -90,6 +91,7 @@ export const GROUP_LABELS: Record = { c_level: "C-Level", has_dependents: "Hat Angehörige", teilzeit_art: "Teilzeitvariante", + kostenstelle: "Kostenstelle", weekday: "Wochentag", }; @@ -137,6 +139,12 @@ export type ReportEmployee = { is_laterale_fuehrung: boolean; is_c_level: boolean; teilzeit_art: string | null; + /** + * Die Kostenstelle der Planstelle zum Stichtag, als „Nummer · Bezeichnung"; + * null ohne Planstelle oder ohne Kontierung. Optional, damit Testbestände + * ohne die Angabe gültig bleiben. + */ + kostenstelle?: string | null; dependents_count: number; /** * Der Name der zuständigen HR — beim Laden aufgelöst, weil der Bericht @@ -262,6 +270,10 @@ export function groupKeyFor(e: ReportEmployee, dim: GroupDimension, lookups: Org case "teilzeit_art": // „Keine" statt „–": die Antwort ist hier eine Aussage, kein Fehlen. return e.teilzeit_art ?? "Keine"; + case "kostenstelle": + // Eine eigene Gruppe: Planstellen ohne Kontierung sind genau die, nach + // denen man hier sucht. + return e.kostenstelle ?? "Ohne Kostenstelle"; case "weekday": // Not a strict partition — see groupKeysFor, which aggregateReport // actually uses. This single-key fallback only covers a direct diff --git a/tests/unit/reports.test.ts b/tests/unit/reports.test.ts index 9fe3be4..4f2a20b 100644 --- a/tests/unit/reports.test.ts +++ b/tests/unit/reports.test.ts @@ -454,3 +454,15 @@ describe("Gruppierung nach Teilzeitvariante", () => { expect(groupKeyFor(emp({ teilzeit_art: null }), "teilzeit_art", lookups)).toBe("Keine"); }); }); + +describe("Kostenstelle im Bericht", () => { + const lookups = { divisionName: new Map(), departmentName: new Map(), teamName: new Map(), locationName: new Map() }; + + it("gruppiert nach der Kostenstelle der Planstelle", () => { + expect(groupKeyFor(emp({ kostenstelle: "11049 · Vertrieb" }), "kostenstelle", lookups)).toBe("11049 · Vertrieb"); + }); + + it("fasst Planstellen ohne Kontierung zu einer eigenen Gruppe", () => { + expect(groupKeyFor(emp({ kostenstelle: null }), "kostenstelle", lookups)).toBe("Ohne Kostenstelle"); + }); +}); \ No newline at end of file