diff --git a/src/components/MemberMoneyDetails.tsx b/src/components/MemberMoneyDetails.tsx new file mode 100644 index 0000000..4a84fba --- /dev/null +++ b/src/components/MemberMoneyDetails.tsx @@ -0,0 +1,71 @@ +import { useState } from 'react' +import { useAppData } from '../state/AppDataContext' +import { allocateBillShares } from '../lib/shares' +import { dateKeyInTimeZone, formatMoney } from '../lib/utils' +import { Avatar, Badge, Modal } from './ui' +import type { Member } from '../types' + +export function MemberMoneyDetails({ member, netCents, onClose }: { member: Member; netCents: number; onClose: () => void }) { + const { data } = useAppData() + const [filter, setFilter] = useState('all') + const month = dateKeyInTimeZone(new Date(), data.household.timezone).slice(0, 7) + const name = (id: string) => data.members.find(person => person.id === id)?.displayName ?? 'Household member' + const rows: { id: string; kind: string; title: string; date: string; detail: string; amount: number; status?: string }[] = [] + for (const expense of data.expenses) { + const paid = expense.payers.filter(share => share.memberId === member.id).reduce((sum, share) => sum + share.amountCents, 0) + const used = expense.beneficiaries.filter(share => share.memberId === member.id).reduce((sum, share) => sum + share.amountCents, 0) + if (!paid && !used) continue + rows.push({ id: `expense:${expense.id}`, kind: 'Purchases', title: expense.title, date: expense.purchasedAt, + detail: `Paid ${formatMoney(paid)} · Share ${formatMoney(used)}`, amount: expense.reversed ? 0 : paid - used, status: expense.reversed ? 'Deleted' : undefined }) + } + for (const period of data.billPeriods) { + const bill = data.bills.find(item => item.id === period.billId) + if (!bill) continue + const payee = period.payeeMemberId ?? bill.payeeMemberId + const shares = period.shares?.length ? period.shares : allocateBillShares(period.amountCents ?? 0, bill) + const ownShare = shares.find(share => share.memberId === member.id)?.amountCents ?? 0 + if (member.id !== payee && !shares.some(share => share.memberId === member.id)) continue + const future = period.periodMonth.slice(0, 7) > month + const pending = period.amountCents == null + const credit = shares.filter(share => share.memberId !== payee).reduce((sum, share) => sum + share.amountCents, 0) + rows.push({ id: `bill:${period.id}`, kind: 'Bills', title: bill.name, date: period.periodMonth, + detail: member.id === payee ? `Paid ${formatMoney(period.amountCents ?? 0)} · Own share ${formatMoney(ownShare)}` : `Share ${formatMoney(ownShare)} · Owed to ${name(payee)}`, + amount: pending || future ? 0 : member.id === payee ? credit : -ownShare, status: pending ? 'Price pending' : future ? 'Upcoming' : undefined }) + } + for (const payment of data.settlements) { + if (payment.fromMemberId !== member.id && payment.toMemberId !== member.id) continue + const sent = payment.fromMemberId === member.id + rows.push({ id: `payment:${payment.id}`, kind: 'Payments', title: `${sent ? 'Sent to' : 'Received from'} ${name(sent ? payment.toMemberId : payment.fromMemberId)}`, date: payment.createdAt, + detail: `${formatMoney(payment.amountCents)}${payment.note ? ` · ${payment.note}` : ''}`, amount: payment.status === 'confirmed' ? (sent ? payment.amountCents : -payment.amountCents) : 0, status: payment.status }) + } + for (const infraction of data.infractions.filter(item => item.memberId === member.id)) { + rows.push({ id: `penalty:${infraction.id}`, kind: 'Penalties', title: infraction.taskTitle, + date: infraction.resolvedAt ?? infraction.disputeDeadline, + detail: `Penalty ${formatMoney(infraction.amountCents)}`, status: infraction.status, + amount: infraction.status === 'upheld' || infraction.status === 'paid' ? -infraction.amountCents : 0 }) + } + for (const payment of data.fundPayments.filter(item => item.memberId === member.id)) { + rows.push({ id: `fund:${payment.id}`, kind: 'Payments', title: 'Paid into penalty account', date: payment.createdAt, + detail: formatMoney(payment.amountCents), status: payment.status, amount: payment.status === 'confirmed' ? payment.amountCents : 0 }) + } + const remainder = netCents - rows.reduce((sum, row) => sum + row.amount, 0) + const visible = rows.filter(row => filter === 'all' || row.kind === filter).sort((a, b) => b.date.localeCompare(a.date)) + return +
+ +
{netCents > 0 ? 'Is owed' : netCents < 0 ? 'Owes' : 'Settled'}{formatMoney(Math.abs(netCents))}
+
+
+ {['all', 'Purchases', 'Bills', 'Payments', 'Penalties'].map(option => )} +
+
+ {visible.map(row =>
+
{row.kind} · {row.date.slice(0, 10)}{row.title}

{row.detail}

{row.status &&
{row.status}
}
+ {formatMoney(row.amount, true)} +
)} + {!visible.length &&

No {filter === 'all' ? 'activity' : filter.toLowerCase()} yet.

} +
+ {remainder !== 0 &&
Other ledger adjustments
Balance entries outside the activity shown above
{formatMoney(remainder, true)}
} +

Pending and rejected payments do not change the balance. Upcoming bills count when their month begins.

+
+} diff --git a/src/components/MoneyDashboard.tsx b/src/components/MoneyDashboard.tsx index 3d38409..0736160 100644 --- a/src/components/MoneyDashboard.tsx +++ b/src/components/MoneyDashboard.tsx @@ -1,4 +1,5 @@ import { PurchaseDetails } from './PurchaseDetails' +import { MemberMoneyDetails } from './MemberMoneyDetails' import { ShareBar, SharePeek } from './ShareBreakdown' import { billBalanceAdjustments, billBreakdown, expenseBreakdown } from '../lib/shares' import { useMemo, useState } from 'react' @@ -163,6 +164,7 @@ export function MoneyDashboard({ openReceipt, } = useAppData() const [detailId, setDetailId] = useState(null) + const [memberDetailId, setMemberDetailId] = useState(null) const [peekId, setPeekId] = useState(null) const selectedExpense = data.expenses.find(e => e.id === detailId) const [activityFilter, setActivityFilter] = useState<'all' | 'expense' | 'settlement'>('all') @@ -171,6 +173,7 @@ export function MoneyDashboard({ const [expandedBillId, setExpandedBillId] = useState(null) const currentMemberId = data.household.currentMemberId const currentMember = data.members.find((member) => member.id === currentMemberId)! + const detailMember = data.members.find(member => member.id === memberDetailId) const currentBalance = data.balances.find((balance) => balance.memberId === currentMemberId) const demoBillBalances = demoMode ? billBalanceAdjustments(data.bills, data.billPeriods, data.household.timezone) : new Map() const balanceFor = (memberId: string, netCents: number) => netCents + (demoBillBalances.get(memberId) ?? 0) @@ -201,6 +204,7 @@ export function MoneyDashboard({ return (
{selectedExpense && setDetailId(null)} />} + {detailMember && balance.memberId === detailMember.id)?.netCents ?? 0)} onClose={() => setMemberDetailId(null)} />}

Shared money

@@ -232,7 +236,7 @@ export function MoneyDashboard({ {data.balances.map((balance) => { const member = data.members.find((item) => item.id === balance.memberId)! return ( -
+
+ ) })}
diff --git a/tests/e2e/member-money.spec.ts b/tests/e2e/member-money.spec.ts new file mode 100644 index 0000000..bcff08a --- /dev/null +++ b/tests/e2e/member-money.spec.ts @@ -0,0 +1,19 @@ +import { expect, test } from '@playwright/test' + +test('opens own and roommate balance histories with payment filters', async ({ page }) => { + await page.goto('/money') + await page.getByRole('button', { name: 'View balance for Brandon' }).click() + const dialog = page.getByRole('dialog', { name: "Brandon's balance" }) + await expect(dialog).toBeVisible() + await expect(dialog.getByText('Monthly rent', { exact: true }).first()).toBeVisible() + await dialog.getByRole('button', { name: 'Payments', exact: true }).click() + await expect(dialog.getByText('Received from Noah')).toBeVisible() + await expect(dialog.getByText('pending', { exact: true })).toBeVisible() + await expect(dialog.getByText('Monthly rent', { exact: true })).toHaveCount(0) + await dialog.getByRole('button', { name: 'Close' }).click() + await page.getByRole('button', { name: 'View balance for Noah' }).click() + const other = page.getByRole('dialog', { name: "Noah's balance" }) + await other.getByRole('button', { name: 'Payments', exact: true }).click() + await expect(other.getByText('Sent to Brandon')).toBeVisible() + await expect(other.getByText('pending', { exact: true })).toBeVisible() +})