import React, { useState, useMemo } from 'react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts'; import { DollarSign, FileText, ShoppingCart, TrendingUp } from 'lucide-react'; import { __ } from '@/lib/i18n'; import { formatMoney, getStoreCurrency } from '@/lib/currency'; import { useDashboardPeriod } from '@/hooks/useDashboardPeriod'; import { useTaxesAnalytics } from '@/hooks/useAnalytics'; import { ErrorCard } from '@/components/ErrorCard'; import { getPageLoadErrorMessage } from '@/lib/errorHandling'; import { StatCard } from './components/StatCard'; import { ChartCard } from './components/ChartCard'; import { DataTable, Column } from './components/DataTable'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { DUMMY_TAXES_DATA, TaxesData, TaxByRate, TaxByLocation } from './data/dummyTaxes'; export default function TaxesReport() { const { period } = useDashboardPeriod(); const store = getStoreCurrency(); // Fetch real data or use dummy data based on toggle const { data, isLoading, error, refetch } = useTaxesAnalytics(DUMMY_TAXES_DATA); const chartData = useMemo(() => { return period === 'all' ? data.chart_data : data.chart_data.slice(-parseInt(period)); }, [data.chart_data, period]); // Calculate period metrics const periodMetrics = useMemo(() => { if (period === 'all') { const totalTax = data.chart_data.reduce((sum: number, d: any) => sum + d.tax, 0); const totalOrders = data.chart_data.reduce((sum: number, d: any) => sum + d.orders, 0); return { total_tax: totalTax, avg_tax_per_order: totalOrders > 0 ? totalTax / totalOrders : 0, orders_with_tax: totalOrders, change_percent: undefined, }; } const periodData = data.chart_data.slice(-parseInt(period)); const previousData = data.chart_data.slice(-parseInt(period) * 2, -parseInt(period)); const totalTax = periodData.reduce((sum: number, d: any) => sum + d.tax, 0); const totalOrders = periodData.reduce((sum: number, d: any) => sum + d.orders, 0); const prevTotalTax = previousData.reduce((sum: number, d: any) => sum + d.tax, 0); const prevTotalOrders = previousData.reduce((sum: number, d: any) => sum + d.orders, 0); const avgTaxPerOrder = totalOrders > 0 ? totalTax / totalOrders : 0; const prevAvgTaxPerOrder = prevTotalOrders > 0 ? prevTotalTax / prevTotalOrders : 0; return { total_tax: totalTax, avg_tax_per_order: avgTaxPerOrder, orders_with_tax: totalOrders, change_percent: prevTotalTax > 0 ? ((totalTax - prevTotalTax) / prevTotalTax) * 100 : 0, avg_tax_per_order_change: prevAvgTaxPerOrder > 0 ? ((avgTaxPerOrder - prevAvgTaxPerOrder) / prevAvgTaxPerOrder) * 100 : 0, orders_with_tax_change: prevTotalOrders > 0 ? ((totalOrders - prevTotalOrders) / prevTotalOrders) * 100 : 0, }; }, [data.chart_data, period]); // Filter table data by period const filteredByRate = useMemo(() => { const factor = period === 'all' ? 1 : parseInt(period) / 30; return data.by_rate.map((r: any) => ({ ...r, orders: Math.round(r.orders * factor), tax_amount: Math.round(r.tax_amount * factor), })); }, [data.by_rate, period]); const filteredByLocation = useMemo(() => { const factor = period === 'all' ? 1 : parseInt(period) / 30; return data.by_location.map((l: any) => ({ ...l, orders: Math.round(l.orders * factor), tax_amount: Math.round(l.tax_amount * factor), })); }, [data.by_location, period]); // Show loading state if (isLoading) { return (

{__('Loading analytics...')}

); } // Show error state if (error) { return ( refetch()} /> ); } const formatCurrency = (value: number) => { return formatMoney(value, { currency: store.currency, symbol: store.symbol, thousandSep: store.thousand_sep, decimalSep: store.decimal_sep, decimals: 0, preferSymbol: true, }); }; const rateColumns: Column[] = [ { key: 'rate', label: __('Tax Rate'), sortable: true }, { key: 'percentage', label: __('Rate %'), sortable: true, align: 'right', render: (value) => `${value.toFixed(1)}%`, }, { key: 'orders', label: __('Orders'), sortable: true, align: 'right', }, { key: 'tax_amount', label: __('Tax Collected'), sortable: true, align: 'right', render: (value) => formatCurrency(value), }, ]; const locationColumns: Column[] = [ { key: 'state_name', label: __('Location'), sortable: true }, { key: 'orders', label: __('Orders'), sortable: true, align: 'right', }, { key: 'tax_amount', label: __('Tax Collected'), sortable: true, align: 'right', render: (value) => formatCurrency(value), }, { key: 'percentage', label: __('% of Total'), sortable: true, align: 'right', render: (value) => `${value.toFixed(1)}%`, }, ]; return (

{__('Taxes Report')}

{__('Tax collection and breakdowns')}

{ const date = new Date(value); return `${date.getMonth() + 1}/${date.getDate()}`; }} /> { if (!active || !payload || !payload.length) return null; return (

{new Date(payload[0].payload.date).toLocaleDateString()}

{__('Tax')}: {formatCurrency(payload[0].payload.tax)}
{__('Orders')}: {payload[0].payload.orders}
); }} />
); }