"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { getTaxSummaryAction } from "@/actions/tax"; type TaxSummaryData = { total_tax_collected: number; total_gross_sales: number; order_count: number; }; function quarterLabel(): string { const now = new Date(); const q = Math.floor(now.getMonth() / 3) + 1; return `Q${q} ${now.getFullYear()}`; } function quarterDateRange(): { start: string; end: string } { const now = new Date(); const q = Math.floor(now.getMonth() / 3); const startDate = new Date(now.getFullYear(), q * 3, 1); const end = now.toISOString().slice(0, 10); return { start: startDate.toISOString().slice(0, 10), end }; } export default function TaxQuarterlySummary({ brandId }: { brandId: string }) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { const range = quarterDateRange(); let cancelled = false; (async () => { const result = await getTaxSummaryAction({ brandId, startDate: range.start, endDate: range.end, }); if (cancelled) return; if (result.success) { setData({ total_tax_collected: result.data.total_tax_collected, total_gross_sales: result.data.total_gross_sales, order_count: result.data.order_count, }); } setLoading(false); })(); return () => { cancelled = true; }; }, [brandId]); if (loading) { return (
Loading tax summary...
); } if (!data || data.order_count === 0) return null; const effectiveRate = data.total_gross_sales > 0 ? (data.total_tax_collected / data.total_gross_sales) * 100 : 0; return (
{quarterLabel()} Tax Collected
View Details →
${data.total_tax_collected.toLocaleString(undefined, { minimumFractionDigits: 2 })} on ${data.total_gross_sales.toLocaleString(undefined, { minimumFractionDigits: 2 })} gross sales · {effectiveRate.toFixed(3)}% rate · {data.order_count} orders
); }