From 734f96107fd0aebf03fc1cad18921421723cc887 Mon Sep 17 00:00:00 2001 From: Hardik Date: Sun, 31 May 2026 06:07:59 +0530 Subject: [PATCH] feat(import): upsert ProductVendorPrice from imported PO line items MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For each line item with a price and a resolved vendor, upsert a ProductVendorPrice record (productId + vendorId → price). This keeps the per-vendor price list in the item catalogue up to date from imports. Co-Authored-By: Claude Sonnet 4.6 --- App/app/(portal)/po/import/actions.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/App/app/(portal)/po/import/actions.ts b/App/app/(portal)/po/import/actions.ts index aed7dff..85e7ea3 100644 --- a/App/app/(portal)/po/import/actions.ts +++ b/App/app/(portal)/po/import/actions.ts @@ -88,13 +88,13 @@ export async function importPo( let productId: string | undefined; if (existing) { productId = existing.id; - // Update lastPrice if we have a better price + // Update lastPrice / lastVendor on the product if (item.unitPrice > 0) { await db.product.update({ where: { id: existing.id }, data: { lastPrice: item.unitPrice, - lastVendorId: resolvedVendorId ?? undefined, + ...(resolvedVendorId ? { lastVendorId: resolvedVendorId } : {}), }, }); } @@ -113,6 +113,15 @@ export async function importPo( productId = newProduct.id; } + // Upsert per-vendor price so the item catalogue reflects actual invoice prices + if (productId && resolvedVendorId && item.unitPrice > 0) { + await db.productVendorPrice.upsert({ + where: { productId_vendorId: { productId, vendorId: resolvedVendorId } }, + update: { price: item.unitPrice }, + create: { productId, vendorId: resolvedVendorId, price: item.unitPrice }, + }); + } + resolvedLineItems.push({ ...item, productId }); }