deltas now accept currency, description, and break payee from all recipients

This commit is contained in:
Bel LaPointe
2023-10-24 05:57:42 -06:00
parent 79effffc47
commit cafb40514c
3 changed files with 54 additions and 21 deletions

View File

@@ -11,18 +11,20 @@ const (
) )
type Delta struct { type Delta struct {
Date time.Time Date time.Time
Account string Account string
Value float64 Value float64
Currency Currency Currency Currency
Description string
} }
func newDelta(d time.Time, a string, v float64) Delta { func newDelta(d time.Time, desc, acc string, v float64, c string) Delta {
return Delta{ return Delta{
Date: d, Date: d,
Account: a, Account: acc,
Value: v, Value: v,
Currency: USD, // TODO Currency: Currency(c),
Description: desc,
} }
} }

View File

@@ -10,7 +10,7 @@ func TestDelta(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
delta := newDelta(d, "name", 34.56) delta := newDelta(d, "", "name", 34.56, "$")
if delta.Date != d { if delta.Date != d {
t.Error(delta.Date) t.Error(delta.Date)
@@ -27,7 +27,7 @@ func TestDelta(t *testing.T) {
t.Log(delta) t.Log(delta)
d2, _ := time.Parse("2006-01-02", "2099-09-08") d2, _ := time.Parse("2006-01-02", "2099-09-08")
delta2 := newDelta(d2, "name", 11.11) delta2 := newDelta(d2, "", "name", 11.11, "$")
combined := delta.Plus(delta2) combined := delta.Plus(delta2)
if combined.Date != d2 { if combined.Date != d2 {

View File

@@ -1,9 +1,9 @@
package ledger package ledger
import ( import (
"io"
"os" "os"
"time"
"github.com/howeyc/ledger"
) )
type File string type File string
@@ -21,23 +21,54 @@ func (file File) Deltas(like ...Like) ([]Delta, error) {
} }
result := make([]Delta, 0, len(transactions)*2) result := make([]Delta, 0, len(transactions)*2)
for _, transaction := range transactions { for _, transaction := range transactions {
for _, acc := range transaction.AccountChanges { sums := map[string]float64{}
value, _ := acc.Balance.Float64() for _, acc := range transaction.recipients {
delta := newDelta(transaction.Date, acc.Name, value) sums[acc.currency] += acc.value
if !likes(like).all(delta) { delta := newDelta(
continue transaction.date,
transaction.description,
acc.name,
acc.value,
acc.currency,
)
if likes(like).all(delta) {
result = append(result, delta)
}
}
for currency, value := range sums {
delta := newDelta(
transaction.date,
transaction.description,
transaction.payee,
value,
currency,
)
if likes(like).all(delta) {
result = append(result, delta)
} }
result = append(result, delta)
} }
} }
return result, nil return result, nil
} }
func (file File) transactions() ([]*ledger.Transaction, error) { type transaction struct {
date time.Time
description string
payee string
recipients []transactionRecipient
}
type transactionRecipient struct {
name string
value float64
currency string
}
func (file File) transactions() ([]transaction, error) {
f, err := os.Open(string(file)) f, err := os.Open(string(file))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
return ledger.ParseLedger(f) return nil, io.EOF
} }