ledger.Transaction.Payee()
cicd / ci (push) Successful in 1m21s Details

main
bel 2024-07-20 20:10:35 -06:00
parent bd85dcc86a
commit 1d18cb50c5
3 changed files with 45 additions and 3 deletions

View File

@ -37,9 +37,9 @@ func (balances Balances) Group(pattern string) Balances {
result := make(Balances) result := make(Balances)
p := regexp.MustCompile(pattern) p := regexp.MustCompile(pattern)
for k, v := range balances { for k, v := range balances {
k2 := p.FindString(k) k2 := k
if k2 == "" { if p.MatchString(k) {
k2 = k k2 = p.FindString(k)
} }
was := result[k2] was := result[k2]

View File

@ -39,6 +39,35 @@ func (deltas Deltas) Transactions() Transactions {
return result return result
} }
func (transaction Transaction) Payee() string {
balances := Deltas(transaction).Balances()
candidates := []string{}
for name, balance := range balances {
everyoneElse := balances.NotLike(`^` + name + `$`).Group(`^`)[""]
matches := true
for currency, value := range balance {
matches = matches && everyoneElse[currency]*-1 == value
}
if matches {
candidates = append(candidates, name)
}
}
slices.Sort(candidates)
if len(candidates) == 0 {
panic(balances)
}
for _, candidate := range candidates {
if strings.HasPrefix(candidate, "Withdrawal") {
return candidate
}
}
return candidates[len(candidates)-1]
}
type transaction struct { type transaction struct {
date string date string
description string description string

View File

@ -8,6 +8,19 @@ import (
"testing" "testing"
) )
func TestTransactionPayee(t *testing.T) {
given := Transaction{
Delta{Name: "x", Transaction: "a", Value: 1},
Delta{Name: "y", Transaction: "a", Value: 2},
Delta{Name: "Withdrawal:z", Transaction: "a", Value: -3},
}
got := given.Payee()
if got != "Withdrawal:z" {
t.Error(got)
}
}
func TestDeltasTransactions(t *testing.T) { func TestDeltasTransactions(t *testing.T) {
given := Deltas{ given := Deltas{
Delta{Date: "2", Name: "x", Transaction: "a"}, Delta{Date: "2", Name: "x", Transaction: "a"},