Berichte: Kostenstelle als Filter und als Gruppierung
All checks were successful
CI / Lint, Typen, Tests, Build (push) Successful in 12m3s
CI / Ausrollen (push) Successful in 11s
CI / Migrationen auf leerer Datenbank (push) Successful in 10m40s

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
This commit is contained in:
2026-10-20 16:08:02 +02:00
parent 0294c792a4
commit ae4918fc6d
7 changed files with 70 additions and 6 deletions

View File

@@ -31,6 +31,7 @@ type SearchParams = Record<string, string | string[] | undefined> & {
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}
/>
</Suspense>

View File

@@ -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,

View File

@@ -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) {
</option>
))}
</select>
{mode === "snapshot" && (
<select
aria-label="Nach Kostenstelle filtern"
value={props.filters.kostenstelle}
onChange={(e) => updateParams({ kostenstelle: e.target.value })}
className={CONTROL_CLASS}
>
<option value="">Alle Kostenstellen</option>
{props.kostenstellen.map((k) => (
<option key={k.id} value={k.id}>
{k.code} · {k.name}
</option>
))}
</select>
)}
{mode === "snapshot" && (
<>
<fieldset className="rounded border border-border px-3 py-2">

View File

@@ -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<string, { name: string; phone: string; relation: string | null }[]>();
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,

View File

@@ -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,

View File

@@ -32,6 +32,7 @@ export type GroupDimension =
| "c_level"
| "has_dependents"
| "teilzeit_art"
| "kostenstelle"
| "weekday";
/**
@@ -90,6 +91,7 @@ export const GROUP_LABELS: Record<GroupDimension, string> = {
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

View File

@@ -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");
});
});