Schema: - New Company model (name, gstNumber, address, telephone, mobile, email, invoiceAddress, isActive) - PurchaseOrder.companyId FK (optional, SET NULL on company delete) - Migration: 20260530000003_add_company Admin: - /admin/companies page with full CRUD (create, edit, deactivate, delete) - Companies table shows name, GST, contact details, status - Companies link added to Admin section of sidebar (Briefcase icon) PO forms (new / edit / import / manager-edit): - Company dropdown appears at the top of Order Information when companies exist - Pre-populated with first active company; selection persisted to DB via companyId Import form: - parseSheet() now extracts companyName from Excel row 1 (col A) - Import preview auto-matches detected company name against known companies - Shows detected name as a hint; user can override before saving Export (PDF + XLSX): - Company constants (CO_NAME, CO_ADDR, CO_TEL, INV_ADDR, INV_GST) are now derived from the linked Company record when present, falling back to the original Pelagia Marine hardcoded defaults when no company is set Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
81 lines
2.4 KiB
TypeScript
81 lines
2.4 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;
|
|
vesselId: string;
|
|
accountId: string;
|
|
companyId?: 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 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: input.vesselId,
|
|
accountId: input.accountId,
|
|
companyId: input.companyId ?? null,
|
|
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 };
|
|
}
|