pelagia-portal/App/app/(portal)/po/[id]/edit/page.tsx
Hardik 565f9d5833 feat: searchable accounting code picker + cost centres grouped by site
Accounting Code search (new/edit/import/manager-edit PO forms):
- New SearchableSelect component (components/ui/searchable-select.tsx):
  type-to-filter by code or name, results grouped by sub-category with
  sticky headers, highlighted selected item, clear button, Escape/outside-click
  to dismiss
- Replaces the plain <select> for the main Accounting Code field on all PO forms
- LineItemsEditor per-row account column also uses SearchableSelect (compact size)
  when multi-account mode is active

Cost Centre dropdown reorganised by site:
- New type CostCentreGroup replaces flat CostCentreOption
- Each site becomes an <optgroup> label (unselectable); the site itself is the
  first selectable option inside ("Haldia (Site)"), followed by its vessels
- Vessels with no site assigned appear under an "Unassigned Vessels" group
- Shared helpers buildCostCentreGroups() and buildAccountGroups() in
  lib/cost-centre-groups.ts — used by all four PO form pages

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-30 17:54:43 +05:30

82 lines
2.9 KiB
TypeScript

import { auth } from "@/auth";
import { db } from "@/lib/db";
import { notFound, redirect } from "next/navigation";
import { EditPoForm } from "./edit-po-form";
import { buildCostCentreGroups, buildAccountGroups } from "@/lib/cost-centre-groups";
import type { Metadata } from "next";
interface Props {
params: Promise<{ id: string }>;
}
export const metadata: Metadata = { title: "Edit Purchase Order" };
export default async function EditPoPage({ params }: Props) {
const session = await auth();
if (!session?.user) redirect("/login");
const { id } = await params;
const po = await db.purchaseOrder.findUnique({
where: { id },
include: { lineItems: { orderBy: { sortOrder: "asc" } } },
});
if (!po) notFound();
if (!["DRAFT", "EDITS_REQUESTED"].includes(po.status)) redirect(`/po/${id}`);
const canEdit = po.submitterId === session.user.id || session.user.role === "SUPERUSER";
if (!canEdit) redirect(`/po/${id}`);
const [vessels, sites, leafAccounts, vendors, noteAction] = await Promise.all([
db.vessel.findMany({ where: { isActive: true }, orderBy: { name: "asc" }, select: { id: true, name: true, code: true, siteId: true } }),
db.site.findMany({ where: { isActive: true }, orderBy: { name: "asc" }, select: { id: true, name: true } }),
db.account.findMany({
where: { isActive: true, children: { none: {} } },
orderBy: { code: "asc" },
select: { id: true, code: true, name: true, parent: { select: { name: true, code: true, parent: { select: { name: true, code: true } } } } },
}),
db.vendor.findMany({ where: { isActive: true }, orderBy: { name: "asc" } }),
po.status === "EDITS_REQUESTED"
? db.pOAction.findFirst({
where: { poId: po.id, actionType: "EDITS_REQUESTED", note: { not: null } },
orderBy: { createdAt: "desc" },
include: { actor: { select: { name: true } } },
})
: Promise.resolve(null),
]);
const costCentres = buildCostCentreGroups(vessels, sites);
const accountGroups = buildAccountGroups(leafAccounts);
const initialCostCentreRef = po.vesselId ? `v:${po.vesselId}` : po.siteId ? `s:${po.siteId}` : "";
const serializedPo = {
...po,
totalAmount: po.totalAmount.toNumber(),
lineItems: po.lineItems.map((li) => ({
...li,
quantity: li.quantity.toNumber(),
unitPrice: li.unitPrice.toNumber(),
totalPrice: li.totalPrice.toNumber(),
gstRate: li.gstRate.toNumber(),
})),
};
return (
<div className="max-w-6xl">
<div className="mb-6">
<h1 className="text-2xl font-semibold text-neutral-900">Edit Purchase Order</h1>
<p className="mt-1 text-sm text-neutral-500 font-mono">{po.poNumber}</p>
</div>
<EditPoForm
po={serializedPo}
costCentres={costCentres}
initialCostCentreRef={initialCostCentreRef}
accounts={accountGroups}
vendors={vendors}
managerNoteAuthor={noteAction?.actor.name ?? null}
/>
</div>
);
}