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 {
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)
}

View File

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

View File

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

View File

@ -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)
}

View File

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

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 like(pattern, d.Account)
return like(pattern, d.Name)
}
}

View File

@ -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))
}
}

View File

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

View File

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