From f3aa76b1810b3bc8b25bd02c76b50384893fc453 Mon Sep 17 00:00:00 2001
From: trisusilo48 <tri.susilo@altama.co.id>
Date: Tue, 6 Aug 2024 15:12:56 +0700
Subject: voucher

---
 src/lib/checkout/components/Checkout.jsx | 118 ++++++++++++++++++++++---------
 1 file changed, 86 insertions(+), 32 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 09a791ee..84f68dab 100644
--- a/src/lib/checkout/components/Checkout.jsx
+++ b/src/lib/checkout/components/Checkout.jsx
@@ -131,6 +131,7 @@ const Checkout = () => {
         setLoadingVoucher(true);
         let dataVoucher = await getVoucher(auth?.id, {
           source: query,
+          type: 'all,brand',
         });
         SetListVoucher(dataVoucher);
 
@@ -146,40 +147,71 @@ const Checkout = () => {
   };
 
   const VoucherCode = async (code) => {
-    const source = 'code=' + code + '&source=' + query;
     // let dataVoucher = await findVoucher(code, auth.id, query);
-    let dataVoucher = await getVoucherNew(source);
+    let dataVoucher = await getVoucher(auth?.id, {
+      source: query,
+      code: code
+    });
     if (dataVoucher.length <= 0) {
       SetFindVoucher(1);
       return;
     }
 
     let addNewLine = dataVoucher[0];
-    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);
+    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);
+          SetFindVoucher(2);
+        }
+        return;
+      }
+      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;
+      SetListVoucher((prevList) => [addNewLine, ...prevList]);
+      SetActiveVoucher(addNewLine.code);
     } else {
-      SetFindVoucher(3);
-      SetButtonTerapkan(true);
+      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);
+          SetFindVoucher(2);
+        }
+        return;
+      }
+      if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) {
+        SetSelisihHargaCode(
+          currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal)
+        );
+        SetFindVoucher(2);
+        return;
+      } else {
+        SetFindVoucher(3);
+        SetButtonTerapkan(true);
+      }
+      SetListVoucherShipping((prevList) => [addNewLine, ...prevList]);
+      setActiveVoucherShipping(addNewLine.code);
     }
-    SetListVoucher((prevList) => [addNewLine, ...prevList]);
-    SetActiveVoucher(addNewLine.code);
   };
 
   useEffect(() => {
@@ -187,7 +219,7 @@ const Checkout = () => {
   }, [bottomPopup]);
 
   useEffect(() => {
-    voucher();
+    // voucher();
     const loadExpedisi = async () => {
       let dataExpedisi = await ExpedisiList();
       dataExpedisi = dataExpedisi.map((expedisi) => ({
@@ -210,13 +242,24 @@ const Checkout = () => {
     };
   }, []);
 
-  const hitungDiscountVoucher = (code) => {
-    let dataVoucherIndex = listVouchers.findIndex(
-      (voucher) => voucher.code == code
-    );
-    let dataActiveVoucher = listVouchers[dataVoucherIndex];
-
-    let countDiscount = dataActiveVoucher.discountVoucher;
+  const hitungDiscountVoucher = (code, source) => {
+    let countDiscount = 0;
+    if(source === 'voucher'){
+      let dataVoucherIndex = listVouchers.findIndex(
+        (voucher) => voucher.code == code
+      );
+      let dataActiveVoucher = listVouchers[dataVoucherIndex];
+  
+      countDiscount = dataActiveVoucher.discountVoucher;
+    }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)
@@ -237,11 +280,22 @@ const Checkout = () => {
     if (!listVouchers) return;
     if (!activeVoucher) return;
 
-    const countDiscount = hitungDiscountVoucher(activeVoucher);
+    console.log('voucher')
+    const countDiscount = hitungDiscountVoucher(activeVoucher, 'voucher');
 
     SetDiscountVoucher(countDiscount);
   }, [activeVoucher, listVouchers]);
 
+  useEffect(() => {
+    if (!listVoucherShippings) return;
+    if (!activeVoucherShipping) return;
+
+    console.log('voucher shipping')
+    const countDiscount = hitungDiscountVoucher(activeVoucherShipping, 'voucher_shipping');
+
+    SetDiscountVoucher(countDiscount);
+  }, [activeVoucherShipping, listVoucherShippings]);
+
   useEffect(() => {
     if (qVoucher === 'PASTIHEMAT' && listVouchers) {
       let code = qVoucher;
-- 
cgit v1.2.3

