From 2bfa2138be7135dc12daa98262fbaf8305698f00 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 7 Nov 2024 13:55:10 +0700
Subject: <iman> add logic quotation no po

---
 src/lib/transaction/components/Transaction.jsx | 116 +++++++++++++++++++++++--
 1 file changed, 110 insertions(+), 6 deletions(-)

(limited to 'src/lib/transaction/components/Transaction.jsx')

diff --git a/src/lib/transaction/components/Transaction.jsx b/src/lib/transaction/components/Transaction.jsx
index 4d401037..777dd771 100644
--- a/src/lib/transaction/components/Transaction.jsx
+++ b/src/lib/transaction/components/Transaction.jsx
@@ -38,7 +38,9 @@ import aprpoveApi from '../api/approveApi';
 import rejectApi from '../api/rejectApi';
 import rejectProductApi from '../api/rejectProductApi';
 import { useRouter } from 'next/router';
-
+import { gtagPurchase } from '@/core/utils/googleTag';
+import { deleteItemCart } from '@/core/utils/cart';
+import axios from 'axios';
 const Transaction = ({ id }) => {
   const router = useRouter();
   const [isModalOpen, setIsModalOpen] = useState(false);
@@ -48,7 +50,7 @@ const Transaction = ({ id }) => {
   const { transaction } = useTransaction({ id });
 
   const statusApprovalWeb = transaction.data?.approvalStep;
-
+  const [isLoading, setIsLoading] = useState(false);
   const { queryAirwayBill } = useAirwayBill({ orderId: id });
   const [airwayBillPopup, setAirwayBillPopup] = useState(null);
 
@@ -84,8 +86,22 @@ const Transaction = ({ id }) => {
   };
 
   const [cancelTransaction, setCancelTransaction] = useState(false);
+  const [continueNoPo, setContinueNoPo] = useState(false);
+  const [continueTransaction, setContinueTransaction] = useState(false);
   const openCancelTransaction = () => setCancelTransaction(true);
+  const openContinueTransaction = () => {
+    if (!transaction.data?.purchaseOrderFile) {
+      setContinueTransaction(true);
+    } else {
+      checkout();
+    }
+  };
+  const ContinueTransaction = () => {
+    setContinueNoPo(true);
+    checkoutNoPO();
+  };
   const closeCancelTransaction = () => setCancelTransaction(false);
+  const closeContinueTransaction = () => setContinueTransaction(false);
 
   const [rejectTransaction, setRejectTransaction] = useState(false);
 
@@ -101,7 +117,6 @@ const Transaction = ({ id }) => {
     }
     closeCancelTransaction();
   };
-
   const checkout = async () => {
     if (!transaction.data?.purchaseOrderFile) {
       toast.error('Mohon upload dokumen PO anda sebelum melanjutkan pesanan');
@@ -112,6 +127,63 @@ const Transaction = ({ id }) => {
     transaction.refetch();
   };
 
+  const checkoutNoPO = async () => {
+    setIsLoading(true);
+    gtagPurchase(
+      transaction.data.products,
+      transaction.data.deliveryAmount,
+      transaction.data.name
+    );
+
+    gtag('event', 'conversion', {
+      send_to: 'AW-954540379/nDymCL3BhaQYENvClMcD',
+      value:
+        transaction.data?.amountTotal +
+        Math.round(parseInt(transaction.data.deliveryAmount * 1.1) / 1000) *
+          1000,
+      currency: 'IDR',
+      transaction_id: transaction.data.id,
+    });
+
+    for (const product of transaction.data.products)
+      deleteItemCart({ productId: product.id });
+    if (transaction.data?.amountTotal > 0) {
+      const payment = await axios.post(
+        `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${transaction.data.id}`
+      );
+      setIsLoading(false);
+      window.location.href = payment.data.redirectUrl;
+    } else {
+      window.location.href = `${
+        process.env.NEXT_PUBLIC_SELF_HOST
+      }/shop/checkout/success?order_id=${transaction.data.name.replace(
+        /\//g,
+        '-'
+      )}`;
+    }
+
+    /* const midtrans = async () => {
+        for (const product of products) deleteItemCart({ productId: product.id });
+        if (grandTotal > 0) {
+          const payment = await axios.post(
+            `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}`
+          );
+          setIsLoading(false);
+          window.location.href = payment.data.redirectUrl;
+        } else {
+          window.location.href = `${
+            process.env.NEXT_PUBLIC_SELF_HOST
+          }/shop/checkout/success?order_id=${isCheckouted.name.replace(
+            /\//g,
+            '-'
+          )}`;
+        }
+      };*/
+    toast.success('Berhasil melanjutkan pesanan');
+    transaction.refetch();
+    y;
+  };
+
   const handleApproval = async () => {
     await aprpoveApi({ id });
     toast.success('Berhasil melanjutkan approval');
@@ -207,6 +279,32 @@ const Transaction = ({ id }) => {
   return (
     transaction.data?.name && (
       <>
+        <BottomPopup
+          active={continueTransaction}
+          close={closeContinueTransaction}
+          title='Lanjutkan Transaksi'
+        >
+          <div className='leading-7 text-gray_r-12/80'>
+            Apakah anda yakin melanjutkan tanpa upload PO?{' '}
+            <span className='underline'>{transaction.data?.name}</span>?
+          </div>
+          <div className='flex justify-end mt-6 gap-x-4'>
+            <button
+              className='btn-solid-red w-full md:w-fit'
+              type='button'
+              onClick={checkoutNoPO}
+            >
+              Ya, Lanjutkan
+            </button>
+            <button
+              className='btn-light w-full md:w-fit'
+              type='button'
+              onClick={closeContinueTransaction}
+            >
+              Batal
+            </button>
+          </div>
+        </BottomPopup>
         <BottomPopup
           active={cancelTransaction}
           close={closeCancelTransaction}
@@ -469,7 +567,10 @@ const Transaction = ({ id }) => {
               )}
             {transaction.data?.status == 'draft' &&
               !auth?.feature?.soApproval && (
-                <button className='btn-yellow w-full mt-4' onClick={checkout}>
+                <button
+                  className='btn-yellow w-full mt-4'
+                  onClick={openContinueTransaction}
+                >
                   Lanjutkan Transaksi
                 </button>
               )}
@@ -563,7 +664,10 @@ const Transaction = ({ id }) => {
                   )}
                 {transaction.data?.status == 'draft' &&
                   !auth?.feature.soApproval && (
-                    <button className='btn-yellow' onClick={checkout}>
+                    <button
+                      className='btn-yellow'
+                      onClick={openContinueTransaction}
+                    >
                       Lanjutkan Transaksi
                     </button>
                   )}
@@ -879,7 +983,7 @@ const Transaction = ({ id }) => {
                     </div>
                   </div>
                 </div>
-              )} 
+              )}
 
               {transaction?.data?.productsRejectLine.length > 0 && (
                 <div className='text-h-sm font-semibold mt-10 mb-4'>
-- 
cgit v1.2.3

