From ae3207e9778c3f2fb01d64714ff15fc073ec093d Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Fri, 1 Dec 2023 10:16:12 +0700
Subject: Update compute different, result, and product list feature

---
 src/app/api/stock-opname/route.tsx | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

(limited to 'src/app/api/stock-opname/route.tsx')

diff --git a/src/app/api/stock-opname/route.tsx b/src/app/api/stock-opname/route.tsx
index 3b3ad5a..e98c3b2 100644
--- a/src/app/api/stock-opname/route.tsx
+++ b/src/app/api/stock-opname/route.tsx
@@ -113,9 +113,7 @@ export async function POST(request: NextRequest) {
     team
   }
 
-  const stockOpname = await prisma.stockOpname.findFirst({
-    where: query
-  })
+  const stockOpname = await prisma.stockOpname.findFirst({ where: query })
 
   const data = {
     ...query,
@@ -165,6 +163,7 @@ const computeIsDifferent = async ({
   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
@@ -182,25 +181,31 @@ const computeIsDifferent = async ({
     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: productId } })
   if (!product) return
 
-  const onhandQty = product?.onhandQty || 0
+  const onhandQty = product.onhandQty
+  const differenceQty = product.differenceQty
+  const allQty = onhandQty + differenceQty
 
   if (!isDifferent) {
     const conditional = {
-      wasVerified: typeof totalQty['VERIFICATION'] === 'number',
+      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']
+      count2EqWithCount3: totalQty['COUNT2'] !== null && totalQty['COUNT3'] !== null && totalQty['COUNT2'] === totalQty['COUNT3'],
     }
 
     isDifferent = !(
-      conditional.wasVerified ||
+      conditional.verificationCheckAll ||
       conditional.anyCountEqWithOnhand ||
+      conditional.anyCountEqWithAllQty ||
       conditional.count1EqWithCount2 ||
       conditional.count1EqWithCount3 ||
       conditional.count2EqWithCount3
-- 
cgit v1.2.3

