From bb01aa8c2423b19247d9e3530ae1a3256d884861 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Thu, 23 Nov 2023 16:38:42 +0700
Subject: Update export data result

---
 src/app/api/stock-opname/export/route.tsx | 80 ++++++++++++++++++++-----------
 1 file changed, 52 insertions(+), 28 deletions(-)

(limited to 'src/app/api/stock-opname/export/route.tsx')

diff --git a/src/app/api/stock-opname/export/route.tsx b/src/app/api/stock-opname/export/route.tsx
index 1595f20..27f99fe 100644
--- a/src/app/api/stock-opname/export/route.tsx
+++ b/src/app/api/stock-opname/export/route.tsx
@@ -14,45 +14,69 @@ export async function GET(request: NextRequest) {
 
   const companyId = parseInt(paramCompanyId)
 
-  const stockOpnames = await prisma.stockOpname.groupBy({
-    by: ['productId'],
-    where: { companyId }
-  })
-
-  type SOLocationProduct = (StockOpnameLocationRes & { product: Product | null })
+  let page = 1;
+  let totalPage = 1;
+  const stockOpnames = []
+  do {
+    const requestParams = new URLSearchParams({
+      page: page.toString(),
+      companyId: companyId.toString()
+    })
+    const stockOpnamesFetch = await fetch(`${SELF_HOST}/api/stock-opname?${requestParams}`)
+    const stockOpnamesJson = await stockOpnamesFetch.json()
+    stockOpnames.push(...stockOpnamesJson.result)
 
-  const datas: SOLocationProduct[] = []
+    page++
+    totalPage = stockOpnamesJson.totalPage
+  } while (page <= totalPage);
 
   for (const opname of stockOpnames) {
     const requestParams = new URLSearchParams({
-      productId: opname.productId.toString(),
+      productId: opname.id.toString(),
       companyId: companyId.toString()
     })
     const detailsFetch = await fetch(`${SELF_HOST}/api/stock-opname/location?${requestParams}`)
     const details: StockOpnameLocationRes[] = await detailsFetch.json()
 
-    const product = await prisma.product.findFirst({ where: { id: opname.productId } })
-    const mappedData: SOLocationProduct[] = details.map((data) => ({
-      ...data,
-      product
-    }))
-
-    datas.push(...mappedData)
+    opname['details'] = details
   }
 
-  const dataSheet = datas.map((data) => ({
-    location: data.name,
-    name: data.product?.name,
-    itemCode: data.product?.itemCode,
-    barcode: data.product?.barcode,
-    onhandQty: data.product?.onhandQty,
-    differenceQty: data.product?.differenceQty,
-    count1: data.COUNT1.quantity,
-    count2: data.COUNT2.quantity,
-    count3: data.COUNT3.quantity,
-    verification: data.VERIFICATION.quantity,
-    isDifferent: data.product?.isDifferent
-  }))
+
+  const dataSheet = []
+
+  for (const opname of stockOpnames) {
+    const defaultItems = {
+      itemCode: opname?.itemCode,
+      barcode: opname?.barcode,
+      name: opname.name,
+    }
+
+    dataSheet.push({
+      ...defaultItems,
+      location: '',
+      count1: opname.quantity.COUNT1,
+      count2: opname.quantity.COUNT2,
+      count3: opname.quantity.COUNT3,
+      verification: opname.quantity.VERIFICATION,
+      onhandQty: opname.onhandQty,
+      differenceQty: opname.differenceQty,
+      isDifferent: opname.isDifferent
+    })
+
+    for (const detail of opname['details']) {
+      dataSheet.push({
+        ...defaultItems,
+        location: detail.name,
+        count1: detail.COUNT1.quantity,
+        count2: detail.COUNT2.quantity,
+        count3: detail.COUNT3.quantity,
+        verification: detail.VERIFICATION.quantity,
+        onhandQty: '',
+        differenceQty: '',
+        isDifferent: ''
+      })
+    }
+  }
 
   const worksheet = XLSX.utils.json_to_sheet(dataSheet)
   const workbook = XLSX.utils.book_new()
-- 
cgit v1.2.3

