ManiCandles/src/pages/ReceiptStockPosting.tsx

63 lines
3.1 KiB
TypeScript

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<string, string>;
}) {
const { can } = usePlatform();
const path = `/goods-receipts/${receiptId}/stock-posting`;
const query = useOptionalPrivateQuery(path, receiptPosting, "MATERIAL_POSTING_NOT_FOUND");
return <Panel title="Material warehouse posting">
<QueryView query={query}>{(posting) => posting ? <>
<p>Posted to warehouse {posting.warehouseId} on {new Date(posting.createdAt).toLocaleString()}.</p>
<Table head={["Material", "Quantity posted", "Stock ledger"]}
rowKeys={posting.entries.map((entry) => entry.id)}
rows={posting.entries.map((entry) => [
lineNames.get(entry.receiptLineId) ?? entry.receiptLineId,
entry.delta,
<WorkspaceLink href={`/admin/material-stock/${entry.stockItemId}`}>View stock ledger</WorkspaceLink>,
])} />
{!posting.entries.length && <p>The inspection accepted zero material. The posting recorded no stock movements.</p>}
</> : can("inventory.manage") ? (
<PostReceipt path={path} receiptId={receiptId} refresh={query.refetch} />
) : <Alert severity="info">This receipt is awaiting warehouse posting.</Alert>}</QueryView>
</Panel>;
}
function PostReceipt({ path, receiptId, refresh }: {
path: string;
receiptId: string;
refresh: () => Promise<unknown>;
}) {
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 ? (
<ActionForm label="Retry warehouse posting" read={() => ({})} submit={submit}>
<Alert severity="warning">The server may have posted this receipt. Retrying uses the same request and warehouse {command.pending.warehouseId}.</Alert>
</ActionForm>
) : <WarehousePostingForm label="Post accepted material" warehouseLabel="Destination warehouse" submit={submit}
instruction="Choose the warehouse to credit with all accepted quantities from this receipt. A receipt can be posted once." />;
}