Files
money/internal/model/model.go
T
nikolaandClaude Opus 5 02814d09e5 Remove manual tagging
A tag could come from two places: rules.toml, or the TUI's t key, which wrote
manual_tag with COALESCE(manual_tag, rule_tag) deciding the winner. That split
paid for itself in the first invariant of the codebase, in ClearOverrides and
the c key, in the * marker on the tag column, and in the one exception to a
disposable index -- a tag set by hand was the only thing in index.db that the
statements could not reproduce.

Now rules.toml decides every tag. The index is derived entirely from the
statements plus that file, so deleting it and re-importing gets back exactly
what was there, and retag has nothing to be careful of. Tagging a one-off means
writing a narrow rule on screen 4, which previews what the glob catches before
it is saved.

A manual tag in an existing index is dropped along with the column the first
time this build opens it, and those rows read as whatever the rules say, or as
untagged. TestManualTagSurvivesRetag guarded the invariant that has just been
removed; TestRetagRewritesEveryTag replaces it with the one that took its
place, and keeps Retag itself covered.

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

78 lines
2.0 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
}
// 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), " "))
}