fix: print: auto: The print command should always display inferred

amounts for --auto generated postings. (#1276)
This commit is contained in:
Stephen Morgan 2021-09-22 11:58:27 +10:00 committed by Simon Michael
parent 4006ab6d2d
commit 9d5397deb6
2 changed files with 37 additions and 11 deletions

View File

@ -15,17 +15,17 @@ module Hledger.Cli.Commands.Print (
) )
where where
import Data.Maybe (isJust)
import Data.Text (Text) import Data.Text (Text)
import Data.List (intersperse) import Data.List (intersperse)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.IO as T import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TB import qualified Data.Text.Lazy.Builder as TB
import Lens.Micro (_Just, has)
import System.Console.CmdArgs.Explicit import System.Console.CmdArgs.Explicit
import Hledger.Read.CsvReader (CSV, printCSV)
import Hledger import Hledger
import Hledger.Read.CsvReader (CSV, printCSV)
import Hledger.Cli.CliOptions import Hledger.Cli.CliOptions
import Hledger.Cli.Utils import Hledger.Cli.Utils
@ -76,21 +76,27 @@ entriesReportAsText opts = TB.toLazyText . foldMap (TB.fromText . showTransactio
where where
whichtxn whichtxn
-- With -x, use the fully-inferred txn with all amounts & txn prices explicit. -- With -x, use the fully-inferred txn with all amounts & txn prices explicit.
| boolopt "explicit" (rawopts_ opts) | boolopt "explicit" (rawopts_ opts) = id
-- Or also, if any of -B/-V/-X/--value are active. -- Or also, if any of -B/-V/-X/--value are active.
-- Because of #551, and because of print -V valuing only one -- Because of #551, and because of print -V valuing only one
-- posting when there's an implicit txn price. -- posting when there's an implicit txn price.
-- So -B/-V/-X/--value implies -x. Is this ok ? -- So -B/-V/-X/--value implies -x. Is this ok ?
|| (isJust . value_ . _rsReportOpts $ reportspec_ opts) = id | has (value . _Just) opts = id
-- By default, use the original as-written-in-the-journal txn. -- By default, use the original as-written-in-the-journal txn.
| otherwise = originalTransaction | 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, and the inferred values of any auto postings
originalTransaction t = t { tpostings = map originalPostingPreservingAccount $ tpostings t } originalTransaction t = t { tpostings = map originalPostingPreservingAccount $ tpostings t }
-- Get the original posting if any, but keep the current possibly rewritten account name. -- Get the original posting if any, but keep the current possibly rewritten account name, and
originalPostingPreservingAccount p = (originalPosting p) { paccount = paccount p } -- the inferred values of any auto postings
originalPostingPreservingAccount p = orig
{ paccount = paccount p
, pamount = pamount $ if isGenerated then p else orig }
where
orig = originalPosting p
isGenerated = "generated-posting" `elem` map fst (ptags p)
-- XXX -- XXX
-- tests_showTransactions = [ -- tests_showTransactions = [

View File

@ -112,3 +112,23 @@ hledger -f - print --explicit
>>>2 >>>2
>>>=0 >>>=0
# 9. Auto postings are always explicit
hledger -f - print --auto
<<<
= a
c *-0.453
d
2021-09-01
a 1000 EUR
b
>>>
2021-09-01 ; modified:
a 1000 EUR
c -453 EUR ; generated-posting: = a
d 453 EUR ; generated-posting: = a
b
>>>2
>>>=0