Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clear-statement-balances.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'leroa': patch
---

Show a reconciling full-ledger balance separately from the current due-date balance on rent statements, and label contractual deposits without implying they have been received.
57 changes: 57 additions & 0 deletions src/lib/statement-totals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import { statementTotals } from './statement-totals.ts';

describe('rent statement ledger totals', () => {
it.each([
{ charges: [], payments: [], charged: 0, paid: 0, ledgerBalance: 0 },
{
charges: [{ amount: 100000, dueDate: '2999-01-01' }],
payments: [],
charged: 100000,
paid: 0,
ledgerBalance: 100000
},
{
charges: [
{ amount: 100000, dueDate: '2000-01-01' },
{ amount: 50000, dueDate: '2999-01-01' }
],
payments: [{ amount: 25000 }],
charged: 150000,
paid: 25000,
ledgerBalance: 125000
},
{
charges: [{ amount: 100000 }],
payments: [{ amount: 100000 }],
charged: 100000,
paid: 100000,
ledgerBalance: 0
},
{
charges: [{ amount: 100000 }],
payments: [{ amount: 125000 }],
charged: 100000,
paid: 125000,
ledgerBalance: -25000
}
])(
'reconciles every listed charge and payment: %j',
({ charges, payments, charged, paid, ledgerBalance }) => {
expect(statementTotals(charges, payments)).toEqual({ charged, paid, ledgerBalance });
}
);
it('keeps the current server balance distinct and never claims the contractual deposit was received', () => {
const source = readFileSync(
new URL('../routes/leases/[id]/statement/+page.svelte', import.meta.url),
'utf8'
);
expect(source).toContain('formatMoney(ledgerBalance, l.currency)');
expect(source).toContain('formatMoney(l.balance, l.currency)');
expect(source).toContain('Ledger balance includes future charges.');
expect(source).toContain('Contractual deposit:');
expect(source.match(/<dt class="text-neutral-500">Current balance<\/dt>/g)).toHaveLength(1);
expect(source).not.toContain('Deposit held:');
});
});
9 changes: 9 additions & 0 deletions src/lib/statement-totals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** Full-ledger totals, including future charges. Does not replace the server's due-date balance. */
export function statementTotals(
charges: readonly { amount: number }[],
payments: readonly { amount: number }[]
) {
const charged = charges.reduce((sum, charge) => sum + charge.amount, 0);
const paid = payments.reduce((sum, payment) => sum + payment.amount, 0);
return { charged, paid, ledgerBalance: charged - paid };
}
21 changes: 16 additions & 5 deletions src/routes/leases/[id]/statement/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { statementTotals } from '#lib/statement-totals.ts';
import { page } from '$app/state';
import { Button } from '#lib/components/ui/button/index.js';
import Printer from '@lucide/svelte/icons/printer';
Expand All @@ -12,8 +13,7 @@

const charges = [...l.charges].sort((a, b) => a.dueDate.localeCompare(b.dueDate));
const payments = [...l.payments].sort((a, b) => a.receivedOn.localeCompare(b.receivedOn));
const charged = charges.reduce((s, c) => s + c.amount, 0);
const paid = payments.reduce((s, p) => s + p.amount, 0);
const { charged, paid, ledgerBalance } = statementTotals(charges, payments);

const billTo = l.tenants.map((t) => tenantName(t));
const term = `${formatDate(l.startDate)} – ${l.endDate ? formatDate(l.endDate) : 'Month-to-month'}`;
Expand Down Expand Up @@ -128,9 +128,20 @@
<dd class="text-right tabular-nums">-{formatMoney(paid, l.currency)}</dd>
</div>
<div class="grid grid-cols-2 gap-x-4 border-t border-neutral-200 pt-2 text-lg">
<dt>Balance {l.currency.toUpperCase()}</dt>
<dt>Ledger balance {l.currency.toUpperCase()}</dt>
<dd class="text-right font-semibold tabular-nums">
{formatMoney(l.balance, l.currency)}
{formatMoney(ledgerBalance, l.currency)}
</dd>
</div>
<div class="grid grid-cols-2 gap-x-4 pt-2">
<dt class="text-neutral-500">Current balance</dt>
<dd class="text-right tabular-nums">{formatMoney(l.balance, l.currency)}</dd>
</div>
<div>
<dt class="sr-only">Balance calculation</dt>
<dd class="text-xs text-neutral-500">
Ledger balance includes future charges. Current balance includes charges due through
today, less all recorded payments. Negative balances are credits.
</dd>
</div>
</dl>
Expand All @@ -139,7 +150,7 @@
<footer class="mt-14 text-sm">
{#if l.depositAmount > 0}
<p class="text-xs text-neutral-500">
Deposit held: {formatMoney(l.depositAmount, l.currency)}
Contractual deposit: {formatMoney(l.depositAmount, l.currency)}
</p>
{/if}
{#if org.logoUrl}
Expand Down
Loading