+ All purchase orders
+ {order.supplier.name} · {order.status}
+ {order.expectedDeliveryAt && Expected delivery {order.expectedDeliveryAt.slice(0, 10)}
}
+ line.id)}
+ rows={order.lines.map((line) => [
+ `${line.materialName} (${line.materialCode})`, `${line.quantity} ${line.unit}`,
+ `${order.currency} ${line.unitPrice}`, `${order.currency} ${line.lineTotal}`,
+ ])} />
+ Merchandise total {order.currency} {order.merchandiseTotal}
+ {can("procurement.manage") && order.status === "DRAFT" && <>
+ {
+ await request(`/purchase-orders/${id}/issue`, purchaseOrder, { method: "POST" });
+ await query.refetch();
+ }} />
+ {
+ await request(`/purchase-orders/${id}/cancel`, purchaseOrder, { method: "POST" });
+ await query.refetch();
+ }} />
+ >}
+ };
+}
+
+export function PurchaseOrdersPage({ orderId }: { orderId?: string }) {
+ const id = uuid.safeParse(orderId);
+ return
+ {orderId ? id.success ? : Invalid purchase order ID. : }
+ ;
+}
+
+function padDecimal(value: string, scale: number) {
+ const [whole, fraction = ""] = value.split(".");
+ return `${whole}.${fraction.padEnd(scale, "0")}`;
+}
+
+export default function ProcurementPage({ materials = false, supplierId }: { materials?: boolean; supplierId?: string }) {
+ const id = uuid.safeParse(supplierId);
+ return (
+
+ {supplierId ? id.success ? : Invalid supplier ID.
: }
+
+ );
+}
diff --git a/test/procurement.test.ts b/test/procurement.test.ts
new file mode 100644
index 0000000..781857c
--- /dev/null
+++ b/test/procurement.test.ts
@@ -0,0 +1,46 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { materialInput, purchaseOrderInput, supplierInput, supplyTerms, supplyTermsInput } from "../src/lib/procurement.ts";
+
+const id = "d66c798a-f764-4b88-8d13-ace328ff1010";
+const terms = {
+ supplierSku: "",
+ leadTimeDays: 10,
+ minOrderQuantity: "0.125",
+ unitPrice: "900000000.01",
+ currency: "INR",
+ active: true,
+};
+
+test("supplier directory normalizes business codes and email addresses", () => {
+ const value = supplierInput.parse({
+ name: " Candle supply ", code: " wax_1 ", contactName: "Supplier contact",
+ email: "CONTACT@example.com", phone: "+91 1234567890", active: true,
+ });
+ assert.equal(value.name, "Candle supply");
+ assert.equal(value.code, "WAX_1");
+ assert.equal(value.email, "contact@example.com");
+});
+
+test("material directory enforces backend material kinds and units", () => {
+ const value = { name: "Wax", code: "WAX", kind: "WAX", unit: "GRAM", active: true };
+ assert.equal(materialInput.safeParse(value).success, true);
+ assert.equal(materialInput.safeParse({ ...value, unit: "BOX" }).success, false);
+});
+
+test("supply terms preserve precise string quantities and enforce request scale", () => {
+ assert.deepEqual(supplyTermsInput.parse(terms), terms);
+ assert.equal(supplyTermsInput.safeParse({ ...terms, minOrderQuantity: "0.000" }).success, false);
+ assert.equal(supplyTermsInput.safeParse({ ...terms, unitPrice: "2.1" }).success, false);
+ assert.equal(supplyTermsInput.safeParse({ ...terms, leadTimeDays: 366 }).success, false);
+ assert.equal(supplyTerms.parse({ ...terms, id, materialId: id, unitPrice: "2", minOrderQuantity: "1.5" }).unitPrice, "2");
+});
+
+test("draft orders require distinct materials, exact quantities and a calendar date", () => {
+ const line = { materialId: id, quantity: "1.001" };
+ const input = { supplierId: id, expectedDeliveryDate: "2026-12-01", lines: [line] };
+ assert.deepEqual(purchaseOrderInput.parse(input), input);
+ assert.equal(purchaseOrderInput.safeParse({ ...input, lines: [line, { ...line, materialId: id.toUpperCase() }] }).success, false);
+ assert.equal(purchaseOrderInput.safeParse({ ...input, expectedDeliveryDate: "2026-02-31" }).success, false);
+ assert.equal(purchaseOrderInput.safeParse({ ...input, lines: [] }).success, false);
+});
diff --git a/test/workflows.ui.test.tsx b/test/workflows.ui.test.tsx
index 43a0706..49dd859 100644
--- a/test/workflows.ui.test.tsx
+++ b/test/workflows.ui.test.tsx
@@ -11,6 +11,7 @@ import { PlatformProvider, usePlatform } from "../src/lib/platform";
import CartPage from "../src/pages/CartPage";
import AccessPage, { RolesPage } from "../src/pages/AccessPage";
import OrdersPage from "../src/pages/OrdersPage";
+import ProcurementPage from "../src/pages/ProcurementPage";
import theme from "../src/theme";
import { Switch, Route } from "wouter";
const id = "d66c798a-f764-4b88-8d13-ace328ff1010";
@@ -69,6 +70,24 @@ const session = {
tokenType: "Bearer",
expiresAt: new Date(Date.now() + 3600000).toISOString(),
};
+test("procurement observers can read suppliers and cannot see write controls", async () => {
+ const fetch = vi.fn(async (url: string) =>
+ url.endsWith("/auth/login")
+ ? json(session)
+ : url.endsWith("/auth/me")
+ ? json({ ...me, permissions: ["procurement.read"] })
+ : url.includes("/suppliers?")
+ ? json([{ id, name: "Wax Supplier", code: "WAX", contactName: "Contact", email: "contact@example.com", phone: "+919999999999", active: true }])
+ : Promise.reject(new Error("Unexpected private request")),
+ );
+ vi.stubGlobal("fetch", fetch);
+ harness();
+ await userEvent.setup().click(screen.getByText("Test login"));
+ const terms = await screen.findByRole("link", { name: "Supply terms and orders for Wax Supplier" });
+ expect(terms.getAttribute("href")).toBe(`/admin/procurement/suppliers/${id}`);
+ expect(screen.queryByRole("button", { name: "Edit Wax Supplier" })).toBeNull();
+ expect(screen.queryByRole("button", { name: "Create supplier" })).toBeNull();
+});
const address = {
id,
recipient: "Test Recipient",