Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Consider all amounts in multi-currency postings #149

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 54 additions & 55 deletions internal/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,41 +787,6 @@ func execHLedgerCommand(journalPath string, prices []price.Price, flags []string
if len(p.Amount) == 0 {
continue
}
amount := p.Amount[0]
totalAmount := decimal.NewFromFloat(amount.Quantity.Value)
totalAmountSet := false

if amount.Commodity != config.DefaultCurrency() {
if amount.Price.Contents.Quantity.Value != 0 {
var unconvertedTotal decimal.Decimal
if amount.Price.Tag == "TotalPrice" {
unconvertedTotal = decimal.NewFromFloat(amount.Price.Contents.Quantity.Value)
} else {
unconvertedTotal = decimal.NewFromFloat(amount.Price.Contents.Quantity.Value).Mul(decimal.NewFromFloat(amount.Quantity.Value))
}

if amount.Price.Contents.Commodity != config.DefaultCurrency() {
pr := lookupPrice(pricesTree, amount.Commodity, date)
if !pr.Equal(decimal.Zero) {
totalAmount = decimal.NewFromFloat(amount.Quantity.Value).Mul(pr)
totalAmountSet = true
}
if !totalAmountSet {
pr = lookupPrice(pricesTree, amount.Price.Contents.Commodity, date)
if !pr.Equal(decimal.Zero) {
totalAmount = unconvertedTotal.Mul(pr)
}
}
} else {
totalAmount = unconvertedTotal
}
} else {
pr := lookupPrice(pricesTree, amount.Commodity, date)
if !pr.Equal(decimal.Zero) {
totalAmount = decimal.NewFromFloat(amount.Quantity.Value).Mul(pr)
}
}
}

var tagRecurring, tagPeriod string

Expand Down Expand Up @@ -859,30 +824,64 @@ func execHLedgerCommand(journalPath string, prices []price.Price, flags []string
if err != nil {
return nil, err
}

}

posting := posting.Posting{
Date: date,
Payee: t.Description,
Account: p.Account,
Commodity: amount.Commodity,
Quantity: decimal.NewFromFloat(amount.Quantity.Value),
Amount: totalAmount,
TransactionID: strconv.FormatInt(t.ID, 10),
Status: strings.ToLower(t.Status),
TagRecurring: tagRecurring,
TagPeriod: tagPeriod,
TransactionBeginLine: t.TSourcePos[0].SourceLine,
TransactionEndLine: t.TSourcePos[1].SourceLine,
Forecast: forecast,
FileName: fileName,
Note: p.Comment,
TransactionNote: t.Comment}
postings = append(postings, &posting)
for _, amount := range p.Amount {
totalAmount := decimal.NewFromFloat(amount.Quantity.Value)
totalAmountSet := false

if amount.Commodity != config.DefaultCurrency() {
if amount.Price.Contents.Quantity.Value != 0 {
var unconvertedTotal decimal.Decimal
if amount.Price.Tag == "TotalPrice" {
unconvertedTotal = decimal.NewFromFloat(amount.Price.Contents.Quantity.Value)
} else {
unconvertedTotal = decimal.NewFromFloat(amount.Price.Contents.Quantity.Value).Mul(decimal.NewFromFloat(amount.Quantity.Value))
}

}
if amount.Price.Contents.Commodity != config.DefaultCurrency() {
pr := lookupPrice(pricesTree, amount.Commodity, date)
if !pr.Equal(decimal.Zero) {
totalAmount = decimal.NewFromFloat(amount.Quantity.Value).Mul(pr)
totalAmountSet = true
}
if !totalAmountSet {
pr = lookupPrice(pricesTree, amount.Price.Contents.Commodity, date)
if !pr.Equal(decimal.Zero) {
totalAmount = unconvertedTotal.Mul(pr)
}
}
} else {
totalAmount = unconvertedTotal
}
} else {
pr := lookupPrice(pricesTree, amount.Commodity, date)
if !pr.Equal(decimal.Zero) {
totalAmount = decimal.NewFromFloat(amount.Quantity.Value).Mul(pr)
}
}
}

posting := posting.Posting{
Date: date,
Payee: t.Description,
Account: p.Account,
Commodity: amount.Commodity,
Quantity: decimal.NewFromFloat(amount.Quantity.Value),
Amount: totalAmount,
TransactionID: strconv.FormatInt(t.ID, 10),
Status: strings.ToLower(t.Status),
TagRecurring: tagRecurring,
TagPeriod: tagPeriod,
TransactionBeginLine: t.TSourcePos[0].SourceLine,
TransactionEndLine: t.TSourcePos[1].SourceLine,
Forecast: forecast,
FileName: fileName,
Note: p.Comment,
TransactionNote: t.Comment}
postings = append(postings, &posting)
}
}
}

return postings, nil
Expand Down
Loading