forked from fahrPool/fahrPool
feat: add Zeiterfassung page with Kommen/Gehen, Korrektur and CSV export
This commit is contained in:
parent
c3cdfc9ac7
commit
02dfc2b23e
|
|
@ -0,0 +1,187 @@
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { kannZeiterfassungNutzen } from "@/lib/auth/roles";
|
||||||
|
import {
|
||||||
|
listSchichten,
|
||||||
|
listOffeneSchichten,
|
||||||
|
findeOffeneSchicht,
|
||||||
|
findeAuftraegeInSchicht,
|
||||||
|
} from "@/lib/zeiterfassung/queries";
|
||||||
|
import { listFahrerNutzer } from "@/lib/dashboard/queries";
|
||||||
|
import { gruppiereProTag, gruppiereProWoche } from "@/lib/zeiterfassung/aggregation";
|
||||||
|
import { nettoMinuten, pauseMinuten } from "@/lib/zeiterfassung/zeit-berechnung";
|
||||||
|
import { Card } from "@/components/ui/Card";
|
||||||
|
import { Button } from "@/components/ui/Button";
|
||||||
|
import { Alert } from "@/components/ui/Alert";
|
||||||
|
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||||||
|
import { KommenGehenButton } from "@/components/zeiterfassung/KommenGehenButton";
|
||||||
|
import { CsvExportButton } from "@/components/zeiterfassung/CsvExportButton";
|
||||||
|
import { KorrekturFormular } from "@/components/zeiterfassung/KorrekturFormular";
|
||||||
|
|
||||||
|
function wochenStart(datum: Date): Date {
|
||||||
|
const kopie = new Date(Date.UTC(datum.getUTCFullYear(), datum.getUTCMonth(), datum.getUTCDate()));
|
||||||
|
const wochentag = kopie.getUTCDay() || 7;
|
||||||
|
kopie.setUTCDate(kopie.getUTCDate() - wochentag + 1);
|
||||||
|
return kopie;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function ZeiterfassungPage({
|
||||||
|
searchParams,
|
||||||
|
}: {
|
||||||
|
searchParams: Promise<{ von?: string; bis?: string; fahrer_id?: string; error?: string }>;
|
||||||
|
}) {
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (profileResult.status !== "ok" || !kannZeiterfassungNutzen(profileResult.profile.role)) {
|
||||||
|
redirect(`/auftraege?error=${encodeURIComponent("Zeiterfassung ist fuer diese Rolle nicht verfuegbar.")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { von, bis, fahrer_id, error } = await searchParams;
|
||||||
|
const heute = new Date();
|
||||||
|
const vonDatum = von ?? wochenStart(heute).toISOString().slice(0, 10);
|
||||||
|
const bisDatum = bis ?? heute.toISOString().slice(0, 10);
|
||||||
|
const zeitraum = { von: `${vonDatum}T00:00:00.000Z`, bis: `${bisDatum}T23:59:59.999Z` };
|
||||||
|
|
||||||
|
const istDisponentOderAdmin = profileResult.profile.role === "admin" || profileResult.profile.role === "disponent";
|
||||||
|
|
||||||
|
const [schichten, offeneSchicht, fahrerListe, offeneSchichtenAlle] = await Promise.all([
|
||||||
|
listSchichten(zeitraum, istDisponentOderAdmin ? fahrer_id : profileResult.profile.id),
|
||||||
|
istDisponentOderAdmin ? Promise.resolve(null) : findeOffeneSchicht(profileResult.profile.id),
|
||||||
|
istDisponentOderAdmin ? listFahrerNutzer() : Promise.resolve([]),
|
||||||
|
istDisponentOderAdmin ? listOffeneSchichten() : Promise.resolve([]),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const auftraegeProSchicht = await Promise.all(
|
||||||
|
schichten.map(async (schicht) => ({ schichtId: schicht.id, auftraege: await findeAuftraegeInSchicht(schicht) }))
|
||||||
|
);
|
||||||
|
const auftraegeMap = new Map(auftraegeProSchicht.map((eintrag) => [eintrag.schichtId, eintrag.auftraege]));
|
||||||
|
|
||||||
|
const fuerAggregation = schichten.map((schicht) => ({
|
||||||
|
fahrerId: schicht.fahrer_id,
|
||||||
|
fahrerName: schicht.fahrer.name,
|
||||||
|
kommtUm: schicht.kommt_um,
|
||||||
|
gehtUm: schicht.geht_um,
|
||||||
|
}));
|
||||||
|
const tagesSummen = gruppiereProTag(fuerAggregation);
|
||||||
|
const wochenSummen = gruppiereProWoche(fuerAggregation);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mx-auto max-w-3xl p-4 md:p-8">
|
||||||
|
<h1 className="mb-6 text-xl font-semibold text-asphalt-grey">Zeiterfassung</h1>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="mb-6">
|
||||||
|
<Alert>{error}</Alert>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!istDisponentOderAdmin && (
|
||||||
|
<div className="mb-6">
|
||||||
|
<KommenGehenButton offeneSchichtId={offeneSchicht?.id ?? null} />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<form className="mb-6 flex flex-wrap items-end gap-3">
|
||||||
|
<FormField label="Von" htmlFor="von">
|
||||||
|
<input id="von" name="von" type="date" defaultValue={vonDatum} className={FIELD_CLASSES} />
|
||||||
|
</FormField>
|
||||||
|
<FormField label="Bis" htmlFor="bis">
|
||||||
|
<input id="bis" name="bis" type="date" defaultValue={bisDatum} className={FIELD_CLASSES} />
|
||||||
|
</FormField>
|
||||||
|
{istDisponentOderAdmin && (
|
||||||
|
<FormField label="Fahrer" htmlFor="fahrer_id">
|
||||||
|
<select id="fahrer_id" name="fahrer_id" defaultValue={fahrer_id ?? ""} className={FIELD_CLASSES}>
|
||||||
|
<option value="">Alle Fahrer</option>
|
||||||
|
{fahrerListe.map((fahrer) => (
|
||||||
|
<option key={fahrer.id} value={fahrer.id}>
|
||||||
|
{fahrer.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</FormField>
|
||||||
|
)}
|
||||||
|
<Button type="submit" fullWidth={false}>
|
||||||
|
Filtern
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
{istDisponentOderAdmin && offeneSchichtenAlle.length > 0 && (
|
||||||
|
<section className="mb-8">
|
||||||
|
<h2 className="mb-3 text-lg font-semibold text-asphalt-grey">Offene Schichten (Korrektur nötig)</h2>
|
||||||
|
<ul className="grid grid-cols-1 gap-3">
|
||||||
|
{offeneSchichtenAlle.map((schicht) => (
|
||||||
|
<li key={schicht.id}>
|
||||||
|
<Card>
|
||||||
|
<p className="text-asphalt-grey">
|
||||||
|
{schicht.fahrer.name} — Kommt: {new Date(schicht.kommt_um).toLocaleString("de-DE")}
|
||||||
|
</p>
|
||||||
|
<KorrekturFormular schichtId={schicht.id} />
|
||||||
|
</Card>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<section className="mb-8">
|
||||||
|
<h2 className="mb-3 text-lg font-semibold text-asphalt-grey">Wochensummen</h2>
|
||||||
|
<ul className="grid grid-cols-1 gap-2">
|
||||||
|
{wochenSummen.map((summe) => (
|
||||||
|
<li key={`${summe.woche}-${summe.fahrerId}`} className="text-sm text-asphalt-grey">
|
||||||
|
{summe.woche} — {summe.fahrerName}: {(summe.nettoMinuten / 60).toFixed(1)} Std
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section className="mb-8">
|
||||||
|
<h2 className="mb-3 text-lg font-semibold text-asphalt-grey">Tagessummen</h2>
|
||||||
|
<ul className="grid grid-cols-1 gap-2">
|
||||||
|
{tagesSummen.map((summe) => (
|
||||||
|
<li key={`${summe.datum}-${summe.fahrerId}`} className="text-sm text-asphalt-grey">
|
||||||
|
{summe.datum} — {summe.fahrerName}: {(summe.nettoMinuten / 60).toFixed(1)} Std
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section>
|
||||||
|
<div className="mb-3 flex items-center justify-between">
|
||||||
|
<h2 className="text-lg font-semibold text-asphalt-grey">Schichten</h2>
|
||||||
|
{istDisponentOderAdmin && <CsvExportButton von={zeitraum.von} bis={zeitraum.bis} fahrerId={fahrer_id} />}
|
||||||
|
</div>
|
||||||
|
<ul className="grid grid-cols-1 gap-3">
|
||||||
|
{schichten.map((schicht) => (
|
||||||
|
<li key={schicht.id}>
|
||||||
|
<Card>
|
||||||
|
<p className="text-asphalt-grey">
|
||||||
|
{schicht.fahrer.name}: {new Date(schicht.kommt_um).toLocaleString("de-DE")}
|
||||||
|
{" – "}
|
||||||
|
{schicht.geht_um ? new Date(schicht.geht_um).toLocaleString("de-DE") : "läuft noch"}
|
||||||
|
</p>
|
||||||
|
{schicht.geht_um && (
|
||||||
|
<p className="mt-1 text-xs text-asphalt-grey/70">
|
||||||
|
Netto: {(nettoMinuten(schicht.kommt_um, schicht.geht_um) / 60).toFixed(1)} Std (Pause:{" "}
|
||||||
|
{pauseMinuten(
|
||||||
|
Math.round((new Date(schicht.geht_um).getTime() - new Date(schicht.kommt_um).getTime()) / 60000)
|
||||||
|
)}{" "}
|
||||||
|
Min)
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
{schicht.korrigiert_von && schicht.korrigiert_am && (
|
||||||
|
<p className="mt-1 text-xs text-status-danger">
|
||||||
|
Korrigiert am {new Date(schicht.korrigiert_am).toLocaleString("de-DE")}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
{(auftraegeMap.get(schicht.id) ?? []).map((auftrag) => (
|
||||||
|
<p key={auftrag.id} className="mt-1 text-xs text-asphalt-grey/70">
|
||||||
|
Auftrag: {auftrag.abholort_adresse} → {auftrag.zielort_adresse}
|
||||||
|
</p>
|
||||||
|
))}
|
||||||
|
</Card>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState, useTransition } from "react";
|
||||||
|
import { exportiereCsv } from "@/app/zeiterfassung/actions";
|
||||||
|
import { Button } from "@/components/ui/Button";
|
||||||
|
import { Alert } from "@/components/ui/Alert";
|
||||||
|
|
||||||
|
export function CsvExportButton({ von, bis, fahrerId }: { von: string; bis: string; fahrerId?: string }) {
|
||||||
|
const [fehler, setFehler] = useState<string | null>(null);
|
||||||
|
const [isPending, startTransition] = useTransition();
|
||||||
|
|
||||||
|
function handleClick() {
|
||||||
|
startTransition(async () => {
|
||||||
|
const ergebnis = await exportiereCsv({ von, bis }, fahrerId);
|
||||||
|
if (!ergebnis.success) {
|
||||||
|
setFehler(ergebnis.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const blob = new Blob([ergebnis.csv], { type: "text/csv;charset=utf-8;" });
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = url;
|
||||||
|
link.download = `zeiterfassung_${von.slice(0, 10)}_${bis.slice(0, 10)}.csv`;
|
||||||
|
link.click();
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{fehler && (
|
||||||
|
<div className="mb-3">
|
||||||
|
<Alert>{fehler}</Alert>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Button type="button" fullWidth={false} variant="secondary" onClick={handleClick} disabled={isPending}>
|
||||||
|
CSV-Export
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState, useTransition } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { kommen, gehen } from "@/app/zeiterfassung/actions";
|
||||||
|
import { Button } from "@/components/ui/Button";
|
||||||
|
import { Alert } from "@/components/ui/Alert";
|
||||||
|
|
||||||
|
export function KommenGehenButton({ offeneSchichtId }: { offeneSchichtId: string | null }) {
|
||||||
|
const router = useRouter();
|
||||||
|
const [fehler, setFehler] = useState<string | null>(null);
|
||||||
|
const [isPending, startTransition] = useTransition();
|
||||||
|
|
||||||
|
function handleClick() {
|
||||||
|
startTransition(async () => {
|
||||||
|
const ergebnis = offeneSchichtId ? await gehen(offeneSchichtId) : await kommen();
|
||||||
|
if (!ergebnis.success) {
|
||||||
|
setFehler(ergebnis.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setFehler(null);
|
||||||
|
router.refresh();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{fehler && (
|
||||||
|
<div className="mb-3">
|
||||||
|
<Alert>{fehler}</Alert>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Button type="button" fullWidth={false} onClick={handleClick} disabled={isPending}>
|
||||||
|
{offeneSchichtId ? "Gehen" : "Kommen"}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState, useTransition } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { schichtKorrigieren } from "@/app/zeiterfassung/actions";
|
||||||
|
import { Button } from "@/components/ui/Button";
|
||||||
|
import { Alert } from "@/components/ui/Alert";
|
||||||
|
import { FIELD_CLASSES } from "@/components/ui/FormField";
|
||||||
|
|
||||||
|
export function KorrekturFormular({ schichtId }: { schichtId: string }) {
|
||||||
|
const router = useRouter();
|
||||||
|
const [fehler, setFehler] = useState<string | null>(null);
|
||||||
|
const [isPending, startTransition] = useTransition();
|
||||||
|
|
||||||
|
function handleSubmit(formData: FormData) {
|
||||||
|
startTransition(async () => {
|
||||||
|
const ergebnis = await schichtKorrigieren(schichtId, formData);
|
||||||
|
if (!ergebnis.success) {
|
||||||
|
setFehler(ergebnis.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setFehler(null);
|
||||||
|
router.refresh();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<form action={handleSubmit} className="mt-3 flex flex-wrap items-end gap-3">
|
||||||
|
{fehler && (
|
||||||
|
<div className="w-full">
|
||||||
|
<Alert>{fehler}</Alert>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{/* datetime-local liefert einen Wert ohne Zeitzone; new Date(...) in
|
||||||
|
der Server Action interpretiert ihn in der Zeitzone des
|
||||||
|
Server-Prozesses, nicht der des Fahrers/Disponenten im Browser.
|
||||||
|
Fuer den lokalen Single-Timezone-Betrieb dieses Projekts akzeptabel,
|
||||||
|
bei mehreren Zeitzonen muesste hier nachgebessert werden. */}
|
||||||
|
<input type="datetime-local" name="geht_um" required className={FIELD_CLASSES} />
|
||||||
|
<Button type="submit" fullWidth={false} disabled={isPending}>
|
||||||
|
Geht-Zeit nachtragen
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { canManageUsers } from "./roles";
|
import { canManageUsers, kannZeiterfassungNutzen } from "./roles";
|
||||||
|
|
||||||
describe("canManageUsers", () => {
|
describe("canManageUsers", () => {
|
||||||
it("erlaubt Nutzerverwaltung für admin", () => {
|
it("erlaubt Nutzerverwaltung für admin", () => {
|
||||||
|
|
@ -18,3 +18,15 @@ describe("canManageUsers", () => {
|
||||||
expect(canManageUsers("readonly")).toBe(false);
|
expect(canManageUsers("readonly")).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("kannZeiterfassungNutzen", () => {
|
||||||
|
it("erlaubt Zeiterfassung fuer admin, disponent und fahrer", () => {
|
||||||
|
expect(kannZeiterfassungNutzen("admin")).toBe(true);
|
||||||
|
expect(kannZeiterfassungNutzen("disponent")).toBe(true);
|
||||||
|
expect(kannZeiterfassungNutzen("fahrer")).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("verbietet Zeiterfassung fuer readonly (Personaldaten)", () => {
|
||||||
|
expect(kannZeiterfassungNutzen("readonly")).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,7 @@ export function canManageUsers(role: Role): boolean {
|
||||||
export function kannStammdatenPflegen(role: Role): boolean {
|
export function kannStammdatenPflegen(role: Role): boolean {
|
||||||
return role === "admin" || role === "disponent";
|
return role === "admin" || role === "disponent";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function kannZeiterfassungNutzen(role: Role): boolean {
|
||||||
|
return role !== "readonly";
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue