import { auth } from "@/auth"; import { db } from "@/lib/db"; import { notFound, redirect } from "next/navigation"; import { ReceiptForm } from "./receipt-form"; import type { Metadata } from "next"; export const metadata: Metadata = { title: "Confirm Receipt" }; interface Props { params: Promise<{ id: string }>; } export default async function ReceiptPage({ params }: Props) { const session = await auth(); if (!session?.user) redirect("/login"); const { id } = await params; const po = await db.purchaseOrder.findUnique({ where: { id }, select: { id: true, poNumber: true, title: true, status: true, submitterId: true, lineItems: { orderBy: { sortOrder: "asc" }, select: { id: true, name: true, quantity: true, unit: true, deliveredQuantity: true, }, }, }, }); if (!po) notFound(); if (po.status !== "PAID_DELIVERED" && po.status !== "PARTIALLY_CLOSED") { redirect(`/po/${id}`); } if (po.submitterId !== session.user.id && session.user.role !== "SUPERUSER") { redirect(`/po/${id}`); } const lineItems = po.lineItems.map((li) => ({ id: li.id, name: li.name, quantity: Number(li.quantity), unit: li.unit, deliveredQuantity: li.deliveredQuantity !== null ? Number(li.deliveredQuantity) : null, })); const isPartiallyReceived = po.status === "PARTIALLY_CLOSED"; return (
{po.poNumber} — {po.title}
{isPartiallyReceived && (This PO has been partially received. Mark the quantities delivered in this batch.
)}