import { useState } from "react"; import { api } from "../api"; import Icon from "./Icon"; import { fmt } from "../units"; import { locationOptions, locationPathById } from "../locationPath"; /** * Charge aufteilen: einen Teil der Menge (in Artikeleinheiten – Packungen/Stück/g) * mit gleichem MHD an einen anderen Lagerort verschieben. Die Restmenge bleibt in * der ursprünglichen Charge; der Gesamtbestand ändert sich nicht. * * `factor` – Basiseinheiten je Artikeleinheit (z.B. 415 ml pro Packung). * `labelFor` – (menge) => Einheitentext, mengenabhängig ("1 Packung"/"3 Packungen"). */ export default function SplitLotDialog({ lot, factor, labelFor, locations, onClose, onDone, onError }) { const total = lot.quantity / factor; // Gesamtmenge in Artikeleinheiten const [menge, setMenge] = useState(""); const [locId, setLocId] = useState(""); const [busy, setBusy] = useState(false); const num = Number(String(menge).trim().replace(",", ".")); const gueltig = menge !== "" && !Number.isNaN(num) && num > 0 && num < total; const rest = gueltig ? total - num : null; const zielGleich = gueltig && (locId || null) === (lot.location_id || null); async function submit() { if (!gueltig || zielGleich || busy) return; setBusy(true); try { await api.splitLot(lot.id, { quantity: num * factor, location_id: locId || null }); onClose(); await onDone(); } catch (err) { onError?.(err.message); } finally { setBusy(false); } } return (
Aktuell {fmt(total)} {labelFor(total)} {lot.location_id ? ` in ${locationPathById(lot.location_id, locations) || "?"}` : " ohne Lagerort"}. Ein Teil davon wird mit gleichem MHD an einen anderen Lagerort verschoben.
Danach: {fmt(num)} {labelFor(num)} am neuen Ort, {fmt(rest)} {labelFor(rest)} bleiben.
)} {zielGleich && (Bitte einen anderen Lagerort als den aktuellen wählen.
)}