summaryrefslogtreecommitdiff
path: root/src/app/api/company
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/api/company')
-rw-r--r--src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx133
1 files changed, 69 insertions, 64 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
index ff9cd74..acfb413 100644
--- a/src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx
+++ b/src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx
@@ -1,99 +1,104 @@
-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"
+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 } }
+type PostParams = { params: { companyId: string; productId: string } };
-const SELF_HOST = process.env.SELF_HOST as 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 totalQty: { [key in keyof typeof Team]: number | 0 } = {
+ COUNT1: 0,
+ COUNT2: 0,
+ COUNT3: 0,
+ VERIFICATION: 0,
+ };
const searchParams = new URLSearchParams({
companyId: params.companyId.toString(),
- productId: params.productId.toString()
- })
+ productId: params.productId.toString(),
+ });
- const stockOpnamesFetch = await fetch(`${SELF_HOST}/api/stock-opname/location?${searchParams}`)
- const stockOpnames: StockOpnameLocationRes[] = await stockOpnamesFetch.json()
+ 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
+ let isDifferent: boolean = false;
+ let verificationCounter: number = 0;
for (const opname of stockOpnames) {
- let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname
+ 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 (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)) totalQty["COUNT1"] = (totalQty["COUNT1"] || 0) + COUNT1.quantity;
+ if (_.isNumber(COUNT2.quantity)) totalQty["COUNT2"] = (totalQty["COUNT2"] || 0) + COUNT2.quantity;
+ if (_.isNumber(COUNT3.quantity)) totalQty["COUNT3"] = (totalQty["COUNT3"] || 0) + COUNT3.quantity;
+ if (_.isNumber(VERIFICATION.quantity)) totalQty["VERIFICATION"] = (totalQty["VERIFICATION"] || 0) + VERIFICATION.quantity;
- if (_.isNumber(VERIFICATION.quantity)) verificationCounter++
+ if (_.isNumber(VERIFICATION.quantity)) verificationCounter++;
}
- const product = await prisma.product.findFirst({ where: { id: parseInt(params.productId) } })
- if (!product) return
+ 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 onhandQty = product.onhandQty;
+ // const differenceQty = product.differenceQty
+ const allQty = totalQty["COUNT1"] + totalQty["COUNT2"] + totalQty["COUNT3"] + totalQty["VERIFICATION"];
+ const differenceQty = onhandQty - allQty;
+ // const allQty = onhandQty + differenceQty
+ const zeroCount1: boolean = totalQty["COUNT1"] === 0;
+ const zeroCount2: boolean = totalQty["COUNT2"] === 0;
+ const zeroCount3: boolean = totalQty["COUNT3"] === 0;
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'],
+ // anyCountEqWithOnhand: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(onhandQty),
+ anyCountEqWithOnhand: (totalQty["COUNT1"] === onhandQty && zeroCount2 && zeroCount3) || (totalQty["COUNT2"] === onhandQty && zeroCount3) || totalQty["COUNT3"] === onhandQty,
+ anyCountEqWithAllQty: [totalQty["COUNT1"], totalQty["COUNT2"], totalQty["COUNT3"]].includes(allQty),
+ count1EqWithCount2: totalQty["COUNT1"] !== 0 && totalQty["COUNT2"] !== 0 && totalQty["COUNT1"] === totalQty["COUNT2"] && totalQty["COUNT3"] === null && totalQty["COUNT1"] !== onhandQty,
+ count1EqWithCount3: totalQty["COUNT1"] !== 0 && totalQty["COUNT3"] !== 0 && totalQty["COUNT1"] === totalQty["COUNT3"],
+ count2EqWithCount3: totalQty["COUNT2"] !== 0 && totalQty["COUNT3"] !== 0 && totalQty["COUNT2"] === totalQty["COUNT3"],
+ };
+
+ if (conditional.verificationCheckAll || conditional.anyCountEqWithOnhand || conditional.count1EqWithCount2 || conditional.count1EqWithCount3 || conditional.count2EqWithCount3) {
+ isDifferent = false;
+ } else {
+ isDifferent = true;
}
- isDifferent = !(
- conditional.verificationCheckAll ||
- conditional.anyCountEqWithOnhand ||
- conditional.anyCountEqWithAllQty ||
- conditional.count1EqWithCount2 ||
- conditional.count1EqWithCount3 ||
- conditional.count2EqWithCount3
- )
-
for (const opname of stockOpnames) {
- let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname
+ let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname;
const detailCondition = {
verificationCheckAll: _.isNumber(VERIFICATION.quantity),
- count1EqWithCount2: COUNT1.quantity === COUNT2.quantity,
+ anyCountEqWithOnhand: (totalQty["COUNT1"] === onhandQty && zeroCount2 && zeroCount3) || (totalQty["COUNT2"] === onhandQty && zeroCount3) || totalQty["COUNT3"] === onhandQty,
+ count1EqWithCount2: totalQty["COUNT3"] === 0 && COUNT1.quantity === COUNT2.quantity && totalQty["COUNT1"] !== onhandQty,
count1EqWithCount3: COUNT1.quantity === COUNT3.quantity,
- count2EqWithCount3: COUNT2.quantity === COUNT3.quantity,
+ count2EqWithCount3: totalQty["COUNT2"] !== null && totalQty["COUNT3"] !== null && totalQty["COUNT2"] === totalQty["COUNT3"],
+ // count3EqWithCount1_2: (COUNT3.quantity !== COUNT1.quantity) && (COUNT3.quantity !== COUNT2.quantity) && (COUNT1.quantity === COUNT2.quantity) && (COUNT3.quantity !== onhandQty)
+ };
+
+ if (detailCondition.verificationCheckAll || detailCondition.anyCountEqWithOnhand || detailCondition.count1EqWithCount2 || detailCondition.count1EqWithCount3 || detailCondition.count2EqWithCount3) {
+ isDifferent = false;
+ } else {
+ isDifferent = true;
+ break;
}
- isDifferent = !(
- detailCondition.verificationCheckAll ||
- detailCondition.count1EqWithCount2 ||
- detailCondition.count1EqWithCount3 ||
- detailCondition.count2EqWithCount3
- )
-
- if (isDifferent) break
}
+
const payload = {
where: { id: product.id },
- data: { isDifferent }
- }
+ data: { isDifferent },
+ };
- await prisma.product.update(payload)
+ await prisma.product.update(payload);
- return NextResponse.json(payload)
-} \ No newline at end of file
+ return NextResponse.json(payload);
+}