From b3711488a7b263c42a05588330f7bc6f6634723d Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 31 Jan 2020 05:57:21 -0800 Subject: [PATCH] lib: apply canonical display styles to auto posting amounts too --- hledger-lib/Hledger/Read/Common.hs | 14 +++++++++----- tests/journal/auto-postings.test | 21 ++++++++++++++++++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index c6231a236..e05bda23b 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -274,9 +274,10 @@ finaliseJournal :: InputOpts -> FilePath -> Text -> Journal -> ExceptT String IO finaliseJournal iopts f txt pj = do t <- liftIO getClockTime -- Infer and apply canonical styles for each commodity (or fail). - -- TODO: since #903's refactoring for hledger 1.12, + -- This affects transaction balancing/assertions/assignments, so needs to be done early. + -- (TODO: since #903's refactoring for hledger 1.12, -- journalApplyCommodityStyles here is seeing the - -- transactions before they get reversesd to normal order. + -- transactions before they get reversesd to normal order.) case journalApplyCommodityStyles pj of Left e -> throwError e Right pj' -> either throwError return $ @@ -292,12 +293,15 @@ finaliseJournal iopts f txt pj = do else \j -> do -- Either monad -- Auto postings are active. -- Balance all transactions without checking balance assertions, - -- then add the auto postings, then check balance assertions. + j' <- journalBalanceTransactions False j + -- then add the auto postings -- (Note adding auto postings after balancing means #893b fails; -- adding them before balancing probably means #893a, #928, #938 fail.) - j' <- journalBalanceTransactions False j let j'' = journalModifyTransactions j' - journalBalanceTransactions (not $ ignore_assertions_ iopts) j'' + -- then apply commodity styles once more, to style the auto posting amounts. (XXX inefficient ?) + j''' <- journalApplyCommodityStyles j'' + -- then check balance assertions. + journalBalanceTransactions (not $ ignore_assertions_ iopts) j''' setYear :: Year -> JournalParser m () setYear y = modify' (\j -> j{jparsedefaultyear=Just y}) diff --git a/tests/journal/auto-postings.test b/tests/journal/auto-postings.test index b0566937f..427d58684 100644 --- a/tests/journal/auto-postings.test +++ b/tests/journal/auto-postings.test @@ -1,5 +1,3 @@ -# Transaction modifier adding (unbalanced) tax postings. -# print. Auto-generated postings are inserted below the matched one. < = ^income (liabilities:tax) *.33 ; income tax @@ -13,7 +11,7 @@ assets:cash $20 assets:bank -# 1. print +# 1. print. Auto-generated postings are inserted below the matched one. $ hledger print -f- --auto 2016-01-01 paycheck ; modified: income:remuneration $-100 @@ -327,3 +325,20 @@ $ hledger -f- register --auto $ hledger -f- print -x --auto >2 /could not balance/ >=1 + +# 17. Commodity display styles are applied to auto posting amounts. +< +2020-01-20 + (a) 1 A +; add an auto posting with decimal places += + (b) *1.5 B +; show B amounts with no decimal places +commodity 0. B + +$ hledger -f- print --auto +2020-01-20 ; modified: + (a) 1 A + (b) 2 B ; generated-posting: = + +>=0