diff --git a/ApiCore-Frontend/src/app/(app)/production-batches/[id]/edit-production-batch-dialog.tsx b/ApiCore-Frontend/src/app/(app)/production-batches/[id]/edit-production-batch-dialog.tsx new file mode 100644 index 0000000..f649a07 --- /dev/null +++ b/ApiCore-Frontend/src/app/(app)/production-batches/[id]/edit-production-batch-dialog.tsx @@ -0,0 +1,172 @@ +"use client"; + +import { FormEvent, useState } from "react"; +import { Pencil } from "lucide-react"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { toast } from "sonner"; +import { updateProductionBatch } from "@/lib/production-batches/api"; +import { ProductionBatch } from "@/lib/production-batches/types"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Textarea } from "@/components/ui/textarea"; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; + +/** + * Corrects a production batch entered with a typo. The article stays fixed: + * moving a batch with sales to another article would rewrite where those + * sales came from. A changed quantity changes the remaining stock - in the + * shop as well. + */ +export function EditProductionBatchDialog({ + batch, +}: { + batch: ProductionBatch; +}) { + const [open, setOpen] = useState(false); + return ( + + + + Bearbeiten + + } + /> + + + Herstellungscharge bearbeiten + + {open ? ( + setOpen(false)} /> + ) : null} + + + ); +} + +function EditForm({ + batch, + onSaved, +}: { + batch: ProductionBatch; + onSaved: () => void; +}) { + const queryClient = useQueryClient(); + const [lotNumber, setLotNumber] = useState(batch.lotNumber); + const [date, setDate] = useState(batch.date.slice(0, 10)); + const [quantity, setQuantity] = useState(String(batch.quantity)); + const [bestBeforeDate, setBestBeforeDate] = useState( + batch.bestBeforeDate?.slice(0, 10) ?? "", + ); + const [notes, setNotes] = useState(batch.notes ?? ""); + const [error, setError] = useState(null); + + const mutation = useMutation({ + mutationFn: () => + updateProductionBatch(batch.id, { + lotNumber: lotNumber.trim(), + date: new Date(date).toISOString(), + quantity: Number(quantity), + bestBeforeDate: bestBeforeDate + ? new Date(bestBeforeDate).toISOString() + : null, + notes: notes.trim(), + }), + onSuccess: () => { + void queryClient.invalidateQueries({ queryKey: ["production-batches"] }); + void queryClient.invalidateQueries({ + queryKey: ["stock", "production-batches", batch.id], + }); + toast.success("Charge gespeichert"); + onSaved(); + }, + onError: (mutationError: Error) => { + const message = + mutationError.message || "Charge konnte nicht gespeichert werden"; + setError(message); + toast.error(message); + }, + }); + + function handleSubmit(event: FormEvent) { + event.preventDefault(); + setError(null); + mutation.mutate(); + } + + return ( +
+
+ + setLotNumber(event.target.value)} + /> +
+
+ + setDate(event.target.value)} + /> +
+
+ + setQuantity(event.target.value)} + /> +

+ Ändert den Rest (auch im Webshop). Fehlende oder gefundene Stück bitte + über „Inventur korrigieren“ buchen, nicht hier. +

+
+
+ + setBestBeforeDate(event.target.value)} + /> +
+
+ +