forked from fahrPool/fahrPool
feat: Auftraege klonen (gleiche Strecke, mehrere Fahrzeuge) und Folgeauftrag ab Zielort
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
236bd528f3
commit
d7651fab8c
|
|
@ -0,0 +1,44 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { createClient } from "@/lib/supabase/server";
|
||||||
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { kannAuftraegeVerwalten } from "@/lib/auth/roles";
|
||||||
|
import { validateKlonEingaben } from "@/lib/auftraege/validate-auftrag";
|
||||||
|
|
||||||
|
// Legt je gewaehltem Fahrzeug einen neuen, offenen Auftrag auf derselben
|
||||||
|
// Strecke an. Ein einziges Insert-Statement: alle oder keiner. Nummer und
|
||||||
|
// firma_id setzen die bestehenden Trigger; RLS erlaubt das Insert nur
|
||||||
|
// admin/disponent mit erstellt_von = auth.uid() (eigentliche Sperre).
|
||||||
|
export async function auftragKlonen(auftragId: string, formData: FormData): Promise<void> {
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (profileResult.status !== "ok" || !kannAuftraegeVerwalten(profileResult.profile.role)) {
|
||||||
|
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege klonen.")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const validierung = validateKlonEingaben(formData);
|
||||||
|
if (!validierung.valid) {
|
||||||
|
redirect(`/auftraege/${auftragId}/klonen?error=${encodeURIComponent(validierung.error)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const supabase = await createClient();
|
||||||
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await supabase.auth.getUser();
|
||||||
|
if (!user) {
|
||||||
|
redirect("/login");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { fahrzeug_ids, ...strecke } = validierung.werte;
|
||||||
|
const { error } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.insert(fahrzeug_ids.map((fahrzeug_id) => ({ ...strecke, fahrzeug_id, erstellt_von: user.id })));
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
redirect(
|
||||||
|
`/auftraege/${auftragId}/klonen?error=${encodeURIComponent("Auftraege konnten nicht angelegt werden.")}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect("/auftraege");
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
import { notFound, redirect } from "next/navigation";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { loadAuftrag, listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
|
||||||
|
import { alsDatetimeLocal, berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung";
|
||||||
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { kannAuftraegeVerwalten } from "@/lib/auth/roles";
|
||||||
|
import { auftragKlonen } from "./actions";
|
||||||
|
import { AuftragStammFelder } from "@/components/auftraege/AuftragStammFelder";
|
||||||
|
import { FahrzeugMehrfachAuswahl } from "@/components/auftraege/FahrzeugMehrfachAuswahl";
|
||||||
|
import { NetzSicheresFormular } from "@/components/pwa/NetzSicheresFormular";
|
||||||
|
import { Card } from "@/components/ui/Card";
|
||||||
|
import { Button, buttonClasses } from "@/components/ui/Button";
|
||||||
|
import { Alert } from "@/components/ui/Alert";
|
||||||
|
|
||||||
|
export default async function AuftragKlonenPage({
|
||||||
|
params,
|
||||||
|
searchParams,
|
||||||
|
}: {
|
||||||
|
params: Promise<{ id: string }>;
|
||||||
|
searchParams: Promise<{ error?: string }>;
|
||||||
|
}) {
|
||||||
|
const { id } = await params;
|
||||||
|
const { error } = await searchParams;
|
||||||
|
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (profileResult.status !== "ok" || !kannAuftraegeVerwalten(profileResult.profile.role)) {
|
||||||
|
redirect(`/auftraege/${id}?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege klonen.")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [auftrag, orte, fahrzeuge] = await Promise.all([loadAuftrag(id), listOrte(), listFahrzeuge()]);
|
||||||
|
if (!auftrag) {
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
const aktion = auftragKlonen.bind(null, auftrag.id);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mx-auto max-w-xl p-4 md:p-8">
|
||||||
|
<h1 className="mb-1 text-xl font-semibold text-asphalt-grey">Auftrag klonen</h1>
|
||||||
|
<p className="mb-6 text-sm text-asphalt-grey/70">
|
||||||
|
Vorlage {auftrag.auftragsnummer}: {auftrag.abholort.adresse} → {auftrag.zielort.adresse}
|
||||||
|
</p>
|
||||||
|
{error && (
|
||||||
|
<div className="mb-6">
|
||||||
|
<Alert>{error}</Alert>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Card>
|
||||||
|
<NetzSicheresFormular action={aktion}>
|
||||||
|
<AuftragStammFelder
|
||||||
|
orte={orte}
|
||||||
|
fahrzeuge={fahrzeuge}
|
||||||
|
werte={{
|
||||||
|
abholortId: auftrag.abholort.id,
|
||||||
|
zielortId: auftrag.zielort.id,
|
||||||
|
zeitplanungTyp: auftrag.zeitplanung_typ,
|
||||||
|
zeitplanungZeitpunkt: auftrag.zeitplanung_zeitpunkt
|
||||||
|
? alsDatetimeLocal(auftrag.zeitplanung_zeitpunkt)
|
||||||
|
: undefined,
|
||||||
|
}}
|
||||||
|
vorbefuelltesDatum={berechneVorbefuelltenZeitpunkt()}
|
||||||
|
neuLinks={null}
|
||||||
|
nummerLabel="Auftragsnummer"
|
||||||
|
ohneFahrzeugUndNummer
|
||||||
|
/>
|
||||||
|
<FahrzeugMehrfachAuswahl fahrzeuge={fahrzeuge} />
|
||||||
|
<p className="mb-4 text-sm text-asphalt-grey/70">Je angehaktem Fahrzeug wird ein eigener Auftrag angelegt.</p>
|
||||||
|
<div className="flex flex-col gap-3 sm:flex-row">
|
||||||
|
<Button type="submit">Auftraege anlegen</Button>
|
||||||
|
<Link href={`/auftraege/${auftrag.id}`} className={buttonClasses("secondary", true)}>
|
||||||
|
Abbrechen
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</NetzSicheresFormular>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ import { notFound } from "next/navigation";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { loadAuftrag, findeFolgeauftrag } from "@/lib/auftraege/queries";
|
import { loadAuftrag, findeFolgeauftrag } from "@/lib/auftraege/queries";
|
||||||
import { formatZeitplanung } from "@/lib/auftraege/zeitplanung";
|
import { formatZeitplanung } from "@/lib/auftraege/zeitplanung";
|
||||||
|
import { folgeauftragUrl } from "@/lib/auftraege/klonen";
|
||||||
import { loadProfile } from "@/lib/auth/profile";
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
import { istEinsatzrolle, kannAuftraegeVerwalten, personBezeichnung } from "@/lib/auth/roles";
|
import { istEinsatzrolle, kannAuftraegeVerwalten, personBezeichnung } from "@/lib/auth/roles";
|
||||||
import { listEinsatzPersonen } from "@/lib/dashboard/queries";
|
import { listEinsatzPersonen } from "@/lib/dashboard/queries";
|
||||||
|
|
@ -360,6 +361,20 @@ export default async function AuftragDetailPage({
|
||||||
</Card>
|
</Card>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{kannAuftraegeVerwalten(profileResult.profile.role) && (
|
||||||
|
<Card className="mb-6">
|
||||||
|
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Als Vorlage nutzen</h2>
|
||||||
|
<div className="flex flex-col gap-3 sm:flex-row">
|
||||||
|
<Link href={`/auftraege/${auftrag.id}/klonen`} className={buttonClasses("secondary", true)}>
|
||||||
|
Klonen – gleiche Strecke
|
||||||
|
</Link>
|
||||||
|
<Link href={folgeauftragUrl(auftrag)} className={buttonClasses("secondary", true)}>
|
||||||
|
Folgeauftrag – ab Zielort
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
{kannStornieren && auftrag.status !== "abgeschlossen" && auftrag.status !== "storniert" && (
|
{kannStornieren && auftrag.status !== "abgeschlossen" && auftrag.status !== "storniert" && (
|
||||||
<Card>
|
<Card>
|
||||||
<NetzSicheresFormular action={stornierenAction}>
|
<NetzSicheresFormular action={stornierenAction}>
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ export function AuftragStammFelder({
|
||||||
vorbefuelltesDatum,
|
vorbefuelltesDatum,
|
||||||
neuLinks,
|
neuLinks,
|
||||||
nummerLabel,
|
nummerLabel,
|
||||||
|
ohneFahrzeugUndNummer = false,
|
||||||
}: {
|
}: {
|
||||||
orte: Ort[];
|
orte: Ort[];
|
||||||
fahrzeuge: Fahrzeug[];
|
fahrzeuge: Fahrzeug[];
|
||||||
|
|
@ -32,6 +33,9 @@ export function AuftragStammFelder({
|
||||||
vorbefuelltesDatum: string;
|
vorbefuelltesDatum: string;
|
||||||
neuLinks: { abholort: string; zielort: string; fahrzeug: string } | null;
|
neuLinks: { abholort: string; zielort: string; fahrzeug: string } | null;
|
||||||
nummerLabel: string;
|
nummerLabel: string;
|
||||||
|
// Klonen: Fahrzeuge kommen aus einer Mehrfachauswahl, die Nummer vergibt
|
||||||
|
// der Trigger -- beides wird hier dann nicht gerendert.
|
||||||
|
ohneFahrzeugUndNummer?: boolean;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
@ -71,22 +75,26 @@ export function AuftragStammFelder({
|
||||||
</Link>
|
</Link>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<FormField label="Fahrzeug" htmlFor="fahrzeug_id">
|
{!ohneFahrzeugUndNummer && (
|
||||||
<select id="fahrzeug_id" name="fahrzeug_id" required defaultValue={werte.fahrzeugId ?? ""} className={FIELD_CLASSES}>
|
<>
|
||||||
<option value="" disabled>
|
<FormField label="Fahrzeug" htmlFor="fahrzeug_id">
|
||||||
Bitte waehlen
|
<select id="fahrzeug_id" name="fahrzeug_id" required defaultValue={werte.fahrzeugId ?? ""} className={FIELD_CLASSES}>
|
||||||
</option>
|
<option value="" disabled>
|
||||||
{fahrzeuge.map((fahrzeug) => (
|
Bitte waehlen
|
||||||
<option key={fahrzeug.id} value={fahrzeug.id}>
|
</option>
|
||||||
{fahrzeugBezeichnung(fahrzeug)}
|
{fahrzeuge.map((fahrzeug) => (
|
||||||
</option>
|
<option key={fahrzeug.id} value={fahrzeug.id}>
|
||||||
))}
|
{fahrzeugBezeichnung(fahrzeug)}
|
||||||
</select>
|
</option>
|
||||||
</FormField>
|
))}
|
||||||
{neuLinks && (
|
</select>
|
||||||
<Link href={neuLinks.fahrzeug} className={LINK_CLASSES}>
|
</FormField>
|
||||||
+ Neues Fahrzeug
|
{neuLinks && (
|
||||||
</Link>
|
<Link href={neuLinks.fahrzeug} className={LINK_CLASSES}>
|
||||||
|
+ Neues Fahrzeug
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<ZeitplanungFeld
|
<ZeitplanungFeld
|
||||||
|
|
@ -95,15 +103,17 @@ export function AuftragStammFelder({
|
||||||
startZeitpunkt={werte.zeitplanungZeitpunkt}
|
startZeitpunkt={werte.zeitplanungZeitpunkt}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormField label={nummerLabel} htmlFor="auftragsnummer">
|
{!ohneFahrzeugUndNummer && (
|
||||||
<input
|
<FormField label={nummerLabel} htmlFor="auftragsnummer">
|
||||||
id="auftragsnummer"
|
<input
|
||||||
name="auftragsnummer"
|
id="auftragsnummer"
|
||||||
type="text"
|
name="auftragsnummer"
|
||||||
defaultValue={werte.auftragsnummer ?? ""}
|
type="text"
|
||||||
className={FIELD_CLASSES}
|
defaultValue={werte.auftragsnummer ?? ""}
|
||||||
/>
|
className={FIELD_CLASSES}
|
||||||
</FormField>
|
/>
|
||||||
|
</FormField>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
import type { Fahrzeug } from "@/lib/auftraege/queries";
|
||||||
|
import { fahrzeugBezeichnung } from "@/lib/orte-fahrzeuge/fahrzeug-bezeichnung";
|
||||||
|
|
||||||
|
// Mehrfachauswahl fuers Klonen: je angehaktem Fahrzeug entsteht ein Auftrag.
|
||||||
|
// Ganze Zeile ist klickbar (mobile Bedienung), nichts ist vorab angehakt.
|
||||||
|
export function FahrzeugMehrfachAuswahl({ fahrzeuge }: { fahrzeuge: Fahrzeug[] }) {
|
||||||
|
return (
|
||||||
|
<fieldset className="mb-4">
|
||||||
|
<legend className="mb-2 block text-sm font-medium text-asphalt-grey">Fahrzeuge</legend>
|
||||||
|
{fahrzeuge.length === 0 ? (
|
||||||
|
<p className="text-sm text-asphalt-grey/70">Noch keine Fahrzeuge angelegt.</p>
|
||||||
|
) : (
|
||||||
|
<ul className="divide-y divide-asphalt-grey/10 rounded-md border border-asphalt-grey/20">
|
||||||
|
{fahrzeuge.map((fahrzeug) => (
|
||||||
|
<li key={fahrzeug.id}>
|
||||||
|
<label className="flex min-h-11 cursor-pointer items-center gap-3 px-3 py-2 text-asphalt-grey">
|
||||||
|
<input type="checkbox" name="fahrzeug_ids" value={fahrzeug.id} className="h-5 w-5 shrink-0" />
|
||||||
|
<span>{fahrzeugBezeichnung(fahrzeug)}</span>
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
|
</fieldset>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue