71 lines
3.6 KiB
TypeScript
71 lines
3.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";
|
|
import { usePlatform } from "@/lib/platform";
|
|
import { MaterialAdjustmentForm, MaterialStockMovements } from "./MaterialStockMovements";
|
|
|
|
const stockPath = "/inventory/material-stock-items";
|
|
|
|
function StockList() {
|
|
const { can } = usePlatform();
|
|
const [offset, setOffset] = useState(0);
|
|
const query = usePrivateQuery(`${stockPath}?limit=25&offset=${offset}`, z.array(materialStockItem));
|
|
return <Panel title="Warehouse balances">
|
|
{can("inventory.adjust") && <p><WorkspaceLink href="/admin/material-stock/adjust">Record a material adjustment</WorkspaceLink></p>}
|
|
<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>
|
|
<QueryView query={item}>{(row) => <MaterialStockMovements stock={row} />}</QueryView>
|
|
</Panel>;
|
|
}
|
|
|
|
export default function MaterialStockPage({ id, adjust = false }: { id?: string; adjust?: boolean }) {
|
|
const parsed = uuid.safeParse(id);
|
|
return <WorkspaceShell title="Material Stock" permission="inventory.read">
|
|
{adjust ? <AdjustmentRoute materialId={id} /> : !id ? <StockList /> : parsed.success ? (
|
|
<StockLedger key={parsed.data} id={parsed.data} />
|
|
) : <Alert severity="warning">Invalid stock item ID.</Alert>}
|
|
</WorkspaceShell>;
|
|
}
|
|
|
|
function AdjustmentRoute({ materialId }: { materialId?: string }) {
|
|
const { can } = usePlatform();
|
|
const id = uuid.safeParse(materialId);
|
|
return <>
|
|
<WorkspaceLink href="/admin/material-stock">All material stock</WorkspaceLink>
|
|
{!can("inventory.adjust") ? <Alert severity="warning">Your account does not have permission to adjust material stock.</Alert> :
|
|
materialId && !id.success ? <Alert severity="warning">Invalid material ID.</Alert> :
|
|
<MaterialAdjustmentForm key={id.data ?? "new"} materialId={id.data} />}
|
|
</>;
|
|
}
|