diff --git a/App/app/(portal)/po/import/actions.ts b/App/app/(portal)/po/import/actions.ts index 65a0c9c..aed7dff 100644 --- a/App/app/(portal)/po/import/actions.ts +++ b/App/app/(portal)/po/import/actions.ts @@ -12,6 +12,9 @@ export type ImportPoInput = { vesselId: string; accountId: string; companyId?: string; + /** Original PO number from the imported Excel — preserved as-is on the PO record. + * If absent, a new structured number is generated. */ + originalPoNumber?: string; /** vendorId of an existing vendor, if pre-matched in the UI */ vendorId?: string; /** Raw vendor name from the Excel — used to auto-create if no vendorId matched */ @@ -119,8 +122,10 @@ export async function importPo( 0 ); - // ── 4. Generate structured PO number ───────────────────────────────────── - const poNumber = await generatePoNumber(input.vesselId, input.companyId); + // ── 4. Determine PO number ──────────────────────────────────────────────── + // Preserve the original PO number from the imported document when available; + // otherwise generate a new structured number starting from 9000+. + const poNumber = input.originalPoNumber?.trim() || await generatePoNumber(input.vesselId, input.companyId); // ── 5. Create PO in CLOSED state ────────────────────────────────────────── // Imported POs bypass the approval workflow — they are historical records. diff --git a/App/app/(portal)/po/import/import-form.tsx b/App/app/(portal)/po/import/import-form.tsx index 95702e7..105426e 100644 --- a/App/app/(portal)/po/import/import-form.tsx +++ b/App/app/(portal)/po/import/import-form.tsx @@ -113,6 +113,7 @@ export function ImportForm({ vessels, accounts, vendors, companies }: Props) { vesselId: preview.vesselId, companyId: preview.companyId || undefined, accountId: preview.accountId, + originalPoNumber: preview.parsed.poNumber || undefined, vendorId: preview.vendorId || undefined, parsedVendorName: preview.parsed.vendorName || undefined, parsedVendorAddress: preview.parsed.vendorAddress || undefined, diff --git a/App/lib/po-number.ts b/App/lib/po-number.ts index 2ccbae3..03fdfbc 100644 --- a/App/lib/po-number.ts +++ b/App/lib/po-number.ts @@ -24,7 +24,9 @@ function currentFY(): string { /** Find the next sequential PO ID (min 200) by scanning existing structured PO numbers. */ async function nextPoId(): Promise { const pos = await db.purchaseOrder.findMany({ select: { poNumber: true } }); - let maxId = 199; + // Floor at 8999 so the first generated ID is 9000, avoiding clashes with + // imported POs that retain their original IDs (which typically start from 1). + let maxId = 8999; for (const { poNumber } of pos) { const parts = poNumber.split("/"); if (parts.length === 4) {