From f6482d24d2b99db4f3fa92db163f5a3bee873c44 Mon Sep 17 00:00:00 2001 From: everythingonblack Date: Thu, 15 May 2025 15:49:40 +0700 Subject: [PATCH] ok --- src/pages/Transactions.js | 224 +++++++++++++++++++++----------------- 1 file changed, 123 insertions(+), 101 deletions(-) diff --git a/src/pages/Transactions.js b/src/pages/Transactions.js index f6dd1ba..61d92df 100644 --- a/src/pages/Transactions.js +++ b/src/pages/Transactions.js @@ -8,71 +8,38 @@ import { declineTransaction, getTransactionsFromCafe, } from "../helpers/transactionHelpers"; -import { getTables } from "../helpers/tableHelper"; +import dayjs from "dayjs"; +import utc from "dayjs/plugin/utc"; +import timezone from "dayjs/plugin/timezone"; + import ButtonWithReplica from "../components/ButtonWithReplica"; -import TableCanvas from "../components/TableCanvas"; - -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; -import timezone from 'dayjs/plugin/timezone'; +dayjs.extend(utc); +dayjs.extend(timezone); export default function Transactions({ shop, shopId, propsShopId, sendParam, deviceType, paymentUrl }) { const { shopIdentifier, tableId } = useParams(); if (sendParam) sendParam({ shopIdentifier, tableId }); - dayjs.extend(utc); - dayjs.extend(timezone); - const [transactions, setTransactions] = useState([]); const [isPaymentLoading, setIsPaymentLoading] = useState(false); const [isPaymentOpen, setIsPaymentOpen] = useState(false); + const [searchTerm, setSearchTerm] = useState(''); +const [matchedItems, setMatchedItems] = useState([]); + +useEffect(() => { + setMatchedItems(searchAndAggregateItems(transactions, searchTerm)); +}, [searchTerm, transactions]); + useEffect(() => { const fetchTransactions = async () => { - if (deviceType == 'clerk') { - try { - let response; - response = await getTransactionsFromCafe(shopId || propsShopId, 5, false); - console.log(response) - if (response) { - setTransactions(response); - return; - } - } catch (error) { - console.error("Error fetching transactions:", error); - } - } - else { - try { - let response; - response = await getMyTransactions(shopId || propsShopId, 5); - console.log(response) - const combinedTransactions = []; - - response.forEach(cafe => { - const { cafeId, name: cafeName, transactions } = cafe; - - transactions.forEach(transaction => { - const newTransaction = { - ...transaction, - cafeId, - cafeName, - DetailedTransactions: transaction.detailedTransactions // Rename here - }; - delete newTransaction.detailedTransactions; // Remove the old key - combinedTransactions.push(newTransaction); - }); - }); - - // combinedTransactions now contains all transactions with cafe info and renamed key - console.log(combinedTransactions) - - // combinedTransactions now contains all transactions with cafe info - setTransactions(combinedTransactions); - } catch (error) { - console.error("Error fetching transactions:", error); - } + try { + let response = await getTransactionsFromCafe(shopId || propsShopId, 5, false); + console.log(response) + if (response) setTransactions(response); + } catch (error) { + console.error("Error fetching transactions:", error); } }; @@ -90,18 +57,48 @@ export default function Transactions({ shop, shopId, propsShopId, sendParam, dev return grandTotal + calculateTotalPrice(transaction.DetailedTransactions); }, 0); }; +const searchAndAggregateItems = (transactions, searchTerm) => { + if (!searchTerm.trim()) return []; + + const normalizedTerm = searchTerm.trim().toLowerCase(); + const aggregatedItems = new Map(); + + transactions.forEach(transaction => { + transaction.DetailedTransactions.forEach(detail => { + const itemName = detail.Item.name; + const itemNameLower = itemName.toLowerCase(); + + if (itemNameLower.includes(normalizedTerm)) { + const key = detail.itemId; + + if (!aggregatedItems.has(key)) { + aggregatedItems.set(key, { + itemId: detail.itemId, + name: itemName, + totalQty: 0, + totalPrice: 0, + }); + } + + const current = aggregatedItems.get(key); + current.totalQty += detail.qty; + current.totalPrice += detail.qty * (detail.promoPrice || detail.price); + } + }); + }); + + return Array.from(aggregatedItems.values()); +}; + const handleConfirm = async (transactionId) => { setIsPaymentLoading(true); try { - const c = await confirmTransaction(transactionId); - if (c) { - // Update the confirmed status locally - setTransactions((prevTransactions) => - prevTransactions.map((transaction) => - transaction.transactionId === transactionId - ? { ...transaction, confirmed: 1 } // Set to confirmed - : transaction + const result = await confirmTransaction(transactionId); + if (result) { + setTransactions(prev => + prev.map(t => + t.transactionId === transactionId ? { ...t, confirmed: 1 } : t ) ); } @@ -115,14 +112,11 @@ export default function Transactions({ shop, shopId, propsShopId, sendParam, dev const handleDecline = async (transactionId) => { setIsPaymentLoading(true); try { - const c = await declineTransaction(transactionId); - if (c) { - // Update the confirmed status locally - setTransactions((prevTransactions) => - prevTransactions.map((transaction) => - transaction.transactionId === transactionId - ? c // Set to confirmed - : transaction + const result = await declineTransaction(transactionId); + if (result) { + setTransactions(prev => + prev.map(t => + t.transactionId === transactionId ? result : t ) ); } @@ -135,12 +129,40 @@ export default function Transactions({ shop, shopId, propsShopId, sendParam, dev return (
-
-

Daftar transaksi - Rp {calculateAllTransactionsTotal(transactions)}

-
- {/* */} -
+

+ Daftar transaksi Rp {calculateAllTransactionsTotal(transactions)} +

+ + setSearchTerm(e.target.value)} + style={{ border:'0px',height: '42px',borderRadius: '15px', margin: '7px auto 10px', width: '88%', paddingLeft: '8px' }} +/> + + + + {/* Existing Transactions List (keep all your JSX below unchanged) */} +
+ +{matchedItems.length > 0 && matchedItems.map(item => ( +
+
    +
  • + {item.name} x {item.totalQty} +
  • +
+
+ Total: + Rp {item.totalPrice} +
+
+))} {transactions && transactions.map((transaction) => (
) : transaction.confirmed === 3 ? (
- - - - - - - - - - -
+ + + + + + + + + + +
) : ( @@ -316,7 +338,7 @@ export default function Transactions({ shop, shopId, propsShopId, sendParam, dev } - {deviceType == 'guestDevice' && transaction.confirmed < 2 && transaction.payment_type != 'cash' && transaction.payment_type != 'paylater/cash' && + {deviceType == 'guestDevice' && transaction.confirmed < 2 && transaction.payment_type != 'cash' && transaction.payment_type != 'paylater/cash' && - {deviceType == 'guestDevice' && transaction.confirmed >=0 && transaction.confirmed < 2 && transaction.payment_type == 'cash' ? + {deviceType == 'guestDevice' && transaction.confirmed >= 0 && transaction.confirmed < 2 && transaction.payment_type == 'cash' ?