| 1 | import { |
| 2 | AlertTriangleIcon, |
| 3 | Hourglass, |
| 4 | Receipt, |
| 5 | TrendingUp, |
| 6 | Wallet, |
| 7 | } from 'lucide-react' |
| 8 | import Link from 'next/link |
| 9 | |
| 10 | import { Card, CardContent |
| 11 | import type { RouteImpl |
| 12 | import { FinancialStat } |
| 13 | import { formatCurrency, sumByCurrency |
| 14 | |
| 15 | interface Invoice { |
| 16 | currency: string |
| 17 | dueDate: Date |
| 18 | status: string |
| 19 | totalAmount: string |
| 20 | } |
| 21 | |
| 22 | interface Expense { |
| 23 | amountCents: number |
| 24 | billable: boolean |
| 25 | currency: string |
| 26 | status: string |
| 27 | } |
| 28 | |
| 29 | interface FinancialOverviewCardProps { |
| 30 | expenses: Expense |
| 31 | invoices: Invoice |
| 32 | isAdmin?: boolean |
| 33 | isClient?: boolean |
| 34 | scopeLabel?: string |
| 35 | viewAllHref?: string |
| 36 | } |
| 37 | |
| 38 | export function FinancialOverviewCard({ |
| 39 | invoices, |
| 40 | expenses, |
| 41 | isClient, |
| 42 | isAdmin, |
| 43 | scopeLabel, |
| 44 | viewAllHref, |
| 45 | }: FinancialOverviewCardProps) |
| 46 | const paid = |
| 47 | const sent = |
| 48 | const overdue = |
| 49 | const draft = |
| 50 | |
| 51 | const paidTotals = |
| 52 | const outstandingTotals = |
| 53 | const overdueTotals = |
| 54 | const draftTotals = |
| 55 | |
| 56 | const billableApproved = |
| 57 | (e) => |
| 58 | ) |
| 59 | const billableTotals = |
| 60 | billableApproved, |
| 61 | (e) => |
| 62 | ) |
| 63 | |
| 64 | return ( |
| 65 | <Card> |
| 66 | <CardHeader className=' |
| 67 | <CardTitle className=' |
| 68 | <Wallet className=' |
| 69 | Financial Overview |
| 70 | </CardTitle> |
| 71 | {viewAllHref ? ( |
| 72 | <Link |
| 73 | className='text-muted-foreground text-xs hover:text-foreground' |
| 74 | href={viewAllHref as |
| 75 | > |
| 76 | View invoices |
| 77 | </Link> |
| 78 | ) : ( |
| 79 | scopeLabel && ( |
| 80 | <span className='text-muted-foreground text-xs |
| 81 | ) |
| 82 | )} |
| 83 | </CardHeader> |
| 84 | <CardContent> |
| 85 | <div className='grid grid-cols-2 gap-4 sm:grid-cols-4 |
| 86 | <FinancialStat |
| 87 | count={paid.length |
| 88 | icon={TrendingUp} |
| 89 | label={isClient ? |
| 90 | tone='text-emerald-600 dark:text-emerald-400' |
| 91 | totals={paidTotals} |
| 92 | unit='invoice' |
| 93 | /> |
| 94 | <FinancialStat |
| 95 | count={sent.length |
| 96 | icon={Hourglass} |
| 97 | label='Outstanding' |
| 98 | totals={outstandingTotals} |
| 99 | unit='invoice' |
| 100 | /> |
| 101 | <FinancialStat |
| 102 | count={overdue.length |
| 103 | icon={AlertTriangleIcon} |
| 104 | label='Overdue' |
| 105 | tone={overdue.length |
| 106 | totals={overdueTotals} |
| 107 | unit='invoice' |
| 108 | /> |
| 109 | <FinancialStat |
| 110 | count={billableApproved.length |
| 111 | icon={Receipt} |
| 112 | label='Billable expenses' |
| 113 | totals={billableTotals} |
| 114 | unit='expense' |
| 115 | /> |
| 116 | </div> |
| 117 | {isAdmin && draft |
| 118 | <p className='mt-4 text-muted-foreground text-xs |
| 119 | {draft.length}{' |
| 120 | {draft.length === |
| 121 | sent |
| 122 | {Object.keys(draftTotals |
| 123 | {Object.entries(draftTotals |
| 124 | .map(([c |
| 125 | .join(', ') |
| 126 | </p> |
| 127 | )} |
| 128 | </CardContent> |
| 129 | </Card> |
| 130 | ) |
| 131 | } |