From 07819844d5ef7e323fd956eacfedecb2f4f4bb80 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Fri, 17 Nov 2023 15:16:23 +0700
Subject: Update result feature

---
 src/app/api/stock-opname/export/route.tsx | 68 +++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 src/app/api/stock-opname/export/route.tsx

(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
new file mode 100644
index 0000000..1595f20
--- /dev/null
+++ b/src/app/api/stock-opname/export/route.tsx
@@ -0,0 +1,68 @@
+import { StockOpnameLocationRes } from "@/common/types/stockOpname";
+import { Product } from "@prisma/client";
+import { NextRequest, NextResponse } from "next/server";
+import { prisma } from "prisma/client";
+import * as XLSX from "xlsx"
+
+const SELF_HOST = process.env.SELF_HOST as string
+
+export async function GET(request: NextRequest) {
+  const searchParams = request.nextUrl.searchParams
+  const paramCompanyId = searchParams.get('companyId')
+
+  if (!paramCompanyId) return NextResponse.json({ error: 'Bad Request. Missing companyId' }, { status: 400 })
+
+  const companyId = parseInt(paramCompanyId)
+
+  const stockOpnames = await prisma.stockOpname.groupBy({
+    by: ['productId'],
+    where: { companyId }
+  })
+
+  type SOLocationProduct = (StockOpnameLocationRes & { product: Product | null })
+
+  const datas: SOLocationProduct[] = []
+
+  for (const opname of stockOpnames) {
+    const requestParams = new URLSearchParams({
+      productId: opname.productId.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)
+  }
+
+  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 worksheet = XLSX.utils.json_to_sheet(dataSheet)
+  const workbook = XLSX.utils.book_new()
+  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1")
+  const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', 'type': 'buffer' })
+
+  return new NextResponse(excelBuffer, {
+    headers: {
+      'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+      'Content-Disposition': 'attachment; filename=export.xlsx'
+    }
+  })
+}
\ No newline at end of file
-- 
cgit v1.2.3

