import { auth } from "@/auth"; import { db } from "@/lib/db"; import { hasPermission } from "@/lib/permissions"; import { redirect } from "next/navigation"; import Link from "next/link"; import { formatCurrency, formatDate, PO_STATUS_LABELS } from "@/lib/utils"; import { PoStatusBadge } from "@/components/po/po-status-badge"; import { HistoryFilters } from "./history-filters"; import { Suspense } from "react"; import type { Metadata } from "next"; import type { POStatus } from "@prisma/client"; export const metadata: Metadata = { title: "History" }; interface Props { searchParams: Promise<{ dateFrom?: string; dateTo?: string; costCentreRef?: string; status?: string; }>; } export default async function HistoryPage({ searchParams }: Props) { const session = await auth(); if (!session?.user) redirect("/login"); if (!hasPermission(session.user.role, "export_reports")) redirect("/dashboard"); const { dateFrom, dateTo, costCentreRef, status } = await searchParams; const where: NonNullable[0]>["where"] = {}; if (dateFrom || dateTo) { const createdAt: { gte?: Date; lt?: Date } = {}; if (dateFrom) createdAt.gte = new Date(dateFrom); if (dateTo) { const end = new Date(dateTo); end.setDate(end.getDate() + 1); createdAt.lt = end; } where.createdAt = createdAt; } if (costCentreRef) { if (costCentreRef.startsWith("v:")) where.vesselId = costCentreRef.slice(2); else if (costCentreRef.startsWith("s:")) where.siteId = costCentreRef.slice(2); } if (status) where.status = status as POStatus; const [orders, vessels, sites] = await Promise.all([ db.purchaseOrder.findMany({ where, include: { submitter: true, vessel: true, site: { select: { name: true } }, account: true }, orderBy: { createdAt: "desc" }, take: 200, }), db.vessel.findMany({ orderBy: { name: "asc" }, select: { id: true, name: true } }), db.site.findMany({ orderBy: { name: "asc" }, select: { id: true, name: true } }), ]); const costCentres = [ ...vessels.map((v) => ({ ref: `v:${v.id}`, name: v.name })), ...sites.map((s) => ({ ref: `s:${s.id}`, name: s.name })), ]; const exportParams = new URLSearchParams({ format: "csv" }); if (dateFrom) exportParams.set("dateFrom", dateFrom); if (dateTo) exportParams.set("dateTo", dateTo); if (costCentreRef) exportParams.set("costCentreRef", costCentreRef); if (status) exportParams.set("status", status); return (

PO History

Export PDF Export CSV
{orders.map((po) => ( ))}
PO Number Title Cost Centre Submitter Status Amount Created
{po.poNumber} {po.title} {po.vessel?.name ?? po.site?.name ?? "—"} {po.submitter.name} {formatCurrency(Number(po.totalAmount), po.currency)} {formatDate(po.createdAt)}
{orders.length === 0 && (
No purchase orders found.
)}
{orders.length === 200 && (

Showing first 200 results — refine filters to narrow results.

)}
); }