fix(po-number): floor at 9000, imported POs keep original PO number

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Hardik 2026-05-31 02:33:42 +05:30
parent 6763a60421
commit ccc93d40f3
3 changed files with 11 additions and 3 deletions

View file

@ -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.

View file

@ -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,

View file

@ -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<number> {
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) {