summaryrefslogtreecommitdiff
path: root/src/app/api/company
diff options
context:
space:
mode:
authorImanuel Marinao <imanuel.mariano.5@gmail.com>2024-07-04 13:40:28 +0700
committerImanuel Marinao <imanuel.mariano.5@gmail.com>2024-07-04 13:40:28 +0700
commitd0c00e30f3f850f8452397db05f41e0c0ecef3f9 (patch)
tree5a7a04fc6ac2480269b99c2a8fc21ab4b917731f /src/app/api/company
parent9a80c0a767c6a35a489b9d45654ae60f7ddc6794 (diff)
<iman> update stock opname
Diffstat (limited to 'src/app/api/company')
-rw-r--r--src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx142
1 files changed, 55 insertions, 87 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 cf76ac3..c533392 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,12 +1,12 @@
-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 | 0 } = {
@@ -14,75 +14,61 @@ export async function POST(request: NextRequest, { params }: PostParams) {
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}`)
- console.log("stockOpnamesFetch",stockOpnamesFetch)
- const stockOpnames: StockOpnameLocationRes[] = await stockOpnamesFetch.json()
- console.log("stockOpnames",stockOpnames)
+ const stockOpnamesFetch = await fetch(`${SELF_HOST}/api/stock-opname/location?${searchParams}`);
+ console.log("stockOpnamesFetch", stockOpnamesFetch);
+ const stockOpnames: StockOpnameLocationRes[] = await stockOpnamesFetch.json();
+ console.log("stockOpnames", stockOpnames);
- 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
-
- 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
+ let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname;
- 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 (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(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 = totalQty['COUNT1'] + totalQty['COUNT2'] + totalQty['COUNT3'] + totalQty['VERIFICATION']
- const differenceQty = onhandQty - allQty
+ 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 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),
- 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'],
+ // anyCountEqWithOnhand: (totalQty["COUNT1"] === onhandQty && zeroCount2 && zeroCount3) || (totalQty["COUNT2"] === onhandQty && zeroCount3) || totalQty["COUNT3"] === onhandQty,
+ anyCountEqWithOnhand: totalQty["COUNT1"] === onhandQty || totalQty["COUNT2"] === onhandQty || totalQty["COUNT1"] === 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"],
};
-
- // isDifferent = !(
- // conditional.verificationCheckAll ||
- // conditional.anyCountEqWithOnhand ||
- // conditional.anyCountEqWithAllQty ||
- // conditional.count1EqWithCount2 ||
- // conditional.count1EqWithCount3 ||
- // conditional.count2EqWithCount3
- // );
- if (
- conditional.verificationCheckAll ||
- conditional.anyCountEqWithOnhand ||
- conditional.count1EqWithCount2 ||
- conditional.count1EqWithCount3 ||
- conditional.count2EqWithCount3
- ) {
+
+ if (conditional.verificationCheckAll || conditional.anyCountEqWithOnhand || conditional.count1EqWithCount2 || conditional.count1EqWithCount3 || conditional.count2EqWithCount3) {
isDifferent = false;
} else {
isDifferent = true;
@@ -90,49 +76,31 @@ export async function POST(request: NextRequest, { params }: PostParams) {
for (const opname of stockOpnames) {
let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname;
-
+
const detailCondition = {
verificationCheckAll: _.isNumber(VERIFICATION.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,
+ // anyCountEqWithOnhand: (totalQty["COUNT1"] === onhandQty && zeroCount2 && zeroCount3) || (totalQty["COUNT2"] === onhandQty && zeroCount3) || totalQty["COUNT3"] === onhandQty,
+ anyCountEqWithOnhand: totalQty["COUNT1"] === onhandQty || totalQty["COUNT2"] === onhandQty || totalQty["COUNT1"] === onhandQty,
+ count1EqWithCount2: totalQty["COUNT3"] === 0 && COUNT1.quantity === COUNT2.quantity && totalQty["COUNT1"] !== onhandQty,
count1EqWithCount3: COUNT1.quantity === COUNT3.quantity,
- count2EqWithCount3: COUNT2.quantity === COUNT3.quantity && (COUNT2.quantity === 0 && COUNT3.quantity === 0),
+ count2EqWithCount3: COUNT2.quantity === COUNT3.quantity && COUNT2.quantity !== 0 && COUNT3.quantity !== 0,
// 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
- ) {
+
+ if (detailCondition.verificationCheckAll || detailCondition.anyCountEqWithOnhand || detailCondition.count1EqWithCount2 || detailCondition.count1EqWithCount3 || detailCondition.count2EqWithCount3) {
isDifferent = false;
} else {
isDifferent = true;
break;
}
- console.log('----------------------------------BATAS SUCI ATAS RECOMPUTE------------------------------');
- console.log('opname:', opname);
- console.log('Is Different2222:', isDifferent);
- console.log('Conditions:', detailCondition);
}
- console.log('Total Quantity:', totalQty);
- console.log('Onhand Quantity:', onhandQty);
- console.log('Difference Quantity:', differenceQty);
- console.log('Conditions:', conditional);
- console.log('Is Different:', isDifferent);
- console.log('----------------------------------BATAS SUCI BAWAH RECOMPUTE------------------------------');
-
const payload = {
where: { id: product.id },
- data: { isDifferent }
- }
-
- await prisma.product.update(payload)
+ data: { isDifferent },
+ };
- return NextResponse.json(payload)
+ await prisma.product.update(payload);
-
-} \ No newline at end of file
+ return NextResponse.json(payload);
+}