Skip to content

Commit

Permalink
Use common transaction type
Browse files Browse the repository at this point in the history
  • Loading branch information
sboehler committed Dec 27, 2021
1 parent e81f5fc commit a8696e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 deletions.
19 changes: 19 additions & 0 deletions lib/journal/ast/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ type Transaction struct {
AddOns []interface{}
}

// Clone clones a transaction.
func (t Transaction) Clone() *Transaction {
var (
tags = make([]Tag, len(t.Tags))
postings = make([]Posting, len(t.Postings))
addOns = make([]interface{}, len(t.AddOns))
)
copy(tags, t.Tags)
copy(postings, t.Postings)
copy(addOns, t.AddOns)
return &Transaction{
Range: t.Range,
Date: t.Date,
Tags: tags,
Postings: postings,
AddOns: addOns,
}
}

// Commodities returns the commodities in this transaction.
func (t Transaction) Commodities() map[*journal.Commodity]bool {
var res = make(map[*journal.Commodity]bool)
Expand Down
42 changes: 15 additions & 27 deletions lib/journal/past/process/valuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/sboehler/knut/lib/journal/ast"
"github.com/sboehler/knut/lib/journal/past"
"github.com/sboehler/knut/lib/journal/val"
"github.com/shopspring/decimal"
)

// Valuator produces valuated days.
Expand Down Expand Up @@ -77,33 +76,20 @@ func (pr Valuator) ProcessStream(ctx context.Context, inCh <-chan *val.Day) (cha
return resCh, errCh
}

func (pr Valuator) valuateAndBookTransaction(b *val.Day, t *ast.Transaction) (*val.Transaction, error) {
var postings []val.Posting
func (pr Valuator) valuateAndBookTransaction(b *val.Day, t *ast.Transaction) (*ast.Transaction, error) {
var res = t.Clone()
for i, posting := range t.Postings {
var (
value decimal.Decimal
err error
)
if pr.Valuation == nil || pr.Valuation == posting.Commodity {
value = posting.Amount
} else {
if value, err = b.Prices.Valuate(posting.Commodity, posting.Amount); err != nil {
if pr.Valuation != nil && pr.Valuation != posting.Commodity {
value, err := b.Prices.Valuate(posting.Commodity, posting.Amount)
if err != nil {
return nil, Error{t, fmt.Sprintf("no price found for commodity %s", posting.Commodity)}
}
posting.Amount = value
}
b.Values.Book(posting.Credit, posting.Debit, value, posting.Commodity)
postings = append(postings, val.Posting{
Source: &t.Postings[i],
Credit: posting.Credit,
Debit: posting.Debit,
Value: value,
Commodity: posting.Commodity,
})
b.Values.Book(posting.Credit, posting.Debit, posting.Amount, posting.Commodity)
res.Postings[i] = posting
}
return &val.Transaction{
Source: t,
Postings: postings,
}, nil
return res, nil
}

// computeValuationTransactions checks whether the valuation for the positions
Expand Down Expand Up @@ -137,8 +123,9 @@ func (pr Valuator) computeValuationTransactions(b *val.Day) {
if diff.IsPositive() {

// create a transaction to adjust the valuation
b.Transactions = append(b.Transactions, &val.Transaction{
Postings: []val.Posting{
b.Transactions = append(b.Transactions, &ast.Transaction{
Date: b.Date,
Postings: []ast.Posting{
{
Credit: valAcc,
Debit: pos.Account,
Expand All @@ -151,8 +138,9 @@ func (pr Valuator) computeValuationTransactions(b *val.Day) {
} else {

// create a transaction to adjust the valuation
b.Transactions = append(b.Transactions, &val.Transaction{
Postings: []val.Posting{
b.Transactions = append(b.Transactions, &ast.Transaction{
Date: b.Date,
Postings: []ast.Posting{
{
Credit: pos.Account,
Debit: valAcc,
Expand Down
18 changes: 1 addition & 17 deletions lib/journal/val/val.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,15 @@ import (
"time"

"github.com/sboehler/knut/lib/balance/prices"
"github.com/sboehler/knut/lib/journal"
"github.com/sboehler/knut/lib/journal/ast"
"github.com/sboehler/knut/lib/journal/past"
"github.com/shopspring/decimal"
)

// Day is a day with valuated transactions and positions.
type Day struct {
Day *past.Day
Date time.Time
Prices prices.NormalizedPrices
Transactions []*Transaction
Transactions []*ast.Transaction
Values past.Amounts
}

// Transaction represents a valuated transaction.
type Transaction struct {
Source *ast.Transaction
Postings []Posting
}

// Posting is a valuated posting.
type Posting struct {
Source *ast.Posting
Credit, Debit *journal.Account
Value decimal.Decimal
Commodity *journal.Commodity
}

0 comments on commit a8696e1

Please sign in to comment.