Drop counterparty, the generic parsers and .money/
counterparty was a structured field only nlb could fill honestly. revolut and traderepublic invented one by running an IBAN-shaped regex over the description they had just built, and the two spellings disagreed -- SI56 1234 5678 9012 345 against SI56123456789012345 -- so a literal rule pattern that worked on one account silently matched nothing on another. It is gone from the model, the index, the rule keys, ls --wide and the rules screen. nlb now appends its IBAN column to the end of the description, where the other two already keep theirs, so match = "*SI56*" works everywhere. That changes those descriptions and with them their fingerprints, so a statement overlapping an already-imported period will re-add rather than dedupe those rows until the index is rebuilt. An index built by an older binary drops the column when it is opened. The index itself moves from .money/index.db up to index.db beside rules.toml. Nothing looks in the old location, so an existing one has to be moved by hand -- otherwise the tool quietly starts a fresh index and the manual tags in the old file, the only thing statements cannot reproduce, stay behind in it. The csv and cmd parsers are gone along with the [csv] and [cmd] config they carried. cmd shelled out to the Python extractors, which were ported to Go and deleted, so it bridged to nothing; csv was a generic column-mapped fallback that no account used, and between them they were the largest configuration surface in the tool. A bank is now described in Go, where it can be tested. The importer tests register their own three-column parser rather than borrow a bank's, so they stay about the directory walk, dedupe and per-file error reporting. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,9 +18,8 @@ const (
|
||||
RulesFile = "rules.toml"
|
||||
// AccountFile is the per-account config inside each account folder.
|
||||
AccountFile = "account.toml"
|
||||
// StateDir holds the rebuildable SQLite index.
|
||||
StateDir = ".money"
|
||||
// IndexFile is the SQLite index inside StateDir.
|
||||
// IndexFile is the rebuildable SQLite index, at the root of the data
|
||||
// directory alongside rules.toml.
|
||||
IndexFile = "index.db"
|
||||
)
|
||||
|
||||
@@ -31,10 +30,6 @@ type Rule struct {
|
||||
Tag string `toml:"tag"`
|
||||
Transfer bool `toml:"transfer"`
|
||||
Account string `toml:"account"` // optional: restrict to one account slug
|
||||
// Counterparty matches the other side's account number, which for
|
||||
// movements between the user's own accounts is often the only reliable
|
||||
// signal. Optional; when set, it must match as well as Match.
|
||||
Counterparty string `toml:"counterparty"`
|
||||
// Type matches the bank's own classification, e.g. Revolut's CARD_PAYMENT.
|
||||
// Optional; when set, it must match as well as Match.
|
||||
Type string `toml:"type"`
|
||||
@@ -62,8 +57,8 @@ func LoadRules(root string) (*Rules, error) {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
for i, rule := range r.Rule {
|
||||
if rule.Match == "" && rule.Counterparty == "" && rule.Type == "" {
|
||||
return nil, fmt.Errorf("%s: rule %d has no match, counterparty or type pattern", path, i+1)
|
||||
if rule.Match == "" && rule.Type == "" {
|
||||
return nil, fmt.Errorf("%s: rule %d has no match or type pattern", path, i+1)
|
||||
}
|
||||
if rule.Tag == "" && !rule.Transfer {
|
||||
return nil, fmt.Errorf("%s: rule %d (%q) sets neither tag nor transfer", path, i+1, rule.Match)
|
||||
@@ -79,8 +74,8 @@ func LoadRules(root string) (*Rules, error) {
|
||||
// The file is rewritten through a temporary file so a failure part-way cannot
|
||||
// leave the user with a truncated config.
|
||||
func AppendRule(root string, r Rule) error {
|
||||
if r.Match == "" && r.Counterparty == "" && r.Type == "" {
|
||||
return fmt.Errorf("a rule needs a match, counterparty or type pattern")
|
||||
if r.Match == "" && r.Type == "" {
|
||||
return fmt.Errorf("a rule needs a match or type pattern")
|
||||
}
|
||||
if r.Tag == "" && !r.Transfer {
|
||||
return fmt.Errorf("a rule needs a tag or transfer = true")
|
||||
@@ -254,7 +249,6 @@ func formatRule(r Rule) string {
|
||||
}
|
||||
}
|
||||
write("match", r.Match)
|
||||
write("counterparty", r.Counterparty)
|
||||
write("type", r.Type)
|
||||
write("account", r.Account)
|
||||
write("tag", r.Tag)
|
||||
@@ -267,41 +261,6 @@ func formatRule(r Rule) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Column locates one field in a CSV row.
|
||||
type Column struct {
|
||||
Col int `toml:"col"`
|
||||
Layout string `toml:"layout"` // date only, Go reference layout
|
||||
Decimal string `toml:"decimal"` // amount only, default "."
|
||||
Thousands string `toml:"thousands"` // amount only, default ""
|
||||
}
|
||||
|
||||
// CSVConfig describes how to read a delimited statement.
|
||||
type CSVConfig struct {
|
||||
Delimiter string `toml:"delimiter"`
|
||||
SkipRows int `toml:"skip_rows"`
|
||||
Encoding string `toml:"encoding"` // "" or "utf-8"; other encodings unsupported for now
|
||||
Date Column `toml:"date"`
|
||||
Description Column `toml:"description"`
|
||||
Amount *Column `toml:"amount"` // single signed column...
|
||||
Debit *Column `toml:"debit"` // ...or a debit/credit pair
|
||||
Credit *Column `toml:"credit"`
|
||||
// Invert flips the sign of the parsed amount, for statements that report
|
||||
// outflows as positive numbers.
|
||||
Invert bool `toml:"invert"`
|
||||
}
|
||||
|
||||
// CmdConfig runs an external extractor (e.g. one of the existing Python
|
||||
// scripts) and reads normalised CSV from its stdout.
|
||||
type CmdConfig struct {
|
||||
// Argv is the command to run. The literal token "{{file}}" is replaced
|
||||
// with the absolute path of the statement being imported.
|
||||
Argv []string `toml:"argv"`
|
||||
// Layout is the date layout the script emits; defaults to 2006-01-02.
|
||||
Layout string `toml:"layout"`
|
||||
// SkipRows skips leading rows of the script's output (e.g. a header).
|
||||
SkipRows int `toml:"skip_rows"`
|
||||
}
|
||||
|
||||
// Account is a parsed account.toml.
|
||||
type Account struct {
|
||||
Slug string // folder name, filled in by LoadAccounts
|
||||
@@ -312,9 +271,7 @@ type Account struct {
|
||||
Parser string `toml:"parser"`
|
||||
// Include restricts which files in the folder are treated as statements.
|
||||
// Defaults to every regular file except account.toml and dotfiles.
|
||||
Include []string `toml:"include"`
|
||||
CSV *CSVConfig `toml:"csv"`
|
||||
Cmd *CmdConfig `toml:"cmd"`
|
||||
Include []string `toml:"include"`
|
||||
}
|
||||
|
||||
// Digits returns the configured minor-unit scale, defaulting to 2.
|
||||
@@ -376,7 +333,7 @@ func loadAccount(dir, slug, cfgPath string) (*Account, error) {
|
||||
|
||||
// IndexPath returns the location of the SQLite index for a data root.
|
||||
func IndexPath(root string) string {
|
||||
return filepath.Join(root, StateDir, IndexFile)
|
||||
return filepath.Join(root, IndexFile)
|
||||
}
|
||||
|
||||
// UserConfig is the small file in the user's config directory that says where
|
||||
|
||||
@@ -169,7 +169,6 @@ func importFile(root string, db *store.DB, acc *config.Account, accountID int64,
|
||||
Date: t.Date,
|
||||
Description: t.Description,
|
||||
AmountMinor: t.AmountMinor,
|
||||
Counterparty: t.Counterparty,
|
||||
Type: t.Type,
|
||||
BalanceMinor: t.BalanceMinor,
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package importer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.petrovv.com/nikola/money/internal/config"
|
||||
"git.petrovv.com/nikola/money/internal/parser"
|
||||
@@ -15,14 +17,49 @@ import (
|
||||
const accountTOML = `
|
||||
name = "Checking"
|
||||
currency = "EUR"
|
||||
parser = "csv"
|
||||
[csv]
|
||||
skip_rows = 1
|
||||
date = { col = 0, layout = "2006-01-02" }
|
||||
description = { col = 1 }
|
||||
amount = { col = 2 }
|
||||
parser = "test"
|
||||
`
|
||||
|
||||
// These tests are about the directory walk, the checksum skip, dedupe and
|
||||
// per-file error reporting -- not about any bank's layout. Driving them with a
|
||||
// real bank parser would drag that bank's quirks (Revolut's fee folding and
|
||||
// COMPLETED filter, the PDF parsers' dependency on pdftotext) into every
|
||||
// fixture, so they register the smallest parser that will do instead.
|
||||
func init() {
|
||||
parser.Register("test", func(acc *config.Account) (parser.Parser, error) {
|
||||
return testParser{digits: acc.Digits()}, nil
|
||||
})
|
||||
}
|
||||
|
||||
// testParser reads "date,description,amount" with one header row.
|
||||
type testParser struct{ digits int }
|
||||
|
||||
func (p testParser) Parse(path string, acc *config.Account) ([]parser.RawTxn, error) {
|
||||
body, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var txns []parser.RawTxn
|
||||
for i, line := range strings.Split(strings.TrimSpace(string(body)), "\n") {
|
||||
if i == 0 || strings.TrimSpace(line) == "" {
|
||||
continue // header
|
||||
}
|
||||
fields := strings.Split(line, ",")
|
||||
if len(fields) != 3 {
|
||||
return nil, fmt.Errorf("row %d: got %d fields, want 3", i+1, len(fields))
|
||||
}
|
||||
if _, err := time.Parse("2006-01-02", fields[0]); err != nil {
|
||||
return nil, fmt.Errorf("row %d: %w", i+1, err)
|
||||
}
|
||||
amount, err := parser.ParseAmount(fields[2], ".", "", p.digits)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("row %d: %w", i+1, err)
|
||||
}
|
||||
txns = append(txns, parser.RawTxn{Date: fields[0], Description: fields[1], AmountMinor: amount})
|
||||
}
|
||||
return txns, nil
|
||||
}
|
||||
|
||||
// newRoot builds a data root with one account and the given statement files.
|
||||
func newRoot(t *testing.T, statements map[string]string) (string, *store.DB, []*config.Account, *rules.Engine) {
|
||||
t.Helper()
|
||||
|
||||
@@ -33,10 +33,6 @@ type Transaction struct {
|
||||
SourceFileID int64
|
||||
SourcePath string
|
||||
|
||||
// Counterparty is the other side's account number (an IBAN, where the
|
||||
// statement gives one). Transfers between the user's own accounts are
|
||||
// often only distinguishable by it.
|
||||
Counterparty 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
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.petrovv.com/nikola/money/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Register("cmd", newCmdParser)
|
||||
}
|
||||
|
||||
// FileToken is replaced with the statement's absolute path in a cmd parser's argv.
|
||||
const FileToken = "{{file}}"
|
||||
|
||||
// cmdRunTimeout bounds an extractor run so a hung script cannot wedge an import.
|
||||
const cmdRunTimeout = 2 * time.Minute
|
||||
|
||||
// cmdParser runs an external extractor and reads normalised CSV from its
|
||||
// stdout: date,description,amount with any further columns ignored. This is
|
||||
// the bridge that lets the existing Python extractors be used unchanged.
|
||||
type cmdParser struct {
|
||||
cfg config.CmdConfig
|
||||
digits int
|
||||
}
|
||||
|
||||
func newCmdParser(acc *config.Account) (Parser, error) {
|
||||
if acc.Cmd == nil || len(acc.Cmd.Argv) == 0 {
|
||||
return nil, fmt.Errorf("account %s: parser \"cmd\" requires [cmd] with a non-empty argv", acc.Slug)
|
||||
}
|
||||
cfg := *acc.Cmd
|
||||
if cfg.Layout == "" {
|
||||
cfg.Layout = "2006-01-02"
|
||||
}
|
||||
if !hasFileToken(cfg.Argv) {
|
||||
return nil, fmt.Errorf("account %s: [cmd] argv must contain %s so the script knows which file to read",
|
||||
acc.Slug, FileToken)
|
||||
}
|
||||
return &cmdParser{cfg: cfg, digits: acc.Digits()}, nil
|
||||
}
|
||||
|
||||
func hasFileToken(argv []string) bool {
|
||||
for _, a := range argv {
|
||||
if strings.Contains(a, FileToken) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *cmdParser) Parse(path string, acc *config.Account) ([]RawTxn, error) {
|
||||
argv := make([]string, len(p.cfg.Argv))
|
||||
for i, a := range p.cfg.Argv {
|
||||
argv[i] = strings.ReplaceAll(a, FileToken, path)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cmdRunTimeout)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, argv[0], argv[1:]...)
|
||||
cmd.Dir = acc.Dir
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
msg := strings.TrimSpace(stderr.String())
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
return nil, fmt.Errorf("extractor %v timed out after %s", argv, cmdRunTimeout)
|
||||
}
|
||||
if msg != "" {
|
||||
return nil, fmt.Errorf("extractor %v failed: %w: %s", argv, err, msg)
|
||||
}
|
||||
return nil, fmt.Errorf("extractor %v failed: %w", argv, err)
|
||||
}
|
||||
|
||||
return p.parseOutput(&stdout, argv)
|
||||
}
|
||||
|
||||
func (p *cmdParser) parseOutput(out io.Reader, argv []string) ([]RawTxn, error) {
|
||||
r := csv.NewReader(out)
|
||||
r.FieldsPerRecord = -1
|
||||
r.LazyQuotes = true
|
||||
|
||||
var txns []RawTxn
|
||||
for row := 0; ; row++ {
|
||||
rec, err := r.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extractor %v: output row %d: %w", argv, row+1, err)
|
||||
}
|
||||
if row < p.cfg.SkipRows || isBlank(rec) {
|
||||
continue
|
||||
}
|
||||
if len(rec) < 3 {
|
||||
return nil, fmt.Errorf("extractor %v: output row %d has %d columns, want date,description,amount",
|
||||
argv, row+1, len(rec))
|
||||
}
|
||||
d, err := time.Parse(p.cfg.Layout, strings.TrimSpace(rec[0]))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extractor %v: output row %d: date %q does not match layout %q",
|
||||
argv, row+1, rec[0], p.cfg.Layout)
|
||||
}
|
||||
amount, err := ParseAmount(rec[2], ".", "", p.digits)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extractor %v: output row %d: %w", argv, row+1, err)
|
||||
}
|
||||
txns = append(txns, RawTxn{
|
||||
Date: d.Format("2006-01-02"),
|
||||
Description: strings.TrimSpace(rec[1]),
|
||||
AmountMinor: amount,
|
||||
})
|
||||
}
|
||||
return txns, nil
|
||||
}
|
||||
@@ -1,175 +0,0 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.petrovv.com/nikola/money/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
Register("csv", newCSVParser)
|
||||
}
|
||||
|
||||
// csvParser reads a delimited statement using column positions from
|
||||
// account.toml. Amounts come either from one signed column, or from a
|
||||
// debit/credit pair.
|
||||
type csvParser struct {
|
||||
cfg config.CSVConfig
|
||||
digits int
|
||||
}
|
||||
|
||||
func newCSVParser(acc *config.Account) (Parser, error) {
|
||||
if acc.CSV == nil {
|
||||
return nil, fmt.Errorf("account %s: parser \"csv\" requires a [csv] section", acc.Slug)
|
||||
}
|
||||
cfg := *acc.CSV
|
||||
if cfg.Amount == nil && cfg.Debit == nil && cfg.Credit == nil {
|
||||
return nil, fmt.Errorf("account %s: [csv] needs either amount or debit/credit columns", acc.Slug)
|
||||
}
|
||||
if cfg.Amount != nil && (cfg.Debit != nil || cfg.Credit != nil) {
|
||||
return nil, fmt.Errorf("account %s: [csv] sets both amount and debit/credit; pick one", acc.Slug)
|
||||
}
|
||||
if cfg.Date.Layout == "" {
|
||||
return nil, fmt.Errorf("account %s: [csv] date needs a layout, e.g. layout = \"02.01.2006\"", acc.Slug)
|
||||
}
|
||||
return &csvParser{cfg: cfg, digits: acc.Digits()}, nil
|
||||
}
|
||||
|
||||
func (p *csvParser) Parse(path string, acc *config.Account) ([]RawTxn, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
r := csv.NewReader(f)
|
||||
r.FieldsPerRecord = -1 // statements are ragged more often than not
|
||||
r.LazyQuotes = true
|
||||
if d := p.cfg.Delimiter; d != "" {
|
||||
runes := []rune(d)
|
||||
if len(runes) != 1 {
|
||||
return nil, fmt.Errorf("delimiter %q must be a single character", d)
|
||||
}
|
||||
r.Comma = runes[0]
|
||||
}
|
||||
|
||||
var out []RawTxn
|
||||
for row := 0; ; row++ {
|
||||
rec, err := r.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: row %d: %w", path, row+1, err)
|
||||
}
|
||||
if row < p.cfg.SkipRows {
|
||||
continue
|
||||
}
|
||||
if isBlank(rec) {
|
||||
continue
|
||||
}
|
||||
txn, err := p.row(rec)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: row %d: %w", path, row+1, err)
|
||||
}
|
||||
out = append(out, txn)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (p *csvParser) row(rec []string) (RawTxn, error) {
|
||||
var t RawTxn
|
||||
|
||||
raw, err := field(rec, p.cfg.Date.Col)
|
||||
if err != nil {
|
||||
return t, fmt.Errorf("date column: %w", err)
|
||||
}
|
||||
d, err := time.Parse(p.cfg.Date.Layout, strings.TrimSpace(raw))
|
||||
if err != nil {
|
||||
return t, fmt.Errorf("date %q does not match layout %q", raw, p.cfg.Date.Layout)
|
||||
}
|
||||
t.Date = d.Format("2006-01-02")
|
||||
|
||||
desc, err := field(rec, p.cfg.Description.Col)
|
||||
if err != nil {
|
||||
return t, fmt.Errorf("description column: %w", err)
|
||||
}
|
||||
t.Description = strings.TrimSpace(desc)
|
||||
|
||||
switch {
|
||||
case p.cfg.Amount != nil:
|
||||
raw, err := field(rec, p.cfg.Amount.Col)
|
||||
if err != nil {
|
||||
return t, fmt.Errorf("amount column: %w", err)
|
||||
}
|
||||
v, err := ParseAmount(raw, p.cfg.Amount.Decimal, p.cfg.Amount.Thousands, p.digits)
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
t.AmountMinor = v
|
||||
default:
|
||||
// Debit/credit pair: exactly one of the two carries a value, and both
|
||||
// are written as positive numbers.
|
||||
debit, err := p.optional(rec, p.cfg.Debit)
|
||||
if err != nil {
|
||||
return t, fmt.Errorf("debit column: %w", err)
|
||||
}
|
||||
credit, err := p.optional(rec, p.cfg.Credit)
|
||||
if err != nil {
|
||||
return t, fmt.Errorf("credit column: %w", err)
|
||||
}
|
||||
if debit != 0 && credit != 0 {
|
||||
return t, fmt.Errorf("both debit (%d) and credit (%d) are set", debit, credit)
|
||||
}
|
||||
t.AmountMinor = credit - abs(debit)
|
||||
}
|
||||
|
||||
if p.cfg.Invert {
|
||||
t.AmountMinor = -t.AmountMinor
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// optional parses a column that may legitimately be blank, as debit/credit
|
||||
// columns always are for half the rows.
|
||||
func (p *csvParser) optional(rec []string, c *config.Column) (int64, error) {
|
||||
if c == nil {
|
||||
return 0, nil
|
||||
}
|
||||
raw, err := field(rec, c.Col)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return 0, nil
|
||||
}
|
||||
return ParseAmount(raw, c.Decimal, c.Thousands, p.digits)
|
||||
}
|
||||
|
||||
func field(rec []string, i int) (string, error) {
|
||||
if i < 0 || i >= len(rec) {
|
||||
return "", fmt.Errorf("index %d out of range, row has %d columns", i, len(rec))
|
||||
}
|
||||
return rec[i], nil
|
||||
}
|
||||
|
||||
func isBlank(rec []string) bool {
|
||||
for _, f := range rec {
|
||||
if strings.TrimSpace(f) != "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func abs(v int64) int64 {
|
||||
if v < 0 {
|
||||
return -v
|
||||
}
|
||||
return v
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.petrovv.com/nikola/money/internal/config"
|
||||
)
|
||||
|
||||
func writeFile(t *testing.T, name, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), name)
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TestCSVSignedAmountColumn(t *testing.T) {
|
||||
path := writeFile(t, "st.csv", `Date,Ref,Description,Amount
|
||||
02.01.2026,X1,LIDL SOFIA 1234,"-45,20"
|
||||
03.01.2026,X2,ACME PAYROLL,"1.500,00"
|
||||
|
||||
04.01.2026,X3,COFFEE,"-3,50"
|
||||
`)
|
||||
acc := &config.Account{Slug: "checking", Currency: "EUR", Parser: "csv", CSV: &config.CSVConfig{
|
||||
SkipRows: 1,
|
||||
Date: config.Column{Col: 0, Layout: "02.01.2006"},
|
||||
Description: config.Column{Col: 2},
|
||||
Amount: &config.Column{Col: 3, Decimal: ",", Thousands: "."},
|
||||
}}
|
||||
p, err := For(acc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := p.Parse(path, acc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []RawTxn{
|
||||
{Date: "2026-01-02", Description: "LIDL SOFIA 1234", AmountMinor: -4520},
|
||||
{Date: "2026-01-03", Description: "ACME PAYROLL", AmountMinor: 150000},
|
||||
{Date: "2026-01-04", Description: "COFFEE", AmountMinor: -350},
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("got %d txns, want %d: %+v", len(got), len(want), got)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Errorf("txn %d = %+v, want %+v", i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSVDebitCreditPair(t *testing.T) {
|
||||
path := writeFile(t, "st.csv", `2026-02-01;RENT;800.00;
|
||||
2026-02-05;SALARY;;2500.00
|
||||
`)
|
||||
acc := &config.Account{Slug: "checking", Currency: "EUR", Parser: "csv", CSV: &config.CSVConfig{
|
||||
Delimiter: ";",
|
||||
Date: config.Column{Col: 0, Layout: "2006-01-02"},
|
||||
Description: config.Column{Col: 1},
|
||||
Debit: &config.Column{Col: 2},
|
||||
Credit: &config.Column{Col: 3},
|
||||
}}
|
||||
p, err := For(acc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := p.Parse(path, acc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("got %d txns, want 2: %+v", len(got), got)
|
||||
}
|
||||
if got[0].AmountMinor != -80000 {
|
||||
t.Errorf("debit row = %d, want -80000", got[0].AmountMinor)
|
||||
}
|
||||
if got[1].AmountMinor != 250000 {
|
||||
t.Errorf("credit row = %d, want 250000", got[1].AmountMinor)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSVConfigErrors(t *testing.T) {
|
||||
cases := map[string]*config.Account{
|
||||
"no csv section": {Slug: "a", Parser: "csv"},
|
||||
"no amount columns": {Slug: "a", Parser: "csv", CSV: &config.CSVConfig{
|
||||
Date: config.Column{Layout: "2006-01-02"},
|
||||
}},
|
||||
"amount and debit together": {Slug: "a", Parser: "csv", CSV: &config.CSVConfig{
|
||||
Date: config.Column{Layout: "2006-01-02"},
|
||||
Amount: &config.Column{Col: 1},
|
||||
Debit: &config.Column{Col: 2},
|
||||
}},
|
||||
"no date layout": {Slug: "a", Parser: "csv", CSV: &config.CSVConfig{
|
||||
Amount: &config.Column{Col: 1},
|
||||
}},
|
||||
}
|
||||
for name, acc := range cases {
|
||||
if _, err := For(acc); err == nil {
|
||||
t.Errorf("%s: expected an error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownParser(t *testing.T) {
|
||||
if _, err := For(&config.Account{Slug: "a", Parser: "nope"}); err == nil {
|
||||
t.Error("expected an error for an unregistered parser")
|
||||
}
|
||||
}
|
||||
+19
-12
@@ -18,8 +18,8 @@ func init() {
|
||||
// nlbParser reads an NLB izpisek PDF.
|
||||
//
|
||||
// A transaction starts on a line beginning with a dd.mm.yy date and ends with
|
||||
// the signed amount and the running balance. Long descriptions and the
|
||||
// counterparty account wrap onto indented continuation lines below.
|
||||
// the signed amount and the running balance. Long descriptions and the other
|
||||
// side's account number wrap onto indented continuation lines below.
|
||||
type nlbParser struct {
|
||||
digits int
|
||||
}
|
||||
@@ -37,7 +37,9 @@ var (
|
||||
`(?P<balance>[-\x{2212}]?[\d.,]+[-\x{2212}]?)\s*$`)
|
||||
// Columns inside a line are separated by four or more spaces.
|
||||
nlbFieldSplit = regexp.MustCompile(`\s{4,}`)
|
||||
// A Slovenian IBAN, which is the counterparty account when present.
|
||||
// A Slovenian IBAN, which NLB prints in a column of its own. It is split
|
||||
// out only so that its wrapped fragments can be rejoined; the result goes
|
||||
// onto the end of the description, where every other parser keeps it.
|
||||
nlbAccount = regexp.MustCompile(`^SI\d{2}(?:\s?\d{4}){3}\s?\d{3}$`)
|
||||
)
|
||||
|
||||
@@ -63,9 +65,12 @@ func (p *nlbParser) Parse(path string, acc *config.Account) ([]RawTxn, error) {
|
||||
// be tested against captured pdftotext output.
|
||||
func parseNLBText(text string, digits int) ([]RawTxn, error) {
|
||||
var (
|
||||
txns []RawTxn
|
||||
accts []string // counterparty per transaction, built up alongside
|
||||
descAt int // description column of the last transaction line
|
||||
txns []RawTxn
|
||||
// The account column per transaction, built up alongside because it
|
||||
// wraps in fragments of its own and has to be rejoined before it can
|
||||
// be appended to the description.
|
||||
trailing []string
|
||||
descAt int // description column of the last transaction line
|
||||
)
|
||||
|
||||
for _, page := range pages(text) {
|
||||
@@ -84,7 +89,7 @@ func parseNLBText(text string, digits int) ([]RawTxn, error) {
|
||||
return nil, fmt.Errorf("line %d: %w", i+1, err)
|
||||
}
|
||||
txns = append(txns, txn)
|
||||
accts = append(accts, account)
|
||||
trailing = append(trailing, account)
|
||||
descAt = col
|
||||
continue
|
||||
}
|
||||
@@ -104,20 +109,22 @@ func parseNLBText(text string, digits int) ([]RawTxn, error) {
|
||||
last := len(txns) - 1
|
||||
txns[last].Description += " " + parts[0]
|
||||
if len(parts) > 1 {
|
||||
accts[last] = strings.TrimSpace(accts[last] + " " + parts[1])
|
||||
trailing[last] = strings.TrimSpace(trailing[last] + " " + parts[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The account column goes last rather than in the position it occupied on
|
||||
// the page, so that an IBAN wrapped over several lines stays contiguous
|
||||
// and a glob can match it.
|
||||
for i := range txns {
|
||||
txns[i].Counterparty = accts[i]
|
||||
txns[i].Description = strings.Join(strings.Fields(txns[i].Description), " ")
|
||||
txns[i].Description = strings.Join(strings.Fields(txns[i].Description+" "+trailing[i]), " ")
|
||||
}
|
||||
return txns, nil
|
||||
}
|
||||
|
||||
// parseNLBLine parses one transaction line, returning the transaction, the
|
||||
// counterparty account found among its columns, and the column at which the
|
||||
// account number found among its columns, and the column at which the
|
||||
// description starts, which is where its wrapped lines will sit.
|
||||
func parseNLBLine(line string, digits int) (RawTxn, string, int, error) {
|
||||
trimmed := strings.TrimRight(line, " \t\r")
|
||||
@@ -154,7 +161,7 @@ func parseNLBLine(line string, digits int) (RawTxn, string, int, error) {
|
||||
return RawTxn{}, "", 0, fmt.Errorf("balance %q: %w", balance, err)
|
||||
}
|
||||
|
||||
// The middle holds the description and, sometimes, the counterparty IBAN.
|
||||
// The middle holds the description and, sometimes, the IBAN.
|
||||
var (
|
||||
account string
|
||||
desc []string
|
||||
|
||||
@@ -4,7 +4,7 @@ import "testing"
|
||||
|
||||
// A page as pdftotext -layout renders it: a header, transaction lines ending
|
||||
// in amount and balance, and indented continuation lines carrying wrapped
|
||||
// descriptions and the counterparty IBAN.
|
||||
// descriptions and the other side's IBAN.
|
||||
// The description column sits at 15, which is what the script's
|
||||
// CONTINUATION_INDENT tells us about the real layout.
|
||||
const nlbPage = ` NLB d.d.
|
||||
@@ -35,14 +35,12 @@ func TestParseNLBText(t *testing.T) {
|
||||
if first.Date != "2026-01-02" {
|
||||
t.Errorf("date = %q, want 2026-01-02", first.Date)
|
||||
}
|
||||
// The continuation line is appended to the description.
|
||||
if first.Description != "PLACILO S KARTICO LIDL SOFIA 4412" {
|
||||
// The continuation line is appended to the description, and the IBAN in
|
||||
// its second column goes last so that it stays contiguous however many
|
||||
// lines it wrapped over.
|
||||
if first.Description != "PLACILO S KARTICO LIDL SOFIA 4412 SI56 1234 5678 9012 345" {
|
||||
t.Errorf("description = %q", first.Description)
|
||||
}
|
||||
// ...and the IBAN in its second column becomes the counterparty.
|
||||
if first.Counterparty != "SI56 1234 5678 9012 345" {
|
||||
t.Errorf("counterparty = %q", first.Counterparty)
|
||||
}
|
||||
if first.AmountMinor != -4520 {
|
||||
t.Errorf("amount = %d, want -4520", first.AmountMinor)
|
||||
}
|
||||
@@ -57,9 +55,9 @@ func TestParseNLBText(t *testing.T) {
|
||||
t.Errorf("description = %q", txns[1].Description)
|
||||
}
|
||||
|
||||
// A transaction with no continuation line keeps an empty counterparty.
|
||||
if txns[2].Counterparty != "" {
|
||||
t.Errorf("counterparty = %q, want empty", txns[2].Counterparty)
|
||||
// A transaction with no continuation line gets nothing appended.
|
||||
if txns[2].Description != "PRENOS NA VARCEVALNI" {
|
||||
t.Errorf("description = %q, want no trailing account number", txns[2].Description)
|
||||
}
|
||||
|
||||
// A trailing minus marks a negative balance.
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
// Package parser turns a statement file into raw transactions.
|
||||
//
|
||||
// Every bank needs its own extraction logic, so parsers are looked up by name
|
||||
// from a registry. Two are built in: "csv" for delimited exports with
|
||||
// configurable columns, and "cmd" for shelling out to an external extractor
|
||||
// (which is how the existing Python scripts are used until they are ported).
|
||||
// Ported extractors register themselves here and become usable by putting
|
||||
// their name in an account.toml.
|
||||
// from a registry that each one joins from an init. A parser becomes usable by
|
||||
// putting its name in an account.toml; there is no generic column-mapped
|
||||
// parser, because a statement layout is better described in Go, where it can
|
||||
// be tested, than in a table of column indexes.
|
||||
package parser
|
||||
|
||||
import (
|
||||
@@ -23,9 +22,6 @@ type RawTxn struct {
|
||||
Description string
|
||||
AmountMinor int64 // signed; negative is an outflow
|
||||
|
||||
// Counterparty is the other side's account number, where the statement
|
||||
// gives one. Optional.
|
||||
Counterparty string
|
||||
// Type is the bank's own classification of the transaction. Optional.
|
||||
Type string
|
||||
// BalanceMinor is the running balance after this transaction, when the
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -36,9 +35,6 @@ type revolutParser struct {
|
||||
warnings []string
|
||||
}
|
||||
|
||||
// revolutIBAN finds a counterparty account inside a description.
|
||||
var revolutIBAN = regexp.MustCompile(`\b[A-Z]{2}\d{2}[A-Z0-9]{11,30}\b`)
|
||||
|
||||
// Columns the parser needs; a missing one is a hard error rather than a
|
||||
// silently empty field.
|
||||
var revolutColumns = []string{
|
||||
@@ -164,18 +160,26 @@ func (p *revolutParser) row(get func(string) string, digits int) (RawTxn, error)
|
||||
desc += fmt.Sprintf(" (fee %s)", model.FormatMinor(fee, digits))
|
||||
}
|
||||
|
||||
counterparty := revolutIBAN.FindString(desc)
|
||||
|
||||
return RawTxn{
|
||||
Date: d.Format("2006-01-02"),
|
||||
Description: desc,
|
||||
AmountMinor: amount - fee,
|
||||
Counterparty: counterparty,
|
||||
Type: strings.TrimSpace(get("Type")),
|
||||
BalanceMinor: &balance,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// isBlank reports whether a CSV record holds nothing but whitespace, which is
|
||||
// what a trailing newline in the export reads as.
|
||||
func isBlank(rec []string) bool {
|
||||
for _, f := range rec {
|
||||
if strings.TrimSpace(f) != "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// headerIndex maps a Revolut column name to its position.
|
||||
type headerIndex map[string]int
|
||||
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.petrovv.com/nikola/money/internal/config"
|
||||
)
|
||||
|
||||
// writeFile drops a statement in a temporary directory and returns its path.
|
||||
func writeFile(t *testing.T, name, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), name)
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// A Revolut export: out of date order, mixed currencies, a pending row, a row
|
||||
// with a fee, and a transfer carrying an IBAN.
|
||||
const revolutCSV = `Type,Product,Started Date,Completed Date,Description,Amount,Fee,Currency,State,Balance
|
||||
@@ -60,9 +72,6 @@ func TestRevolutParse(t *testing.T) {
|
||||
if !strings.Contains(transfer.Description, "(fee 0.35)") {
|
||||
t.Errorf("description = %q, want a fee note", transfer.Description)
|
||||
}
|
||||
if transfer.Counterparty != "SI56123456789012345" {
|
||||
t.Errorf("counterparty = %q, want the IBAN from the description", transfer.Counterparty)
|
||||
}
|
||||
if transfer.BalanceMinor == nil || *transfer.BalanceMinor != 73441 {
|
||||
t.Errorf("balance = %v, want 73441", transfer.BalanceMinor)
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ var (
|
||||
trAmount = regexp.MustCompile(`[-\x{2212}]?€\s?[-\x{2212}]?[\d,]+\.\d{2}`)
|
||||
trToken = regexp.MustCompile(`\S+`)
|
||||
trFullDate = regexp.MustCompile(`^\d{2} [A-Z][a-z]{2} \d{4}$`)
|
||||
trIBAN = regexp.MustCompile(`\b[A-Z]{2}\d{2}[A-Z0-9]{11,30}\b`)
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -197,7 +196,6 @@ func parseTradeRepublicBlock(lines []string, cols map[string]int, digits int) (R
|
||||
Date: d.Format("2006-01-02"),
|
||||
Description: desc,
|
||||
AmountMinor: amount,
|
||||
Counterparty: trIBAN.FindString(desc),
|
||||
Type: strings.Join(words["TYPE"], " "),
|
||||
BalanceMinor: amounts["BALANCE"],
|
||||
}, true, nil
|
||||
|
||||
@@ -120,9 +120,6 @@ func TestParseTradeRepublicText(t *testing.T) {
|
||||
if transfer.Description != "Standing order to sav-ings SI56123456789012345" {
|
||||
t.Errorf("description = %q, want the wrapped fragment joined without a space", transfer.Description)
|
||||
}
|
||||
if transfer.Counterparty != "SI56123456789012345" {
|
||||
t.Errorf("counterparty = %q", transfer.Counterparty)
|
||||
}
|
||||
if transfer.AmountMinor != -50000 {
|
||||
t.Errorf("amount = %d, want -50000", transfer.AmountMinor)
|
||||
}
|
||||
|
||||
@@ -37,16 +37,15 @@ func (e *Engine) Match(accountSlug string, t model.Transaction) *config.Rule {
|
||||
|
||||
// MatchIndex returns the position of the first rule matching a transaction, or
|
||||
// -1 if none does. Every pattern a rule sets must match: a rule with both
|
||||
// match and counterparty is an "and", not an "or".
|
||||
// match and type is an "and", not an "or".
|
||||
//
|
||||
// The position matters as well as the rule: because the first match wins, a
|
||||
// rule that is fully shadowed by an earlier one never applies to anything, and
|
||||
// only the index reveals that.
|
||||
func (e *Engine) MatchIndex(accountSlug string, t model.Transaction) int {
|
||||
var (
|
||||
description = model.NormalizeDescription(t.Description)
|
||||
counterparty = model.NormalizeDescription(t.Counterparty)
|
||||
kind = model.NormalizeDescription(t.Type)
|
||||
description = model.NormalizeDescription(t.Description)
|
||||
kind = model.NormalizeDescription(t.Type)
|
||||
)
|
||||
for i := range e.rules {
|
||||
r := &e.rules[i]
|
||||
@@ -56,9 +55,6 @@ func (e *Engine) MatchIndex(accountSlug string, t model.Transaction) int {
|
||||
if r.Match != "" && !glob.Match(r.Match, description) {
|
||||
continue
|
||||
}
|
||||
if r.Counterparty != "" && !glob.Match(r.Counterparty, counterparty) {
|
||||
continue
|
||||
}
|
||||
if r.Type != "" && !glob.Match(r.Type, kind) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -201,25 +201,16 @@ func TestAccountScopedRule(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Movements between the user's own accounts are often only identifiable by
|
||||
// the counterparty IBAN, so rules can match on it.
|
||||
func TestCounterpartyAndTypeRules(t *testing.T) {
|
||||
// Besides the description, a rule can match the bank's own classification of
|
||||
// the transaction, on its own or alongside a description glob.
|
||||
func TestTypeRules(t *testing.T) {
|
||||
engine := New(&config.Rules{Rule: []config.Rule{
|
||||
{Counterparty: "SI56123456789012345", Tag: "transfer", Transfer: true},
|
||||
{Type: "CARD_PAYMENT", Match: "*LIDL*", Tag: "groceries"},
|
||||
{Type: "ATM", Tag: "cash"},
|
||||
}})
|
||||
|
||||
// Counterparty alone is enough, whatever the description says.
|
||||
tag, transfer := engine.ApplyTxn("checking", model.Transaction{
|
||||
Description: "Standing order", Counterparty: "SI56123456789012345",
|
||||
})
|
||||
if tag != "transfer" || !transfer {
|
||||
t.Errorf("tag=%q transfer=%v, want transfer/true", tag, transfer)
|
||||
}
|
||||
|
||||
// A rule setting several patterns requires all of them to match.
|
||||
tag, _ = engine.ApplyTxn("checking", model.Transaction{
|
||||
tag, _ := engine.ApplyTxn("checking", model.Transaction{
|
||||
Description: "LIDL SOFIA", Type: "CARD_PAYMENT",
|
||||
})
|
||||
if tag != "groceries" {
|
||||
|
||||
+24
-11
@@ -1,6 +1,6 @@
|
||||
// Package store is the SQLite index over the statements. It is entirely
|
||||
// rebuildable: delete .money/index.db and re-import to get it back, except for
|
||||
// manual tags and manual transfer overrides, which live only here.
|
||||
// rebuildable: delete index.db and re-import to get it back, except for manual
|
||||
// tags and manual transfer overrides, which live only here.
|
||||
package store
|
||||
|
||||
import (
|
||||
@@ -48,7 +48,6 @@ CREATE TABLE IF NOT EXISTS transactions (
|
||||
date TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
amount_minor INTEGER NOT NULL,
|
||||
counterparty TEXT NOT NULL DEFAULT '',
|
||||
type TEXT NOT NULL DEFAULT '',
|
||||
balance_minor INTEGER,
|
||||
rule_tag TEXT,
|
||||
@@ -65,12 +64,17 @@ CREATE INDEX IF NOT EXISTS idx_txn_account ON transactions(account_id);
|
||||
// migrations bring an index created by an older build up to date. SQLite
|
||||
// errors on a duplicate column, which is how we detect "already applied".
|
||||
var migrations = []string{
|
||||
`ALTER TABLE transactions ADD COLUMN counterparty TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE transactions ADD COLUMN type TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE transactions ADD COLUMN balance_minor INTEGER`,
|
||||
}
|
||||
|
||||
// migrate applies any column that this index is missing.
|
||||
// dropped are columns an older build created that this one no longer reads.
|
||||
// Nothing indexes or constrains them, so they can simply go; leaving them
|
||||
// would keep a NOT NULL column alive that no INSERT here ever names.
|
||||
var dropped = []string{"counterparty"}
|
||||
|
||||
// migrate brings an index created by an older build up to date, adding the
|
||||
// columns it lacks and removing the ones it should no longer have.
|
||||
func migrate(db *sql.DB) error {
|
||||
have, err := columns(db, "transactions")
|
||||
if err != nil {
|
||||
@@ -85,6 +89,15 @@ func migrate(db *sql.DB) error {
|
||||
return fmt.Errorf("migrate (%s): %w", stmt, err)
|
||||
}
|
||||
}
|
||||
for _, name := range dropped {
|
||||
if !have[name] {
|
||||
continue
|
||||
}
|
||||
stmt := fmt.Sprintf(`ALTER TABLE transactions DROP COLUMN %s`, name)
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
return fmt.Errorf("migrate (%s): %w", stmt, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -118,7 +131,7 @@ func addedColumn(stmt string) string {
|
||||
// Open opens (creating if needed) the index at path.
|
||||
func Open(path string) (*DB, error) {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return nil, fmt.Errorf("create state dir: %w", err)
|
||||
return nil, fmt.Errorf("create index dir: %w", err)
|
||||
}
|
||||
sqlDB, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
@@ -224,11 +237,11 @@ func (d *DB) InsertTransaction(t model.Transaction) (bool, error) {
|
||||
res, err := d.sql.Exec(`
|
||||
INSERT INTO transactions
|
||||
(account_id, source_file_id, fingerprint, date, description, amount_minor,
|
||||
counterparty, type, balance_minor, rule_tag, rule_transfer)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULLIF(?, ''), ?)
|
||||
type, balance_minor, rule_tag, rule_transfer)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, NULLIF(?, ''), ?)
|
||||
ON CONFLICT(account_id, fingerprint) DO NOTHING`,
|
||||
t.AccountID, t.SourceFileID, t.Fingerprint, t.Date, t.Description,
|
||||
t.AmountMinor, t.Counterparty, t.Type, balance, t.RuleTag, boolToInt(t.RuleTransfer))
|
||||
t.AmountMinor, t.Type, balance, t.RuleTag, boolToInt(t.RuleTransfer))
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("insert transaction: %w", err)
|
||||
}
|
||||
@@ -253,7 +266,7 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
q := `
|
||||
SELECT t.id, t.account_id, a.slug, a.currency, a.minor_digits,
|
||||
t.fingerprint, t.date, t.description, t.amount_minor,
|
||||
COALESCE(s.path, ''), t.counterparty, t.type, t.balance_minor,
|
||||
COALESCE(s.path, ''), t.type, t.balance_minor,
|
||||
COALESCE(t.rule_tag, ''), COALESCE(t.manual_tag, ''),
|
||||
t.rule_transfer, t.manual_transfer
|
||||
FROM transactions t
|
||||
@@ -293,7 +306,7 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
)
|
||||
if err := rows.Scan(&t.ID, &t.AccountID, &t.AccountSlug, &t.Currency, &t.MinorDigits,
|
||||
&t.Fingerprint, &t.Date, &t.Description, &t.AmountMinor, &t.SourcePath,
|
||||
&t.Counterparty, &t.Type, &balance,
|
||||
&t.Type, &balance,
|
||||
&t.RuleTag, &t.ManualTag, &ruleTransfer, &manualTransfer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+2
-5
@@ -599,16 +599,13 @@ func (m *Model) reloadRuleList() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// rulePattern renders whichever patterns a rule sets, labelled so a
|
||||
// counterparty or type rule is not mistaken for a description one.
|
||||
// rulePattern renders whichever patterns a rule sets, labelled so a type rule
|
||||
// is not mistaken for a description one.
|
||||
func rulePattern(r config.Rule) string {
|
||||
var parts []string
|
||||
if r.Match != "" {
|
||||
parts = append(parts, r.Match)
|
||||
}
|
||||
if r.Counterparty != "" {
|
||||
parts = append(parts, "counterparty:"+r.Counterparty)
|
||||
}
|
||||
if r.Type != "" {
|
||||
parts = append(parts, "type:"+r.Type)
|
||||
}
|
||||
|
||||
@@ -388,7 +388,7 @@ func newRuleModel(t *testing.T) (*Model, *store.DB, string) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
|
||||
db, err := store.Open(filepath.Join(root, ".money", "index.db"))
|
||||
db, err := store.Open(config.IndexPath(root))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -437,8 +437,8 @@ func newRuleModel(t *testing.T) (*Model, *store.DB, string) {
|
||||
}
|
||||
|
||||
m := New(root, db, []*config.Account{
|
||||
{Slug: "checking", Currency: "EUR", Parser: "csv"},
|
||||
{Slug: "savings", Currency: "EUR", Parser: "csv"},
|
||||
{Slug: "checking", Currency: "EUR", Parser: "revolut"},
|
||||
{Slug: "savings", Currency: "EUR", Parser: "revolut"},
|
||||
}, rules.New(&config.Rules{}))
|
||||
if err := m.reload(); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -1125,8 +1125,8 @@ func newEmptyModel(t *testing.T, accounts []*config.Account) *Model {
|
||||
// disk, but nothing is registered in the index until an import runs.
|
||||
func TestEmptyAccountsWithConfiguredFolders(t *testing.T) {
|
||||
m := newEmptyModel(t, []*config.Account{
|
||||
{Slug: "checking", Currency: "EUR", Parser: "csv"},
|
||||
{Slug: "savings", Currency: "EUR", Parser: "csv"},
|
||||
{Slug: "checking", Currency: "EUR", Parser: "revolut"},
|
||||
{Slug: "savings", Currency: "EUR", Parser: "revolut"},
|
||||
})
|
||||
|
||||
view := m.View()
|
||||
|
||||
Reference in New Issue
Block a user