pelagia-portal/App/app/(portal)/po/import/actions.ts
Hardik cc7251e6b7 feat: Cost Centre covers vessels and sites, vessel codes, Accounting Code rename, vessel-site assignment
- Undo Vessel→Cost Centre rename in admin (admin shows "Vessel Management" again)
- Sidebar: "Cost Centres"→"Vessels", "Accounts"→"Accounting Codes"
- PO forms (new/edit/import/manager-edit) now show both Vessels (with code) and Sites in the
  Cost Centre dropdown, encoded as v:<id> / s:<id> via a costCentreRef field
- vesselId on PurchaseOrder is now nullable; siteId is set when a site is the cost centre
- History, approvals, dashboard, my-orders, payments display vessel.name ?? site.name as Cost Centre
- History and approvals cost centre filters use costCentreRef URL param supporting both types
- Admin vessel form: adds Site assignment dropdown
- Admin accounts: renamed to "Accounting Code" throughout (pages, forms, sidebar)
- PO detail and exports: "Account" label renamed to "Accounting Code"
- Site detail: "Assigned Vessels (Cost Centres)" heading; vessel detail breadcrumb fixed
- Create PO links from vessel/site detail use ?costCentreRef= param
- Export routes handle costCentreRef filter param (with legacy vesselId fallback)
- DB migration: ALTER TABLE PurchaseOrder ALTER COLUMN vesselId DROP NOT NULL
- CLAUDE.md updated with Cost Centre Model documentation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-30 03:04:29 +05:30

83 lines
2.6 KiB
TypeScript

"use server";
import { auth } from "@/auth";
import { db } from "@/lib/db";
import { hasPermission } from "@/lib/permissions";
import { generatePoNumber } from "@/lib/utils";
import { revalidatePath } from "next/cache";
import type { ParsedImportLine } from "@/app/api/po/import/route";
export type ImportPoInput = {
title: string;
costCentreRef: string;
accountId: string;
vendorId?: string;
piQuotationNo?: string;
placeOfDelivery?: string;
tcDelivery?: string;
tcDispatch?: string;
tcInspection?: string;
tcTransitInsurance?: string;
tcPaymentTerms?: string;
tcOthers?: string;
lineItems: ParsedImportLine[];
};
export async function importPo(
input: ImportPoInput
): Promise<{ id: string } | { error: string }> {
const session = await auth();
if (!session?.user) return { error: "Unauthorized" };
if (!hasPermission(session.user.role, "create_po") && session.user.role !== "ADMIN") {
return { error: "You do not have permission to import purchase orders." };
}
const importVesselId = input.costCentreRef.startsWith("v:") ? input.costCentreRef.slice(2) : null;
const importSiteId = input.costCentreRef.startsWith("s:") ? input.costCentreRef.slice(2) : null;
const total = input.lineItems.reduce(
(sum, item) => sum + item.quantity * item.unitPrice * (1 + (item.gstRate ?? 0.18)),
0
);
const po = await db.purchaseOrder.create({
data: {
poNumber: generatePoNumber(),
title: input.title,
status: "DRAFT",
totalAmount: total,
currency: "INR",
vesselId: importVesselId,
siteId: importSiteId,
accountId: input.accountId,
vendorId: input.vendorId ?? null,
piQuotationNo: input.piQuotationNo ?? null,
placeOfDelivery: input.placeOfDelivery ?? null,
tcDelivery: input.tcDelivery ?? null,
tcDispatch: input.tcDispatch ?? null,
tcInspection: input.tcInspection ?? null,
tcTransitInsurance: input.tcTransitInsurance ?? null,
tcPaymentTerms: input.tcPaymentTerms ?? null,
tcOthers: input.tcOthers ?? null,
submitterId: session.user.id,
lineItems: {
create: input.lineItems.map((item, idx) => ({
name: item.name,
quantity: item.quantity,
unit: item.unit,
unitPrice: item.unitPrice,
totalPrice: item.quantity * item.unitPrice,
gstRate: item.gstRate ?? 0.18,
sortOrder: idx,
})),
},
actions: {
create: { actionType: "CREATED", actorId: session.user.id },
},
},
});
revalidatePath("/my-orders");
revalidatePath("/dashboard");
return { id: po.id };
}