Follow-up to the merged #11 PR (which shipped the enum-based catalogue): make categories user-defined data and the PO T&C a dynamic editor. - categories are a TermsCategory TABLE (not an enum) — admins add new ones; - every PO T&C line is catalogued, incl. the previously-fixed boilerplate (seeded under a "General" category) and an "Others" bucket; - the PO form is a dynamic editor: "+ Add term", pick a category, type/pick a clause (components/po/po-terms-editor.tsx), used by new/edit/manager-edit. Migration: the already-released 20260624140000 migration is untouched; a new 20260624150000 FORWARD migration renames the enum, creates the table, migrates existing enum clauses onto category rows, adds isDefault/sortOrder + the two fixed lines under General, and adds PurchaseOrder.terms (JSON snapshot that supersedes the legacy tc* columns for export/detail; old POs fall back to tc*). Tests rewritten for category creation + catalogue/default helpers. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
89 lines
3.6 KiB
TypeScript
89 lines
3.6 KiB
TypeScript
import { auth } from "@/auth";
|
|
import { db } from "@/lib/db";
|
|
import { hasPermission } from "@/lib/permissions";
|
|
import { redirect } from "next/navigation";
|
|
import { NewPoForm } from "./new-po-form";
|
|
import { buildAccountGroups } from "@/lib/cost-centre-groups";
|
|
import { formatDeliveryLocation } from "@/lib/delivery-location";
|
|
import { getTermsCatalogue, getDefaultPoTerms } from "@/lib/terms-data";
|
|
import type { Metadata } from "next";
|
|
import type { LineItemInput } from "@/lib/validations/po";
|
|
import type { CartItem } from "@/lib/cart";
|
|
|
|
export const metadata: Metadata = { title: "New Purchase Order" };
|
|
|
|
interface Props {
|
|
searchParams: Promise<{ cart?: string; vesselId?: string }>;
|
|
}
|
|
|
|
export default async function NewPoPage({ searchParams }: Props) {
|
|
const session = await auth();
|
|
if (!session?.user) redirect("/login");
|
|
|
|
if (!hasPermission(session.user.role, "create_po")) redirect("/dashboard");
|
|
|
|
const { cart, vesselId: initialVesselId } = await searchParams;
|
|
|
|
let initialLineItems: LineItemInput[] | undefined;
|
|
let initialVendorId: string | undefined;
|
|
if (cart) {
|
|
try {
|
|
const cartItems: CartItem[] = JSON.parse(decodeURIComponent(cart));
|
|
if (Array.isArray(cartItems) && cartItems.length > 0) {
|
|
initialLineItems = cartItems.map((item) => ({
|
|
name: item.name,
|
|
description: item.description ?? "",
|
|
quantity: item.quantity,
|
|
unit: item.unit,
|
|
size: "",
|
|
unitPrice: item.unitPrice,
|
|
gstRate: 0.18,
|
|
productId: item.productId,
|
|
}));
|
|
const vendorIds = [...new Set(cartItems.map((i) => i.vendorId).filter(Boolean))];
|
|
if (vendorIds.length === 1) initialVendorId = vendorIds[0];
|
|
}
|
|
} catch {
|
|
// malformed cart param — ignore
|
|
}
|
|
}
|
|
|
|
const [vessels, leafAccounts, vendors, companies, deliveryLocations] = 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 } } } }),
|
|
]);
|
|
|
|
const accounts = buildAccountGroups(leafAccounts);
|
|
const deliveryOptions = deliveryLocations.map((l) => formatDeliveryLocation(l.company.name, l.address));
|
|
const [termsCatalogue, defaultTerms] = await Promise.all([getTermsCatalogue(), getDefaultPoTerms()]);
|
|
|
|
return (
|
|
<div className="max-w-6xl">
|
|
<div className="mb-6">
|
|
<h1 className="text-2xl font-semibold text-neutral-900">New Purchase Order</h1>
|
|
<p className="mt-1 text-sm text-neutral-500">
|
|
Fill in the details below. You can save as draft or submit directly for approval.
|
|
</p>
|
|
</div>
|
|
<NewPoForm
|
|
vessels={vessels}
|
|
accounts={accounts}
|
|
vendors={vendors}
|
|
companies={companies}
|
|
deliveryOptions={deliveryOptions}
|
|
termsCatalogue={termsCatalogue}
|
|
defaultTerms={defaultTerms}
|
|
initialLineItems={initialLineItems}
|
|
initialVendorId={initialVendorId}
|
|
initialVesselId={initialVesselId}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|