|
| 1 | +import { getPayoutsOwed } from '@/app/actions/platform/payouts' |
| 2 | +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' |
| 3 | +import { MarkPayoutPaidDialog } from '@/components/platform/mark-payout-paid-dialog' |
| 4 | +import { |
| 5 | + IconCoin, |
| 6 | + IconReportMoney, |
| 7 | + IconWalletOff, |
| 8 | +} from '@tabler/icons-react' |
| 9 | + |
| 10 | +const usd = (n: number) => |
| 11 | + new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(n ?? 0) |
| 12 | + |
| 13 | +const PROVIDER_LABEL: Record<string, string> = { |
| 14 | + paypal: 'PayPal', |
| 15 | + binance: 'Binance Pay', |
| 16 | + lemonsqueezy: 'Lemon Squeezy', |
| 17 | +} |
| 18 | + |
| 19 | +export default async function PlatformPayoutsPage() { |
| 20 | + const owed = await getPayoutsOwed() |
| 21 | + |
| 22 | + const totalOwed = owed.reduce((sum, t) => sum + t.netOwed, 0) |
| 23 | + const totalCollected = owed.reduce((sum, t) => sum + t.grossCollected, 0) |
| 24 | + const totalPaidOut = owed.reduce((sum, t) => sum + t.alreadyPaid, 0) |
| 25 | + const schoolsOwed = owed.filter((t) => t.netOwed > 0).length |
| 26 | + |
| 27 | + const metricCards = [ |
| 28 | + { |
| 29 | + title: 'Currently Owed', |
| 30 | + value: usd(totalOwed), |
| 31 | + sub: `${schoolsOwed} school${schoolsOwed === 1 ? '' : 's'} awaiting payout`, |
| 32 | + icon: IconWalletOff, |
| 33 | + bg: 'bg-amber-50 dark:bg-amber-950/40', |
| 34 | + iconColor: 'text-amber-600 dark:text-amber-400', |
| 35 | + }, |
| 36 | + { |
| 37 | + title: 'Collected (single-account providers)', |
| 38 | + value: usd(totalCollected), |
| 39 | + sub: 'PayPal, Binance Pay, Lemon Squeezy — 100% lands in your account', |
| 40 | + icon: IconCoin, |
| 41 | + bg: 'bg-blue-50 dark:bg-blue-950/40', |
| 42 | + iconColor: 'text-blue-600 dark:text-blue-400', |
| 43 | + }, |
| 44 | + { |
| 45 | + title: 'Paid Out (all time)', |
| 46 | + value: usd(totalPaidOut), |
| 47 | + sub: 'Manually recorded payouts to schools', |
| 48 | + icon: IconReportMoney, |
| 49 | + bg: 'bg-emerald-50 dark:bg-emerald-950/40', |
| 50 | + iconColor: 'text-emerald-600 dark:text-emerald-400', |
| 51 | + }, |
| 52 | + ] |
| 53 | + |
| 54 | + return ( |
| 55 | + <main className="flex-1 px-4 py-6 sm:px-6 lg:px-8" data-testid="platform-payouts"> |
| 56 | + <div className="mb-8"> |
| 57 | + <h1 className="text-2xl font-bold tracking-tight">Payouts</h1> |
| 58 | + <p className="text-sm text-muted-foreground mt-1"> |
| 59 | + PayPal, Binance Pay, and Lemon Squeezy don't split automatically — 100% of every sale |
| 60 | + lands in your account. This is what you owe each school back, based on their revenue split. |
| 61 | + </p> |
| 62 | + </div> |
| 63 | + |
| 64 | + <div className="mb-8 grid gap-3 sm:grid-cols-3" data-testid="payouts-metrics"> |
| 65 | + {metricCards.map((card) => ( |
| 66 | + <Card key={card.title} className="relative overflow-hidden"> |
| 67 | + <CardContent className="p-5"> |
| 68 | + <div className="flex items-start justify-between"> |
| 69 | + <div className="min-w-0 flex-1"> |
| 70 | + <p className="text-[11px] font-medium uppercase tracking-wider text-muted-foreground"> |
| 71 | + {card.title} |
| 72 | + </p> |
| 73 | + <p className="mt-2 text-2xl font-bold tracking-tight tabular-nums" data-testid="metric-value"> |
| 74 | + {card.value} |
| 75 | + </p> |
| 76 | + <p className="mt-1 text-[11px] text-muted-foreground/70">{card.sub}</p> |
| 77 | + </div> |
| 78 | + <div className={`flex h-9 w-9 shrink-0 items-center justify-center rounded-lg ${card.bg}`}> |
| 79 | + <card.icon className={`h-[18px] w-[18px] ${card.iconColor}`} strokeWidth={1.75} /> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </CardContent> |
| 83 | + </Card> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + |
| 87 | + <Card data-testid="payouts-by-tenant"> |
| 88 | + <CardHeader> |
| 89 | + <CardTitle>By school</CardTitle> |
| 90 | + </CardHeader> |
| 91 | + <CardContent> |
| 92 | + {owed.length === 0 ? ( |
| 93 | + <p className="text-muted-foreground text-sm">No schools yet.</p> |
| 94 | + ) : ( |
| 95 | + <div className="overflow-x-auto"> |
| 96 | + <table className="w-full text-sm"> |
| 97 | + <thead> |
| 98 | + <tr className="border-b text-left text-[11px] uppercase tracking-wider text-muted-foreground"> |
| 99 | + <th className="pb-2 font-medium">School</th> |
| 100 | + <th className="pb-2 font-medium">Providers</th> |
| 101 | + <th className="pb-2 text-right font-medium">Collected</th> |
| 102 | + <th className="pb-2 text-right font-medium">School %</th> |
| 103 | + <th className="pb-2 text-right font-medium">Paid so far</th> |
| 104 | + <th className="pb-2 text-right font-medium">Owed</th> |
| 105 | + <th className="pb-2 text-right font-medium"></th> |
| 106 | + </tr> |
| 107 | + </thead> |
| 108 | + <tbody> |
| 109 | + {owed.map((t) => ( |
| 110 | + <tr key={t.tenantId} className="border-b last:border-0"> |
| 111 | + <td className="py-2.5 font-medium">{t.tenantName}</td> |
| 112 | + <td className="py-2.5 text-muted-foreground"> |
| 113 | + {Object.keys(t.byProvider).length === 0 |
| 114 | + ? '—' |
| 115 | + : Object.keys(t.byProvider).map((p) => PROVIDER_LABEL[p] ?? p).join(', ')} |
| 116 | + </td> |
| 117 | + <td className="py-2.5 text-right tabular-nums">{usd(t.grossCollected)}</td> |
| 118 | + <td className="py-2.5 text-right tabular-nums text-muted-foreground"> |
| 119 | + {t.schoolPercentage}% |
| 120 | + </td> |
| 121 | + <td className="py-2.5 text-right tabular-nums text-muted-foreground"> |
| 122 | + {usd(t.alreadyPaid)} |
| 123 | + </td> |
| 124 | + <td className="py-2.5 text-right tabular-nums font-medium text-amber-600 dark:text-amber-400"> |
| 125 | + {usd(t.netOwed)} |
| 126 | + </td> |
| 127 | + <td className="py-2.5 text-right"> |
| 128 | + <MarkPayoutPaidDialog |
| 129 | + tenantId={t.tenantId} |
| 130 | + tenantName={t.tenantName} |
| 131 | + netOwed={t.netOwed} |
| 132 | + /> |
| 133 | + </td> |
| 134 | + </tr> |
| 135 | + ))} |
| 136 | + </tbody> |
| 137 | + </table> |
| 138 | + </div> |
| 139 | + )} |
| 140 | + </CardContent> |
| 141 | + </Card> |
| 142 | + |
| 143 | + <p className="mt-6 text-[11px] text-muted-foreground/70"> |
| 144 | + Stripe and Solana sales already split automatically and never appear here. Binance Pay |
| 145 | + (personal account) and manual/offline sales settle straight to the school and also never |
| 146 | + appear here — only PayPal, Binance Pay (merchant), and Lemon Squeezy do, since those settle |
| 147 | + 100% into your account today. |
| 148 | + </p> |
| 149 | + </main> |
| 150 | + ) |
| 151 | +} |
0 commit comments