forked from fahrPool/fahrPool
192 lines
8.1 KiB
TypeScript
192 lines
8.1 KiB
TypeScript
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", { timeZone: "Europe/Berlin" })}
|
||
</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", { timeZone: "Europe/Berlin" })}
|
||
{" – "}
|
||
{schicht.geht_um
|
||
? new Date(schicht.geht_um).toLocaleString("de-DE", { timeZone: "Europe/Berlin" })
|
||
: "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", { timeZone: "Europe/Berlin" })}
|
||
</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>
|
||
);
|
||
}
|