Files
money/internal/report/report.go
T
nikolaandClaude Opus 5 b0026c5a79 Add money: statement-driven personal finance tracker
A data directory holds one folder per account. Statements dropped into
those folders are parsed into a rebuildable SQLite index, categorised by
ordered glob rules in rules.toml, and browsed or hand-tagged in a Bubble
Tea TUI. Movements between the user's own accounts are marked as
transfers by the same rules and excluded from spending totals.

Manual tags and transfer marks are stored separately from the rule-derived
ones and always win, so editing rules.toml and re-running retag never
destroys hand edits.

Parsers are pluggable. Three are ported from the Python extractors they
replace -- nlb and traderepublic read PDFs via pdftotext -layout, revolut
reads the CSV export -- alongside a configurable-column CSV parser and a
cmd parser that shells out to an external script.

Both ports fix two latent bugs in the originals: the sign character class
rejected the typographic minus U+2212 that some PDF fonts emit, and NLB's
hardcoded continuation indent broke when pdftotext compressed runs of
spaces, so the threshold is now measured from the description column.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-09 00:37:20 +02:00

120 lines
2.9 KiB
Go

// Package report aggregates transactions for the summary views.
//
// Transfers between the user's own accounts are excluded everywhere: moving
// money from checking to savings is not income and not spending.
package report
import (
"sort"
"git.petrovv.com/nikola/money/internal/model"
)
// TagTotal is one row of a spending breakdown.
type TagTotal struct {
Tag string
Currency string
In int64 // sum of inflows, positive
Out int64 // sum of outflows, positive
Count int
Digits int
}
// Net is inflow minus outflow.
func (t TagTotal) Net() int64 { return t.In - t.Out }
// ByTag groups transactions by currency and effective tag. Untagged rows are
// collected under "(untagged)" so they stay visible instead of vanishing.
const Untagged = "(untagged)"
// ByTag returns totals sorted by currency, then by largest outflow first.
func ByTag(txns []model.Transaction) []TagTotal {
type key struct{ currency, tag string }
acc := map[key]*TagTotal{}
for _, t := range txns {
if t.IsTransfer() {
continue
}
tag := t.Tag()
if tag == "" {
tag = Untagged
}
k := key{t.Currency, tag}
row, ok := acc[k]
if !ok {
row = &TagTotal{Tag: tag, Currency: t.Currency, Digits: t.MinorDigits}
acc[k] = row
}
if t.AmountMinor < 0 {
row.Out += -t.AmountMinor
} else {
row.In += t.AmountMinor
}
row.Count++
}
out := make([]TagTotal, 0, len(acc))
for _, row := range acc {
out = append(out, *row)
}
sort.Slice(out, func(i, j int) bool {
if out[i].Currency != out[j].Currency {
return out[i].Currency < out[j].Currency
}
if out[i].Out != out[j].Out {
return out[i].Out > out[j].Out
}
return out[i].Tag < out[j].Tag
})
return out
}
// CurrencyTotal is the bottom line for one currency.
type CurrencyTotal struct {
Currency string
In int64
Out int64
Digits int
}
// Net is inflow minus outflow.
func (c CurrencyTotal) Net() int64 { return c.In - c.Out }
// Totals sums the per-tag rows per currency. Currencies are never combined,
// because the tool holds no exchange rates.
func Totals(rows []TagTotal) []CurrencyTotal {
acc := map[string]*CurrencyTotal{}
for _, r := range rows {
c, ok := acc[r.Currency]
if !ok {
c = &CurrencyTotal{Currency: r.Currency, Digits: r.Digits}
acc[r.Currency] = c
}
c.In += r.In
c.Out += r.Out
}
out := make([]CurrencyTotal, 0, len(acc))
for _, c := range acc {
out = append(out, *c)
}
sort.Slice(out, func(i, j int) bool { return out[i].Currency < out[j].Currency })
return out
}
// Months lists the distinct YYYY-MM present in txns, most recent first.
func Months(txns []model.Transaction) []string {
seen := map[string]bool{}
for _, t := range txns {
if len(t.Date) >= 7 {
seen[t.Date[:7]] = true
}
}
out := make([]string, 0, len(seen))
for m := range seen {
out = append(out, m)
}
sort.Sort(sort.Reverse(sort.StringSlice(out)))
return out
}