From 1bfd8851c66f4029ee6e5088b3776819bf61f4b7 Mon Sep 17 00:00:00 2001
From: trisusilo48 <tri.susilo@altama.co.id>
Date: Wed, 28 Aug 2024 14:58:42 +0700
Subject: voucher shipping code

---
 src/lib/checkout/components/Checkout.jsx | 206 ++++++++++++++++++-------------
 1 file changed, 121 insertions(+), 85 deletions(-)

(limited to 'src/lib/checkout/components/Checkout.jsx')

diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx
index 384e7105..2e3d4e58 100644
--- a/src/lib/checkout/components/Checkout.jsx
+++ b/src/lib/checkout/components/Checkout.jsx
@@ -150,68 +150,88 @@ const Checkout = () => {
     // let dataVoucher = await findVoucher(code, auth.id, query);
     let dataVoucher = await getVoucher(auth?.id, {
       source: query,
-      code: code
+      code: code,
     });
     if (dataVoucher.length <= 0) {
       SetFindVoucher(1);
       return;
     }
 
-    let addNewLine = dataVoucher[0];
-    console.log('addNewLine', addNewLine);
-    if (addNewLine.applyType != 'shipping') {
-      let checkList = listVouchers?.findIndex(
-        (voucher) => voucher.code == addNewLine.code
-      );
-      if (checkList >= 0) {
-        if (listVouchers[checkList].canApply) {
-          ToggleSwitch(code);
-          SetCodeVoucher(null);
-        } else {
-          SetSelisihHargaCode(listVouchers[checkList].differenceToApply);
+    dataVoucher.forEach((addNewLine) => {
+      if (addNewLine.applyType !== 'shipping') {
+        // Mencari voucher dalam listVouchers
+        let checkList = listVouchers?.findIndex(
+          (voucher) => voucher.code === addNewLine.code
+        );
+
+        if (checkList >= 0) {
+          if (listVouchers[checkList].canApply) {
+            ToggleSwitch(addNewLine.code); // Perbaikan: Gunakan code voucher yang benar
+            SetCodeVoucher(null);
+          } else {
+            SetSelisihHargaCode(listVouchers[checkList].differenceToApply);
+            SetFindVoucher(2);
+          }
+          return; // Hentikan eksekusi lebih lanjut pada iterasi ini
+        }
+
+        // Memeriksa apakah subtotal memenuhi syarat minimal pembelian
+        if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) {
+          SetSelisihHargaCode(
+            currencyFormat(
+              addNewLine.minPurchaseAmount - cartCheckout?.subtotal
+            )
+          );
           SetFindVoucher(2);
+          return;
+        } else {
+          SetFindVoucher(3);
+          SetButtonTerapkan(true);
         }
-        return;
-      }
-      if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) {
-        SetSelisihHargaCode(
-          currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal)
-        );
-        SetFindVoucher(2);
-        return;
+
+        // Tambahkan voucher ke list dan set voucher aktif
+        SetListVoucher((prevList) => [addNewLine, ...prevList]);
+        SetActiveVoucher(addNewLine.code);
       } else {
-        SetFindVoucher(3);
-        SetButtonTerapkan(true);
-      }
-      SetListVoucher((prevList) => [addNewLine, ...prevList]);
-      SetActiveVoucher(addNewLine.code);
-    } else {
-      let checkList = listVoucherShippings?.findIndex(
-        (voucher) => voucher.code == addNewLine.code
-      );
-      if (checkList >= 0) {
-        if (listVoucherShippings[checkList].canApply) {
-          ToggleSwitch(code);
-          SetCodeVoucher(null);
-        } else {
-          SetSelisihHargaCode(listVoucherShippings[checkList].differenceToApply);
+        // Mencari voucher dalam listVoucherShippings
+        let checkList = listVoucherShippings?.findIndex(
+          (voucher) => voucher.code === addNewLine.code
+        );
+
+        if (checkList >= 0) {
+          if (listVoucherShippings[checkList].canApply) {
+            ToggleSwitch(addNewLine.code); // Perbaikan: Gunakan code voucher yang benar
+            SetCodeVoucher(null);
+          } else {
+            SetSelisihHargaCode(
+              listVoucherShippings[checkList].differenceToApply
+            );
+            SetFindVoucher(2);
+          }
+          return; // Hentikan eksekusi lebih lanjut pada iterasi ini
+        }
+
+        // Memeriksa apakah subtotal memenuhi syarat minimal pembelian
+        if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) {
+          SetSelisihHargaCode(
+            currencyFormat(
+              addNewLine.minPurchaseAmount - cartCheckout?.subtotal
+            )
+          );
           SetFindVoucher(2);
+          return;
+        } else {
+          SetFindVoucher(3);
+          SetButtonTerapkan(true);
         }
-        return;
-      }
-      if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) {
-        SetSelisihHargaCode(
-          currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal)
-        );
-        SetFindVoucher(2);
-        return;
-      } else {
-        SetFindVoucher(3);
-        SetButtonTerapkan(true);
+
+        // Tambahkan voucher ke list pengiriman dan set voucher aktif pengiriman
+        SetListVoucherShipping((prevList) => [addNewLine, ...prevList]);
+        setActiveVoucherShipping(addNewLine.code);
       }
-      SetListVoucherShipping((prevList) => [addNewLine, ...prevList]);
-      setActiveVoucherShipping(addNewLine.code);
-    }
+    });
+
+    // let addNewLine = dataVoucher[0];
   };
 
   useEffect(() => {
@@ -244,22 +264,21 @@ const Checkout = () => {
 
   const hitungDiscountVoucher = (code, source) => {
     let countDiscount = 0;
-    if(source === 'voucher'){
+    if (source === 'voucher') {
       let dataVoucherIndex = listVouchers.findIndex(
         (voucher) => voucher.code == code
       );
       let dataActiveVoucher = listVouchers[dataVoucherIndex];
-  
+
       countDiscount = dataActiveVoucher.discountVoucher;
-    }else{
+    } else {
       let dataVoucherIndex = listVoucherShippings.findIndex(
         (voucher) => voucher.code == code
       );
       let dataActiveVoucher = listVoucherShippings[dataVoucherIndex];
-  
+
       countDiscount = dataActiveVoucher.discountVoucher;
     }
-    
 
     /*if (dataActiveVoucher.discountType === 'percentage') {
       countDiscount = cartCheckout?.subtotal * (dataActiveVoucher.discountAmount / 100)
@@ -276,25 +295,24 @@ const Checkout = () => {
     return countDiscount;
   };
 
-  useEffect(() => {
-    if (!listVouchers) return;
-    if (!activeVoucher) return;
+  // useEffect(() => {
+  //   if (!listVouchers) return;
+  //   if (!activeVoucher) return;
 
-    console.log('voucher')
-    const countDiscount = hitungDiscountVoucher(activeVoucher, 'voucher');
+  //   console.log('voucher')
+  //   const countDiscount = hitungDiscountVoucher(activeVoucher, 'voucher');
 
-    SetDiscountVoucher(countDiscount);
-  }, [activeVoucher, listVouchers]);
+  //   SetDiscountVoucher(countDiscount);
+  // }, [activeVoucher, listVouchers]);
 
-  useEffect(() => {
-    if (!listVoucherShippings) return;
-    if (!activeVoucherShipping) return;
+  // useEffect(() => {
+  //   if (!listVoucherShippings) return;
+  //   if (!activeVoucherShipping) return;
 
-    console.log('voucher shipping')
-    const countDiscount = hitungDiscountVoucher(activeVoucherShipping, 'voucher_shipping');
+  //   const countDiscount = hitungDiscountVoucher(activeVoucherShipping, 'voucher_shipping');
 
-    SetDiscountVoucher(countDiscount);
-  }, [activeVoucherShipping, listVoucherShippings]);
+  //   SetDiscountVoucherOngkir(countDiscount);
+  // }, [activeVoucherShipping, listVoucherShippings]);
 
   useEffect(() => {
     if (qVoucher === 'PASTIHEMAT' && listVouchers) {
@@ -553,6 +571,10 @@ const Checkout = () => {
 
   const finalShippingAmt = biayaKirim - discShippingAmt;
 
+  const totalDiscountVoucher =
+    cartCheckout?.discountVoucher +
+    (cartCheckout?.discountVoucherShipping || 0);
+
   return (
     <>
       <BottomPopup
@@ -663,6 +685,19 @@ const Checkout = () => {
           )}
 
           <hr className='mt-8 mb-4 border-gray_r-8' />
+          {/* {!loadingVoucher &&
+            listVouchers?.length === 1 &&
+            listVoucherShippings?.length === 1}
+          {
+            <div className='flex items-center justify-center mt-4 mb-4'>
+              <div className='text-center'>
+                <h1 className='font-bold mb-4'>Tidak ada voucher tersedia</h1>
+                <p className='text-gray-500'>
+                  Maaf, saat ini tidak ada voucher yang tersedia.
+                </p>
+              </div>
+            </div>
+          } */}
 
           {listVoucherShippings && listVoucherShippings?.length > 0 && (
             <div>
@@ -801,16 +836,7 @@ const Checkout = () => {
           <hr className='mt-8 mb-4 border-gray_r-8' />
 
           <div>
-            {!loadingVoucher && listVouchers?.length === 0 ? (
-              <div className='flex items-center justify-center mt-4 mb-4'>
-                <div className='text-center'>
-                  <h1 className='font-bold mb-4'>Tidak ada voucher tersedia</h1>
-                  <p className='text-gray-500'>
-                    Maaf, saat ini tidak ada voucher yang tersedia.
-                  </p>
-                </div>
-              </div>
-            ) : (
+            {loadingVoucher && listVouchers?.length > 0 && (
               <h3 className='font-semibold mb-4'>
                 Promo Khusus Untuk {auth?.name}
               </h3>
@@ -1074,7 +1100,12 @@ const Checkout = () => {
         <div className='p-4 flex flex-col gap-y-4'>
           {!!products &&
             snakecaseKeys(products).map((item, index) => (
-              <CartItem key={index} item={item} editable={false} selfPicking={selectedExpedisi === '1,32' ? true : false}/>
+              <CartItem
+                key={index}
+                item={item}
+                editable={false}
+                selfPicking={selectedExpedisi === '1,32' ? true : false}
+              />
             ))}
         </div>
 
@@ -1137,7 +1168,7 @@ const Checkout = () => {
                 <div className='flex gap-x-2 justify-between'>
                   <div className='text-gray_r-11'>Diskon Voucher</div>
                   <div className='text-danger-500'>
-                    - {currencyFormat(discountVoucher)}
+                    - {currencyFormat(cartCheckout?.discountVoucher)}
                   </div>
                 </div>
               )}
@@ -1365,7 +1396,12 @@ const Checkout = () => {
                 <div className='flex flex-col gap-y-8 border-t border-gray-300 pt-8'>
                   {!!products &&
                     snakecaseKeys(products).map((item, index) => (
-                      <CartItem key={index} item={item} editable={false} selfPicking={selectedExpedisi === '1,32' ? true : false} />
+                      <CartItem
+                        key={index}
+                        item={item}
+                        editable={false}
+                        selfPicking={selectedExpedisi === '1,32' ? true : false}
+                      />
                     ))}
                 </div>
               </div>
@@ -1432,7 +1468,7 @@ const Checkout = () => {
                       <div className='flex gap-x-2 justify-between'>
                         <div className='text-gray_r-11'>Diskon Voucher</div>
                         <div className='text-danger-500'>
-                          - {currencyFormat(discountVoucher)}
+                          - {currencyFormat(cartCheckout?.discountVoucher)}
                         </div>
                       </div>
                     )}
@@ -1501,10 +1537,10 @@ const Checkout = () => {
                           className='object-contain object-center h-6 w-full rounded-md'
                         />
                       </span>
-                      {activeVoucher ? (
+                      {activeVoucher || activeVoucherShipping ? (
                         <div className=''>
                           <div className='text-left text-sm text-black font-semibold'>
-                            Hemat {currencyFormat(discountVoucher)}
+                            Hemat {currencyFormat(totalDiscountVoucher)}
                           </div>
                           <div className='text-left mt-1 text-green-600 text-xs'>
                             Voucher berhasil digunakan
-- 
cgit v1.2.3

