From a0e32b6a8af5a57ef9d0222f8ecefc69a4f92294 Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Thu, 28 Aug 2025 08:11:07 +0700
Subject: <Miqdad> Change String

---
 src/lib/transaction/components/Transactions.jsx | 145 +++++++++++++++---------
 1 file changed, 90 insertions(+), 55 deletions(-)

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

diff --git a/src/lib/transaction/components/Transactions.jsx b/src/lib/transaction/components/Transactions.jsx
index 0d7e3f2c..826d88ac 100644
--- a/src/lib/transaction/components/Transactions.jsx
+++ b/src/lib/transaction/components/Transactions.jsx
@@ -40,7 +40,7 @@ import { Calendar } from 'lucide-react';
 import { DateRangePicker } from 'react-date-range';
 import 'react-date-range/dist/styles.css';
 import 'react-date-range/dist/theme/default.css';
-import { fetchPaymentSummary } from '../api/regenerate';
+// import { fetchPaymentSummary } from '../api/regenerate';
 
 const Transactions = ({ context = '' }) => {
   const auth = useAuth();
@@ -73,8 +73,49 @@ const Transactions = ({ context = '' }) => {
   const [cachedAllData, setCachedAllData] = useState(null);
   const [currentData, setCurrentData] = useState([]);
   const calendarRef = useRef(null);
+  // --- helpers aturan regenerate ---
+  const getApproval = (so) =>
+    (so?.approvalStatus || so?.approval_status || '').toLowerCase();
+
+  const isApprovalPending = (so) =>
+    ['pengajuan1', 'pengajuan2'].includes(getApproval(so));
+
+  const isWebSource = (so) => {
+    const code = (so?.source || so?.sourceCode || '').toLowerCase();
+    const id = so?.sourceId ?? so?.source_id;
+    return code === 'website' || code === 'web' || id === 59; // sesuaikan id sumber web kamu
+  };
+
+  const getPaymentStatus = (so) =>
+    (
+      so?.paymentSummary?.paymentStatus ||
+      so?.paymentSummary?.status ||
+      so?.paymentStatus ||
+      ''
+    ).toLowerCase();
+
+  // kosong | expire | belum settlement → boleh regenerate
+  const isPaymentUnsettled = (so) => {
+    const ps = getPaymentStatus(so);
+    if (!ps) return true; // kosong
+    if (['expire', 'expired'].includes(ps)) return true;
+    // dianggap “belum settlement” jika bukan status paid/settled
+    return !['settlement', 'paid', 'capture', 'success', 'settled'].includes(
+      ps
+    );
+  };
+
+  // WAJIB semua true
+  const canRegeneratePayment = (so) =>
+    isApprovalPending(so) && isWebSource(so) && isPaymentUnsettled(so);
+
+  // Jika backend sudah kirim flag eligibleContinue, tetap wajib lolos aturan front-end
+  const eligibleToShow = (so) => {
+    const backend =
+      typeof so?.eligibleContinue === 'boolean' ? so.eligibleContinue : true;
+    return backend && canRegeneratePayment(so);
+  };
 
-  // loading id utk tombol lanjutkan transaksi
   const [contLoadingId, setContLoadingId] = useState(null);
 
   const parseDate = (date) => {
@@ -364,6 +405,7 @@ const Transactions = ({ context = '' }) => {
     router.push(`${router.pathname}`);
   };
 
+
   const formatDate = (dateString) => {
     const months = [
       'Januari',
@@ -383,60 +425,53 @@ const Transactions = ({ context = '' }) => {
     return `${day} ${months[parseInt(month, 10) - 1]} ${year}`;
   };
 
-  // ==== Lanjutkan Transaksi ====
-  const handleContinuePayment = async (saleOrder) => {
-    try {
-      const partnerId =
-        auth?.partnerId || auth?.partner_id || auth?.partner?.id || auth?.id;
+const handleContinuePayment = async (saleOrder) => {
+  try {
+    if (!canRegeneratePayment(saleOrder)) {
+      toast.error(
+        'Tidak memenuhi syarat: approval belum pengajuan1/2, sumber bukan web, atau pembayaran sudah settle.'
+      );
+      return;
+    }
+    const partnerId =
+      auth?.partnerId || auth?.partner_id || auth?.partner?.id || auth?.id;
+    setContLoadingId(saleOrder.id);
 
-      setContLoadingId(saleOrder.id);
+    // pakai URL yang sudah ada dulu
+    const existingUrl =
+      saleOrder?.paymentSummary?.redirectUrl || saleOrder?.paymentLinkMidtrans;
 
-      // 1) coba pakai URL yang sudah dikirim dari list
-      const existingUrl =
-        saleOrder?.paymentSummary?.redirectUrl ||
-        saleOrder?.paymentLinkMidtrans;
+    if (existingUrl) {
+      window.open(existingUrl, '_blank', 'noopener,noreferrer');
+      toast.success('Membuka halaman pembayaran…');
+      return;
+    }
 
-      if (existingUrl) {
-        window.open(existingUrl, '_blank', 'noopener,noreferrer');
-        toast.success('Membuka halaman pembayaran…');
-        return;
-      }
+    // fallback autogen
+    const res = await fetchPaymentSummary({
+      partnerId,
+      saleOrderId: saleOrder.id,
+      autogen: true,
+    });
+    const redirectUrl = res?.redirect_url || res?.redirectUrl;
+    if (redirectUrl) {
+      window.open(redirectUrl, '_blank', 'noopener,noreferrer');
+      toast.success('Membuka halaman pembayaran…');
+    } else {
+      toast.error('Link pembayaran belum tersedia.');
+    }
+  } catch (e) {
+    const msg =
+      e?.response?.data?.description ||
+      e?.message ||
+      'Gagal melanjutkan transaksi';
+    toast.error(msg);
+  } finally {
+    setContLoadingId(null);
+  }
+};
 
-      // 2) fallback: generate baru via API
-      const res = await fetchPaymentSummary({
-        partnerId,
-        saleOrderId: saleOrder.id,
-        autogen: true,
-      });
-
-      const eligible =
-        res?.eligible_continue ??
-        res?.eligibleContinue ??
-        res?.eligible ??
-        false;
-
-      if (!eligible) {
-        toast.error('Transaksi belum memenuhi syarat untuk dilanjutkan.');
-        return;
-      }
 
-      const redirectUrl = res?.redirect_url || res?.redirectUrl;
-      if (redirectUrl) {
-        window.open(redirectUrl, '_blank', 'noopener,noreferrer');
-        toast.success('Membuka halaman pembayaran…');
-      } else {
-        toast.error('Link pembayaran belum tersedia.');
-      }
-    } catch (e) {
-      const msg =
-        e?.response?.data?.description ||
-        e?.message ||
-        'Gagal melanjutkan transaksi';
-      toast.error(msg);
-    } finally {
-      setContLoadingId(null);
-    }
-  };
 
   return (
     <>
@@ -646,7 +681,7 @@ const Transactions = ({ context = '' }) => {
                       Beli Lagi
                     </button>
 
-                    {saleOrder?.eligibleContinue && (
+                    {eligibleToShow(saleOrder) && (
                       <button
                         type='button'
                         disabled={contLoadingId === saleOrder.id}
@@ -659,7 +694,7 @@ const Transactions = ({ context = '' }) => {
                       >
                         {contLoadingId === saleOrder.id
                           ? 'Memproses…'
-                          : 'Lanjutkan Transaksi'}
+                          : 'Lanjutkan Pembayaran'}
                       </button>
                     )}
                   </div>
@@ -1140,7 +1175,7 @@ const Transactions = ({ context = '' }) => {
                                 Beli Lagi
                               </button>
 
-                              {saleOrder?.eligibleContinue && (
+                              {eligibleToShow(saleOrder) && (
                                 <button
                                   type='button'
                                   disabled={contLoadingId === saleOrder.id}
@@ -1153,7 +1188,7 @@ const Transactions = ({ context = '' }) => {
                                 >
                                   {contLoadingId === saleOrder.id
                                     ? 'Memproses…'
-                                    : 'Lanjutkan Transaksi'}
+                                    : 'Lanjutkan Pembayaran'}
                                 </button>
                               )}
                             </div>
-- 
cgit v1.2.3

