import { useQueryClient } from "@tanstack/react-query"; import Alert from "@mui/material/Alert"; import { ActionForm, Panel, QueryView, Table, WorkspaceLink } from "@/components/WorkspaceTools"; import { WarehousePostingForm } from "@/components/WarehousePostingForm"; import { useOptionalPrivateQuery } from "@/components/useOptionalPrivateQuery"; import { ApiError } from "@/lib/api"; import { receiptPosting, receiptPostingInput } from "@/lib/material-stock"; import { usePlatform } from "@/lib/platform"; import { useIdempotentCommand } from "@/lib/use-idempotent-command"; export function ReceiptStockPosting({ receiptId, lineNames }: { receiptId: string; lineNames: Map; }) { const { can } = usePlatform(); const path = `/goods-receipts/${receiptId}/stock-posting`; const query = useOptionalPrivateQuery(path, receiptPosting, "MATERIAL_POSTING_NOT_FOUND"); return {(posting) => posting ? <>

Posted to warehouse {posting.warehouseId} on {new Date(posting.createdAt).toLocaleString()}.

entry.id)} rows={posting.entries.map((entry) => [ lineNames.get(entry.receiptLineId) ?? entry.receiptLineId, entry.delta, View stock ledger, ])} /> {!posting.entries.length &&

The inspection accepted zero material. The posting recorded no stock movements.

} : can("inventory.manage") ? ( ) : This receipt is awaiting warehouse posting.} ; } function PostReceipt({ path, receiptId, refresh }: { path: string; receiptId: string; refresh: () => Promise; }) { const { scope } = usePlatform(); const client = useQueryClient(); const command = useIdempotentCommand(path, receiptPostingInput, receiptPosting, receiptId); async function submit(input: unknown) { try { await command.run(input); } catch (error) { if (error instanceof ApiError && error.code === "MATERIAL_RECEIPT_ALREADY_POSTED") await refresh(); throw error; } await client.invalidateQueries({ predicate: ({ queryKey }) => queryKey[0] === "private" && queryKey[1] === scope && typeof queryKey[2] === "string" && queryKey[2].startsWith("/inventory/material-stock-items"), }); await refresh(); } return command.pending ? ( ({})} submit={submit}> The server may have posted this receipt. Retrying uses the same request and warehouse {command.pending.warehouseId}. ) : ; }