diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-07-03 17:48:31 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-07-03 17:48:31 +0700 |
| commit | a711c3d7f3a41bb5ac20485723a0f629fbfde1b7 (patch) | |
| tree | 319d9741f34e322ce604a609b060005faa4370da | |
| parent | a2854778bfba99eb1eded911b48964f076e17a27 (diff) | |
<iman> update code logic
| -rw-r--r-- | package.json | 1 | ||||
| -rw-r--r-- | src/app/api/company/[companyId]/product/[productId]/compute-different/route.tsx | 93 | ||||
| -rw-r--r-- | src/app/api/stock-opname/route.tsx | 61 | ||||
| -rw-r--r-- | src/modules/result/components/Table.tsx | 2 |
4 files changed, 112 insertions, 45 deletions
diff --git a/package.json b/package.json index 3ed98f1..1b5ef6a 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "bcrypt": "^5.1.1", "clsx": "^2.0.0", "cookies-next": "^4.0.0", + "dotenv": "^16.4.5", "framer-motion": "^10.16.4", "jsonwebtoken": "^9.0.2", "lodash": "^4.17.21", 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..f2e8c3d 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 @@ -9,11 +9,11 @@ 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 totalQty: { [key in keyof typeof Team]: number | 0 } = { + COUNT1: 0, + COUNT2: 0, + COUNT3: 0, + VERIFICATION: 0, } const searchParams = new URLSearchParams({ @@ -22,7 +22,9 @@ export async function POST(request: NextRequest, { params }: PostParams) { }) 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 @@ -35,10 +37,11 @@ export async function POST(request: NextRequest, { params }: PostParams) { 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++ } @@ -47,46 +50,76 @@ export async function POST(request: NextRequest, { params }: PostParams) { if (!product) return const onhandQty = product.onhandQty - const differenceQty = product.differenceQty - const allQty = onhandQty + differenceQty + // const differenceQty = product.differenceQty + const allQty = totalQty['COUNT1'] + totalQty['COUNT2'] + totalQty['COUNT3'] + totalQty['VERIFICATION'] + const differenceQty = onhandQty - allQty + // const allQty = onhandQty + differenceQty const conditional = { verificationCheckAll: verificationCounter > 0 && verificationCounter === stockOpnames.length, - anyCountEqWithOnhand: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(onhandQty), + // anyCountEqWithOnhand: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(onhandQty), + anyCountEqWithOnhand: totalQty['COUNT1'] === onhandQty || totalQty['COUNT2'] === onhandQty || totalQty['COUNT1'] === 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 = !( + count1EqWithCount2: totalQty['COUNT1'] !== 0 && totalQty['COUNT2'] !== 0 && totalQty['COUNT1'] === totalQty['COUNT2'], + 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.anyCountEqWithAllQty || conditional.count1EqWithCount2 || conditional.count1EqWithCount3 || conditional.count2EqWithCount3 - ) + ) { + isDifferent = false; + } else { + isDifferent = true; + } for (const opname of stockOpnames) { - let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname - + let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname; + const detailCondition = { verificationCheckAll: _.isNumber(VERIFICATION.quantity), + anyCountEqWithOnhand: totalQty['COUNT1'] === onhandQty || totalQty['COUNT2'] === onhandQty || totalQty['COUNT1'] === onhandQty , count1EqWithCount2: COUNT1.quantity === COUNT2.quantity, count1EqWithCount3: COUNT1.quantity === COUNT3.quantity, - count2EqWithCount3: COUNT2.quantity === COUNT3.quantity, - } - - isDifferent = !( + count2EqWithCount3: COUNT2.quantity === COUNT3.quantity && (COUNT2.quantity === 0 && COUNT3.quantity === 0), + }; + + if ( detailCondition.verificationCheckAll || + detailCondition.anyCountEqWithOnhand || detailCondition.count1EqWithCount2 || detailCondition.count1EqWithCount3 || detailCondition.count2EqWithCount3 - ) - - if (isDifferent) break + ) { + isDifferent = false; + } else { + isDifferent = true; + break; + } + // console.log('opname:', opname); + // console.log('Is Different2222:', isDifferent); + // console.log('Conditions:', detailCondition); } + // console.log('----------------------------------BATAS SUCI ATAS------------------------------'); + // 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------------------------------'); + const payload = { where: { id: product.id }, @@ -96,4 +129,6 @@ export async function POST(request: NextRequest, { params }: PostParams) { 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 154ed14..250e70b 100644 --- a/src/app/api/stock-opname/route.tsx +++ b/src/app/api/stock-opname/route.tsx @@ -138,11 +138,11 @@ const computeIsDifferent = async ({ companyId: number, productId: number }) => { - 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({ @@ -176,47 +176,76 @@ const computeIsDifferent = async ({ if (!product) return const onhandQty = product.onhandQty - const differenceQty = product.differenceQty - const allQty = onhandQty + differenceQty + const allQty = totalQty['COUNT1'] + totalQty['COUNT2'] + totalQty['COUNT3'] + totalQty['VERIFICATION'] + const differenceQty = onhandQty - allQty + // 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), + // anyCountEqWithOnhand: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(onhandQty), + anyCountEqWithOnhand: totalQty['COUNT1'] === onhandQty || totalQty['COUNT2'] === onhandQty || totalQty['COUNT1'] === 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 = !( + // isDifferent = !( + // conditional.verificationCheckAll || + // conditional.anyCountEqWithOnhand || + // conditional.anyCountEqWithAllQty || + // conditional.count1EqWithCount2 || + // conditional.count1EqWithCount3 || + // conditional.count2EqWithCount3 + // ) + if ( conditional.verificationCheckAll || conditional.anyCountEqWithOnhand || - conditional.anyCountEqWithAllQty || conditional.count1EqWithCount2 || conditional.count1EqWithCount3 || conditional.count2EqWithCount3 - ) + ) { + isDifferent = false; + } else { + isDifferent = true; + } for (const opname of stockOpnames) { let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname const detailCondition = { verificationCheckAll: _.isNumber(VERIFICATION.quantity), + anyCountEqWithOnhand: totalQty['COUNT1'] === onhandQty || totalQty['COUNT2'] === onhandQty || totalQty['COUNT1'] === onhandQty , count1EqWithCount2: COUNT1.quantity === COUNT2.quantity, count1EqWithCount3: COUNT1.quantity === COUNT3.quantity, - count2EqWithCount3: COUNT2.quantity === COUNT3.quantity, + count2EqWithCount3: COUNT2.quantity === COUNT3.quantity && (COUNT2.quantity === 0 && COUNT3.quantity === 0), } - isDifferent = !( + if ( detailCondition.verificationCheckAll || + detailCondition.anyCountEqWithOnhand || detailCondition.count1EqWithCount2 || detailCondition.count1EqWithCount3 || detailCondition.count2EqWithCount3 - ) - - if (isDifferent) break + ) { + isDifferent = false; + } else { + isDifferent = true; + break; + } + console.log('Is Different2222:', isDifferent); + console.log('Conditions:', detailCondition); } + console.log('----------------------------------BATAS SUCI ATAS------------------------------'); + console.log('Total Quantity:', totalQty); + console.log('Onhand Quantity:', onhandQty); + console.log('Difference Quantity:', differenceQty); + console.log('Conditions:', conditional); + console.log('Is Different tambah data:', isDifferent); + console.log('----------------------------------BATAS SUCI BAWAH------------------------------'); + 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 082f988..94ed5bf 100644 --- a/src/modules/result/components/Table.tsx +++ b/src/modules/result/components/Table.tsx @@ -78,7 +78,9 @@ const Table = () => { const recompute = async (productId: number, companyId: string) => { const response = await fetch(`/api/company/${companyId}/product/${productId}/compute-different`, { method: 'POST' }) + console.log("response",response) const stockOpname = await response.json() + console.log("stockOpname",stockOpname) toast(`Berhasil menghitung ulang status barang, hasilnya ${stockOpname.data.isDifferent ? 'selisih' : 'aman'}`) stockOpnames.refetch() } |
