Files
money/internal/model/model.go
T
nikolaandClaude Opus 5 ebf7770569 Pair transfers from rules.toml
The boolean transfer flag went two commits ago because a one-sided verdict let
half a movement vanish and left the report unbalanced. This is what replaces
it: a [[transfer]] block names both legs, and only a matched pair is dropped
from the report -- both legs together, never one.

Legs pair within five days, nearest date first, and a transaction belongs to at
most one transfer, so the first definition to claim a leg keeps it, exactly as
the first matching rule keeps a tag. The pairing is derived state like the tags:
Engine.Link rewrites the whole transfers table from rules.toml, which is why
retag re-derives both halves of what that file decides, and why it runs over
the whole index rather than a filtered view -- pairing inside one would let a
movement count as a transfer in one report and not in another. An unmatched leg
is not a transfer and keeps counting, surfaced as a warning instead.

Within one currency the amount is the evidence and must be the exact opposite.
Across currencies it is not checked at all: there are no rates here, so the two
numbers are unrelated and the dates carry the pairing alone.

tolerance_pct is the one exception, per definition, for a route where the bank
takes a fee and the two statements genuinely disagree. It defaults to zero and
belongs on the one definition that charges; a global or default tolerance would
loosen every route that does not. The difference it admits is not forgiven --
the pair leaves the report entirely, so a fee hidden inside one would be
spending that appears nowhere. Pair.Fee is what left less what arrived, and
report.Excluded carries it out per currency alongside the legs. It counts only
pairs whose legs are both in view, for the same reason it counts legs and not
transfers: half a pair cannot say what the other half received.

The screens:

- 6 builds a definition against the index as you type, showing the pairs it
  would form and the legs it would catch but leave unpaired. Six fields need
  more room than the rule builder's four, so the form sheds its spacing, then
  its hints, then the borders on unfocused fields.
- 7 lists every definition with what it pairs. Two counts, because they mean
  different things: an unpaired leg is a definition doing something and not
  finishing it, no pairs at all is dead weight. Tol names the tolerance, blank
  where amounts must agree.
- 3 grows a (transfers) row under TOTAL, and a fees row beneath it, or the
  report silently disagrees with the account balances.

Two things that are not part of transfers but are the same day's work:

- ls --uniq lists each account and description once, normalised the way a glob
  sees them, which is the shape of "what still needs a rule?" -- fifty visits
  to one shop are one pattern to write, not fifty rows to read.
- The rule builder's preview now filters to what the glob matches instead of
  marking matches in a full list. The count carries the context the rows no
  longer can: 2 of 7, measured against everything still in view.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-15 19:19:12 +02:00

111 lines
3.4 KiB
Go

// Package model holds the core domain types shared by every other package.
package model
import (
"fmt"
"strings"
)
// Account is one folder in the data root.
type Account struct {
ID int64
Slug string // folder name, unique
Name string // human readable, from account.toml
Currency string
MinorDigits int
}
// Transaction is a single line extracted from a statement, plus whatever the
// rules engine and the user have said about it.
//
// Amount is stored in minor units (cents) and signed: negative is an outflow.
type Transaction struct {
ID int64
AccountID int64
AccountSlug string
Currency string
MinorDigits int
Fingerprint string
Date string // YYYY-MM-DD
Description string
AmountMinor int64
SourceFileID int64
SourcePath string
// Type is the bank's own classification, e.g. Revolut's CARD_PAYMENT.
Type string
// BalanceMinor is the running balance the statement reported after this
// transaction, when it reports one. Used to verify the balance chain.
BalanceMinor *int64
// RuleTag is the category, decided entirely by rules.toml. It is derived
// state: `money retag` rewrites it wholesale.
RuleTag string
// TransferID names the matched movement this transaction is one leg of, or
// nil when it is not part of one. Derived from the [[transfer]] blocks in
// rules.toml and rewritten wholesale alongside the tags.
TransferID *int64
}
// IsTransferLeg reports whether this transaction was paired with its opposite
// number on another account, and so is money moved rather than money spent.
func (t Transaction) IsTransferLeg() bool { return t.TransferID != nil }
// TransferTag is shown in the tag column for a matched transfer leg that no
// rule has tagged, so a paired leg reads as accounted for rather than as a
// blank waiting to be filled in. It is bracketed like report.Untagged because
// it is the same kind of thing: a label the tool supplies, not one the user
// wrote.
const TransferTag = "(transfer)"
// DisplayTag is the tag to show for a transaction.
//
// It is display only. Nothing writes it back: rule_tag stays exactly what
// rules.toml made it, so a transfer can never smuggle a tag into the index and
// `money retag` remains safe to run at any time. A rule tag wins when there is
// one, since that is the user's own word for the transaction and the transfers
// screen is where the pairing is explained anyway.
func (t Transaction) DisplayTag() string {
if t.RuleTag != "" {
return t.RuleTag
}
if t.IsTransferLeg() {
return TransferTag
}
return ""
}
// FormatAmount renders the amount using the account's minor-unit scale.
func (t Transaction) FormatAmount() string {
return FormatMinor(t.AmountMinor, t.MinorDigits)
}
// FormatMinor renders minor units as a decimal string, e.g. -4520/2 -> "-45.20".
func FormatMinor(minor int64, digits int) string {
if digits <= 0 {
return fmt.Sprintf("%d", minor)
}
neg := minor < 0
if neg {
minor = -minor
}
scale := int64(1)
for i := 0; i < digits; i++ {
scale *= 10
}
whole, frac := minor/scale, minor%scale
s := fmt.Sprintf("%d.%0*d", whole, digits, frac)
if neg {
s = "-" + s
}
return s
}
// NormalizeDescription collapses whitespace and upper-cases, so that
// fingerprints and glob matching are insensitive to statement formatting noise.
func NormalizeDescription(s string) string {
return strings.ToUpper(strings.Join(strings.Fields(s), " "))
}