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>
This commit is contained in:
+68
-11
@@ -1,6 +1,6 @@
|
||||
// Package store is the SQLite index over the statements. It is entirely
|
||||
// rebuildable: delete index.db and re-import to get it back, except for the
|
||||
// tags set by hand, which live only here.
|
||||
// rebuildable: delete index.db and re-import to get it back. Nothing lives
|
||||
// only here.
|
||||
package store
|
||||
|
||||
import (
|
||||
@@ -56,6 +56,16 @@ CREATE TABLE IF NOT EXISTS transactions (
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_txn_date ON transactions(date);
|
||||
CREATE INDEX IF NOT EXISTS idx_txn_account ON transactions(account_id);
|
||||
|
||||
-- One row per matched movement between the user's own accounts, derived from
|
||||
-- the [[transfer]] blocks in rules.toml. A leg belongs to at most one transfer,
|
||||
-- which UNIQUE enforces rather than trusting the pairing to be well behaved.
|
||||
CREATE TABLE IF NOT EXISTS transfers (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
def_index INTEGER NOT NULL,
|
||||
out_txn_id INTEGER NOT NULL UNIQUE REFERENCES transactions(id),
|
||||
in_txn_id INTEGER NOT NULL UNIQUE REFERENCES transactions(id)
|
||||
);
|
||||
`
|
||||
|
||||
// Open opens (creating if needed) the index at path.
|
||||
@@ -186,10 +196,15 @@ func (d *DB) InsertTransaction(t model.Transaction) (bool, error) {
|
||||
// Filter narrows a transaction query.
|
||||
type Filter struct {
|
||||
AccountSlug string
|
||||
Untagged bool // only rows with no effective tag
|
||||
Month string // YYYY-MM
|
||||
Search string // case-insensitive substring of the description
|
||||
Limit int
|
||||
// Untagged selects the rows still waiting for a verdict: no tag, and not a
|
||||
// leg of a matched transfer. A paired leg has been accounted for by the
|
||||
// transfer that claimed it, so listing it as untagged would ask the user to
|
||||
// write a rule for something that is already spoken for and that the report
|
||||
// leaves out anyway.
|
||||
Untagged bool
|
||||
Month string // YYYY-MM
|
||||
Search string // case-insensitive substring of the description
|
||||
Limit int
|
||||
}
|
||||
|
||||
// Transactions returns rows matching f, newest first.
|
||||
@@ -198,10 +213,11 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
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.type, t.balance_minor,
|
||||
COALESCE(t.rule_tag, '')
|
||||
COALESCE(t.rule_tag, ''), x.id
|
||||
FROM transactions t
|
||||
JOIN accounts a ON a.id = t.account_id
|
||||
LEFT JOIN source_files s ON s.id = t.source_file_id
|
||||
LEFT JOIN transfers x ON x.out_txn_id = t.id OR x.in_txn_id = t.id
|
||||
WHERE 1 = 1`
|
||||
var args []any
|
||||
if f.AccountSlug != "" {
|
||||
@@ -209,7 +225,7 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
args = append(args, f.AccountSlug)
|
||||
}
|
||||
if f.Untagged {
|
||||
q += ` AND NULLIF(t.rule_tag, '') IS NULL`
|
||||
q += ` AND NULLIF(t.rule_tag, '') IS NULL AND x.id IS NULL`
|
||||
}
|
||||
if f.Month != "" {
|
||||
q += ` AND substr(t.date, 1, 7) = ?`
|
||||
@@ -229,18 +245,23 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
var out []model.Transaction
|
||||
for rows.Next() {
|
||||
var (
|
||||
t model.Transaction
|
||||
balance sql.NullInt64
|
||||
t model.Transaction
|
||||
balance sql.NullInt64
|
||||
transfer sql.NullInt64
|
||||
)
|
||||
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.Type, &balance, &t.RuleTag); err != nil {
|
||||
&t.Type, &balance, &t.RuleTag, &transfer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if balance.Valid {
|
||||
v := balance.Int64
|
||||
t.BalanceMinor = &v
|
||||
}
|
||||
if transfer.Valid {
|
||||
v := transfer.Int64
|
||||
t.TransferID = &v
|
||||
}
|
||||
if needle != "" && !strings.Contains(model.NormalizeDescription(t.Description), needle) {
|
||||
continue
|
||||
}
|
||||
@@ -282,6 +303,42 @@ func (d *DB) ApplyRuleResults(rs []RuleAssignment) error {
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// TransferLink is one matched pair, as decided by the transfer definitions.
|
||||
type TransferLink struct {
|
||||
DefIndex int
|
||||
OutID int64
|
||||
InID int64
|
||||
}
|
||||
|
||||
// ReplaceTransfers rewrites the whole pairing in one transaction. Like the
|
||||
// tags, it is derived from a file the user edits, so it is replaced wholesale
|
||||
// rather than patched: a definition removed from rules.toml must take its pairs
|
||||
// with it.
|
||||
func (d *DB) ReplaceTransfers(links []TransferLink) error {
|
||||
tx, err := d.sql.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
if _, err := tx.Exec(`DELETE FROM transfers`); err != nil {
|
||||
return fmt.Errorf("clear transfers: %w", err)
|
||||
}
|
||||
stmt, err := tx.Prepare(
|
||||
`INSERT INTO transfers (def_index, out_txn_id, in_txn_id) VALUES (?, ?, ?)`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for _, l := range links {
|
||||
if _, err := stmt.Exec(l.DefIndex, l.OutID, l.InID); err != nil {
|
||||
return fmt.Errorf("link transfer %d→%d: %w", l.OutID, l.InID, err)
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Balance sums every transaction in an account.
|
||||
func (d *DB) Balance(accountID int64) (int64, error) {
|
||||
var v sql.NullInt64
|
||||
|
||||
Reference in New Issue
Block a user