import { auth } from "@/auth"; import { db } from "@/lib/db"; import { hasPermission } from "@/lib/permissions"; import { notFound, redirect } from "next/navigation"; import { ApprovalActions } from "./approval-actions"; import { PoDetail } from "@/components/po/po-detail"; import { ManagerEditPoForm } from "./manager-edit-po-form"; import { buildAccountGroups } from "@/lib/cost-centre-groups"; import type { CompanyOption } from "@/app/(portal)/po/new/new-po-form"; import type { Metadata } from "next"; interface Props { params: Promise<{ id: string }>; } export const metadata: Metadata = { title: "Review PO" }; export default async function ApprovalDetailPage({ params }: Props) { const session = await auth(); if (!session?.user) redirect("/login"); if (!hasPermission(session.user.role, "approve_po")) redirect("/dashboard"); const { id } = await params; const currentUser = await db.user.findUnique({ where: { id: session.user.id }, select: { signatureKey: true }, }); const hasSignature = !!(currentUser?.signatureKey); const [po, vessels, leafAccounts, vendors, companies] = await Promise.all([ db.purchaseOrder.findUnique({ where: { id }, include: { submitter: true, vessel: true, site: { select: { id: true, name: true } }, account: true, vendor: true, lineItems: { orderBy: { sortOrder: "asc" } }, documents: { orderBy: { uploadedAt: "desc" } }, actions: { include: { actor: true }, orderBy: { createdAt: "asc" } }, receipt: true, }, }), db.vessel.findMany({ where: { isActive: true }, orderBy: { name: "asc" }, select: { id: true, name: true, code: true } }), db.account.findMany({ where: { isActive: true, children: { none: {} } }, orderBy: { code: "asc" }, select: { id: true, code: true, name: true, parent: { select: { name: true, code: true, parent: { select: { name: true, code: true } } } } }, }), db.vendor.findMany({ where: { isActive: true }, orderBy: { name: "asc" } }), db.company.findMany({ where: { isActive: true }, orderBy: { name: "asc" }, select: { id: true, name: true } }), ]); if (!po) notFound(); if (po.status !== "MGR_REVIEW") redirect(`/po/${id}`); const accounts = buildAccountGroups(leafAccounts); const serializedPo = { ...po, totalAmount: po.totalAmount.toNumber(), lineItems: po.lineItems.map((li) => ({ ...li, quantity: li.quantity.toNumber(), unitPrice: li.unitPrice.toNumber(), totalPrice: li.totalPrice.toNumber(), gstRate: li.gstRate.toNumber(), })), }; return (

Review Purchase Order

{po.poNumber} — {po.title}

{!po.vendorId && (

Vendor required before approval

This PO has no vendor assigned. Use “Request Vendor ID” to route it for vendor selection, or assign a vendor via the Edit PO form on desktop.

)}
{hasSignature ? ( ) : (

Signature required to approve POs

You must upload your approval signature before you can approve, reject, or request edits on purchase orders.

Go to Profile to upload your signature →
)}
); }