pelagia-portal/App/app/(portal)/admin/vessels/[id]/page.tsx
Hardik 0b10ba5e54
All checks were successful
PR checks / checks (pull_request) Successful in 32s
feat(po): cancel POs (manager/superuser) + optional supersede link (#53)
Managers and superusers can cancel a PO from any state via a confirmation modal
that requires typing "cancel" and a mandatory reason. A cancelled PO becomes a
terminal CANCELLED state and drops out of every spend tracker/graph (those filter
on POST_APPROVAL_STATUSES / explicit whitelists, none of which include CANCELLED).

A cancelled PO may optionally be linked to the existing PO that supersedes it
(by PO number); the replacement shows the reciprocal "supersedes" link. No
vessel/account/vendor match is enforced and the link can be added any time.

Cancelled POs remain visible (greyed in history) and exportable, with a diagonal
"CANCELLED" watermark on both the PDF and XLSX exports.

- schema: POStatus CANCELLED; cancelledAt/cancellationReason; self-referential
  supersededById relation; ActionType CANCELLED/SUPERSEDED (+ migration)
- state machine canCancel(); cancel_po permission (MANAGER + SUPERUSER)
- cancelPo / supersedePo server actions + PO_CANCELLED notification
- cancel modal + supersede form; cancelled banner with reciprocal links
- exhaustive CANCELLED entries in all status label/variant maps
- diagonal CANCELLED watermark embedded for PDF (CSS) and XLSX (image)
- integration tests (cancel from any state, reason/role guards, supersede)

Inventory reversal on cancel is deferred to #55 (inventory is feature-flagged off).

Closes #53

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 12:20:54 +05:30

110 lines
5 KiB
TypeScript

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<Metadata> {
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<string, string> = {
DRAFT: "Draft", SUBMITTED: "Submitted", MGR_REVIEW: "Under Review",
MGR_APPROVED: "Approved", SENT_FOR_PAYMENT: "Sent for Payment",
PAID_DELIVERED: "Paid", CLOSED: "Closed", REJECTED: "Rejected", CANCELLED: "Cancelled",
};
const totalSpend = vessel.purchaseOrders.filter(p => p.status === "CLOSED" || p.status === "PAID_DELIVERED")
.reduce((s, p) => s + Number(p.totalAmount), 0);
return (
<div className="max-w-5xl space-y-6">
<div className="flex items-center gap-2 text-sm text-neutral-500">
<Link href="/admin/vessels" className="hover:text-neutral-700">Cost Centres</Link>
<span>/</span>
<span className="text-neutral-900 font-medium">{vessel.name}</span>
</div>
<div className="flex items-start justify-between gap-4">
<div>
<div className="flex items-center gap-3 mb-1">
<span className={`rounded-full px-2.5 py-0.5 text-xs font-medium ${vessel.isActive ? "bg-success-100 text-success-700" : "bg-neutral-100 text-neutral-500"}`}>
{vessel.isActive ? "Active" : "Inactive"}
</span>
</div>
<h1 className="text-2xl font-semibold text-neutral-900">{vessel.name}</h1>
</div>
<Link href={`/po/new?vesselId=${vessel.id}`} className="rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700">
+ Create PO
</Link>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="rounded-lg border border-neutral-200 bg-white px-5 py-4">
<p className="text-xs text-neutral-500 mb-1">Total POs</p>
<p className="text-2xl font-semibold text-neutral-900">{vessel.purchaseOrders.length}</p>
</div>
<div className="rounded-lg border border-neutral-200 bg-white px-5 py-4">
<p className="text-xs text-neutral-500 mb-1">Total Spend (closed)</p>
<p className="text-2xl font-semibold text-neutral-900">{formatCurrency(totalSpend)}</p>
</div>
</div>
<div className="rounded-lg border border-neutral-200 bg-white p-6">
<h2 className="text-sm font-semibold text-neutral-900 mb-4">Purchase Orders</h2>
{vessel.purchaseOrders.length === 0 ? (
<p className="text-sm text-neutral-400 italic">No POs yet.</p>
) : (
<table className="w-full text-sm">
<thead>
<tr className="border-b border-neutral-200">
<th className="pb-2 text-left font-medium text-neutral-600">PO</th>
<th className="pb-2 text-left font-medium text-neutral-600 pl-4">Vendor</th>
<th className="pb-2 text-left font-medium text-neutral-600 pl-4">Status</th>
<th className="pb-2 text-right font-medium text-neutral-600 pl-4">Amount</th>
<th className="pb-2 text-right font-medium text-neutral-600 pl-4">Date</th>
</tr>
</thead>
<tbody className="divide-y divide-neutral-100">
{vessel.purchaseOrders.map((po) => (
<tr key={po.id}>
<td className="py-2 pr-4"><Link href={`/po/${po.id}`} className="font-mono text-xs text-primary-600 hover:underline">{po.poNumber}</Link></td>
<td className="py-2 pl-4 text-neutral-600">{po.vendor?.name ?? <span className="italic text-neutral-400"></span>}</td>
<td className="py-2 pl-4 text-neutral-600">{STATUS_LABELS[po.status] ?? po.status}</td>
<td className="py-2 pl-4 text-right">{formatCurrency(Number(po.totalAmount))}</td>
<td className="py-2 pl-4 text-right text-neutral-500">{formatDate(po.createdAt)}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
</div>
);
}