55 lines
2.6 KiB
TypeScript
55 lines
2.6 KiB
TypeScript
import { useState } from "react";
|
|
import { z } from "zod";
|
|
import Alert from "@mui/material/Alert";
|
|
import { WorkspaceShell } from "@/components/WorkspaceShell";
|
|
import { Pager, Panel, QueryView, Table, WorkspaceLink, usePrivateQuery } from "@/components/WorkspaceTools";
|
|
import { uuid } from "@/lib/commerce";
|
|
import { materialStockEntry, materialStockItem } from "@/lib/material-stock";
|
|
|
|
const stockPath = "/inventory/material-stock-items";
|
|
|
|
function StockList() {
|
|
const [offset, setOffset] = useState(0);
|
|
const query = usePrivateQuery(`${stockPath}?limit=25&offset=${offset}`, z.array(materialStockItem));
|
|
return <Panel title="Warehouse balances">
|
|
<QueryView query={query}>{(rows) => <>
|
|
<Table head={["Material", "Warehouse", "On hand", "Ledger"]} rowKeys={rows.map((row) => row.id)}
|
|
rows={rows.map((row) => [
|
|
`${row.material.name} (${row.material.code})`, row.warehouse.name,
|
|
`${row.onHand} ${row.material.unit}`,
|
|
<WorkspaceLink href={`/admin/material-stock/${row.id}`}>Ledger for {row.material.name} at {row.warehouse.name}</WorkspaceLink>,
|
|
])} />
|
|
<Pager offset={offset} count={rows.length} onPage={setOffset} />
|
|
</>}</QueryView>
|
|
</Panel>;
|
|
}
|
|
|
|
function StockLedger({ id }: { id: string }) {
|
|
const [offset, setOffset] = useState(0);
|
|
const item = usePrivateQuery(`${stockPath}/${id}`, materialStockItem);
|
|
const ledger = usePrivateQuery(`${stockPath}/${id}/ledger?limit=25&offset=${offset}`, z.array(materialStockEntry));
|
|
return <Panel title="Material stock ledger">
|
|
<WorkspaceLink href="/admin/material-stock">All material stock</WorkspaceLink>
|
|
<QueryView query={item}>{(row) => (
|
|
<p>{row.material.name} at {row.warehouse.name} · On hand {row.onHand} {row.material.unit}</p>
|
|
)}</QueryView>
|
|
<QueryView query={ledger}>{(rows) => <>
|
|
<Table head={["Time", "Movement", "Quantity change"]} rowKeys={rows.map((row) => row.id)}
|
|
rows={rows.map((row) => [
|
|
new Date(row.createdAt).toLocaleString(), row.kind.replaceAll("_", " ").toLowerCase(),
|
|
row.delta.startsWith("-") ? row.delta : `+${row.delta}`,
|
|
])} />
|
|
<Pager offset={offset} count={rows.length} onPage={setOffset} />
|
|
</>}</QueryView>
|
|
</Panel>;
|
|
}
|
|
|
|
export default function MaterialStockPage({ id }: { id?: string }) {
|
|
const parsed = uuid.safeParse(id);
|
|
return <WorkspaceShell title="Material Stock" permission="inventory.read">
|
|
{!id ? <StockList /> : parsed.success ? (
|
|
<StockLedger key={parsed.data} id={parsed.data} />
|
|
) : <Alert severity="warning">Invalid stock item ID.</Alert>}
|
|
</WorkspaceShell>;
|
|
}
|