Add tabbed Inspect Ledgers with General Journal (grundbok)
Restructured Inspect Ledgers into three tabs: - Account Ledger (huvudbok): existing view, moved to AccountLedgerTab - General Journal (grundbok): new transaction-grouped view - Search: placeholder (disabled) General Journal features: - Backend endpoint: /inspect/general-journal returns transactions with nested rows, account paths (Parent » Child), account types - Transaction header rows: Tx#, date+time, description, doc indicator - Expandable/collapsible per transaction with smooth animation - Child rows: account type chip (color-coded), account path with parent breadcrumb, debit/credit columns - Year/month selectors, transaction count, currency label - Foreign currency chips on rows where applicable Backend: queries all accounts for org, groups rows by transaction, builds account paths from parent hierarchy, returns structured JSON. Made-with: Cursor
This commit is contained in:
@@ -1,330 +1,40 @@
|
||||
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, FormControlLabel, Checkbox,
|
||||
} 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; accountId: number; accountName: string
|
||||
debitCents: number; creditCents: number; balanceCents: number
|
||||
foreignCurrencyCode: string; foreignCurrencyAmountCents: number
|
||||
dependencyType: number | null; dependencyForeignId: number | null
|
||||
}
|
||||
|
||||
interface LedgerResponse {
|
||||
accountId: number; accountName: string; isGroup: boolean; groupAccountCount: 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 [selectedAccountId, setSelectedAccountId] = useState<number | null>(null)
|
||||
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
|
||||
api.get<AccountOption[]>(`/organizations/${activeOrg.organizationId}/inspect/accounts-tree`)
|
||||
.then(setAccounts)
|
||||
.catch(() => {})
|
||||
}, [activeOrg])
|
||||
|
||||
const isGroupAccount = useMemo(() => {
|
||||
if (!selectedAccountId) return false
|
||||
return accounts.some(a => a.parentId === selectedAccountId)
|
||||
}, [selectedAccountId, accounts])
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg || !selectedAccountId) return
|
||||
setLoading(true)
|
||||
const ownParam = ownOnly ? '&ownOnly=true' : ''
|
||||
api.get<LedgerResponse>(
|
||||
`/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccountId}&year=${year}&month=${month}${ownParam}`
|
||||
)
|
||||
.then(setData)
|
||||
.catch(() => setData(null))
|
||||
.finally(() => setLoading(false))
|
||||
}, [activeOrg, selectedAccountId, 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
|
||||
import { useState } from 'react'
|
||||
import { Box, Typography, Tabs, Tab } from '@mui/material'
|
||||
import AccountLedgerTab from './inspect/AccountLedgerTab'
|
||||
import GeneralJournalTab from './inspect/GeneralJournalTab'
|
||||
|
||||
function TabPanel({ children, value, index }: { children: React.ReactNode; value: number; index: number }) {
|
||||
return (
|
||||
<Box>
|
||||
<Typography variant="h5" sx={{ fontWeight: 600, mb: 3 }}>Inspect Ledgers</Typography>
|
||||
<Box role="tabpanel" hidden={value !== index} sx={{ pt: 2 }}>
|
||||
{value === index && children}
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
<Paper sx={{ p: 2, mb: 3, display: 'flex', gap: 2, alignItems: 'center', flexWrap: 'wrap' }} elevation={2}>
|
||||
<Autocomplete
|
||||
options={accountsWithDepth}
|
||||
groupBy={a => a.accountType}
|
||||
getOptionLabel={a => a.name}
|
||||
value={accountsWithDepth.find(a => a.id === selectedAccountId) ?? null}
|
||||
onChange={(_, v) => { setSelectedAccountId(v?.id ?? null); setOwnOnly(false) }}
|
||||
isOptionEqualToValue={(opt, val) => opt.id === val.id}
|
||||
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', 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>
|
||||
)}
|
||||
/>
|
||||
export default function InspectLedgersPage() {
|
||||
const [tab, setTab] = useState(0)
|
||||
|
||||
<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>
|
||||
|
||||
{!selectedAccountId ? (
|
||||
<Paper sx={{ p: 4, textAlign: 'center' }} elevation={1}>
|
||||
<Typography color="text.secondary">Select an account to inspect its ledger entries.</Typography>
|
||||
</Paper>
|
||||
) : (
|
||||
<>
|
||||
{isGroupAccount && (
|
||||
<Box sx={{ mb: 1 }}>
|
||||
<FormControlLabel
|
||||
control={<Checkbox size="small" checked={ownOnly} onChange={e => setOwnOnly(e.target.checked)} />}
|
||||
label={<Typography variant="body2" sx={{ fontSize: 12 }}>
|
||||
Show only this account's own transactions (exclude sub-accounts)
|
||||
</Typography>}
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{loading ? (
|
||||
<Skeleton height={400} />
|
||||
) : data && data.entries.length > 0 ? (
|
||||
<>
|
||||
<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" color="text.secondary" sx={{ fontStyle: 'italic' }}>
|
||||
Amounts in {activeOrg.currencyCode || 'presentation currency'}
|
||||
</Typography>
|
||||
</Box>
|
||||
|
||||
<TableContainer component={Paper} elevation={2}>
|
||||
{(() => {
|
||||
const showAccountCol = isGroupAccount && !ownOnly
|
||||
return (
|
||||
<Table size="small" sx={{ tableLayout: 'fixed', '& td, & th': { py: 0.6, fontSize: 13 } }}>
|
||||
<colgroup>
|
||||
<col style={{ width: 60 }} />
|
||||
<col style={{ width: 130 }} />
|
||||
{showAccountCol && <col style={{ width: 180 }} />}
|
||||
<col />
|
||||
<col style={{ width: 100 }} />
|
||||
<col style={{ width: 100 }} />
|
||||
<col style={{ width: 110 }} />
|
||||
</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>
|
||||
<TableCell align="right" sx={{ color: 'white', fontWeight: 600 }}>Balance</TableCell>
|
||||
</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 }}>
|
||||
{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>
|
||||
{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>
|
||||
</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>
|
||||
))}
|
||||
|
||||
{/* 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 />
|
||||
</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 ? (
|
||||
<Paper sx={{ p: 4, textAlign: 'center' }} elevation={1}>
|
||||
<Typography color="text.secondary">No entries for this period.</Typography>
|
||||
</Paper>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
return (
|
||||
<Box>
|
||||
<Typography variant="h5" sx={{ fontWeight: 600, mb: 2 }}>Inspect Ledgers</Typography>
|
||||
|
||||
<Tabs value={tab} onChange={(_, v) => setTab(v)} sx={{ borderBottom: 1, borderColor: 'divider' }}>
|
||||
<Tab label="Account Ledger" />
|
||||
<Tab label="General Journal" />
|
||||
<Tab label="Search" disabled />
|
||||
</Tabs>
|
||||
|
||||
<TabPanel value={tab} index={0}>
|
||||
<AccountLedgerTab />
|
||||
</TabPanel>
|
||||
<TabPanel value={tab} index={1}>
|
||||
<GeneralJournalTab />
|
||||
</TabPanel>
|
||||
<TabPanel value={tab} index={2}>
|
||||
<Typography color="text.secondary" sx={{ p: 4, textAlign: 'center' }}>
|
||||
Transaction search — coming soon.
|
||||
</Typography>
|
||||
</TabPanel>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
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, FormControlLabel, Checkbox,
|
||||
} 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; accountId: number; accountName: string
|
||||
debitCents: number; creditCents: number; balanceCents: number
|
||||
foreignCurrencyCode: string; foreignCurrencyAmountCents: number
|
||||
dependencyType: number | null; dependencyForeignId: number | null
|
||||
}
|
||||
|
||||
interface LedgerResponse {
|
||||
accountId: number; accountName: string; isGroup: boolean; groupAccountCount: 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 AccountLedgerTab() {
|
||||
const { activeOrg } = useOrg()
|
||||
const [accounts, setAccounts] = useState<AccountOption[]>([])
|
||||
const [selectedAccountId, setSelectedAccountId] = useState<number | null>(null)
|
||||
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
|
||||
api.get<AccountOption[]>(`/organizations/${activeOrg.organizationId}/inspect/accounts-tree`)
|
||||
.then(setAccounts)
|
||||
.catch(() => {})
|
||||
}, [activeOrg])
|
||||
|
||||
const isGroupAccount = useMemo(() => {
|
||||
if (!selectedAccountId) return false
|
||||
return accounts.some(a => a.parentId === selectedAccountId)
|
||||
}, [selectedAccountId, accounts])
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg || !selectedAccountId) return
|
||||
setLoading(true)
|
||||
const ownParam = ownOnly ? '&ownOnly=true' : ''
|
||||
api.get<LedgerResponse>(
|
||||
`/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccountId}&year=${year}&month=${month}${ownParam}`
|
||||
)
|
||||
.then(setData)
|
||||
.catch(() => setData(null))
|
||||
.finally(() => setLoading(false))
|
||||
}, [activeOrg, selectedAccountId, 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
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Paper sx={{ p: 2, mb: 3, display: 'flex', gap: 2, alignItems: 'center', flexWrap: 'wrap' }} elevation={2}>
|
||||
<Autocomplete
|
||||
options={accountsWithDepth}
|
||||
groupBy={a => a.accountType}
|
||||
getOptionLabel={a => a.name}
|
||||
value={accountsWithDepth.find(a => a.id === selectedAccountId) ?? null}
|
||||
onChange={(_, v) => { setSelectedAccountId(v?.id ?? null); setOwnOnly(false) }}
|
||||
isOptionEqualToValue={(opt, val) => opt.id === val.id}
|
||||
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', 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>
|
||||
)}
|
||||
/>
|
||||
|
||||
<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>
|
||||
|
||||
{!selectedAccountId ? (
|
||||
<Paper sx={{ p: 4, textAlign: 'center' }} elevation={1}>
|
||||
<Typography color="text.secondary">Select an account to inspect its ledger entries.</Typography>
|
||||
</Paper>
|
||||
) : (
|
||||
<>
|
||||
{isGroupAccount && (
|
||||
<Box sx={{ mb: 1 }}>
|
||||
<FormControlLabel
|
||||
control={<Checkbox size="small" checked={ownOnly} onChange={e => setOwnOnly(e.target.checked)} />}
|
||||
label={<Typography variant="body2" sx={{ fontSize: 12 }}>
|
||||
Show only this account's own transactions (exclude sub-accounts)
|
||||
</Typography>}
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{loading ? (
|
||||
<Skeleton height={400} />
|
||||
) : data && data.entries.length > 0 ? (
|
||||
<>
|
||||
<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" color="text.secondary" sx={{ fontStyle: 'italic' }}>
|
||||
Amounts in {activeOrg.currencyCode || 'presentation currency'}
|
||||
</Typography>
|
||||
</Box>
|
||||
|
||||
<TableContainer component={Paper} elevation={2}>
|
||||
{(() => {
|
||||
const showAccountCol = isGroupAccount && !ownOnly
|
||||
return (
|
||||
<Table size="small" sx={{ tableLayout: 'fixed', '& td, & th': { py: 0.6, fontSize: 13 } }}>
|
||||
<colgroup>
|
||||
<col style={{ width: 60 }} />
|
||||
<col style={{ width: 130 }} />
|
||||
{showAccountCol && <col style={{ width: 180 }} />}
|
||||
<col />
|
||||
<col style={{ width: 100 }} />
|
||||
<col style={{ width: 100 }} />
|
||||
<col style={{ width: 110 }} />
|
||||
</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>
|
||||
<TableCell align="right" sx={{ color: 'white', fontWeight: 600 }}>Balance</TableCell>
|
||||
</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 }}>
|
||||
{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>
|
||||
{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>
|
||||
</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>
|
||||
))}
|
||||
|
||||
{/* 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 />
|
||||
</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 ? (
|
||||
<Paper sx={{ p: 4, textAlign: 'center' }} elevation={1}>
|
||||
<Typography color="text.secondary">No entries for this period.</Typography>
|
||||
</Paper>
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import {
|
||||
Box, Typography, Table, TableBody, TableCell, TableContainer,
|
||||
TableHead, TableRow, Paper, Select, MenuItem, FormControl,
|
||||
InputLabel, Skeleton, Chip, Tooltip,
|
||||
} from '@mui/material'
|
||||
import { ChevronRight, AttachFile } from '@mui/icons-material'
|
||||
import { useOrg } from '../../contexts/OrgContext'
|
||||
import { api } from '../../api/client'
|
||||
import { formatCents } from '../../utils/money'
|
||||
|
||||
interface JournalRow {
|
||||
rowId: number; accountId: number; accountName: string; accountType: string
|
||||
debitCents: number; creditCents: number
|
||||
foreignCurrencyCode: string; foreignCurrencyAmountCents: number
|
||||
}
|
||||
|
||||
interface JournalTransaction {
|
||||
transactionId: number; sequenceId: number; dateTime: string
|
||||
description: string; blockchainHash: string; hasDocumentation: boolean
|
||||
rows: JournalRow[]
|
||||
}
|
||||
|
||||
interface JournalResponse {
|
||||
year: number; month: number; from: string; to: string
|
||||
transactionCount: number; transactions: JournalTransaction[]
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
const TYPE_ABBREV: Record<string, string> = {
|
||||
Asset: 'Asset', Debt: 'Debt', Income: 'Income', Cost: 'Cost',
|
||||
}
|
||||
|
||||
export default function GeneralJournalTab() {
|
||||
const { activeOrg } = useOrg()
|
||||
const [year, setYear] = useState(currentYear)
|
||||
const [month, setMonth] = useState(new Date().getMonth() + 1)
|
||||
const [data, setData] = useState<JournalResponse | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [collapsedTxs, setCollapsedTxs] = useState<Set<number>>(new Set())
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeOrg) return
|
||||
setLoading(true)
|
||||
api.get<JournalResponse>(
|
||||
`/organizations/${activeOrg.organizationId}/inspect/general-journal?year=${year}&month=${month}`
|
||||
)
|
||||
.then(setData)
|
||||
.catch(() => setData(null))
|
||||
.finally(() => setLoading(false))
|
||||
}, [activeOrg, year, month])
|
||||
|
||||
const toggleTx = (txId: number) => {
|
||||
setCollapsedTxs(prev => {
|
||||
const next = new Set(prev)
|
||||
if (next.has(txId)) next.delete(txId)
|
||||
else next.add(txId)
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
if (!activeOrg) return null
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Box sx={{ display: 'flex', gap: 2, mb: 2, alignItems: 'center' }}>
|
||||
<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>
|
||||
{data && (
|
||||
<Typography variant="body2" color="text.secondary">
|
||||
{data.transactionCount} transactions, {data.from} to {data.to}
|
||||
</Typography>
|
||||
)}
|
||||
<Box sx={{ flex: 1 }} />
|
||||
<Typography variant="body2" color="text.secondary" sx={{ fontStyle: 'italic' }}>
|
||||
Amounts in {activeOrg.currencyCode || 'presentation currency'}
|
||||
</Typography>
|
||||
</Box>
|
||||
|
||||
{loading ? (
|
||||
<Skeleton height={500} />
|
||||
) : data && data.transactions.length > 0 ? (
|
||||
<TableContainer component={Paper} elevation={2}>
|
||||
<Table size="small" sx={{ tableLayout: 'fixed', '& td, & th': { py: 0.4, fontSize: 13 } }}>
|
||||
<colgroup>
|
||||
<col style={{ width: 30 }} />
|
||||
<col style={{ width: 60 }} />
|
||||
<col style={{ width: 120 }} />
|
||||
<col />
|
||||
<col style={{ width: 100 }} />
|
||||
<col style={{ width: 100 }} />
|
||||
</colgroup>
|
||||
<TableHead>
|
||||
<TableRow sx={{ bgcolor: 'primary.main' }}>
|
||||
<TableCell sx={{ color: 'white' }} />
|
||||
<TableCell sx={{ color: 'white', fontWeight: 600 }}>Tx#</TableCell>
|
||||
<TableCell sx={{ color: 'white', fontWeight: 600 }}>Date</TableCell>
|
||||
<TableCell sx={{ color: 'white', fontWeight: 600 }}>Description / Account</TableCell>
|
||||
<TableCell align="right" sx={{ color: 'white', fontWeight: 600 }}>Debit</TableCell>
|
||||
<TableCell align="right" sx={{ color: 'white', fontWeight: 600 }}>Credit</TableCell>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{data.transactions.map(tx => {
|
||||
const isCollapsed = collapsedTxs.has(tx.transactionId)
|
||||
|
||||
return [
|
||||
// Transaction header row
|
||||
<TableRow
|
||||
key={`tx-${tx.transactionId}`}
|
||||
sx={{
|
||||
bgcolor: 'rgba(25, 118, 210, 0.04)',
|
||||
borderTop: '1px solid',
|
||||
borderColor: 'divider',
|
||||
cursor: 'pointer',
|
||||
'&:hover': { bgcolor: 'rgba(25, 118, 210, 0.08)' },
|
||||
}}
|
||||
onClick={() => toggleTx(tx.transactionId)}
|
||||
>
|
||||
<TableCell sx={{ p: '0 !important', textAlign: 'center' }}>
|
||||
<Box sx={{
|
||||
display: 'inline-flex',
|
||||
transition: 'transform 0.2s ease',
|
||||
transform: isCollapsed ? 'rotate(0deg)' : 'rotate(90deg)',
|
||||
}}>
|
||||
<ChevronRight sx={{ fontSize: 16, color: 'text.secondary' }} />
|
||||
</Box>
|
||||
</TableCell>
|
||||
<TableCell sx={{ fontWeight: 700, fontFamily: 'monospace', fontSize: 12 }}>
|
||||
{tx.sequenceId || tx.transactionId}
|
||||
</TableCell>
|
||||
<TableCell sx={{ fontWeight: 600, fontSize: 12 }}>
|
||||
{new Date(tx.dateTime).toLocaleDateString('sv-SE')}
|
||||
<Typography component="span" sx={{ ml: 0.5, fontSize: 11, color: 'text.disabled' }}>
|
||||
{new Date(tx.dateTime).toLocaleTimeString('sv-SE', { hour: '2-digit', minute: '2-digit' })}
|
||||
</Typography>
|
||||
</TableCell>
|
||||
<TableCell sx={{ fontWeight: 600 }}>
|
||||
{tx.description}
|
||||
{tx.hasDocumentation && (
|
||||
<AttachFile sx={{ fontSize: 14, ml: 0.5, color: 'text.secondary', verticalAlign: 'middle' }} />
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell />
|
||||
<TableCell />
|
||||
</TableRow>,
|
||||
|
||||
// Transaction row entries (collapsible)
|
||||
...tx.rows.map(row => (
|
||||
<TableRow
|
||||
key={`row-${row.rowId}`}
|
||||
hover
|
||||
sx={{
|
||||
'& td': {
|
||||
overflow: 'hidden',
|
||||
paddingTop: isCollapsed ? '0px !important' : undefined,
|
||||
paddingBottom: isCollapsed ? '0px !important' : undefined,
|
||||
borderBottom: isCollapsed ? 'none' : undefined,
|
||||
transition: 'padding-top 0.3s ease, padding-bottom 0.3s ease',
|
||||
},
|
||||
'& td > *': {
|
||||
maxHeight: isCollapsed ? 0 : 30,
|
||||
overflow: 'hidden',
|
||||
opacity: isCollapsed ? 0 : 1,
|
||||
transition: 'max-height 0.3s ease, opacity 0.25s ease',
|
||||
},
|
||||
}}
|
||||
>
|
||||
<TableCell />
|
||||
<TableCell />
|
||||
<TableCell>
|
||||
<Chip
|
||||
label={TYPE_ABBREV[row.accountType] || row.accountType}
|
||||
size="small"
|
||||
variant="outlined"
|
||||
sx={{ fontSize: 10, height: 18, minWidth: 48 }}
|
||||
color={
|
||||
row.accountType === 'Asset' ? 'success' :
|
||||
row.accountType === 'Debt' ? 'error' :
|
||||
row.accountType === 'Income' ? 'info' : 'warning'
|
||||
}
|
||||
/>
|
||||
</TableCell>
|
||||
<TableCell sx={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
|
||||
<Tooltip title={row.accountName} placement="top-start">
|
||||
<Typography component="span" variant="body2" sx={{ fontSize: 13, color: 'text.secondary' }}>
|
||||
{row.accountName}
|
||||
</Typography>
|
||||
</Tooltip>
|
||||
{row.foreignCurrencyCode && (
|
||||
<Chip label={`${row.foreignCurrencyCode} ${formatCents(Math.abs(row.foreignCurrencyAmountCents))}`}
|
||||
size="small" variant="outlined" sx={{ ml: 1, fontSize: 10, height: 18 }} />
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell align="right" sx={{
|
||||
fontFamily: '"Roboto Mono", monospace',
|
||||
color: row.debitCents > 0 ? 'text.primary' : 'text.disabled',
|
||||
}}>
|
||||
<span>{row.debitCents > 0 ? formatCents(row.debitCents) : ''}</span>
|
||||
</TableCell>
|
||||
<TableCell align="right" sx={{
|
||||
fontFamily: '"Roboto Mono", monospace',
|
||||
color: row.creditCents > 0 ? 'error.main' : 'text.disabled',
|
||||
}}>
|
||||
<span>{row.creditCents > 0 ? formatCents(row.creditCents) : ''}</span>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)),
|
||||
]
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
) : data ? (
|
||||
<Paper sx={{ p: 4, textAlign: 'center' }} elevation={1}>
|
||||
<Typography color="text.secondary">No transactions for this period.</Typography>
|
||||
</Paper>
|
||||
) : null}
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
@@ -122,6 +122,95 @@ public static class InspectLedgerEndpoints
|
||||
}).OrderBy(a => a.accountType).ThenBy(a => a.name));
|
||||
});
|
||||
|
||||
group.MapGet("/general-journal", async (int orgId, 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 allAccounts = await repo.GetAccountsForOrganization(orgId);
|
||||
var accountMap = allAccounts.ToDictionary(a => a.FinancialAccountId);
|
||||
|
||||
string AccountPath(int acctId)
|
||||
{
|
||||
if (!accountMap.TryGetValue(acctId, out var acct)) return $"#{acctId}";
|
||||
if (acct.ParentFinancialAccountId != 0 && accountMap.TryGetValue(acct.ParentFinancialAccountId, out var parent))
|
||||
return $"{parent.Name} » {acct.Name}";
|
||||
return acct.Name;
|
||||
}
|
||||
|
||||
// Get ALL rows for ALL accounts in this org for the period, sorted by transaction
|
||||
var allRows = new List<(UsingCash.Basic.Types.BasicFinancialTransactionRow Row, int AccountId)>();
|
||||
foreach (var acct in allAccounts)
|
||||
{
|
||||
var rows = await repo.GetRowsForAccountInRange(acct.FinancialAccountId, from, to);
|
||||
allRows.AddRange(rows.Select(r => (r, acct.FinancialAccountId)));
|
||||
}
|
||||
|
||||
// Group by transaction, ordered by transaction datetime
|
||||
var txCache = new Dictionary<int, UsingCash.Basic.Types.BasicFinancialTransaction>();
|
||||
foreach (var (row, _) in allRows)
|
||||
{
|
||||
if (!txCache.ContainsKey(row.FinancialTransactionId))
|
||||
{
|
||||
var tx = await repo.GetTransaction(row.FinancialTransactionId);
|
||||
if (tx != null) txCache[tx.FinancialTransactionId] = tx;
|
||||
}
|
||||
}
|
||||
|
||||
var grouped = allRows
|
||||
.GroupBy(r => r.Row.FinancialTransactionId)
|
||||
.Where(g => txCache.ContainsKey(g.Key))
|
||||
.OrderBy(g => txCache[g.Key].DateTime)
|
||||
.ThenBy(g => g.Key)
|
||||
.Select(g =>
|
||||
{
|
||||
var tx = txCache[g.Key];
|
||||
return new
|
||||
{
|
||||
transactionId = tx.FinancialTransactionId,
|
||||
sequenceId = tx.OrganizationSequenceId,
|
||||
dateTime = tx.DateTime,
|
||||
description = tx.Comment,
|
||||
blockchainHash = tx.BlockchainHash,
|
||||
hasDocumentation = tx.HasDocumentation,
|
||||
rows = g.Select(r => new
|
||||
{
|
||||
rowId = r.Row.FinancialTransactionRowId,
|
||||
accountId = r.AccountId,
|
||||
accountName = AccountPath(r.AccountId),
|
||||
accountType = accountMap.TryGetValue(r.AccountId, out var a) ? a.AccountType.ToString() : "",
|
||||
debitCents = r.Row.AmountCents > 0 ? r.Row.AmountCents : 0,
|
||||
creditCents = r.Row.AmountCents < 0 ? -r.Row.AmountCents : 0,
|
||||
foreignCurrencyCode = r.Row.ForeignCurrencyCode,
|
||||
foreignCurrencyAmountCents = r.Row.ForeignCurrencyAmountCents,
|
||||
}).ToList(),
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
return Results.Ok(new
|
||||
{
|
||||
year = yr,
|
||||
month = mo,
|
||||
from = from.ToString("yyyy-MM-dd"),
|
||||
to = to.ToString("yyyy-MM-dd"),
|
||||
transactionCount = grouped.Count,
|
||||
transactions = grouped,
|
||||
});
|
||||
});
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user