1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "prisma/client";
import * as XLSX from "xlsx";
export async function POST(request: NextRequest) {
const searchParams = request.nextUrl.searchParams
const companyId = searchParams.get('companyId')
if (!companyId) return NextResponse.json({ error: 'Bad Request. Missing companyId' }, { status: 400 })
const intCompanyId = parseInt(companyId)
const body = await request.arrayBuffer();
const workbook = XLSX.read(body, { type: 'buffer' })
const worksheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[worksheetName]
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,
}));
const whereCompany = { companyId: intCompanyId }
await prisma.stockOpname.deleteMany({ where: whereCompany })
await prisma.product.deleteMany({ where: whereCompany })
await prisma.product.createMany({ data: newProducts })
return NextResponse.json(true)
}
|