diff --git a/ledger/delta.go b/ledger/delta.go index 1ea0252..3793753 100644 --- a/ledger/delta.go +++ b/ledger/delta.go @@ -10,16 +10,16 @@ const ( type Delta struct { Date string - Account string + Name string Value float64 Currency Currency Description string } -func newDelta(d, desc, acc string, v float64, c string) Delta { +func newDelta(d, desc, name string, v float64, c string) Delta { return Delta{ Date: d, - Account: acc, + Name: name, Value: v, Currency: Currency(c), Description: desc, @@ -29,12 +29,12 @@ func newDelta(d, desc, acc string, v float64, c string) Delta { func (delta Delta) Plus(other Delta) Delta { return Delta{ Date: other.Date, - Account: delta.Account, + Name: delta.Name, Value: delta.Value + other.Value, Currency: other.Currency, } } func (delta Delta) Debug() string { - return fmt.Sprintf("{@%s %s:\"%s\" %.2f %s}", delta.Date, delta.Account, delta.Description, delta.Value, delta.Currency) + return fmt.Sprintf("{@%s %s:\"%s\" %.2f %s}", delta.Date, delta.Name, delta.Description, delta.Value, delta.Currency) } diff --git a/ledger/delta_test.go b/ledger/delta_test.go index 03f0a44..42d441d 100644 --- a/ledger/delta_test.go +++ b/ledger/delta_test.go @@ -11,8 +11,8 @@ func TestDelta(t *testing.T) { if delta.Date != d { t.Error(delta.Date) } - if delta.Account != "name" { - t.Error(delta.Account) + if delta.Name != "name" { + t.Error(delta.Name) } if delta.Value != 34.56 { t.Error(delta.Value) @@ -29,8 +29,8 @@ func TestDelta(t *testing.T) { if combined.Date != d2 { t.Error(combined.Date) } - if combined.Account != "name" { - t.Error(combined.Account) + if combined.Name != "name" { + t.Error(combined.Name) } if combined.Value != 45.67 { t.Error(combined.Value) diff --git a/ledger/deltas.go b/ledger/deltas.go index 61d1848..fe43aed 100644 --- a/ledger/deltas.go +++ b/ledger/deltas.go @@ -15,13 +15,13 @@ func (deltas Deltas) Like(like ...Like) Deltas { func (deltas Deltas) Balances() (map[string]map[Currency]float64, error) { result := make(map[string]map[Currency]float64) for _, delta := range deltas { - if _, ok := result[delta.Account]; !ok { - result[delta.Account] = make(map[Currency]float64) + if _, ok := result[delta.Name]; !ok { + result[delta.Name] = make(map[Currency]float64) } - if _, ok := result[delta.Account][delta.Currency]; !ok { - result[delta.Account][delta.Currency] = 0 + if _, ok := result[delta.Name][delta.Currency]; !ok { + result[delta.Name][delta.Currency] = 0 } - result[delta.Account][delta.Currency] += delta.Value + result[delta.Name][delta.Currency] += delta.Value } return result, nil diff --git a/ledger/file.go b/ledger/file.go index 0c0a4c8..edb40e4 100644 --- a/ledger/file.go +++ b/ledger/file.go @@ -16,14 +16,14 @@ func (files Files) Deltas(like ...Like) (Deltas, error) { result := make(Deltas, 0, len(transactions)*2) for _, transaction := range transactions { sums := map[string]float64{} - for _, acc := range transaction.recipients { - sums[acc.currency] += acc.value + for _, recipient := range transaction.recipients { + sums[recipient.currency] += recipient.value delta := newDelta( transaction.date, transaction.description, - acc.name, - acc.value, - acc.currency, + recipient.name, + recipient.value, + recipient.currency, ) result = append(result, delta) } diff --git a/ledger/file_test.go b/ledger/file_test.go index 3fdb432..7348b7d 100644 --- a/ledger/file_test.go +++ b/ledger/file_test.go @@ -39,7 +39,7 @@ func TestFileTestdata(t *testing.T) { }) t.Run("balances like", func(t *testing.T) { - balances, err := deltas.Like(LikeAcc(`^AssetAccount:`)).Balances() + balances, err := deltas.Like(LikeName(`^AssetAccount:`)).Balances() if err != nil { t.Fatal(err) } @@ -85,7 +85,7 @@ func TestFileTestdata(t *testing.T) { }) t.Run("balances like", func(t *testing.T) { - balances, err := deltas.Like(LikeAcc(`AssetAccount:Cash:Fidelity76`)).Balances() + balances, err := deltas.Like(LikeName(`AssetAccount:Cash:Fidelity76`)).Balances() if err != nil { t.Fatal(err) } @@ -102,28 +102,28 @@ func TestFileDeltas(t *testing.T) { happy := []Delta{ { Date: "2022-12-12", - Account: "AssetAccount:Cash:Fidelity76", + Name: "AssetAccount:Cash:Fidelity76", Value: -97.92, Currency: USD, Description: "Electricity / Power Bill TG2PJ-2PLP5", }, { Date: "2022-12-12", - Account: "Withdrawal:0:SharedHome:DominionEnergy", + Name: "Withdrawal:0:SharedHome:DominionEnergy", Value: 97.92, Currency: USD, Description: "Electricity / Power Bill TG2PJ-2PLP5", }, { Date: "2022-12-12", - Account: "AssetAccount:Cash:Fidelity76", + Name: "AssetAccount:Cash:Fidelity76", Value: -1.00, Currency: USD, Description: "Test pay chase TG32S-BT2FF", }, { Date: "2022-12-12", - Account: "Debts:Credit:ChaseFreedomUltdVisa", + Name: "Debts:Credit:ChaseFreedomUltdVisa", Value: 1.00, Currency: USD, Description: "Test pay chase TG32S-BT2FF", diff --git a/ledger/like.go b/ledger/like.go index d069e02..de3c31b 100644 --- a/ledger/like.go +++ b/ledger/like.go @@ -18,9 +18,9 @@ func LikeAfter(date string) Like { } } -func LikeAcc(pattern string) Like { +func LikeName(pattern string) Like { return func(d Delta) bool { - return like(pattern, d.Account) + return like(pattern, d.Name) } } diff --git a/ledger/like_test.go b/ledger/like_test.go index c1f8f99..7517df6 100644 --- a/ledger/like_test.go +++ b/ledger/like_test.go @@ -2,22 +2,22 @@ package ledger import "testing" -func TestLikeAcc(t *testing.T) { - delta := Delta{Account: "x"} - if got := LikeAcc("^x$")(delta); !got { +func TestLikeName(t *testing.T) { + delta := Delta{Name: "x"} + if got := LikeName("^x$")(delta); !got { t.Error(got) } - if got := LikeAcc("^y$")(delta); got { + if got := LikeName("^y$")(delta); got { t.Error(got) } } func TestLikesAll(t *testing.T) { - delta := Delta{Account: "x"} - if likes := (likes{LikeAcc("^x$")}); !likes.all(delta) { + delta := Delta{Name: "x"} + if likes := (likes{LikeName("^x$")}); !likes.all(delta) { t.Error(likes.all(delta)) } - if likes := (likes{LikeAcc("^y$")}); likes.all(delta) { + if likes := (likes{LikeName("^y$")}); likes.all(delta) { t.Error(likes.all(delta)) } } diff --git a/ledger/todo.yaml b/ledger/todo.yaml index 184e7a9..69b1e59 100755 --- a/ledger/todo.yaml +++ b/ledger/todo.yaml @@ -1,5 +1,7 @@ -todo: -- add like date range -- from account to name everywhere +todo: [] scheduled: [] -done: [] +done: +- todo: add like date range + ts: Tue Oct 24 20:53:30 MDT 2023 +- todo: from account to name everywhere + ts: Tue Oct 24 21:08:58 MDT 2023 diff --git a/ledger/transaction.go b/ledger/transaction.go index 036351d..da6bca3 100644 --- a/ledger/transaction.go +++ b/ledger/transaction.go @@ -106,7 +106,7 @@ func _readTransaction(r *bufio.Reader) (transaction, error) { } for { - name, value, currency, err := readTransactionAccount(r) + name, value, currency, err := readTransactionName(r) if name != "" { if currency == "" { result.payee = name @@ -176,7 +176,7 @@ func _readTransactionLine(r *bufio.Reader) ([]byte, error) { return b2[:n], err } -func readTransactionAccount(r *bufio.Reader) (string, float64, string, error) { +func readTransactionName(r *bufio.Reader) (string, float64, string, error) { line, err := readTransactionLine(r) if err != nil { return "", 0, "", err