diff options
| -rw-r--r-- | src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx | 83 | ||||
| -rw-r--r-- | src/app/api/stock-opname/route.tsx | 34 | ||||
| -rw-r--r-- | src/modules/result/components/Table.tsx | 10 |
3 files changed, 109 insertions, 18 deletions
diff --git a/src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx b/src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx new file mode 100644 index 0000000..2141c27 --- /dev/null +++ b/src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx @@ -0,0 +1,83 @@ +import { StockOpnameLocationRes } from "@/common/types/stockOpname" +import { NextRequest, NextResponse } from "next/server" +import { prisma } from "prisma/client" +import { Team } from "prisma/generated/client" +import _ from "lodash" + +type PostParams = { params: { companyId: string, productId: string } } + +const SELF_HOST = process.env.SELF_HOST as string + +export async function POST(request: NextRequest, { params }: PostParams) { + const totalQty: { [key in keyof typeof Team]: number | null } = { + COUNT1: null, + COUNT2: null, + COUNT3: null, + VERIFICATION: null + } + + const searchParams = new URLSearchParams({ + companyId: params.companyId.toString(), + productId: params.productId.toString() + }) + + const stockOpnamesFetch = await fetch(`${SELF_HOST}/api/stock-opname/location?${searchParams}`) + const stockOpnames: StockOpnameLocationRes[] = await stockOpnamesFetch.json() + + let isDifferent: boolean = false + let verificationCounter: number = 0 + + for (const opname of stockOpnames) { + let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname + + if (totalQty['COUNT1'] === null && _.isNumber(COUNT1.quantity)) totalQty['COUNT1'] = 0 + if (totalQty['COUNT2'] === null && _.isNumber(COUNT2.quantity)) totalQty['COUNT2'] = 0 + if (totalQty['COUNT3'] === null && _.isNumber(COUNT3.quantity)) totalQty['COUNT3'] = 0 + if (totalQty['VERIFICATION'] === null && _.isNumber(VERIFICATION.quantity)) totalQty['VERIFICATION'] = 0 + + if (_.isNumber(totalQty['COUNT1']) && _.isNumber(COUNT1.quantity)) totalQty['COUNT1'] += COUNT1.quantity + if (_.isNumber(totalQty['COUNT2']) && _.isNumber(COUNT2.quantity)) totalQty['COUNT2'] += COUNT2.quantity + if (_.isNumber(totalQty['COUNT3']) && _.isNumber(COUNT3.quantity)) totalQty['COUNT3'] += COUNT3.quantity + if (_.isNumber(totalQty['VERIFICATION']) && _.isNumber(VERIFICATION.quantity)) totalQty['VERIFICATION'] += VERIFICATION.quantity + + if (_.isNumber(COUNT1.quantity) && _.isNumber(COUNT2.quantity) && COUNT1.quantity !== COUNT2.quantity) isDifferent = true + if (_.isNumber(COUNT1.quantity) && _.isNumber(COUNT3.quantity) && COUNT1.quantity !== COUNT3.quantity) isDifferent = true + if (_.isNumber(COUNT2.quantity) && _.isNumber(COUNT3.quantity) && COUNT2.quantity !== COUNT3.quantity) isDifferent = true + + if (_.isNumber(VERIFICATION.quantity)) verificationCounter++ + } + + const product = await prisma.product.findFirst({ where: { id: parseInt(params.productId) } }) + if (!product) return + + const onhandQty = product.onhandQty + const differenceQty = product.differenceQty + const allQty = onhandQty + differenceQty + + const conditional = { + verificationCheckAll: verificationCounter > 0 && verificationCounter === stockOpnames.length, + anyCountEqWithOnhand: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(onhandQty), + anyCountEqWithAllQty: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(allQty), + count1EqWithCount2: totalQty['COUNT1'] !== null && totalQty['COUNT2'] !== null && totalQty['COUNT1'] === totalQty['COUNT2'], + count1EqWithCount3: totalQty['COUNT1'] !== null && totalQty['COUNT3'] !== null && totalQty['COUNT1'] === totalQty['COUNT3'], + count2EqWithCount3: totalQty['COUNT2'] !== null && totalQty['COUNT3'] !== null && totalQty['COUNT2'] === totalQty['COUNT3'], + } + + isDifferent = !( + conditional.verificationCheckAll || + conditional.anyCountEqWithOnhand || + conditional.anyCountEqWithAllQty || + conditional.count1EqWithCount2 || + conditional.count1EqWithCount3 || + conditional.count2EqWithCount3 + ) + + const payload = { + where: { id: product.id }, + data: { isDifferent } + } + + await prisma.product.update(payload) + + return NextResponse.json(payload) +}
\ No newline at end of file diff --git a/src/app/api/stock-opname/route.tsx b/src/app/api/stock-opname/route.tsx index cba16e2..4c2dee4 100644 --- a/src/app/api/stock-opname/route.tsx +++ b/src/app/api/stock-opname/route.tsx @@ -183,26 +183,24 @@ const computeIsDifferent = async ({ const differenceQty = product.differenceQty const allQty = onhandQty + differenceQty - if (!isDifferent) { - const conditional = { - verificationCheckAll: verificationCounter > 0 && verificationCounter === stockOpnames.length, - anyCountEqWithOnhand: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(onhandQty), - anyCountEqWithAllQty: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(allQty), - count1EqWithCount2: totalQty['COUNT1'] !== null && totalQty['COUNT2'] !== null && totalQty['COUNT1'] === totalQty['COUNT2'], - count1EqWithCount3: totalQty['COUNT1'] !== null && totalQty['COUNT3'] !== null && totalQty['COUNT1'] === totalQty['COUNT3'], - count2EqWithCount3: totalQty['COUNT2'] !== null && totalQty['COUNT3'] !== null && totalQty['COUNT2'] === totalQty['COUNT3'], - } - - isDifferent = !( - conditional.verificationCheckAll || - conditional.anyCountEqWithOnhand || - conditional.anyCountEqWithAllQty || - conditional.count1EqWithCount2 || - conditional.count1EqWithCount3 || - conditional.count2EqWithCount3 - ) + const conditional = { + verificationCheckAll: verificationCounter > 0 && verificationCounter === stockOpnames.length, + anyCountEqWithOnhand: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(onhandQty), + anyCountEqWithAllQty: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(allQty), + count1EqWithCount2: totalQty['COUNT1'] !== null && totalQty['COUNT2'] !== null && totalQty['COUNT1'] === totalQty['COUNT2'], + count1EqWithCount3: totalQty['COUNT1'] !== null && totalQty['COUNT3'] !== null && totalQty['COUNT1'] === totalQty['COUNT3'], + count2EqWithCount3: totalQty['COUNT2'] !== null && totalQty['COUNT3'] !== null && totalQty['COUNT2'] === totalQty['COUNT3'], } + isDifferent = !( + conditional.verificationCheckAll || + conditional.anyCountEqWithOnhand || + conditional.anyCountEqWithAllQty || + conditional.count1EqWithCount2 || + conditional.count1EqWithCount3 || + conditional.count2EqWithCount3 + ) + await prisma.product.update({ where: { id: product.id }, data: { isDifferent } diff --git a/src/modules/result/components/Table.tsx b/src/modules/result/components/Table.tsx index 20cc2d6..082f988 100644 --- a/src/modules/result/components/Table.tsx +++ b/src/modules/result/components/Table.tsx @@ -76,6 +76,13 @@ const Table = () => { stockOpnames.refetch() } + const recompute = async (productId: number, companyId: string) => { + const response = await fetch(`/api/company/${companyId}/product/${productId}/compute-different`, { method: 'POST' }) + const stockOpname = await response.json() + toast(`Berhasil menghitung ulang status barang, hasilnya ${stockOpname.data.isDifferent ? 'selisih' : 'aman'}`) + stockOpnames.refetch() + } + const isLoading = stockOpnames.isLoading || stockOpnames.isRefetching const COL_LENGTH = 9 @@ -149,6 +156,9 @@ const Table = () => { </Button> </DropdownTrigger> <DropdownMenu> + <DropdownItem key="recompute" onPress={() => recompute(stockOpname.id, company)}> + Re-compute + </DropdownItem> <DropdownItem key="toggleDifferent" onPress={() => toggleDifferent(stockOpname.id)}> Tandai {stockOpname.isDifferent ? 'aman' : 'selisih'} </DropdownItem> |
