/* Relay — Reports screen */ function ReportsScreen({ allOrders, now }) { const { PLATFORMS } = window.RelayData; const [range, setRange] = React.useState("today"); const [typeFilter, setTypeFilter] = React.useState("all"); const [sort, setSort] = React.useState({ col: "revenue", dir: "desc" }); const ranges = [ { id: "today", label: "Bugün" }, { id: "yesterday", label: "Dün" }, { id: "7d", label: "Son 7 gün" }, { id: "30d", label: "Son 30 gün" }, ]; const DAY = 86400000; const startOfToday = new Date(); startOfToday.setHours(0, 0, 0, 0); const t0 = startOfToday.getTime(); function inRange(ts) { if (range === "today") return ts >= t0; if (range === "yesterday") return ts >= t0 - DAY && ts < t0; if (range === "7d") return ts >= t0 - 6 * DAY; if (range === "30d") return ts >= t0 - 29 * DAY; return true; } const scoped = React.useMemo(() => allOrders.filter((o) => inRange(o.placedAt) && (typeFilter === "all" || o.type === typeFilter) ), [allOrders, range, typeFilter]); const sold = scoped.filter((o) => o.status !== "cancelled"); const cancelled = scoped.filter((o) => o.status === "cancelled"); const revenue = sold.reduce((s, o) => s + o.total, 0); const aov = sold.length ? revenue / sold.length : 0; const items = sold.reduce((s, o) => s + o.items.reduce((a, it) => a + it.qty, 0), 0); // per-platform breakdown const byPlatform = React.useMemo(() => { const m = {}; sold.forEach((o) => { const p = m[o.platformId] || (m[o.platformId] = { id: o.platformId, orders: 0, revenue: 0, items: 0 }); p.orders++; p.revenue += o.total; p.items += o.items.reduce((a, it) => a + it.qty, 0); }); let rows = Object.values(m).map((r) => ({ ...r, aov: r.orders ? r.revenue / r.orders : 0, ...PLATFORMS[r.id] })); rows.sort((a, b) => { const d = sort.dir === "asc" ? 1 : -1; return (a[sort.col] > b[sort.col] ? 1 : a[sort.col] < b[sort.col] ? -1 : 0) * d; }); return rows; }, [sold, sort]); // trend chart: revenue by day (7d/30d) or by hour bucket (today/yesterday) const chart = React.useMemo(() => { if (range === "today" || range === "yesterday") { const base = range === "today" ? t0 : t0 - DAY; const buckets = [["09",9],["12",12],["15",15],["18",18],["21",21]].map(([lbl,h]) => ({ label: lbl, value: 0 })); sold.forEach((o) => { const h = new Date(o.placedAt).getHours(); let idx = h < 11 ? 0 : h < 14 ? 1 : h < 17 ? 2 : h < 20 ? 3 : 4; buckets[idx].value += o.total; }); return buckets.map((b) => ({ ...b, top: b.value ? Math.round(b.value) : "" })); } const days = range === "7d" ? 7 : 30; const step = range === "7d" ? 1 : 5; const out = []; for (let d = days - 1; d >= 0; d -= step) { const dayStart = t0 - d * DAY; const dayEnd = dayStart + step * DAY; const v = sold.filter((o) => o.placedAt >= dayStart && o.placedAt < dayEnd).reduce((s, o) => s + o.total, 0); const dt = new Date(dayStart); out.push({ label: dt.toLocaleDateString("tr-TR", { day: "2-digit", month: "short" }), value: v, top: v ? Math.round(v / 1000) + "k" : "" }); } return out; }, [sold, range]); const shareRows = byPlatform.map((r) => ({ id: r.id, label: r.name, color: r.color, value: r.revenue })); function exportToast(fmt) { const rangeLabels = { today: "Bugünkü", yesterday: "Dünkü", "7d": "Son 7 günlük", "30d": "Son 30 günlük" }; const label = rangeLabels[range] || range; window.__relayToast && window.__relayToast(`${label} rapor ${fmt} formatında dışa aktarılıyor…`); } const th = { fontSize: 10.5, fontWeight: 600, color: "var(--text-3)", textTransform: "uppercase", letterSpacing: ".05em", padding: "0 0 0 0" }; return ( } subtitle="Bağlı tüm kanallardaki satış performansı."> exportToast("Excel")}>Excel'e Aktar exportToast("PDF")}>PDF'e Aktar {/* filters */}
seçili aralıkta {scoped.length} sipariş
{/* KPI cards */}
{/* charts row */}
{shareRows.length ? : }
{/* platform table (sortable) */}
{byPlatform.length === 0 ? : byPlatform.map((r) => (
{r.orders} {r.items} {r.aov.toFixed(0)} {Math.round(r.revenue).toLocaleString()}
))} {byPlatform.length > 0 && (
Toplam {sold.length} {items} {aov.toFixed(0)} {Math.round(revenue).toLocaleString()}
)}
); } function BigStat({ label, value, sub, accent }) { return (
{label}
{value}
{sub}
); } function Panel({ title, note, children }) { return (

{title}

{note && {note}}
{children}
); } function Empty() { return
Bu aralıkta veri bulunmuyor.
; } Object.assign(window, { ReportsScreen, Panel, BigStat });