Compare commits

..

2 Commits

Author SHA1 Message Date
bel
1d18cb50c5 ledger.Transaction.Payee()
All checks were successful
cicd / ci (push) Successful in 1m21s
2024-07-20 20:10:35 -06:00
bel
bd85dcc86a register cleaner 2024-07-20 19:56:29 -06:00
4 changed files with 50 additions and 8 deletions

View File

@@ -62,13 +62,13 @@
for (var t of transactions) {
result += `<tr>`
result += ` <td style="width: 6em;">${t[0].Date}</td>`
result += ` <td>${t[0].Description}</td>`
result += ` <td><table>`
result += ` <td style="width: 10em;">${t[0].Description}</td>`
result += ` <td><table style="margin: 0;">`
for (var delta of t) {
result += ` <tr>`
result += ` <td>${delta.Name}</td>`
result += ` <td style="text-align: right">${delta.Currency}</td>`
result += ` <td style="text-align: right">${delta.Value}</td>`
result += ` <td><span style="font-variant: petite-caps;">${delta.Name.split(":")[0]}</span><span style="opacity: 0.6;"> :${delta.Name.split(":").slice(1, 100).join(":")}</span></td>`
result += ` <td style="text-align: right; width: 2em;">${delta.Currency}</td>`
result += ` <td style="text-align: right; width: 5em;">${delta.Value}</td>`
result += ` </tr>`
}
result += ` </table></td>`

View File

@@ -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]

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

View File

@@ -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"},