ledger.Transaction.Payee()
cicd / ci (push) Successful in 1m21s
Details
cicd / ci (push) Successful in 1m21s
Details
parent
bd85dcc86a
commit
1d18cb50c5
|
|
@ -37,9 +37,9 @@ func (balances Balances) Group(pattern string) Balances {
|
|||
result := make(Balances)
|
||||
p := regexp.MustCompile(pattern)
|
||||
for k, v := range balances {
|
||||
k2 := p.FindString(k)
|
||||
if k2 == "" {
|
||||
k2 = k
|
||||
k2 := k
|
||||
if p.MatchString(k) {
|
||||
k2 = p.FindString(k)
|
||||
}
|
||||
|
||||
was := result[k2]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -8,6 +8,19 @@ import (
|
|||
"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) {
|
||||
given := Deltas{
|
||||
Delta{Date: "2", Name: "x", Transaction: "a"},
|
||||
|
|
|
|||
Loading…
Reference in New Issue