The report answered for the whole index, which is the one window a spending report is least often asked for: last January's groceries sat in the same total as last week's, and nothing on screen said which was which. It now opens on the month that has just ended -- the last one a statement can be complete for -- and left/right step along an axis beside the totals, from all time down through the named windows to the oldest month the index holds. The period narrows the report and nothing else. reloadReport runs its own query rather than reusing the rows the transaction list is showing: the two share the account, the search and the untagged toggle, and differ only in the date bounds, so opening on last month must not hide the rest of the index from the list beside it. Nothing may put the period into m.filter, which is exactly what would make it leak. The windows are relative to today, never to the newest statement. "Last month" with nothing in it reports nothing and says so, because silently answering for a month nobody asked for is worse than an empty screen; an empty period and an empty index therefore give different messages, one asking for another period and the other for an import. The rolling windows run to the end of this month rather than to the last complete one -- "last 3 months" is asked in order to see what is happening now, and leaving out the days since the 1st answers a question nobody put. The axis is built over the whole index rather than the rows in view, or it would grow and shrink as the account or search filter changed and move under the cursor; a reload rebuilds it, since an import can reach further back, but keeps the window the user was on. s cycles how those rows are arranged: largest out first as before, then in, net lowest first so the biggest losses lead, count, and the tag A to Z. A letter rather than a chord because the report is not a form. The marked heading says which column the rows are read from and the help names what the key does next, as the rule builder's preview already does. The sort rearranges rows and never changes which rows there are, so the order stays out of the filter for the same reason the period does. Currency remains the outer key under every order -- there are no rates here, so two currencies interleaved by amount would invite a comparison that cannot be made -- and every order falls back to the tag, so ties keep a fixed position instead of reshuffling between reloads. money report takes the same choice as --sort out|in|net|count|tag, and a misspelt one is refused rather than silently reporting in the default order. It keeps --month and has no equivalent of the wider windows. store.Filter gains From and To, compared as strings because dates are stored ISO-8601 and a string comparison is therefore a date comparison. store.Months replaces report.Months, which nothing had ever called: the axis needs the months of the whole index, not of a slice already in hand. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
313 lines
9.0 KiB
Go
313 lines
9.0 KiB
Go
// Package report aggregates transactions for the summary views.
|
|
package report
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"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)"
|
|
|
|
// Order is how the rows of a breakdown are arranged within a currency.
|
|
//
|
|
// Currency is always the outer key and no order can change that: the tool holds
|
|
// no exchange rates, so two currencies interleaved by amount would invite a
|
|
// comparison between numbers that cannot be compared.
|
|
type Order int
|
|
|
|
const (
|
|
// OrderOut is the default, and the question a spending report is usually
|
|
// opened to answer: where did the money go.
|
|
OrderOut Order = iota // largest outflow first
|
|
OrderIn // largest inflow first
|
|
OrderNet // lowest net first, so the biggest losses lead
|
|
OrderCount // most transactions first
|
|
OrderTag // tag name, A→Z
|
|
|
|
numOrders // how many there are, for cycling; not an order itself
|
|
)
|
|
|
|
// Next is the order after o, wrapping round. There are too many for a toggle,
|
|
// so a view offering the choice cycles through them.
|
|
func (o Order) Next() Order { return (o + 1) % numOrders }
|
|
|
|
// String is the short name the `--sort` flag takes, and ParseOrder accepts.
|
|
func (o Order) String() string {
|
|
switch o {
|
|
case OrderIn:
|
|
return "in"
|
|
case OrderNet:
|
|
return "net"
|
|
case OrderCount:
|
|
return "count"
|
|
case OrderTag:
|
|
return "tag"
|
|
}
|
|
return "out"
|
|
}
|
|
|
|
// Label says what the order actually does, for a view with room to say it.
|
|
func (o Order) Label() string {
|
|
switch o {
|
|
case OrderIn:
|
|
return "largest in first"
|
|
case OrderNet:
|
|
return "lowest net first"
|
|
case OrderCount:
|
|
return "most transactions first"
|
|
case OrderTag:
|
|
return "tag A→Z"
|
|
}
|
|
return "largest out first"
|
|
}
|
|
|
|
// Column is the heading the order sorts on, so a view can mark the column the
|
|
// rows are arranged by without keeping its own copy of this mapping.
|
|
func (o Order) Column() string {
|
|
switch o {
|
|
case OrderIn:
|
|
return "In"
|
|
case OrderNet:
|
|
return "Net"
|
|
case OrderCount:
|
|
return "N"
|
|
case OrderTag:
|
|
return "Tag"
|
|
}
|
|
return "Out"
|
|
}
|
|
|
|
// Ascending reports which way the order runs. Only the tag reads naturally
|
|
// upwards; every amount and count is asked for biggest-first.
|
|
func (o Order) Ascending() bool { return o == OrderTag }
|
|
|
|
// Orders is every order, in the cycle Next follows.
|
|
func Orders() []Order {
|
|
out := make([]Order, 0, numOrders)
|
|
for o := Order(0); o < numOrders; o++ {
|
|
out = append(out, o)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ParseOrder turns a short name back into an order, naming the alternatives
|
|
// when it cannot, since a misspelt sort would otherwise silently report in an
|
|
// order the caller did not ask for.
|
|
func ParseOrder(s string) (Order, error) {
|
|
for _, o := range Orders() {
|
|
if o.String() == s {
|
|
return o, nil
|
|
}
|
|
}
|
|
var names []string
|
|
for _, o := range Orders() {
|
|
names = append(names, o.String())
|
|
}
|
|
return OrderOut, fmt.Errorf("unknown sort %q: want one of %s", s, strings.Join(names, ", "))
|
|
}
|
|
|
|
// ByTag returns totals grouped by currency and arranged within each by order.
|
|
//
|
|
// Matched transfer legs are left out entirely. Both legs of a transfer are
|
|
// dropped together, so a report over everything is unchanged in total by money
|
|
// the user moved between their own accounts; a report scoped to one account or
|
|
// one month may see only one leg, and dropping it is still right, since the
|
|
// money was not spent. What was left out is available from Excluded.
|
|
//
|
|
// A pair whose definition sets a tolerance_pct is dropped the same way, fee and
|
|
// all — so with such a definition in play the report *is* short by the fee, and
|
|
// callers are expected to show Excluded alongside it rather than leave that
|
|
// difference unexplained.
|
|
func ByTag(txns []model.Transaction, order Order) []TagTotal {
|
|
type key struct{ currency, tag string }
|
|
acc := map[key]*TagTotal{}
|
|
|
|
for _, t := range txns {
|
|
if t.IsTransferLeg() {
|
|
continue
|
|
}
|
|
tag := t.RuleTag
|
|
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 {
|
|
a, b := out[i], out[j]
|
|
if a.Currency != b.Currency {
|
|
return a.Currency < b.Currency
|
|
}
|
|
switch order {
|
|
case OrderIn:
|
|
if a.In != b.In {
|
|
return a.In > b.In
|
|
}
|
|
case OrderNet:
|
|
if a.Net() != b.Net() {
|
|
return a.Net() < b.Net()
|
|
}
|
|
case OrderCount:
|
|
if a.Count != b.Count {
|
|
return a.Count > b.Count
|
|
}
|
|
case OrderTag:
|
|
// The tag is the whole order here, and also the tie-break below.
|
|
default:
|
|
if a.Out != b.Out {
|
|
return a.Out > b.Out
|
|
}
|
|
}
|
|
// Every order falls back to the tag, so rows that tie on the chosen
|
|
// column keep a fixed position instead of shuffling between reloads.
|
|
return a.Tag < b.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
|
|
}
|
|
|
|
// TransferTotal is what ByTag left out for one currency.
|
|
//
|
|
// It counts legs rather than pairs on purpose: a report scoped to one account
|
|
// or month usually holds only one side of a movement, and claiming a whole
|
|
// transfer was excluded when only half of it was in view would be a lie. The
|
|
// same goes for the two directions, which is why they are separate totals: an
|
|
// exchange between currencies has its legs in different columns entirely, so
|
|
// one currency can see only what left and the other only what arrived.
|
|
type TransferTotal struct {
|
|
Currency string
|
|
Out int64 // sum of the leaving legs in view, positive
|
|
In int64 // sum of the arriving legs in view
|
|
Legs int
|
|
Digits int
|
|
// Fee is what those movements cost: the amount that left less the amount
|
|
// that arrived, for pairs whose legs are *both* in view. A definition with a
|
|
// tolerance_pct pairs legs that disagree, and since the pair leaves the
|
|
// report entirely, this is the only place that difference is still money
|
|
// rather than nothing. Zero when no definition allows a mismatch.
|
|
Fee int64
|
|
// Pairs counts the movements Fee was computed from — complete pairs in this
|
|
// currency, not legs. A report scoped to one account or month usually holds
|
|
// one side of a movement, and half a pair cannot say what the other half
|
|
// received, so it contributes to Legs and Out/In but not here.
|
|
Pairs int
|
|
}
|
|
|
|
// Excluded summarises the transfer legs ByTag dropped, so a report can account
|
|
// for the difference between its total and the account balances.
|
|
func Excluded(txns []model.Transaction) []TransferTotal {
|
|
// Both legs of a pair carry the same TransferID, so the ones whose partner
|
|
// is also in view can be found without going back to the index.
|
|
byID := map[int64][]model.Transaction{}
|
|
for _, t := range txns {
|
|
if t.IsTransferLeg() {
|
|
byID[*t.TransferID] = append(byID[*t.TransferID], t)
|
|
}
|
|
}
|
|
|
|
acc := map[string]*TransferTotal{}
|
|
row := func(t model.Transaction) *TransferTotal {
|
|
r, ok := acc[t.Currency]
|
|
if !ok {
|
|
r = &TransferTotal{Currency: t.Currency, Digits: t.MinorDigits}
|
|
acc[t.Currency] = r
|
|
}
|
|
return r
|
|
}
|
|
|
|
for _, t := range txns {
|
|
if !t.IsTransferLeg() {
|
|
continue
|
|
}
|
|
r := row(t)
|
|
r.Legs++
|
|
if t.AmountMinor < 0 {
|
|
r.Out += -t.AmountMinor
|
|
} else {
|
|
r.In += t.AmountMinor
|
|
}
|
|
}
|
|
|
|
for _, legs := range byID {
|
|
// An exchange has its legs in two currencies, and there is no rate here
|
|
// to subtract one from the other, so it has no fee to report.
|
|
if len(legs) != 2 || legs[0].Currency != legs[1].Currency {
|
|
continue
|
|
}
|
|
r := row(legs[0])
|
|
r.Pairs++
|
|
r.Fee -= legs[0].AmountMinor + legs[1].AmountMinor
|
|
}
|
|
|
|
out := make([]TransferTotal, 0, len(acc))
|
|
for _, r := range acc {
|
|
out = append(out, *r)
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return out[i].Currency < out[j].Currency })
|
|
return out
|
|
}
|