main
bel 2023-10-24 21:09:02 -06:00
parent 21fed9437f
commit 5d0e7c9a3d
9 changed files with 42 additions and 40 deletions

View File

@ -10,16 +10,16 @@ const (
type Delta struct { type Delta struct {
Date string Date string
Account string Name string
Value float64 Value float64
Currency Currency Currency Currency
Description string 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{ return Delta{
Date: d, Date: d,
Account: acc, Name: name,
Value: v, Value: v,
Currency: Currency(c), Currency: Currency(c),
Description: desc, Description: desc,
@ -29,12 +29,12 @@ func newDelta(d, desc, acc string, v float64, c string) Delta {
func (delta Delta) Plus(other Delta) Delta { func (delta Delta) Plus(other Delta) Delta {
return Delta{ return Delta{
Date: other.Date, Date: other.Date,
Account: delta.Account, Name: delta.Name,
Value: delta.Value + other.Value, Value: delta.Value + other.Value,
Currency: other.Currency, Currency: other.Currency,
} }
} }
func (delta Delta) Debug() string { 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)
} }

View File

@ -11,8 +11,8 @@ func TestDelta(t *testing.T) {
if delta.Date != d { if delta.Date != d {
t.Error(delta.Date) t.Error(delta.Date)
} }
if delta.Account != "name" { if delta.Name != "name" {
t.Error(delta.Account) t.Error(delta.Name)
} }
if delta.Value != 34.56 { if delta.Value != 34.56 {
t.Error(delta.Value) t.Error(delta.Value)
@ -29,8 +29,8 @@ func TestDelta(t *testing.T) {
if combined.Date != d2 { if combined.Date != d2 {
t.Error(combined.Date) t.Error(combined.Date)
} }
if combined.Account != "name" { if combined.Name != "name" {
t.Error(combined.Account) t.Error(combined.Name)
} }
if combined.Value != 45.67 { if combined.Value != 45.67 {
t.Error(combined.Value) t.Error(combined.Value)

View File

@ -15,13 +15,13 @@ func (deltas Deltas) Like(like ...Like) Deltas {
func (deltas Deltas) Balances() (map[string]map[Currency]float64, error) { func (deltas Deltas) Balances() (map[string]map[Currency]float64, error) {
result := make(map[string]map[Currency]float64) result := make(map[string]map[Currency]float64)
for _, delta := range deltas { for _, delta := range deltas {
if _, ok := result[delta.Account]; !ok { if _, ok := result[delta.Name]; !ok {
result[delta.Account] = make(map[Currency]float64) result[delta.Name] = make(map[Currency]float64)
} }
if _, ok := result[delta.Account][delta.Currency]; !ok { if _, ok := result[delta.Name][delta.Currency]; !ok {
result[delta.Account][delta.Currency] = 0 result[delta.Name][delta.Currency] = 0
} }
result[delta.Account][delta.Currency] += delta.Value result[delta.Name][delta.Currency] += delta.Value
} }
return result, nil return result, nil

View File

@ -16,14 +16,14 @@ func (files Files) Deltas(like ...Like) (Deltas, error) {
result := make(Deltas, 0, len(transactions)*2) result := make(Deltas, 0, len(transactions)*2)
for _, transaction := range transactions { for _, transaction := range transactions {
sums := map[string]float64{} sums := map[string]float64{}
for _, acc := range transaction.recipients { for _, recipient := range transaction.recipients {
sums[acc.currency] += acc.value sums[recipient.currency] += recipient.value
delta := newDelta( delta := newDelta(
transaction.date, transaction.date,
transaction.description, transaction.description,
acc.name, recipient.name,
acc.value, recipient.value,
acc.currency, recipient.currency,
) )
result = append(result, delta) result = append(result, delta)
} }

View File

@ -39,7 +39,7 @@ func TestFileTestdata(t *testing.T) {
}) })
t.Run("balances like", func(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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -85,7 +85,7 @@ func TestFileTestdata(t *testing.T) {
}) })
t.Run("balances like", func(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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -102,28 +102,28 @@ func TestFileDeltas(t *testing.T) {
happy := []Delta{ happy := []Delta{
{ {
Date: "2022-12-12", Date: "2022-12-12",
Account: "AssetAccount:Cash:Fidelity76", Name: "AssetAccount:Cash:Fidelity76",
Value: -97.92, Value: -97.92,
Currency: USD, Currency: USD,
Description: "Electricity / Power Bill TG2PJ-2PLP5", Description: "Electricity / Power Bill TG2PJ-2PLP5",
}, },
{ {
Date: "2022-12-12", Date: "2022-12-12",
Account: "Withdrawal:0:SharedHome:DominionEnergy", Name: "Withdrawal:0:SharedHome:DominionEnergy",
Value: 97.92, Value: 97.92,
Currency: USD, Currency: USD,
Description: "Electricity / Power Bill TG2PJ-2PLP5", Description: "Electricity / Power Bill TG2PJ-2PLP5",
}, },
{ {
Date: "2022-12-12", Date: "2022-12-12",
Account: "AssetAccount:Cash:Fidelity76", Name: "AssetAccount:Cash:Fidelity76",
Value: -1.00, Value: -1.00,
Currency: USD, Currency: USD,
Description: "Test pay chase TG32S-BT2FF", Description: "Test pay chase TG32S-BT2FF",
}, },
{ {
Date: "2022-12-12", Date: "2022-12-12",
Account: "Debts:Credit:ChaseFreedomUltdVisa", Name: "Debts:Credit:ChaseFreedomUltdVisa",
Value: 1.00, Value: 1.00,
Currency: USD, Currency: USD,
Description: "Test pay chase TG32S-BT2FF", Description: "Test pay chase TG32S-BT2FF",

View File

@ -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 func(d Delta) bool {
return like(pattern, d.Account) return like(pattern, d.Name)
} }
} }

View File

@ -2,22 +2,22 @@ package ledger
import "testing" import "testing"
func TestLikeAcc(t *testing.T) { func TestLikeName(t *testing.T) {
delta := Delta{Account: "x"} delta := Delta{Name: "x"}
if got := LikeAcc("^x$")(delta); !got { if got := LikeName("^x$")(delta); !got {
t.Error(got) t.Error(got)
} }
if got := LikeAcc("^y$")(delta); got { if got := LikeName("^y$")(delta); got {
t.Error(got) t.Error(got)
} }
} }
func TestLikesAll(t *testing.T) { func TestLikesAll(t *testing.T) {
delta := Delta{Account: "x"} delta := Delta{Name: "x"}
if likes := (likes{LikeAcc("^x$")}); !likes.all(delta) { if likes := (likes{LikeName("^x$")}); !likes.all(delta) {
t.Error(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)) t.Error(likes.all(delta))
} }
} }

View File

@ -1,5 +1,7 @@
todo: todo: []
- add like date range
- from account to name everywhere
scheduled: [] 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

View File

@ -106,7 +106,7 @@ func _readTransaction(r *bufio.Reader) (transaction, error) {
} }
for { for {
name, value, currency, err := readTransactionAccount(r) name, value, currency, err := readTransactionName(r)
if name != "" { if name != "" {
if currency == "" { if currency == "" {
result.payee = name result.payee = name
@ -176,7 +176,7 @@ func _readTransactionLine(r *bufio.Reader) ([]byte, error) {
return b2[:n], err 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) line, err := readTransactionLine(r)
if err != nil { if err != nil {
return "", 0, "", err return "", 0, "", err