Replaces the free-text "Place of Delivery" with a dropdown sourced from a new admin-managed Delivery Locations list (each = a Company FK + free-text address). - schema + migration: new DeliveryLocation model (companyId, address, isActive). - permission: manage_delivery_locations granted to Manager + SuperUser + Admin (Manager-accessible, not admin-only, per the issue). - admin screen /admin/delivery-locations: table + Add/Edit dialogs + activate/deactivate + delete (mirrors /admin/sites); sidebar link under Administration for Manager/SuperUser/Admin. - PO forms (new / edit / manager-edit): shared <DeliveryLocationField> native select populated from active locations, formatted "Company — address". - PurchaseOrder.placeOfDelivery stays a free-text SNAPSHOT (no FK) — the dropdown only changes how the value is picked, so export/import/historical POs are unchanged, and an edit preserves a current value not in the list as a "(current)" option. Deleting a location is therefore always safe. - tests: delivery-location CRUD + permission guard (6). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
84 lines
3.2 KiB
TypeScript
84 lines
3.2 KiB
TypeScript
import { auth } from "@/auth";
|
|
import { db } from "@/lib/db";
|
|
import { notFound, redirect } from "next/navigation";
|
|
import { EditPoForm } from "./edit-po-form";
|
|
import { buildAccountGroups } from "@/lib/cost-centre-groups";
|
|
import { formatDeliveryLocation } from "@/lib/delivery-location";
|
|
import type { CompanyOption } from "@/app/(portal)/po/new/new-po-form";
|
|
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, leafAccounts, vendors, companies, deliveryLocations, noteAction] = await Promise.all([
|
|
db.vessel.findMany({ where: { isActive: true }, orderBy: { name: "asc" }, select: { id: true, name: true, code: 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" } }),
|
|
db.company.findMany({ where: { isActive: true }, orderBy: { name: "asc" }, select: { id: true, name: true, code: true } }),
|
|
db.deliveryLocation.findMany({ where: { isActive: true }, orderBy: { createdAt: "asc" }, include: { company: { select: { name: true } } } }),
|
|
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 accounts = buildAccountGroups(leafAccounts);
|
|
const deliveryOptions = deliveryLocations.map((l) => formatDeliveryLocation(l.company.name, l.address));
|
|
|
|
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}
|
|
vessels={vessels}
|
|
accounts={accounts}
|
|
vendors={vendors}
|
|
companies={companies}
|
|
deliveryOptions={deliveryOptions}
|
|
managerNoteAuthor={noteAction?.actor.name ?? null}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|