diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 796e2a9bf..2aec34e9d 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -605,12 +605,13 @@ amountStyleApplyWithRounding iscost q news@AmountStyle{asprecision=newp, asround HardRounding -> news{asprecision=if iscost then oldp else newp} AllRounding -> news --- | Set this amount style's rounding strategy when being applied to amounts. +-- | Set this amount style's rounding strategy when it is being applied to amounts. amountStyleSetRounding :: Rounding -> AmountStyle -> AmountStyle amountStyleSetRounding r as = as{asrounding=r} +-- | Set these amount styles' rounding strategy when they are being applied to amounts. amountStylesSetRounding :: Rounding -> M.Map CommoditySymbol AmountStyle -> M.Map CommoditySymbol AmountStyle -amountStylesSetRounding r = M.map (amountStyleSetRounding r) +amountStylesSetRounding r = M.map (amountStyleSetRounding r) -- | Default amount style amountstyle = AmountStyle L False Nothing (Just '.') (Precision 0) NoRounding diff --git a/hledger/Hledger/Cli/Commands/Close.hs b/hledger/Hledger/Cli/Commands/Close.hs index 33f053be0..2e94c1ec6 100644 --- a/hledger/Hledger/Cli/Commands/Close.hs +++ b/hledger/Hledger/Cli/Commands/Close.hs @@ -23,6 +23,7 @@ import Safe (lastDef, readMay, readDef) import System.FilePath (takeFileName) import Data.Char (isDigit) import Hledger.Read.RulesReader (parseBalanceAssertionType) +import Hledger.Cli.Commands.Print (roundFlag, amountStylesSetRoundingFromRawOpts) defclosedesc = "closing balances" defopendesc = "opening balances" @@ -50,6 +51,7 @@ closemode = hledgerCommandMode ,flagReq ["close-acct"] (\s opts -> Right $ setopt "close-acct" s opts) "ACCT" "set closing transaction's destination account" ,flagReq ["open-desc"] (\s opts -> Right $ setopt "open-desc" s opts) "DESC" "set opening transaction's description" ,flagReq ["open-acct"] (\s opts -> Right $ setopt "open-acct" s opts) "ACCT" "set opening transaction's source account" + ,roundFlag ] [generalflagsgroup1] (hiddenflags @@ -256,6 +258,8 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do ++ [posting{paccount=openacct, pamount=if explicit then mixedAmountSetFullPrecision (maNegate totalamt) else missingmixedamt} | not interleaved] -- print them - maybe (pure ()) (T.putStr . showTransaction) mclosetxn - maybe (pure ()) (T.putStr . showTransaction) mopentxn + -- allow user-specified rounding with --round, like print + let styles = amountStylesSetRoundingFromRawOpts rawopts $ journalCommodityStyles j + maybe (pure ()) (T.putStr . showTransaction . styleAmounts styles) mclosetxn + maybe (pure ()) (T.putStr . showTransaction . styleAmounts styles) mopentxn diff --git a/hledger/Hledger/Cli/Commands/Print.hs b/hledger/Hledger/Cli/Commands/Print.hs index 64b1d41f1..e3e8d249a 100644 --- a/hledger/Hledger/Cli/Commands/Print.hs +++ b/hledger/Hledger/Cli/Commands/Print.hs @@ -11,6 +11,9 @@ module Hledger.Cli.Commands.Print ( printmode ,print' -- ,entriesReportAsText + ,roundFlag + ,roundFromRawOpts + ,amountStylesSetRoundingFromRawOpts ,transactionWithMostlyOriginalPostings ) where @@ -31,7 +34,7 @@ import System.Exit (exitFailure) import Safe (lastMay, minimumDef) import Data.Function ((&)) import Data.List.Extra (nubSort) - +import qualified Data.Map as M printmode = hledgerCommandMode $(embedFileRelative "Hledger/Cli/Commands/Print.txt") @@ -39,18 +42,7 @@ printmode = hledgerCommandMode "show all amounts explicitly" ,flagNone ["show-costs"] (setboolopt "show-costs") "show transaction prices even with conversion postings" - ,flagReq ["round"] (\s opts -> Right $ setopt "round" s opts) "TYPE" $ - intercalate "\n" - ["how much rounding or padding should be done when displaying amounts ?" - ,"none - show original decimal digits," - ," as in journal" - ,"soft - just add or remove decimal zeros" - ," to match precision (default)" - ,"hard - round posting amounts to precision" - ," (can unbalance transactions)" - ,"all - also round cost amounts to precision" - ," (can unbalance transactions)" - ] + ,roundFlag ,flagNone ["new"] (setboolopt "new") "show only newer-dated transactions added in each file since last run" ,let arg = "DESC" in @@ -63,6 +55,19 @@ printmode = hledgerCommandMode hiddenflags ([], Just $ argsFlag "[QUERY]") +roundFlag = flagReq ["round"] (\s opts -> Right $ setopt "round" s opts) "TYPE" $ + intercalate "\n" + ["how much rounding or padding should be done when displaying amounts ?" + ,"none - show original decimal digits," + ," as in journal" + ,"soft - just add or remove decimal zeros" + ," to match precision (default)" + ,"hard - round posting amounts to precision" + ," (can unbalance transactions)" + ,"all - also round cost amounts to precision" + ," (can unbalance transactions)" + ] + -- | Get the --round option's value, if any. Can fail with a parse error. roundFromRawOpts :: RawOpts -> Maybe Rounding roundFromRawOpts = lastMay . collectopts roundfromrawopt @@ -75,6 +80,14 @@ roundFromRawOpts = lastMay . collectopts roundfromrawopt | n=="round" = error' $ "--round's value should be none, soft, hard or all; got: "++v | otherwise = Nothing +-- | Set these amount styles' rounding strategy when they are being applied to amounts, +-- according to the value of the --round option, if any. +amountStylesSetRoundingFromRawOpts :: RawOpts -> M.Map CommoditySymbol AmountStyle -> M.Map CommoditySymbol AmountStyle +amountStylesSetRoundingFromRawOpts rawopts styles = + case roundFromRawOpts rawopts of + Just r -> amountStylesSetRounding r styles + Nothing -> styles + -- | Print journal transactions in standard format. print' :: CliOpts -> Journal -> IO () print' opts j = do @@ -105,12 +118,7 @@ printEntries opts@CliOpts{rawopts_=rawopts, reportspec_=rspec} j = writeOutputLazyText opts $ render $ entriesReport rspec j where -- print does user-specified rounding or (by default) no rounding, in all output formats - styles = - case roundFromRawOpts rawopts of - Nothing -> styles0 - Just NoRounding -> styles0 - Just r -> amountStylesSetRounding r styles0 - where styles0 = journalCommodityStyles j + styles = amountStylesSetRoundingFromRawOpts rawopts $ journalCommodityStyles j fmt = outputFormatFromOpts opts render | fmt=="txt" = entriesReportAsText . styleAmounts styles . map maybeoriginalamounts diff --git a/hledger/test/close.test b/hledger/test/close.test index 1591b1b19..5cc3f61e3 100644 --- a/hledger/test/close.test +++ b/hledger/test/close.test @@ -259,3 +259,16 @@ $ hledger -f- close --migrate -e 2001 >= +# ** 18. close supports --round, like print. +$ hledger -f- close --migrate -e 2001 --round=hard -c '$1.0' +2000-12-31 closing balances ; start: + assets:a $-1000.0 = $0.0 + assets:b $-1000.0 = $0.0 + equity:opening/closing balances + +2001-01-01 opening balances ; start: + assets:a $1000.0 = $1000.0 + assets:b $1000.0 = $1000.0 + equity:opening/closing balances + +>=