From 7376e70d47d429787900eb49cb6102b389e0690c Mon Sep 17 00:00:00 2001
From: Mqdd <ahmadmiqdad27@gmail.com>
Date: Tue, 27 Jan 2026 15:19:11 +0700
Subject: <Miqdad> 2026

---
 src/app/api/stock-opname/route.tsx | 44 ++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 16 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 384c834..e1adb0f 100644
--- a/src/app/api/stock-opname/route.tsx
+++ b/src/app/api/stock-opname/route.tsx
@@ -174,37 +174,49 @@ const computeIsDifferent = async ({ companyId, productId }: { companyId: number;
   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 && onhandQty!== 0) || (totalQty["COUNT3"] === onhandQty && onhandQty!== 0),
-    anyCountEqWithAllQty: [totalQty["COUNT1"], totalQty["COUNT2"], totalQty["COUNT3"]].includes(allQty),
-    count1EqWithCount2: totalQty["COUNT1"] !== null && totalQty["COUNT2"] !== null && totalQty["COUNT1"] === totalQty["COUNT2"] && totalQty["COUNT3"] === null,
-    count1EqWithCount3: totalQty["COUNT1"] !== null && totalQty["COUNT3"] !== null && 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) {
+  // Jika ada verifikasi qty, langsung AMAN
+  if (verificationCounter > 0) {
     isDifferent = false;
   } else {
-    isDifferent = true;
+    // Jika tidak ada qty verifikasi, cek kondisi tim 1/2/3
+    const conditional = {
+      // anyCountEqWithOnhand: [totalQty['COUNT1'], totalQty['COUNT2'], totalQty['COUNT3']].includes(onhandQty),
+      anyCountEqWithOnhand: (totalQty["COUNT1"] === onhandQty && zeroCount2 && zeroCount3) || (totalQty["COUNT2"] === onhandQty && zeroCount3 && onhandQty!== 0) || (totalQty["COUNT3"] === onhandQty && onhandQty!== 0),
+      anyCountEqWithAllQty: [totalQty["COUNT1"], totalQty["COUNT2"], totalQty["COUNT3"]].includes(allQty),
+      count1EqWithCount2: totalQty["COUNT1"] !== null && totalQty["COUNT2"] !== null && totalQty["COUNT1"] === totalQty["COUNT2"] && totalQty["COUNT3"] === null,
+      count1EqWithCount3: totalQty["COUNT1"] !== null && totalQty["COUNT3"] !== null && totalQty["COUNT1"] === totalQty["COUNT3"],
+      count2EqWithCount3: totalQty["COUNT2"] !== 0 && totalQty["COUNT3"] !== 0 && totalQty["COUNT2"] === totalQty["COUNT3"],
+    };
+
+    if (conditional.anyCountEqWithOnhand || conditional.count1EqWithCount2 || conditional.count1EqWithCount3 || conditional.count2EqWithCount3) {
+      isDifferent = false;
+    } 
+    else {
+      isDifferent = true;
+    }
   }
 
   for (const opname of stockOpnames) {
     let { COUNT1, COUNT2, COUNT3, VERIFICATION } = opname;
 
+    // Normalize: treat missing (null/undefined) same as 0
+    const count1 = _.isNumber(COUNT1.quantity) ? COUNT1.quantity : 0;
+    const count2 = _.isNumber(COUNT2.quantity) ? COUNT2.quantity : 0;
+    const count3 = _.isNumber(COUNT3.quantity) ? COUNT3.quantity : 0;
+
     const detailCondition = {
       verificationCheckAll: _.isNumber(VERIFICATION.quantity),
       anyCountEqWithOnhand: (totalQty["COUNT1"] === onhandQty && zeroCount2 && zeroCount3) || (totalQty["COUNT2"] === onhandQty && zeroCount3 && onhandQty!== 0) || (totalQty["COUNT3"] === onhandQty && onhandQty!== 0),
-      count1EqWithCount2: totalQty["COUNT3"] === 0 && COUNT1.quantity === COUNT2.quantity,
-      count1EqWithCount3: COUNT1.quantity === COUNT3.quantity,
-      count2EqWithCount3: totalQty["COUNT2"] !== 0 && totalQty["COUNT3"] !== 0 && totalQty["COUNT2"] === totalQty["COUNT3"],
+      count1EqWithCount2: totalQty["COUNT3"] === 0 && count1 === count2,
+      count1EqWithCount3: count1 === count3,
+      count2EqWithCount3: totalQty["COUNT2"] !== 0 && totalQty["COUNT3"] !== 0 && count2 === 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 {
+    }
+    else {
       isDifferent = true;
       break;
     }
-- 
cgit v1.2.3

