forked from fahrPool/fahrPool
212 lines
8.5 KiB
TypeScript
212 lines
8.5 KiB
TypeScript
import Link from "next/link";
|
||
import { redirect } from "next/navigation";
|
||
import { loadProfile } from "@/lib/auth/profile";
|
||
import { kannReportsSehen, rollenBezeichnung } from "@/lib/auth/roles";
|
||
import { berlinTag } from "@/lib/datum/berlin";
|
||
import { formatDauer } from "@/lib/kalender/kalender";
|
||
import { berechneReport, MAX_TAGE, reportZeitraum, SCHNELLWAHL, type Report, type ReportZeitraum } from "@/lib/reports/report";
|
||
import { ladeReportDaten } from "@/lib/reports/queries";
|
||
import { Alert } from "@/components/ui/Alert";
|
||
import { Button, buttonClasses } from "@/components/ui/Button";
|
||
import { Card } from "@/components/ui/Card";
|
||
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||
import { DruckenKnopf } from "@/components/reports/DruckenKnopf";
|
||
|
||
function deutsch(tag: string): string {
|
||
return `${tag.slice(8, 10)}.${tag.slice(5, 7)}.${tag.slice(0, 4)}`;
|
||
}
|
||
|
||
function prozentText(wert: number | null): string {
|
||
return wert === null ? "–" : `${wert} %`;
|
||
}
|
||
|
||
export default async function ReportsPage({
|
||
searchParams,
|
||
}: {
|
||
searchParams: Promise<{ von?: string; bis?: string; wahl?: string }>;
|
||
}) {
|
||
const profileResult = await loadProfile();
|
||
if (profileResult.status !== "ok") {
|
||
return null;
|
||
}
|
||
if (!kannReportsSehen(profileResult.profile.role)) {
|
||
redirect(`/auftraege?error=${encodeURIComponent("Reports sind für diese Rolle nicht verfügbar.")}`);
|
||
}
|
||
|
||
const jetzt = new Date();
|
||
const zeitraum = reportZeitraum(await searchParams, berlinTag(jetzt));
|
||
const daten = await ladeReportDaten(zeitraum.von, zeitraum.bis);
|
||
|
||
return (
|
||
<div className="mx-auto max-w-6xl p-4 md:p-8">
|
||
<div className="mb-4 flex flex-wrap items-center justify-between gap-3">
|
||
<h1 className="text-xl font-semibold text-asphalt-grey">
|
||
Reports <span className="text-base font-normal text-status-neutral">{deutsch(zeitraum.von)} – {deutsch(zeitraum.bis)}</span>
|
||
</h1>
|
||
<div className="no-print flex gap-2">
|
||
<a href={`/reports/export?von=${zeitraum.von}&bis=${zeitraum.bis}`} className={buttonClasses("secondary", false)}>
|
||
CSV
|
||
</a>
|
||
<DruckenKnopf />
|
||
</div>
|
||
</div>
|
||
|
||
<ZeitraumWahl zeitraum={zeitraum} />
|
||
|
||
{zeitraum.gekuerzt && (
|
||
<div className="mb-6">
|
||
<Alert>Der Zeitraum wurde auf {MAX_TAGE} Tage gekürzt.</Alert>
|
||
</div>
|
||
)}
|
||
|
||
{daten.status === "fehler" ? (
|
||
<div className="flex flex-col gap-3">
|
||
<Alert>Der Report konnte nicht geladen werden.</Alert>
|
||
<Link
|
||
href={`/reports?von=${zeitraum.von}&bis=${zeitraum.bis}`}
|
||
className="flex min-h-11 items-center text-performance-blue underline"
|
||
>
|
||
Erneut laden
|
||
</Link>
|
||
</div>
|
||
) : (
|
||
<Inhalt report={berechneReport({ ...daten, von: zeitraum.von, bis: zeitraum.bis, jetzt })} />
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function ZeitraumWahl({ zeitraum }: { zeitraum: ReportZeitraum }) {
|
||
return (
|
||
<div className="no-print mb-6 flex flex-col gap-3">
|
||
<nav aria-label="Zeitraum" className="flex flex-wrap gap-2">
|
||
{SCHNELLWAHL.map((s) => (
|
||
<Link
|
||
key={s.id}
|
||
href={`/reports?wahl=${s.id}`}
|
||
aria-current={zeitraum.wahl === s.id ? "page" : undefined}
|
||
className={`flex min-h-11 items-center rounded-md px-4 font-medium ${
|
||
zeitraum.wahl === s.id
|
||
? "bg-performance-blue text-white"
|
||
: "border border-asphalt-grey bg-white text-asphalt-grey hover:bg-aluminium-grey"
|
||
}`}
|
||
>
|
||
{s.label}
|
||
</Link>
|
||
))}
|
||
</nav>
|
||
<form className="flex flex-wrap items-end gap-3">
|
||
<FormField label="Von" htmlFor="von">
|
||
<input id="von" name="von" type="date" defaultValue={zeitraum.von} className={FIELD_CLASSES} />
|
||
</FormField>
|
||
<FormField label="Bis" htmlFor="bis">
|
||
<input id="bis" name="bis" type="date" defaultValue={zeitraum.bis} className={FIELD_CLASSES} />
|
||
</FormField>
|
||
<Button type="submit" fullWidth={false}>
|
||
Anzeigen
|
||
</Button>
|
||
</form>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function Inhalt({ report }: { report: Report }) {
|
||
const k = report.kacheln;
|
||
const kacheln: { label: string; wert: string }[] = [
|
||
{ label: "Aufträge abgeschlossen", wert: String(k.abgeschlossen) },
|
||
{ label: "storniert (angelegt im Zeitraum)", wert: String(k.storniert) },
|
||
{ label: "neu angelegt", wert: String(k.angelegt) },
|
||
{ label: "km gesamt", wert: k.kmGesamt.toLocaleString("de-DE") },
|
||
{ label: "Ø km je Auftrag", wert: k.kmSchnitt === null ? "–" : k.kmSchnitt.toLocaleString("de-DE") },
|
||
{ label: "Arbeitszeit", wert: formatDauer(k.arbeitMin) },
|
||
{ label: "Fahrzeit", wert: formatDauer(k.fahrtMin) },
|
||
{ label: "Auslastung", wert: prozentText(k.auslastung) },
|
||
];
|
||
const maxVerlauf = Math.max(1, ...report.verlauf.map((v) => v.anzahl));
|
||
|
||
return (
|
||
<div className="flex flex-col gap-6">
|
||
<section className="grid grid-cols-2 gap-3 md:grid-cols-4">
|
||
{kacheln.map((kachel) => (
|
||
<Card key={kachel.label} className="p-4">
|
||
<p className="text-sm text-status-neutral">{kachel.label}</p>
|
||
<p className="text-2xl font-semibold text-asphalt-grey">{kachel.wert}</p>
|
||
</Card>
|
||
))}
|
||
</section>
|
||
|
||
<Card>
|
||
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Abgeschlossene Aufträge im Verlauf</h2>
|
||
<div className="flex h-40 items-end gap-1" role="list">
|
||
{report.verlauf.map((v) => (
|
||
<div
|
||
key={v.label}
|
||
role="listitem"
|
||
aria-label={`${v.label}: ${v.anzahl}`}
|
||
title={`${v.label}: ${v.anzahl}`}
|
||
className="flex h-full min-w-0 flex-1 flex-col justify-end"
|
||
>
|
||
<div className="rounded-t bg-performance-blue" style={{ height: `${(v.anzahl / maxVerlauf) * 100}%` }} />
|
||
</div>
|
||
))}
|
||
</div>
|
||
<div className="mt-1 flex justify-between text-xs text-status-neutral">
|
||
<span>{report.verlauf[0]?.label}</span>
|
||
<span>{report.verlauf[report.verlauf.length - 1]?.label}</span>
|
||
</div>
|
||
</Card>
|
||
|
||
<section>
|
||
<h2 className="mb-3 text-lg font-semibold text-asphalt-grey">Je Person</h2>
|
||
{report.zeilen.length === 0 ? (
|
||
<p className="text-status-neutral">Keine Aktivität im Zeitraum.</p>
|
||
) : (
|
||
<>
|
||
<table className="hidden w-full border-collapse text-sm md:table">
|
||
<thead>
|
||
<tr className="text-left text-asphalt-grey">
|
||
{["Name", "Rolle", "Abgeschlossen", "km", "Fahrzeit", "Arbeitszeit", "Auslastung"].map((t) => (
|
||
<th key={t} className="p-2 font-semibold">
|
||
{t}
|
||
</th>
|
||
))}
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{report.zeilen.map((z) => (
|
||
<tr key={z.person.id} className="border-t border-aluminium-grey">
|
||
<td className="p-2 font-medium">{z.person.name}</td>
|
||
<td className="p-2">{rollenBezeichnung(z.person.role)}</td>
|
||
<td className="p-2">{z.abgeschlossen}</td>
|
||
<td className="p-2">{z.km.toLocaleString("de-DE")}</td>
|
||
<td className="p-2">{formatDauer(z.fahrtMin)}</td>
|
||
<td className="p-2">{formatDauer(z.arbeitMin)}</td>
|
||
<td className="p-2">{prozentText(z.auslastung)}</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
<ul className="flex flex-col gap-3 md:hidden">
|
||
{report.zeilen.map((z) => (
|
||
<li key={z.person.id}>
|
||
<Card className="p-4">
|
||
<p className="font-semibold text-asphalt-grey">
|
||
{z.person.name} <span className="text-sm font-normal text-status-neutral">{rollenBezeichnung(z.person.role)}</span>
|
||
</p>
|
||
<p className="text-sm text-asphalt-grey">
|
||
{z.abgeschlossen} Aufträge · {z.km.toLocaleString("de-DE")} km
|
||
</p>
|
||
<p className="text-sm text-status-neutral">
|
||
Fahrt {formatDauer(z.fahrtMin)} · Arbeit {formatDauer(z.arbeitMin)} · {prozentText(z.auslastung)}
|
||
</p>
|
||
</Card>
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</>
|
||
)}
|
||
</section>
|
||
</div>
|
||
);
|
||
}
|