import { useState } from "react"; import { z } from "zod"; import Alert from "@mui/material/Alert"; import { ActionForm, Field, Pager, Panel, QueryView, Table, readText, usePrivateQuery } from "@/components/WorkspaceTools"; import { usePlatform } from "@/lib/platform"; import { batchDetail, batchUsage, usageEvent, usageInput } from "@/lib/production"; import { useIdempotentCommand } from "@/lib/use-idempotent-command"; export function BatchMaterialUsage({ batch }: { batch: z.infer }) { const { can } = usePlatform(); const [offset, setOffset] = useState(0); const path = `/production-batches/${batch.id}/material-usage`; const query = usePrivateQuery(`${path}?limit=25&offset=${offset}`, batchUsage); const command = useIdempotentCommand(path, usageInput, usageEvent, batch.id); const labels = new Map(batch.plannedMaterials.map((line) => [line.id, `${line.materialName} (${line.materialCode}) · ${line.unit}`])); const pending = command.pending; return {(data) => <> line.bomLineId)} rows={data.materials.map((line) => [ `${line.materialName} (${line.materialCode}) · ${line.unit}`, line.plannedQuantity, line.consumedQuantity, line.wastedQuantity, line.varianceQuantity, ])} />

Usage history

event.id)} rows={data.events.map((event) => [ new Date(event.createdAt).toLocaleString(), labels.get(event.bomLineId) ?? event.bomLineId, event.kind, event.quantity, event.reason ?? "—", ])} /> {can("production.material.record") && (pending || (batch.status === "IN_PROGRESS" && data.total < 500)) && ( pending ? {} : ({ bomLineId: readText(form, "bomLineId"), kind: readText(form, "kind"), quantity: readText(form, "quantity").trim(), reason: readText(form, "reason").trim() || undefined, })} submit={async (input) => { await command.run(input); const result = await query.refetch(); if (result.data) setOffset(Math.floor(Math.max(0, result.data.total - 1) / 25) * 25); }}> {pending ? ( The server may have saved this entry. Retrying reuses {pending.quantity} for {labels.get(pending.bomLineId) ?? pending.bomLineId} as {pending.kind.toLowerCase()}. ) : <> ({ value, label }))} /> } )} {batch.status === "IN_PROGRESS" && data.total === 500 && This batch has reached the 500-event limit.} } ; }