Adds two PO-level charges shown below GST, per issue #133 ask 2. - Stored as ABSOLUTE rupee amounts on PurchaseOrder.tcsAmount / discountAmount (Decimal?, default 0; null/0 on historical & imported POs). Migration added. - Discount is applied post-GST. totalAmount folds the charges in (net payable = subtotal + GST + TCS − Discount), so payments / reports / advance all use the true amount due. lib/po-money.ts is the single source of truth. - Forms (create + edit) render a shared TcsDiscountFields with a % control bidirectionally linked to the rupee value (percentage is convenience only, taken against the GST-inclusive total; only the absolute amount is persisted). - createPo / updatePo store & compute; both manager-edit actions PRESERVE the PO's TCS/Discount when recomputing the total; import leaves them at 0. - PO detail shows TCS / Discount / Net payable below GST; PDF + XLSX export show the same breakdown and a corrected grand total. Tests: lib/po-money unit tests; po-tcs-discount integration test (create / edit / manager-line-edit preservation). Docs: CLAUDE.md GST section + wiki Purchase Orders (TCS/Discount + a full "what import sets vs. not" field-mapping table). Full unit (360) + integration (305) suites green; tsc clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
96 lines
3.9 KiB
TypeScript
96 lines
3.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 { buildAccountGroups } from "@/lib/cost-centre-groups";
|
|
import { formatDeliveryLocation } from "@/lib/delivery-location";
|
|
import { getTermsCatalogue } from "@/lib/terms-data";
|
|
import { parsePoTerms, legacyPoTerms } from "@/lib/terms";
|
|
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, projectCodes, 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 } } } }),
|
|
db.projectCode.findMany({ where: { isActive: true }, orderBy: { code: "asc" }, select: { code: 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 projectCodeOptions = projectCodes.map((c) => c.code);
|
|
const termsCatalogue = await getTermsCatalogue();
|
|
const savedTerms = parsePoTerms(po.terms);
|
|
const initialTerms = savedTerms.length > 0 ? savedTerms : legacyPoTerms(po);
|
|
|
|
const serializedPo = {
|
|
...po,
|
|
totalAmount: po.totalAmount.toNumber(),
|
|
tcsAmount: po.tcsAmount ? po.tcsAmount.toNumber() : 0,
|
|
discountAmount: po.discountAmount ? po.discountAmount.toNumber() : 0,
|
|
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}
|
|
projectCodeOptions={projectCodeOptions}
|
|
termsCatalogue={termsCatalogue}
|
|
initialTerms={initialTerms}
|
|
managerNoteAuthor={noteAction?.actor.name ?? null}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|