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 { PaymentActions } from "./payment-actions";
import type { Metadata } from "next";
export const metadata: Metadata = { title: "Payments" };
export default async function PaymentsPage() {
const session = await auth();
if (!session?.user) redirect("/login");
if (!hasPermission(session.user.role, "process_payment")) redirect("/dashboard");
const queue = await db.purchaseOrder.findMany({
where: { status: { in: ["MGR_APPROVED", "SENT_FOR_PAYMENT", "PARTIALLY_PAID", "PARTIALLY_CLOSED"] } },
include: { submitter: true, vessel: true, site: { select: { name: true } }, account: true, vendor: true },
orderBy: { approvedAt: "asc" },
});
return (
Payment Queue
{queue.length} order{queue.length !== 1 ? "s" : ""} in the payment pipeline
{queue.length === 0 ? (
No orders in the payment queue.
) : (
{queue.map((po) => (
{po.poNumber}
{po.title}
{po.vessel.name}
·
{po.submitter.name}
{po.vendor && (
<>
·
{po.vendor.name}
>
)}
{po.approvedAt && (
<>
·
Approved {formatDate(po.approvedAt)}
>
)}
{formatCurrency(Number(po.totalAmount), po.currency)}
View PO →
{po.status === "SENT_FOR_PAYMENT"
? "Processing — awaiting confirmation"
: po.status === "PARTIALLY_PAID"
? "Partially paid — additional payment needed"
: po.status === "PARTIALLY_CLOSED"
? "Partially received — awaiting more payments"
: "Ready for payment"}
{(po.status === "PARTIALLY_PAID" || po.status === "PARTIALLY_CLOSED") && po.paidAmount != null && (
Paid {formatCurrency(Number(po.paidAmount), po.currency)} of {formatCurrency(Number(po.totalAmount), po.currency)}
)}
{/* Manager's advance decision (issue #92) — shown until the first payment lands. */}
{po.status === "SENT_FOR_PAYMENT" &&
po.paidAmount == null &&
po.suggestedAdvancePayment != null &&
Number(po.suggestedAdvancePayment) < Number(po.totalAmount) && (
Advance requested: {formatCurrency(Number(po.suggestedAdvancePayment), po.currency)} of{" "}
{formatCurrency(Number(po.totalAmount), po.currency)}
)}
))}
)}
);
}