deltas now accept currency, description, and break payee from all recipients
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package ledger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/howeyc/ledger"
|
||||
"time"
|
||||
)
|
||||
|
||||
type File string
|
||||
@@ -21,23 +21,54 @@ func (file File) Deltas(like ...Like) ([]Delta, error) {
|
||||
}
|
||||
result := make([]Delta, 0, len(transactions)*2)
|
||||
for _, transaction := range transactions {
|
||||
for _, acc := range transaction.AccountChanges {
|
||||
value, _ := acc.Balance.Float64()
|
||||
delta := newDelta(transaction.Date, acc.Name, value)
|
||||
if !likes(like).all(delta) {
|
||||
continue
|
||||
sums := map[string]float64{}
|
||||
for _, acc := range transaction.recipients {
|
||||
sums[acc.currency] += acc.value
|
||||
delta := newDelta(
|
||||
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
|
||||
}
|
||||
|
||||
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))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return ledger.ParseLedger(f)
|
||||
return nil, io.EOF
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user