import { redirect } from "next/navigation"; import { currentUser } from "@/lib/session"; import { prisma } from "@/lib/db"; import { loadOrderDoc } from "@/lib/orderdoc"; import { fmtDate, money } from "@/lib/compute"; import AutoPrint from "@/components/AutoPrint"; export const dynamic = "force-dynamic"; /* The purchase order as an A4 sheet: the supplier's own product codes, a tick box per line to * work down while keying it into the supplier's site, the account number, the order number, a * space for the supplier's reference, and a signature. Black on white, nothing that will not * print. Rendering it stamps printedAt on the order. */ export default async function SupplierOrderSheet({ searchParams }: { searchParams: Promise> }) { const user = await currentUser(); if (!user) redirect("/auth"); if (user.role !== "ADMIN") redirect("/app/orders"); const q = await searchParams; const id = String(q.id || "").trim().slice(0, 64); if (!id) redirect("/app/orders"); const d = await loadOrderDoc(user.facilityId, id).catch(() => null); if (!d) redirect("/app/orders"); await prisma.order.update({ where: { id }, data: { printedAt: new Date() } }).catch(() => undefined); const ink = "#201e1d"; const lb: React.CSSProperties = { fontSize: 9, fontWeight: 700, letterSpacing: ".08em", textTransform: "uppercase", color: ink }; const cell: React.CSSProperties = { padding: "6px 6px", borderBottom: `1px solid ${ink}`, fontSize: 11.5, verticalAlign: "top" }; const r: React.CSSProperties = { ...cell, textAlign: "right", fontVariantNumeric: "tabular-nums" }; const box = ; return (
← Orders
Purchase order

{d.order.code}{d.order.ref ? · {d.order.ref} : null}

{d.facility.name}
{d.facility.location}{d.facility.slipOrg ? <>
{d.facility.slipOrg} : null}
Supplier
{d.supplier.name || "—"}{d.supplier.contact ? ` · ${d.supplier.contact}` : ""}{d.supplier.phone ? ` · ${d.supplier.phone}` : ""}{d.supplier.email ? <>
{d.supplier.email} : null}
Date
{fmtDate(d.order.date)}{d.order.expected ? ` · expected ${fmtDate(d.order.expected)}` : ""}
Our account with supplier
{d.supplier.account || "—"}
Supplier's order reference
{d.order.ref ? {d.order.ref} : }
{d.staff &&
Ordered for
{d.staff.name}{d.staff.dept ? ` · ${d.staff.dept}` : ""}{d.staff.cc ? ` · cost centre ${d.staff.cc}` : ""}
}
{d.lines.map((l, i) => ( ))}
Supplier code Description Size Qty Unit Total
{box} {l.code || "—"} {l.description} {l.size} {l.qty} {l.unit ? money(l.unit) : "—"} {l.unit ? money(l.total) : "—"}
{d.lines.reduce((t, l) => t + l.qty, 0)} units · {d.lines.length} line{d.lines.length === 1 ? "" : "s"}
Total {money(d.total)} ex tax, at last known unit costs
{d.order.notes &&
Notes: {d.order.notes}
}
Ordered by
Date placed
Please quote order {d.order.code} on the invoice.{d.facility.coordinator ? ` Questions: ${d.facility.coordinator}${d.facility.coordinatorPhone ? ` · ${d.facility.coordinatorPhone}` : ""}${d.facility.coordinatorEmail ? ` · ${d.facility.coordinatorEmail}` : ""}.` : ""}
); }