From 71921135f665e4bda09808dbc826cf9d73f60771 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 11 Oct 2015 16:07:31 -0700 Subject: [PATCH] include P amounts in canonicalisation (fixes #131) Since market price amounts didn't contribute to the canonical commodity styles, they were being reset to the null style. And this propagated to the reported amounts when -V was in effect, causing much confusion. Now, market prices contribute to canonicalisation and the expected styles are preserved even with -V. cf https://github.com/simonmichael/hledger/issues/131#issuecomment-133545140 --- hledger-lib/Hledger/Data/Journal.hs | 22 ++++++++++++++++------ tests/journal/market-prices.test | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 tests/journal/market-prices.test diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 31a7c506a..0a24bdf5d 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -583,13 +583,23 @@ journalConvertAmountsToCost j@Journal{jtxns=ts} = j{jtxns=map fixtransaction ts} -- Just (UnitPrice ma) -> c:(concatMap amountCommodities $ amounts ma) -- Just (TotalPrice ma) -> c:(concatMap amountCommodities $ amounts ma) --- | Get all this journal's (mixed) amounts, in the order parsed. -journalMixedAmounts :: Journal -> [MixedAmount] -journalMixedAmounts = map pamount . journalPostings - --- | Get all this journal's component amounts, roughly in the order parsed. +-- | Get an ordered list of the amounts in this journal which will +-- influence amount style canonicalisation. These are: +-- +-- * amounts in market price directives (in parse order) +-- * amounts in postings (in parse order) +-- +-- Amounts in default commodity directives also influence +-- canonicalisation, but earlier, as amounts are parsed. +-- Amounts in posting prices are not used for canonicalisation. +-- journalAmounts :: Journal -> [Amount] -journalAmounts = concatMap flatten . journalMixedAmounts where flatten (Mixed as) = as +journalAmounts j = + concat + [map mpamount $ jmarketprices j + ,concatMap flatten $ map pamount $ journalPostings j + ] + where flatten (Mixed as) = as -- | The fully specified date span enclosing the dates (primary or secondary) -- of all this journal's transactions and postings, or DateSpan Nothing Nothing diff --git a/tests/journal/market-prices.test b/tests/journal/market-prices.test new file mode 100644 index 000000000..dc8b09f7e --- /dev/null +++ b/tests/journal/market-prices.test @@ -0,0 +1,25 @@ +# market prices defined with the P directive are used by -V/--value. + +# 1. market prices (a) are affected by D directives and (b) contribute +# to amount style canonicalisation. +# (https://github.com/simonmichael/hledger/issues/131#issuecomment-133545140) + +hledger -f- balance -V +<<< +D 1000.00 H + +P 2015/08/14 EEEE 41.66 +P 2015/08/14 FFFF 74.62 +P 2015/08/14 GGGG 32.39 + +2015/08/15 + a 2.4120 EEEE @@ 100 + a 0.3350 FFFF @@ 25 + a 0.7718 GGGG @@ 25 + b +>>> + 150.48 H a + -150.00 H b +-------------------- + 0.48 H +>>>=0