120 lines
5.7 KiB
TypeScript
120 lines
5.7 KiB
TypeScript
import { useState } from "react";
|
|
import { z } from "zod";
|
|
import Alert from "@mui/material/Alert";
|
|
import Button from "@mui/material/Button";
|
|
import { ActionForm, Field, Panel, QueryView, Table, readText, usePrivateQuery } from "@/components/WorkspaceTools";
|
|
import { isUnknownOutcome } from "@/lib/api";
|
|
import { goodsReceipt, parseInspection, purchaseOrder, receiptInput } from "@/lib/procurement";
|
|
import { usePlatform } from "@/lib/platform";
|
|
|
|
type Order = z.infer<typeof purchaseOrder>;
|
|
type Receipt = z.infer<typeof goodsReceipt>;
|
|
|
|
export function PurchaseOrderReceipts({ order }: { order: Order }) {
|
|
const { can, request } = usePlatform();
|
|
const path = `/purchase-orders/${order.id}/receipts`;
|
|
const query = usePrivateQuery(path, z.array(goodsReceipt));
|
|
const [selected, select] = useState("");
|
|
const [uncertain, setUncertain] = useState(false);
|
|
const lineNames = new Map(order.lines.map((line) => [line.id, `${line.materialName} (${line.materialCode}) · ${line.unit}`]));
|
|
return (
|
|
<Panel title="Goods receipts">
|
|
<QueryView query={query}>{(receipts) => {
|
|
const receipt = receipts.find((entry) => entry.id === selected);
|
|
return <>
|
|
<Table head={["Received", "Delivery note", "Inspection", "Actions"]}
|
|
rowKeys={receipts.map((entry) => entry.id)}
|
|
rows={receipts.map((entry) => [
|
|
new Date(entry.createdAt).toLocaleString(), entry.deliveryNote || "—",
|
|
entry.lines.every((line) => line.inspection) ? "Inspected" : "Pending",
|
|
<Button onClick={() => select(entry.id)} aria-pressed={selected === entry.id}>
|
|
View receipt {entry.deliveryNote || entry.id.slice(0, 8)}
|
|
</Button>,
|
|
])} />
|
|
{receipt && <Panel title="Receipt details">
|
|
<Table head={["Material", "Delivered", "Accepted", "Rejected", "Reason"]}
|
|
rowKeys={receipt.lines.map((line) => line.id)}
|
|
rows={receipt.lines.map((line) => [
|
|
lineNames.get(line.purchaseOrderLineId) ?? line.purchaseOrderLineId,
|
|
line.quantity, line.inspection?.acceptedQuantity ?? "—", line.inspection?.rejectedQuantity ?? "—",
|
|
line.inspection?.rejectionReason ?? "—",
|
|
])} />
|
|
{can("procurement.manage") && receipt.lines.every((line) => !line.inspection) && (
|
|
<InspectionForm key={receipt.id} receipt={receipt} lineNames={lineNames} refresh={query.refetch} />
|
|
)}
|
|
</Panel>}
|
|
{can("procurement.manage") && (uncertain ? (
|
|
<Alert severity="warning">The server may have saved this receipt. Review the receipt list before recording the delivery again. <Button onClick={() => query.refetch()}>Refresh receipts</Button></Alert>
|
|
) : (
|
|
<ActionForm label="Record goods receipt"
|
|
read={(data) => receiptInput.parse({
|
|
deliveryNote: readText(data, "deliveryNote").trim() || undefined,
|
|
lines: order.lines.flatMap((line) => {
|
|
const quantity = readText(data, line.id).trim();
|
|
return quantity ? [{ purchaseOrderLineId: line.id, quantity }] : [];
|
|
}),
|
|
})}
|
|
submit={async (body) => {
|
|
try {
|
|
const saved = await request(path, goodsReceipt, { method: "POST", body });
|
|
await query.refetch();
|
|
select(saved.id);
|
|
} catch (error) {
|
|
if (isUnknownOutcome(error)) {
|
|
setUncertain(true);
|
|
await query.refetch();
|
|
}
|
|
throw error;
|
|
}
|
|
}}>
|
|
<Field name="deliveryNote" label="Delivery note" required={false} maxLength={80} />
|
|
{order.lines.map((line) => (
|
|
<Field key={line.id} name={line.id} label={`Delivered quantity for ${line.materialName} (${line.materialCode})`}
|
|
required={false} helper={`Use three decimal places in ${line.unit}. Leave blank for undelivered materials.`} />
|
|
))}
|
|
</ActionForm>
|
|
))}
|
|
</>;
|
|
}}</QueryView>
|
|
</Panel>
|
|
);
|
|
}
|
|
|
|
function InspectionForm({ receipt, lineNames, refresh }: {
|
|
receipt: Receipt;
|
|
lineNames: Map<string, string>;
|
|
refresh: () => Promise<unknown>;
|
|
}) {
|
|
const { request } = usePlatform();
|
|
return (
|
|
<ActionForm label="Finalize inspection" reset={false}
|
|
read={(data) => parseInspection({
|
|
lines: receipt.lines.map((line) => ({
|
|
receiptLineId: line.id,
|
|
acceptedQuantity: readText(data, `${line.id}-accepted`).trim(),
|
|
rejectedQuantity: readText(data, `${line.id}-rejected`).trim(),
|
|
rejectionReason: readText(data, `${line.id}-reason`).trim() || undefined,
|
|
})),
|
|
}, receipt)}
|
|
submit={async (body) => {
|
|
try {
|
|
await request(`/goods-receipts/${receipt.id}/inspect`, goodsReceipt, { method: "POST", body });
|
|
} catch (error) {
|
|
if (isUnknownOutcome(error)) await refresh();
|
|
throw error;
|
|
}
|
|
await refresh();
|
|
}}>
|
|
<p>Inspection is final. Enter accepted and rejected quantities with three decimal places for every line.</p>
|
|
{receipt.lines.map((line) => (
|
|
<fieldset key={line.id}>
|
|
<legend>{lineNames.get(line.purchaseOrderLineId) ?? line.purchaseOrderLineId} · Delivered {line.quantity}</legend>
|
|
<Field name={`${line.id}-accepted`} label="Accepted quantity" />
|
|
<Field name={`${line.id}-rejected`} label="Rejected quantity" value="0.000" />
|
|
<Field name={`${line.id}-reason`} label="Rejection reason" required={false} maxLength={500} helper="Required when any quantity is rejected." />
|
|
</fieldset>
|
|
))}
|
|
</ActionForm>
|
|
);
|
|
}
|