summaryrefslogtreecommitdiff
path: root/src/app/api/stock-opname/export/route.tsx
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-11-17 15:16:23 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-11-17 15:16:23 +0700
commit07819844d5ef7e323fd956eacfedecb2f4f4bb80 (patch)
treed83ba8c6ff7f47f568889ff8371ee9d9918ed166 /src/app/api/stock-opname/export/route.tsx
parent10ce1ad59969576244ba786d7d17da2da3fe6f61 (diff)
Update result feature
Diffstat (limited to 'src/app/api/stock-opname/export/route.tsx')
-rw-r--r--src/app/api/stock-opname/export/route.tsx68
1 files changed, 68 insertions, 0 deletions
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