From 2af2659018d08cd8685a6e727994ab76f0735189 Mon Sep 17 00:00:00 2001
From: Mqdd <ahmadmiqdad27@gmail.com>
Date: Sun, 15 Feb 2026 11:18:35 +0700
Subject: <Miqdad> Locator

---
 src/app/api/product/import/route.tsx | 55 ++++++++++++++++++++++++++++--------
 1 file changed, 43 insertions(+), 12 deletions(-)

(limited to 'src/app/api/product/import/route.tsx')

diff --git a/src/app/api/product/import/route.tsx b/src/app/api/product/import/route.tsx
index 37b4690..f96c2d9 100644
--- a/src/app/api/product/import/route.tsx
+++ b/src/app/api/product/import/route.tsx
@@ -16,18 +16,49 @@ export async function POST(request: NextRequest) {
   const fileData: any[] = XLSX.utils.sheet_to_json(worksheet, { header: 1 })
   fileData.shift();
 
-  const newProducts = fileData.map(row => ({
-    id: undefined,
-    isDifferent: false,
-    name: row[0]?.toString() || '', // Handle undefined values
-    barcode: row[1]?.toString() || '', // Handle undefined values
-    itemCode: row[2]?.toString() || '', // Handle undefined values
-    onhandQty: row[3] || 0, // Handle undefined values
-    differenceQty: row[4] || 0, // Handle undefined values
-    companyId: intCompanyId, // Use the parsed company ID
-    externalId: row[6] ? row[6].toString() : null, // Handle undefined values
-    value: row[7] != null ? Number(row[7]) || 0 : null,
-  }));
+  // Fetch all locations for this company for location validation
+  const locations = await prisma.location.findMany({
+    where: { companyId: intCompanyId }
+  })
+
+  // Create a map for quick location lookup by name (case-insensitive) and by id
+  const locationByName = new Map(
+    locations.map(loc => [loc.name.toLowerCase(), loc.id])
+  )
+  const locationById = new Map(
+    locations.map(loc => [loc.id.toString(), loc.id])
+  )
+
+  const newProducts = fileData.map(row => {
+    let locationId: number | null = null
+    
+    // Parse locationId from column 8
+    const locationValue = row[8]?.toString().trim()
+    
+    if (locationValue) {
+      // Try to match by ID first, then by name
+      const idAsNumber = parseInt(locationValue)
+      if (!isNaN(idAsNumber) && locationById.has(idAsNumber.toString())) {
+        locationId = idAsNumber
+      } else if (locationByName.has(locationValue.toLowerCase())) {
+        locationId = locationByName.get(locationValue.toLowerCase()) || null
+      }
+    }
+
+    return {
+      id: undefined,
+      isDifferent: false,
+      name: row[0]?.toString() || '', // Handle undefined values
+      barcode: row[1]?.toString() || '', // Handle undefined values
+      itemCode: row[2]?.toString() || '', // Handle undefined values
+      onhandQty: row[3] || 0, // Handle undefined values
+      differenceQty: row[4] || 0, // Handle undefined values
+      companyId: intCompanyId, // Use the parsed company ID
+      externalId: row[6] ? row[6].toString() : null, // Handle undefined values
+      value: row[7] != null ? Number(row[7]) || 0 : null,
+      locationId: locationId, // Set locationId or null if not found/provided
+    }
+  });
 
   const whereCompany = { companyId: intCompanyId }
 
-- 
cgit v1.2.3

