Add Inspect Ledgers page and API endpoints
Backend:
- GET /api/organizations/{orgId}/inspect/accounts-tree: flat account list
grouped by type for the account selector
- GET /api/organizations/{orgId}/inspect/ledger: rich ledger entries with
transaction comment, sequence ID, debit/credit split, running balance,
foreign currency, dependency info — joins rows with parent transactions
Frontend:
- InspectLedgersPage: searchable Autocomplete account selector grouped
by account type, year/month dropdowns, transaction grid with Tx#,
date+time, description (with tooltip), debit, credit, running balance,
foreign currency chips, period totals row, prior balance display
- Fixed column widths via tableLayout: fixed + colgroup
Also cleans up unused imports in AccountTreeGrid and removes stale
AnimatedRow function.
Made-with: Cursor
This commit is contained in:
@@ -9,6 +9,7 @@ import ProfitLossPage from './pages/ProfitLossPage'
|
||||
import ChartOfAccountsPage from './pages/ChartOfAccountsPage'
|
||||
import CryptoTreasuryPage from './pages/CryptoTreasuryPage'
|
||||
import ExpenseClaimsPage from './pages/ExpenseClaimsPage'
|
||||
import InspectLedgersPage from './pages/InspectLedgersPage'
|
||||
import PlaceholderPage from './pages/PlaceholderPage'
|
||||
|
||||
function RequireAuth({ children }: { children: React.ReactNode }) {
|
||||
@@ -25,7 +26,7 @@ export default function App() {
|
||||
<Route path="/" element={<RequireAuth><AppShell /></RequireAuth>}>
|
||||
<Route index element={<DashboardPage />} />
|
||||
<Route path="ledgers/chart-of-accounts" element={<ChartOfAccountsPage />} />
|
||||
<Route path="ledgers/inspect" element={<PlaceholderPage title="Inspect Ledgers" />} />
|
||||
<Route path="ledgers/inspect" element={<InspectLedgersPage />} />
|
||||
<Route path="ledgers/balance-sheet" element={<BalanceSheetPage />} />
|
||||
<Route path="ledgers/profit-loss" element={<ProfitLossPage />} />
|
||||
<Route path="ledgers/budget-vs-actual" element={<PlaceholderPage title="Budget vs Actual" />} />
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { useState, useMemo, useCallback, useRef, useEffect } from 'react'
|
||||
import { useState, useMemo, useCallback } from 'react'
|
||||
import {
|
||||
Table, TableBody, TableCell, TableContainer, TableHead, TableRow,
|
||||
Paper, IconButton, Box, Typography,
|
||||
} from '@mui/material'
|
||||
import { ExpandMore, ChevronRight, Folder, FolderOpen, Description } from '@mui/icons-material'
|
||||
import { ChevronRight, Folder, FolderOpen, Description } from '@mui/icons-material'
|
||||
|
||||
export interface TreeNode {
|
||||
id: number
|
||||
@@ -83,46 +83,6 @@ function formatValue(cents: number, reverseSign: boolean): string {
|
||||
return val < 0 ? `−${formatted}` : formatted
|
||||
}
|
||||
|
||||
function AnimatedRow({ visible, children }: { visible: boolean; children: React.ReactNode }) {
|
||||
const rowRef = useRef<HTMLTableRowElement>(null)
|
||||
const [height, setHeight] = useState<number | undefined>(undefined)
|
||||
const [mounted, setMounted] = useState(visible)
|
||||
|
||||
useEffect(() => {
|
||||
if (visible) {
|
||||
setMounted(true)
|
||||
requestAnimationFrame(() => {
|
||||
if (rowRef.current) setHeight(rowRef.current.scrollHeight)
|
||||
})
|
||||
} else {
|
||||
setHeight(0)
|
||||
const timer = setTimeout(() => setMounted(false), 300)
|
||||
return () => clearTimeout(timer)
|
||||
}
|
||||
}, [visible])
|
||||
|
||||
useEffect(() => {
|
||||
if (mounted && visible && rowRef.current) {
|
||||
setHeight(rowRef.current.scrollHeight)
|
||||
}
|
||||
}, [mounted, visible])
|
||||
|
||||
if (!mounted) return null
|
||||
|
||||
return (
|
||||
<tr
|
||||
ref={rowRef}
|
||||
style={{
|
||||
maxHeight: height !== undefined ? height : 'none',
|
||||
overflow: 'hidden',
|
||||
opacity: visible ? 1 : 0,
|
||||
transition: 'max-height 0.3s ease, opacity 0.25s ease',
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</tr>
|
||||
)
|
||||
}
|
||||
|
||||
export default function AccountTreeGrid({
|
||||
nodes, columnHeaders, title, onDownloadCsv, reverseSign = false,
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import {
|
||||
Box, Typography, Paper, Table, TableBody, TableCell, TableContainer,
|
||||
TableHead, TableRow, Select, MenuItem, FormControl, InputLabel,
|
||||
Skeleton, Chip, Tooltip, TextField, Autocomplete,
|
||||
} from '@mui/material'
|
||||
import { useOrg } from '../contexts/OrgContext'
|
||||
import { api } from '../api/client'
|
||||
import { formatCents } from '../utils/money'
|
||||
|
||||
interface AccountOption {
|
||||
id: number; name: string; parentId: number; accountType: string; open: boolean
|
||||
}
|
||||
|
||||
interface LedgerEntry {
|
||||
rowId: number; transactionId: number; sequenceId: number
|
||||
dateTime: string; description: string; 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
|
||||
from: string; to: string; priorBalanceCents: number
|
||||
entries: LedgerEntry[]; entryCount: number
|
||||
}
|
||||
|
||||
const MONTHS = [
|
||||
{ value: 0, label: 'Full Year' },
|
||||
{ value: 1, label: 'January' }, { value: 2, label: 'February' },
|
||||
{ value: 3, label: 'March' }, { value: 4, label: 'April' },
|
||||
{ value: 5, label: 'May' }, { value: 6, label: 'June' },
|
||||
{ value: 7, label: 'July' }, { value: 8, label: 'August' },
|
||||
{ value: 9, label: 'September' }, { value: 10, label: 'October' },
|
||||
{ value: 11, label: 'November' }, { value: 12, label: 'December' },
|
||||
]
|
||||
|
||||
const currentYear = new Date().getFullYear()
|
||||
const YEARS = Array.from({ length: 15 }, (_, i) => currentYear - i)
|
||||
|
||||
export default function InspectLedgersPage() {
|
||||
const { activeOrg } = useOrg()
|
||||
const [accounts, setAccounts] = useState<AccountOption[]>([])
|
||||
const [selectedAccount, setSelectedAccount] = useState<AccountOption | null>(null)
|
||||
const [year, setYear] = useState(currentYear)
|
||||
const [month, setMonth] = useState(0)
|
||||
const [data, setData] = useState<LedgerResponse | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg) return
|
||||
api.get<AccountOption[]>(`/organizations/${activeOrg.organizationId}/inspect/accounts-tree`)
|
||||
.then(setAccounts)
|
||||
.catch(() => {})
|
||||
}, [activeOrg])
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg || !selectedAccount) return
|
||||
setLoading(true)
|
||||
api.get<LedgerResponse>(
|
||||
`/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccount.id}&year=${year}&month=${month}`
|
||||
)
|
||||
.then(setData)
|
||||
.catch(() => setData(null))
|
||||
.finally(() => setLoading(false))
|
||||
}, [activeOrg, selectedAccount, year, month])
|
||||
|
||||
if (!activeOrg) return null
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Typography variant="h5" sx={{ fontWeight: 600, mb: 3 }}>Inspect Ledgers</Typography>
|
||||
|
||||
<Paper sx={{ p: 2, mb: 3, display: 'flex', gap: 2, alignItems: 'center', flexWrap: 'wrap' }} elevation={2}>
|
||||
<Autocomplete
|
||||
options={accounts}
|
||||
groupBy={a => a.accountType}
|
||||
getOptionLabel={a => a.name}
|
||||
value={selectedAccount}
|
||||
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>
|
||||
{!option.open && <Chip label="Closed" size="small" color="default" variant="outlined" sx={{ fontSize: 10 }} />}
|
||||
</Box>
|
||||
</li>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormControl size="small" sx={{ minWidth: 100 }}>
|
||||
<InputLabel>Year</InputLabel>
|
||||
<Select value={year} label="Year" onChange={e => setYear(Number(e.target.value))}>
|
||||
{YEARS.map(y => <MenuItem key={y} value={y}>{y}</MenuItem>)}
|
||||
</Select>
|
||||
</FormControl>
|
||||
|
||||
<FormControl size="small" sx={{ minWidth: 130 }}>
|
||||
<InputLabel>Month</InputLabel>
|
||||
<Select value={month} label="Month" onChange={e => setMonth(Number(e.target.value))}>
|
||||
{MONTHS.map(m => <MenuItem key={m.value} value={m.value}>{m.label}</MenuItem>)}
|
||||
</Select>
|
||||
</FormControl>
|
||||
</Paper>
|
||||
|
||||
{!selectedAccount ? (
|
||||
<Paper sx={{ p: 4, textAlign: 'center' }} elevation={1}>
|
||||
<Typography color="text.secondary">Select an account to inspect its ledger entries.</Typography>
|
||||
</Paper>
|
||||
) : loading ? (
|
||||
<Skeleton height={400} />
|
||||
) : data && data.entries.length > 0 ? (
|
||||
<>
|
||||
<Box sx={{ mb: 1, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||
<Typography variant="body2" color="text.secondary">
|
||||
{data.entryCount} entries, {data.from} to {data.to}
|
||||
</Typography>
|
||||
<Typography variant="body2" sx={{ fontFamily: 'monospace' }}>
|
||||
Prior balance: {formatCents(data.priorBalanceCents)}
|
||||
</Typography>
|
||||
</Box>
|
||||
|
||||
<TableContainer component={Paper} elevation={2}>
|
||||
<Table size="small" sx={{ tableLayout: 'fixed', '& td, & th': { py: 0.6, fontSize: 13 } }}>
|
||||
<colgroup>
|
||||
<col style={{ width: 60 }} />
|
||||
<col style={{ width: 140 }} />
|
||||
<col />
|
||||
<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>
|
||||
<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>
|
||||
<TableCell align="right" sx={{ color: 'white', fontWeight: 600 }}>Balance</TableCell>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{data.entries.map(entry => (
|
||||
<TableRow key={entry.rowId} hover sx={{ cursor: 'pointer' }}>
|
||||
<TableCell sx={{ fontFamily: 'monospace', color: 'text.secondary', fontSize: 12 }}>
|
||||
{entry.sequenceId || entry.transactionId}
|
||||
</TableCell>
|
||||
<TableCell sx={{ fontSize: 12 }}>
|
||||
{new Date(entry.dateTime).toLocaleDateString('sv-SE')}
|
||||
<Typography component="span" sx={{ ml: 0.5, fontSize: 11, color: 'text.disabled' }}>
|
||||
{new Date(entry.dateTime).toLocaleTimeString('sv-SE', { hour: '2-digit', minute: '2-digit' })}
|
||||
</Typography>
|
||||
</TableCell>
|
||||
<TableCell sx={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
|
||||
<Tooltip title={entry.description} placement="top-start">
|
||||
<span>{entry.description}</span>
|
||||
</Tooltip>
|
||||
{entry.foreignCurrencyCode && (
|
||||
<Chip label={`${entry.foreignCurrencyCode} ${formatCents(Math.abs(entry.foreignCurrencyAmountCents))}`}
|
||||
size="small" variant="outlined" sx={{ ml: 1, fontSize: 10, height: 18 }} />
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell align="right" sx={{
|
||||
fontFamily: '"Roboto Mono", monospace',
|
||||
color: entry.debitCents > 0 ? 'text.primary' : 'text.disabled',
|
||||
}}>
|
||||
{entry.debitCents > 0 ? formatCents(entry.debitCents) : ''}
|
||||
</TableCell>
|
||||
<TableCell align="right" sx={{
|
||||
fontFamily: '"Roboto Mono", monospace',
|
||||
color: entry.creditCents > 0 ? 'error.main' : 'text.disabled',
|
||||
}}>
|
||||
{entry.creditCents > 0 ? formatCents(entry.creditCents) : ''}
|
||||
</TableCell>
|
||||
<TableCell align="right" sx={{
|
||||
fontFamily: '"Roboto Mono", monospace',
|
||||
fontWeight: 600,
|
||||
color: entry.balanceCents < 0 ? 'error.main' : 'text.primary',
|
||||
}}>
|
||||
{formatCents(entry.balanceCents)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
|
||||
<TableRow sx={{ bgcolor: 'grey.100' }}>
|
||||
<TableCell colSpan={3} 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>
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
</>
|
||||
) : data ? (
|
||||
<Paper sx={{ p: 4, textAlign: 'center' }} elevation={1}>
|
||||
<Typography color="text.secondary">No entries for this period.</Typography>
|
||||
</Paper>
|
||||
) : null}
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using UsingCash.Database.Repositories;
|
||||
|
||||
namespace UsingCash.Api.Endpoints;
|
||||
|
||||
public static class InspectLedgerEndpoints
|
||||
{
|
||||
public static RouteGroupBuilder MapInspectLedgerEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/api/organizations/{orgId:int}/inspect");
|
||||
|
||||
group.MapGet("/ledger", async (int orgId, int accountId, int? year, int? month,
|
||||
FinancialRepository repo) =>
|
||||
{
|
||||
var yr = year ?? DateTime.UtcNow.Year;
|
||||
var mo = month ?? 0;
|
||||
|
||||
DateTime from, to;
|
||||
if (mo > 0 && mo <= 12)
|
||||
{
|
||||
from = new DateTime(yr, mo, 1);
|
||||
to = mo == 12 ? new DateTime(yr + 1, 1, 1) : new DateTime(yr, mo + 1, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
from = new DateTime(yr, 1, 1);
|
||||
to = new DateTime(yr + 1, 1, 1);
|
||||
}
|
||||
|
||||
var rows = await repo.GetRowsForAccountInRange(accountId, from, to);
|
||||
long priorBalance = await repo.GetAccountBalanceCents(accountId, from);
|
||||
|
||||
var entries = new List<object>();
|
||||
long runningBalance = priorBalance;
|
||||
|
||||
foreach (var row in rows)
|
||||
{
|
||||
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,
|
||||
transactionId = row.FinancialTransactionId,
|
||||
sequenceId = tx?.OrganizationSequenceId ?? 0,
|
||||
dateTime = tx?.DateTime,
|
||||
description = tx?.Comment ?? "",
|
||||
accountName = account?.Name ?? "",
|
||||
debitCents = row.AmountCents > 0 ? row.AmountCents : 0,
|
||||
creditCents = row.AmountCents < 0 ? -row.AmountCents : 0,
|
||||
balanceCents = runningBalance,
|
||||
foreignCurrencyCode = row.ForeignCurrencyCode,
|
||||
foreignCurrencyAmountCents = row.ForeignCurrencyAmountCents,
|
||||
dependencyType = dep?.TypeId,
|
||||
dependencyForeignId = dep?.ForeignId,
|
||||
});
|
||||
}
|
||||
|
||||
return Results.Ok(new
|
||||
{
|
||||
accountId,
|
||||
year = yr,
|
||||
month = mo,
|
||||
from = from.ToString("yyyy-MM-dd"),
|
||||
to = to.ToString("yyyy-MM-dd"),
|
||||
priorBalanceCents = priorBalance,
|
||||
entries,
|
||||
entryCount = entries.Count,
|
||||
});
|
||||
});
|
||||
|
||||
group.MapGet("/accounts-tree", async (int orgId, FinancialRepository repo) =>
|
||||
{
|
||||
var accounts = await repo.GetAccountsForOrganization(orgId);
|
||||
return Results.Ok(accounts.Select(a => new
|
||||
{
|
||||
id = a.FinancialAccountId,
|
||||
name = a.Name,
|
||||
parentId = a.ParentFinancialAccountId,
|
||||
accountType = a.AccountType.ToString(),
|
||||
open = a.Open,
|
||||
}).OrderBy(a => a.accountType).ThenBy(a => a.name));
|
||||
});
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ app.MapOrganizationEndpoints();
|
||||
app.MapUserEndpoints();
|
||||
app.MapEndOfMonthEndpoints();
|
||||
app.MapTreeReportEndpoints();
|
||||
app.MapInspectLedgerEndpoints();
|
||||
app.MapDevEndpoints();
|
||||
|
||||
app.UseDefaultFiles();
|
||||
|
||||
Reference in New Issue
Block a user