import { auth } from "@/auth"; import { db } from "@/lib/db"; import { hasPermission } from "@/lib/permissions"; import { notFound, redirect } from "next/navigation"; import Link from "next/link"; import { formatCurrency, formatDate } from "@/lib/utils"; import type { Metadata } from "next"; interface Props { params: Promise<{ id: string }> } export async function generateMetadata({ params }: Props): Promise { const { id } = await params; const v = await db.vessel.findUnique({ where: { id }, select: { name: true } }); return { title: v?.name ?? "Cost Centre Detail" }; } export default async function VesselDetailPage({ params }: Props) { const session = await auth(); if (!session?.user) redirect("/login"); if (!hasPermission(session.user.role, "manage_sites") && !hasPermission(session.user.role, "manage_vessels_accounts")) redirect("/dashboard"); const { id } = await params; const vessel = await db.vessel.findUnique({ where: { id }, include: { purchaseOrders: { select: { id: true, poNumber: true, status: true, totalAmount: true, createdAt: true, vendor: { select: { name: true } } }, orderBy: { createdAt: "desc" }, take: 10, }, }, }); if (!vessel) notFound(); const STATUS_LABELS: Record = { DRAFT: "Draft", SUBMITTED: "Submitted", MGR_REVIEW: "Under Review", MGR_APPROVED: "Approved", SENT_FOR_PAYMENT: "Sent for Payment", PAID_DELIVERED: "Paid", CLOSED: "Closed", REJECTED: "Rejected", }; const totalSpend = vessel.purchaseOrders.filter(p => p.status === "CLOSED" || p.status === "PAID_DELIVERED") .reduce((s, p) => s + Number(p.totalAmount), 0); return (
Cost Centres / {vessel.name}
{vessel.isActive ? "Active" : "Inactive"}

{vessel.name}

+ Create PO

Total POs

{vessel.purchaseOrders.length}

Total Spend (closed)

{formatCurrency(totalSpend)}

Purchase Orders

{vessel.purchaseOrders.length === 0 ? (

No POs yet.

) : ( {vessel.purchaseOrders.map((po) => ( ))}
PO Vendor Status Amount Date
{po.poNumber} {po.vendor?.name ?? } {STATUS_LABELS[po.status] ?? po.status} {formatCurrency(Number(po.totalAmount))} {formatDate(po.createdAt)}
)}
); }