print: be sure to value all postings; -V/-X/--value now imply -x

Previously -B implied -x; now any of the valuation flags do.
This avoids a bug where print -V of a transaction with an implicit
commodity conversion would convert only some of its postings to value.

Also, more valuation tests.
This commit is contained in:
Simon Michael 2020-02-10 12:41:36 -08:00
parent ae9d8d6e56
commit dad4d9b662
3 changed files with 78 additions and 7 deletions

View File

@ -15,6 +15,7 @@ module Hledger.Cli.Commands.Print (
) )
where where
import Data.Maybe (isJust)
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import System.Console.CmdArgs.Explicit import System.Console.CmdArgs.Explicit
@ -59,14 +60,18 @@ printEntries opts@CliOpts{reportopts_=ropts} j = do
writeOutput opts $ render $ entriesReport ropts' q j writeOutput opts $ render $ entriesReport ropts' q j
entriesReportAsText :: CliOpts -> EntriesReport -> String entriesReportAsText :: CliOpts -> EntriesReport -> String
entriesReportAsText opts = concatMap (showTransaction . gettxn) entriesReportAsText opts = concatMap (showTransaction . whichtxn)
where where
gettxn | useexplicittxn = id -- use fully inferred amounts & txn prices whichtxn
| otherwise = originalTransaction -- use original as-written amounts/txn prices -- With -x, use the fully-inferred txn with all amounts & txn prices explicit.
-- Original vs inferred transactions/postings were causing problems here, disabling -B (#551). | boolopt "explicit" (rawopts_ opts)
-- Use the explicit one if -B or -x are active. -- Or also, if any of -B/-V/-X/--value are active.
-- This passes tests; does it also mean -B sometimes shows missing amounts unnecessarily ? -- Because of #551, and because of print -V valuing only one
useexplicittxn = boolopt "explicit" (rawopts_ opts) || (valuationTypeIsCost $ reportopts_ opts) -- posting when there's an implicit txn price.
-- So -B/-V/-X/--value implies -x. Is this ok ?
|| (isJust $ value_ $ reportopts_ opts) = id
-- By default, use the original as-written-in-the-journal txn.
| otherwise = originalTransaction
-- Replace this transaction's postings with the original postings if any, but keep the -- Replace this transaction's postings with the original postings if any, but keep the
-- current possibly rewritten account names. -- current possibly rewritten account names.

View File

@ -41,6 +41,7 @@ Similarly, when a transaction price is implied but not written, it will not appe
You can use the `-x`/`--explicit` flag to make all amounts and transaction prices explicit, You can use the `-x`/`--explicit` flag to make all amounts and transaction prices explicit,
which can be useful for troubleshooting or for making your journal more readable and which can be useful for troubleshooting or for making your journal more readable and
robust against data entry errors. robust against data entry errors.
`-x` is also implied by using any of `-B`,`-V`,`-X`,`--value`.
Note, `-x`/`--explicit` will cause postings with a multi-commodity amount Note, `-x`/`--explicit` will cause postings with a multi-commodity amount
(these can arise when a multi-commodity transaction has an implicit amount) (these can arise when a multi-commodity transaction has an implicit amount)

View File

@ -566,3 +566,68 @@ Budget performance in 2000q1, valued at 2000-01-15:
a || 5 B [ 50% of 10 B] 5 B [ 50% of 10 B] 5 B [ 50% of 10 B] 15 B [ 50% of 30 B] 5 B [ 50% of 10 B] a || 5 B [ 50% of 10 B] 5 B [ 50% of 10 B] 5 B [ 50% of 10 B] 15 B [ 50% of 30 B] 5 B [ 50% of 10 B]
---++---------------------------------------------------------------------------------------------------------- ---++----------------------------------------------------------------------------------------------------------
|| 5 B [ 50% of 10 B] 5 B [ 50% of 10 B] 5 B [ 50% of 10 B] 15 B [ 50% of 30 B] 5 B [ 50% of 10 B] || 5 B [ 50% of 10 B] 5 B [ 50% of 10 B] 5 B [ 50% of 10 B] 15 B [ 50% of 30 B] 5 B [ 50% of 10 B]
# 50. --value=then with --historical. How is the starting total valued ?
# Currently not supported.
<
P 2020-01-01 A 1 B
P 2020-02-01 A 2 B
P 2020-03-01 A 3 B
P 2020-04-01 A 4 B
2020-01-01
(a) 1 A
2020-02-01
(a) 1 A
2020-03-01
(a) 1 A
2020-04-01
(a) 1 A
$ hledger -f- reg --value=then -b 2020-03 -H
>2 /not yet implemented/
>=1
# 51. --value=then with a report interval. How are the summary amounts valued ?
# Currently each interval's unvalued sum is valued on its first day.
<
P 2020-01-01 A 1 B
P 2020-02-01 A 2 B
P 2020-03-01 A 3 B
P 2020-04-01 A 4 B
2020-01-01
(a) 1 A
2020-02-01
(a) 1 A
2020-03-01
(a) 1 A
2020-04-01
(a) 1 A
$ hledger -f- reg --value=then -Q
2020q1 a 3 B 3 B
2020q2 a 4 B 7 B
>=0
# 52. print --value should affect all postings, including when there's an implicit transaction price
<
P 2020-01-01 A 1 C
P 2020-01-01 B 1 C
2020-01-01
a 1 A
b -1 B
$ hledger -f- print -V
2020-01-01
a 1 C
b -1 C
>=0