ledger.Transaction.Payee()
All checks were successful
cicd / ci (push) Successful in 1m21s

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

View File

@@ -39,6 +39,35 @@ func (deltas Deltas) Transactions() Transactions {
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 {
date string
description string