Inspect Ledgers: group view, tree dropdown, prior/closing balance rows
Backend: inspect/ledger endpoint now supports group queries: - Selecting a parent account returns merged transactions from all descendants, sorted chronologically - ?ownOnly=true filters to just the selected account's own transactions - Response includes isGroup, groupAccountCount, accountName per entry Frontend improvements: - Account dropdown shows tree indentation matching account hierarchy - Prior balance row at top of ledger, closing balance at bottom - Period totals row shows only debit/credit sums (no balance) - Account column appears automatically for group views - Checkbox "Show only this account's own transactions (exclude sub-accounts)" appears when viewing a parent account with children - Currency label above table - Entries sorted by date within group view Made-with: Cursor
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useState, useEffect, useMemo } from 'react'
|
||||
import {
|
||||
Box, Typography, Paper, Table, TableBody, TableCell, TableContainer,
|
||||
TableHead, TableRow, Select, MenuItem, FormControl, InputLabel,
|
||||
Skeleton, Chip, Tooltip, TextField, Autocomplete,
|
||||
Skeleton, Chip, Tooltip, TextField, Autocomplete, FormControlLabel, Checkbox,
|
||||
} from '@mui/material'
|
||||
import { useOrg } from '../contexts/OrgContext'
|
||||
import { api } from '../api/client'
|
||||
@@ -14,14 +14,15 @@ interface AccountOption {
|
||||
|
||||
interface LedgerEntry {
|
||||
rowId: number; transactionId: number; sequenceId: number
|
||||
dateTime: string; description: string; accountName: string
|
||||
dateTime: string; description: string; accountId: number; accountName: string
|
||||
debitCents: number; creditCents: number; balanceCents: number
|
||||
foreignCurrencyCode: string; foreignCurrencyAmountCents: number
|
||||
dependencyType: number | null; dependencyForeignId: number | null
|
||||
}
|
||||
|
||||
interface LedgerResponse {
|
||||
accountId: number; year: number; month: number
|
||||
accountId: number; accountName: string; isGroup: boolean; groupAccountCount: number
|
||||
year: number; month: number
|
||||
from: string; to: string; priorBalanceCents: number
|
||||
entries: LedgerEntry[]; entryCount: number
|
||||
}
|
||||
@@ -43,10 +44,12 @@ export default function InspectLedgersPage() {
|
||||
const { activeOrg } = useOrg()
|
||||
const [accounts, setAccounts] = useState<AccountOption[]>([])
|
||||
const [selectedAccount, setSelectedAccount] = useState<AccountOption | null>(null)
|
||||
// Type adapter for autocomplete which uses AccountWithDepth
|
||||
const [year, setYear] = useState(currentYear)
|
||||
const [month, setMonth] = useState(0)
|
||||
const [data, setData] = useState<LedgerResponse | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [ownOnly, setOwnOnly] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg) return
|
||||
@@ -58,13 +61,49 @@ export default function InspectLedgersPage() {
|
||||
useEffect(() => {
|
||||
if (!activeOrg || !selectedAccount) return
|
||||
setLoading(true)
|
||||
const ownParam = ownOnly ? '&ownOnly=true' : ''
|
||||
api.get<LedgerResponse>(
|
||||
`/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccount.id}&year=${year}&month=${month}`
|
||||
`/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccount.id}&year=${year}&month=${month}${ownParam}`
|
||||
)
|
||||
.then(setData)
|
||||
.catch(() => setData(null))
|
||||
.finally(() => setLoading(false))
|
||||
}, [activeOrg, selectedAccount, year, month])
|
||||
}, [activeOrg, selectedAccount, year, month, ownOnly])
|
||||
|
||||
interface AccountWithDepth extends AccountOption {
|
||||
depth: number
|
||||
hasChildren: boolean
|
||||
}
|
||||
|
||||
const accountsWithDepth = useMemo(() => {
|
||||
const byId = new Map<number, AccountOption>()
|
||||
const childrenOf = new Map<number, AccountOption[]>()
|
||||
|
||||
for (const a of accounts) {
|
||||
byId.set(a.id, a)
|
||||
if (!childrenOf.has(a.parentId)) childrenOf.set(a.parentId, [])
|
||||
childrenOf.get(a.parentId)!.push(a)
|
||||
}
|
||||
|
||||
const result: AccountWithDepth[] = []
|
||||
|
||||
function walk(parentId: number, depth: number, accountType: string) {
|
||||
const children = (childrenOf.get(parentId) || [])
|
||||
.filter(a => a.accountType === accountType)
|
||||
.sort((a, b) => a.name.localeCompare(b.name))
|
||||
for (const child of children) {
|
||||
const hasChildren = (childrenOf.get(child.id) || []).length > 0
|
||||
result.push({ ...child, depth, hasChildren })
|
||||
walk(child.id, depth + 1, accountType)
|
||||
}
|
||||
}
|
||||
|
||||
for (const type of ['Asset', 'Debt', 'Income', 'Cost']) {
|
||||
walk(0, 0, type)
|
||||
}
|
||||
|
||||
return result
|
||||
}, [accounts])
|
||||
|
||||
if (!activeOrg) return null
|
||||
|
||||
@@ -74,17 +113,19 @@ export default function InspectLedgersPage() {
|
||||
|
||||
<Paper sx={{ p: 2, mb: 3, display: 'flex', gap: 2, alignItems: 'center', flexWrap: 'wrap' }} elevation={2}>
|
||||
<Autocomplete
|
||||
options={accounts}
|
||||
options={accountsWithDepth}
|
||||
groupBy={a => a.accountType}
|
||||
getOptionLabel={a => a.name}
|
||||
value={selectedAccount}
|
||||
value={selectedAccount ? accountsWithDepth.find(a => a.id === selectedAccount.id) ?? null : null}
|
||||
onChange={(_, v) => setSelectedAccount(v)}
|
||||
sx={{ minWidth: 350, flex: 1 }}
|
||||
renderInput={params => <TextField {...params} label="Account" size="small" />}
|
||||
renderOption={(props, option) => (
|
||||
<li {...props} key={option.id}>
|
||||
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, width: '100%' }}>
|
||||
<Typography variant="body2" sx={{ flex: 1 }}>{option.name}</Typography>
|
||||
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%', pl: option.depth * 2 }}>
|
||||
<Typography variant="body2" sx={{ flex: 1, fontSize: 13 }}>
|
||||
{option.name}
|
||||
</Typography>
|
||||
{!option.open && <Chip label="Closed" size="small" color="default" variant="outlined" sx={{ fontSize: 10 }} />}
|
||||
</Box>
|
||||
</li>
|
||||
@@ -114,29 +155,47 @@ export default function InspectLedgersPage() {
|
||||
<Skeleton height={400} />
|
||||
) : data && data.entries.length > 0 ? (
|
||||
<>
|
||||
<Box sx={{ mb: 1, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||
<Box sx={{ mb: 1, display: 'flex', justifyContent: 'space-between', alignItems: 'center', flexWrap: 'wrap', gap: 1 }}>
|
||||
<Typography variant="body2" color="text.secondary">
|
||||
{data.entryCount} entries, {data.from} to {data.to}
|
||||
{data.isGroup && !ownOnly && (
|
||||
<Typography component="span" sx={{ ml: 1, fontSize: 12, color: 'text.disabled' }}>
|
||||
({data.groupAccountCount} accounts)
|
||||
</Typography>
|
||||
)}
|
||||
</Typography>
|
||||
<Typography variant="body2" sx={{ fontFamily: 'monospace' }}>
|
||||
Prior balance: {formatCents(data.priorBalanceCents)}
|
||||
{data.isGroup && (
|
||||
<FormControlLabel
|
||||
control={<Checkbox size="small" checked={ownOnly} onChange={e => setOwnOnly(e.target.checked)} />}
|
||||
label={<Typography variant="body2" sx={{ fontSize: 12 }}>
|
||||
Show only "{data.accountName}" own transactions (exclude sub-accounts)
|
||||
</Typography>}
|
||||
/>
|
||||
)}
|
||||
<Typography variant="body2" color="text.secondary" sx={{ fontStyle: 'italic' }}>
|
||||
Amounts in {activeOrg.name}'s currency
|
||||
</Typography>
|
||||
</Box>
|
||||
|
||||
<TableContainer component={Paper} elevation={2}>
|
||||
{(() => {
|
||||
const showAccountCol = data.isGroup && !ownOnly
|
||||
return (
|
||||
<Table size="small" sx={{ tableLayout: 'fixed', '& td, & th': { py: 0.6, fontSize: 13 } }}>
|
||||
<colgroup>
|
||||
<col style={{ width: 60 }} />
|
||||
<col style={{ width: 140 }} />
|
||||
<col style={{ width: 130 }} />
|
||||
{showAccountCol && <col style={{ width: 180 }} />}
|
||||
<col />
|
||||
<col style={{ width: 100 }} />
|
||||
<col style={{ width: 100 }} />
|
||||
<col style={{ width: 110 }} />
|
||||
<col style={{ width: 110 }} />
|
||||
<col style={{ width: 120 }} />
|
||||
</colgroup>
|
||||
<TableHead>
|
||||
<TableRow sx={{ bgcolor: 'primary.main' }}>
|
||||
<TableCell sx={{ color: 'white', fontWeight: 600 }}>Tx#</TableCell>
|
||||
<TableCell sx={{ color: 'white', fontWeight: 600 }}>Date</TableCell>
|
||||
{showAccountCol && <TableCell sx={{ color: 'white', fontWeight: 600 }}>Account</TableCell>}
|
||||
<TableCell sx={{ color: 'white', fontWeight: 600 }}>Description</TableCell>
|
||||
<TableCell align="right" sx={{ color: 'white', fontWeight: 600 }}>Debit</TableCell>
|
||||
<TableCell align="right" sx={{ color: 'white', fontWeight: 600 }}>Credit</TableCell>
|
||||
@@ -144,6 +203,24 @@ export default function InspectLedgersPage() {
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{/* Prior balance row */}
|
||||
<TableRow sx={{ bgcolor: 'rgba(25, 118, 210, 0.04)' }}>
|
||||
<TableCell />
|
||||
<TableCell />
|
||||
{showAccountCol && <TableCell />}
|
||||
<TableCell sx={{ fontWeight: 600, fontStyle: 'italic', color: 'text.secondary' }}>
|
||||
Prior balance
|
||||
</TableCell>
|
||||
<TableCell />
|
||||
<TableCell />
|
||||
<TableCell align="right" sx={{
|
||||
fontFamily: '"Roboto Mono", monospace', fontWeight: 600,
|
||||
color: data.priorBalanceCents < 0 ? 'error.main' : 'text.primary',
|
||||
}}>
|
||||
{formatCents(data.priorBalanceCents)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
|
||||
{data.entries.map(entry => (
|
||||
<TableRow key={entry.rowId} hover sx={{ cursor: 'pointer' }}>
|
||||
<TableCell sx={{ fontFamily: 'monospace', color: 'text.secondary', fontSize: 12 }}>
|
||||
@@ -155,6 +232,13 @@ export default function InspectLedgersPage() {
|
||||
{new Date(entry.dateTime).toLocaleTimeString('sv-SE', { hour: '2-digit', minute: '2-digit' })}
|
||||
</Typography>
|
||||
</TableCell>
|
||||
{showAccountCol && (
|
||||
<TableCell sx={{ fontSize: 12, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
|
||||
<Tooltip title={entry.accountName} placement="top-start">
|
||||
<span>{entry.accountName}</span>
|
||||
</Tooltip>
|
||||
</TableCell>
|
||||
)}
|
||||
<TableCell sx={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
|
||||
<Tooltip title={entry.description} placement="top-start">
|
||||
<span>{entry.description}</span>
|
||||
@@ -186,22 +270,42 @@ export default function InspectLedgersPage() {
|
||||
</TableRow>
|
||||
))}
|
||||
|
||||
<TableRow sx={{ bgcolor: 'grey.100' }}>
|
||||
<TableCell colSpan={3} sx={{ fontWeight: 700 }}>
|
||||
Period totals
|
||||
</TableCell>
|
||||
{/* Period totals row — debit and credit sums only */}
|
||||
<TableRow sx={{ bgcolor: 'grey.100', borderTop: '2px solid', borderColor: 'divider' }}>
|
||||
<TableCell />
|
||||
<TableCell />
|
||||
{showAccountCol && <TableCell />}
|
||||
<TableCell sx={{ fontWeight: 700 }}>Period totals</TableCell>
|
||||
<TableCell align="right" sx={{ fontWeight: 700, fontFamily: '"Roboto Mono", monospace' }}>
|
||||
{formatCents(data.entries.reduce((s, e) => s + e.debitCents, 0))}
|
||||
</TableCell>
|
||||
<TableCell align="right" sx={{ fontWeight: 700, fontFamily: '"Roboto Mono", monospace', color: 'error.main' }}>
|
||||
{formatCents(data.entries.reduce((s, e) => s + e.creditCents, 0))}
|
||||
</TableCell>
|
||||
<TableCell align="right" sx={{ fontWeight: 700, fontFamily: '"Roboto Mono", monospace' }}>
|
||||
{formatCents(data.entries[data.entries.length - 1]?.balanceCents ?? 0)}
|
||||
<TableCell />
|
||||
</TableRow>
|
||||
|
||||
{/* Closing balance row */}
|
||||
<TableRow sx={{ bgcolor: 'rgba(25, 118, 210, 0.04)' }}>
|
||||
<TableCell />
|
||||
<TableCell />
|
||||
{showAccountCol && <TableCell />}
|
||||
<TableCell sx={{ fontWeight: 600, fontStyle: 'italic', color: 'text.secondary' }}>
|
||||
Closing balance
|
||||
</TableCell>
|
||||
<TableCell />
|
||||
<TableCell />
|
||||
<TableCell align="right" sx={{
|
||||
fontFamily: '"Roboto Mono", monospace', fontWeight: 700,
|
||||
color: (data.entries[data.entries.length - 1]?.balanceCents ?? data.priorBalanceCents) < 0 ? 'error.main' : 'text.primary',
|
||||
}}>
|
||||
{formatCents(data.entries[data.entries.length - 1]?.balanceCents ?? data.priorBalanceCents)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</TableBody>
|
||||
</Table>
|
||||
)
|
||||
})()}
|
||||
</TableContainer>
|
||||
</>
|
||||
) : data ? (
|
||||
|
||||
@@ -9,10 +9,11 @@ public static class InspectLedgerEndpoints
|
||||
var group = app.MapGroup("/api/organizations/{orgId:int}/inspect");
|
||||
|
||||
group.MapGet("/ledger", async (int orgId, int accountId, int? year, int? month,
|
||||
FinancialRepository repo) =>
|
||||
bool? ownOnly, FinancialRepository repo) =>
|
||||
{
|
||||
var yr = year ?? DateTime.UtcNow.Year;
|
||||
var mo = month ?? 0;
|
||||
var showOwnOnly = ownOnly ?? false;
|
||||
|
||||
DateTime from, to;
|
||||
if (mo > 0 && mo <= 12)
|
||||
@@ -26,21 +27,53 @@ public static class InspectLedgerEndpoints
|
||||
to = new DateTime(yr + 1, 1, 1);
|
||||
}
|
||||
|
||||
var rows = await repo.GetRowsForAccountInRange(accountId, from, to);
|
||||
long priorBalance = await repo.GetAccountBalanceCents(accountId, from);
|
||||
var allAccounts = await repo.GetAccountsForOrganization(orgId);
|
||||
var selectedAccount = allAccounts.FirstOrDefault(a => a.FinancialAccountId == accountId);
|
||||
if (selectedAccount == null) return Results.NotFound();
|
||||
|
||||
var accountIds = new List<int> { accountId };
|
||||
if (!showOwnOnly)
|
||||
{
|
||||
void CollectDescendants(int parentId)
|
||||
{
|
||||
foreach (var child in allAccounts.Where(a => a.ParentFinancialAccountId == parentId))
|
||||
{
|
||||
accountIds.Add(child.FinancialAccountId);
|
||||
CollectDescendants(child.FinancialAccountId);
|
||||
}
|
||||
}
|
||||
CollectDescendants(accountId);
|
||||
}
|
||||
|
||||
bool isGroup = accountIds.Count > 1;
|
||||
var accountNameMap = allAccounts.ToDictionary(a => a.FinancialAccountId, a => a.Name);
|
||||
|
||||
var allRows = new List<(UsingCash.Basic.Types.BasicFinancialTransactionRow Row, int AccountId)>();
|
||||
long priorBalance = 0;
|
||||
|
||||
foreach (var acctId in accountIds)
|
||||
{
|
||||
priorBalance += await repo.GetAccountBalanceCents(acctId, from);
|
||||
var rows = await repo.GetRowsForAccountInRange(acctId, from, to);
|
||||
allRows.AddRange(rows.Select(r => (r, acctId)));
|
||||
}
|
||||
|
||||
allRows.Sort((a, b) =>
|
||||
{
|
||||
var cmp = a.Row.CreatedDateTime.CompareTo(b.Row.CreatedDateTime);
|
||||
return cmp != 0 ? cmp : a.Row.FinancialTransactionRowId.CompareTo(b.Row.FinancialTransactionRowId);
|
||||
});
|
||||
|
||||
var entries = new List<object>();
|
||||
long runningBalance = priorBalance;
|
||||
|
||||
foreach (var row in rows)
|
||||
foreach (var (row, acctId) in allRows)
|
||||
{
|
||||
runningBalance += row.AmountCents;
|
||||
|
||||
var tx = await repo.GetTransaction(row.FinancialTransactionId);
|
||||
var dep = await repo.GetTransactionDependency(row.FinancialTransactionId);
|
||||
|
||||
var account = await repo.GetAccount(row.FinancialAccountId);
|
||||
|
||||
entries.Add(new
|
||||
{
|
||||
rowId = row.FinancialTransactionRowId,
|
||||
@@ -48,7 +81,8 @@ public static class InspectLedgerEndpoints
|
||||
sequenceId = tx?.OrganizationSequenceId ?? 0,
|
||||
dateTime = tx?.DateTime,
|
||||
description = tx?.Comment ?? "",
|
||||
accountName = account?.Name ?? "",
|
||||
accountId = acctId,
|
||||
accountName = accountNameMap.GetValueOrDefault(acctId, ""),
|
||||
debitCents = row.AmountCents > 0 ? row.AmountCents : 0,
|
||||
creditCents = row.AmountCents < 0 ? -row.AmountCents : 0,
|
||||
balanceCents = runningBalance,
|
||||
@@ -62,6 +96,9 @@ public static class InspectLedgerEndpoints
|
||||
return Results.Ok(new
|
||||
{
|
||||
accountId,
|
||||
accountName = selectedAccount.Name,
|
||||
isGroup,
|
||||
groupAccountCount = accountIds.Count,
|
||||
year = yr,
|
||||
month = mo,
|
||||
from = from.ToString("yyyy-MM-dd"),
|
||||
|
||||
Reference in New Issue
Block a user