From 828e0b2301bfe7ff95b776dacf5f844dd10756d0 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Sat, 9 Dec 2023 12:44:08 +0700
Subject: Fix compute different product stock opname

---
 src/app/api/stock-opname/route.tsx | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 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 4c2dee4..38284bf 100644
--- a/src/app/api/stock-opname/route.tsx
+++ b/src/app/api/stock-opname/route.tsx
@@ -169,10 +169,6 @@ const computeIsDifferent = async ({
     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++
   }
 
@@ -201,6 +197,26 @@ const computeIsDifferent = async ({
     conditional.count2EqWithCount3
   )
 
+  for (const opname of stockOpnames) {
+    let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname
+
+    const detailCondition = {
+      verificationCheckAll: _.isNumber(VERIFICATION.quantity),
+      count1EqWithCount2: COUNT1.quantity !== null && COUNT2.quantity !== null && COUNT1.quantity === COUNT2.quantity,
+      count1EqWithCount3: COUNT1.quantity !== null && COUNT3.quantity !== null && COUNT1.quantity === COUNT3.quantity,
+      count2EqWithCount3: COUNT2.quantity !== null && COUNT3.quantity !== null && COUNT2.quantity === COUNT3.quantity,
+    }
+
+    isDifferent = !(
+      detailCondition.verificationCheckAll ||
+      detailCondition.count1EqWithCount2 ||
+      detailCondition.count1EqWithCount3 ||
+      detailCondition.count2EqWithCount3
+    )
+
+    if (isDifferent) break
+  }
+
   await prisma.product.update({
     where: { id: product.id },
     data: { isDifferent }
-- 
cgit v1.2.3

