From b88fb936374fc31c9f15a947ece26db01d4771e8 Mon Sep 17 00:00:00 2001 From: 2ManyProjects Date: Mon, 28 Apr 2025 21:09:36 -0500 Subject: [PATCH] fixed product section of the reports page --- frontend/src/pages/Admin/ReportsPage.jsx | 160 ++++++++++++++++------- 1 file changed, 116 insertions(+), 44 deletions(-) diff --git a/frontend/src/pages/Admin/ReportsPage.jsx b/frontend/src/pages/Admin/ReportsPage.jsx index 0327277..d9c65b5 100644 --- a/frontend/src/pages/Admin/ReportsPage.jsx +++ b/frontend/src/pages/Admin/ReportsPage.jsx @@ -235,6 +235,11 @@ const ReportsPage = () => { .slice(0, 8); // Top 8 locations }, [filteredOrders]); + // Debug - log filtered orders + useEffect(() => { + console.log("Filtered orders:", filteredOrders); + }, [filteredOrders]); + // Prepare time series data for revenue chart const revenueTimeSeriesData = useMemo(() => { if (!orders) return []; @@ -293,9 +298,41 @@ const ReportsPage = () => { return Array.from(dataMap.values()); }, [filteredOrders, timelineType, fromDate, toDate]); + // Fetch order details for product analysis + const { data: orderDetails, isLoading: orderDetailsLoading } = useQuery({ + queryKey: ['admin-order-details', filteredOrders.map(order => order.id)], + queryFn: async () => { + // Only fetch if we have filtered orders + if (!filteredOrders.length) return []; + + // Fetch details for each order in parallel + const detailPromises = filteredOrders.map(order => + apiClient.get(`/admin/orders/${order.id}`) + .then(response => response.data) + .catch(error => { + console.error(`Error fetching details for order ${order.id}:`, error); + return null; + }) + ); + + const results = await Promise.all(detailPromises); + return results.filter(Boolean); // Remove any failed requests + }, + enabled: filteredOrders.length > 0 + }); + + + // Debug - log order details + useEffect(() => { + if (orderDetails) { + console.log("Order details:", orderDetails); + } + }, [orderDetails]); + + // Prepare product sales data const productSalesData = useMemo(() => { - if (!orders || !products) return []; + if (!products || !orderDetails) return []; const productSales = {}; @@ -310,13 +347,23 @@ const ReportsPage = () => { }; }); - // Aggregate sales data from orders - filteredOrders.forEach(order => { + // Aggregate sales data from order details + orderDetails.forEach(order => { if (order.items && Array.isArray(order.items)) { order.items.forEach(item => { if (productSales[item.product_id]) { - productSales[item.product_id].quantity += item.quantity; - productSales[item.product_id].revenue += parseFloat(item.price_at_purchase) * item.quantity; + productSales[item.product_id].quantity += parseInt(item.quantity) || 0; + productSales[item.product_id].revenue += parseFloat(item.price_at_purchase || 0) * (parseInt(item.quantity) || 0); + } else { + // Handle case where product might exist in orders but not in current products list + console.log(`Product ${item.product_id} found in orders but not in products list`); + productSales[item.product_id] = { + id: item.product_id, + name: item.product_name || `Product ${item.product_id.substring(0, 8)}...`, + category: item.product_category || 'Unknown', + quantity: parseInt(item.quantity) || 0, + revenue: parseFloat(item.price_at_purchase || 0) * (parseInt(item.quantity) || 0) + }; } }); } @@ -326,7 +373,7 @@ const ReportsPage = () => { return Object.values(productSales) .filter(product => product.quantity > 0) .sort((a, b) => b.quantity - a.quantity); - }, [filteredOrders, products]); + }, [products, orderDetails]); // Top selling products for the chart const topSellingProducts = useMemo(() => { @@ -358,7 +405,7 @@ const ReportsPage = () => { }, [productSalesData]); // Loading state - const isLoading = ordersLoading || productsLoading; + const isLoading = ordersLoading || productsLoading || orderDetailsLoading; // Handle export reports const handleExportCSV = () => { @@ -629,24 +676,32 @@ const ReportsPage = () => { Top Selling Products - - - - - - - { - return name === 'revenue' ? `$${value.toFixed(2)}` : value; - }} /> - - - - - - + {topSellingProducts.length > 0 ? ( + + + + + + + { + return name === 'revenue' ? `${value.toFixed(2)}` : value; + }} /> + + + + + + + ) : ( + + + No product sales data available for the selected time period. + + + )} @@ -654,28 +709,45 @@ const ReportsPage = () => { Product Sales Details - - - - - - - - - - - {productSalesData.map((product) => ( - - - - - + {productSalesData.length > 0 ? ( +
ProductCategoryUnits SoldRevenue
{product.name}{product.category}{product.quantity}${product.revenue.toFixed(2)}
+ + + + + + - ))} - -
ProductCategoryUnits SoldRevenue
+ + + {productSalesData.map((product) => ( + + {product.name} + {product.category} + {product.quantity} + ${product.revenue.toFixed(2)} + + ))} + + + ) : ( + + + No product sales data available for the selected time period. + + + )}
+ + {orderDetailsLoading && ( + + + + Loading product data... + + + )} )}