feat:close: support --round, like print
This commit is contained in:
parent
d3ad743b36
commit
a4c2776a18
@ -605,10 +605,11 @@ amountStyleApplyWithRounding iscost q news@AmountStyle{asprecision=newp, asround
|
|||||||
HardRounding -> news{asprecision=if iscost then oldp else newp}
|
HardRounding -> news{asprecision=if iscost then oldp else newp}
|
||||||
AllRounding -> news
|
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 :: Rounding -> AmountStyle -> AmountStyle
|
||||||
amountStyleSetRounding r as = as{asrounding=r}
|
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 :: Rounding -> M.Map CommoditySymbol AmountStyle -> M.Map CommoditySymbol AmountStyle
|
||||||
amountStylesSetRounding r = M.map (amountStyleSetRounding r)
|
amountStylesSetRounding r = M.map (amountStyleSetRounding r)
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import Safe (lastDef, readMay, readDef)
|
|||||||
import System.FilePath (takeFileName)
|
import System.FilePath (takeFileName)
|
||||||
import Data.Char (isDigit)
|
import Data.Char (isDigit)
|
||||||
import Hledger.Read.RulesReader (parseBalanceAssertionType)
|
import Hledger.Read.RulesReader (parseBalanceAssertionType)
|
||||||
|
import Hledger.Cli.Commands.Print (roundFlag, amountStylesSetRoundingFromRawOpts)
|
||||||
|
|
||||||
defclosedesc = "closing balances"
|
defclosedesc = "closing balances"
|
||||||
defopendesc = "opening 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 ["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-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"
|
,flagReq ["open-acct"] (\s opts -> Right $ setopt "open-acct" s opts) "ACCT" "set opening transaction's source account"
|
||||||
|
,roundFlag
|
||||||
]
|
]
|
||||||
[generalflagsgroup1]
|
[generalflagsgroup1]
|
||||||
(hiddenflags
|
(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]
|
++ [posting{paccount=openacct, pamount=if explicit then mixedAmountSetFullPrecision (maNegate totalamt) else missingmixedamt} | not interleaved]
|
||||||
|
|
||||||
-- print them
|
-- print them
|
||||||
maybe (pure ()) (T.putStr . showTransaction) mclosetxn
|
-- allow user-specified rounding with --round, like print
|
||||||
maybe (pure ()) (T.putStr . showTransaction) mopentxn
|
let styles = amountStylesSetRoundingFromRawOpts rawopts $ journalCommodityStyles j
|
||||||
|
maybe (pure ()) (T.putStr . showTransaction . styleAmounts styles) mclosetxn
|
||||||
|
maybe (pure ()) (T.putStr . showTransaction . styleAmounts styles) mopentxn
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,9 @@ module Hledger.Cli.Commands.Print (
|
|||||||
printmode
|
printmode
|
||||||
,print'
|
,print'
|
||||||
-- ,entriesReportAsText
|
-- ,entriesReportAsText
|
||||||
|
,roundFlag
|
||||||
|
,roundFromRawOpts
|
||||||
|
,amountStylesSetRoundingFromRawOpts
|
||||||
,transactionWithMostlyOriginalPostings
|
,transactionWithMostlyOriginalPostings
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
@ -31,7 +34,7 @@ import System.Exit (exitFailure)
|
|||||||
import Safe (lastMay, minimumDef)
|
import Safe (lastMay, minimumDef)
|
||||||
import Data.Function ((&))
|
import Data.Function ((&))
|
||||||
import Data.List.Extra (nubSort)
|
import Data.List.Extra (nubSort)
|
||||||
|
import qualified Data.Map as M
|
||||||
|
|
||||||
printmode = hledgerCommandMode
|
printmode = hledgerCommandMode
|
||||||
$(embedFileRelative "Hledger/Cli/Commands/Print.txt")
|
$(embedFileRelative "Hledger/Cli/Commands/Print.txt")
|
||||||
@ -39,18 +42,7 @@ printmode = hledgerCommandMode
|
|||||||
"show all amounts explicitly"
|
"show all amounts explicitly"
|
||||||
,flagNone ["show-costs"] (setboolopt "show-costs")
|
,flagNone ["show-costs"] (setboolopt "show-costs")
|
||||||
"show transaction prices even with conversion postings"
|
"show transaction prices even with conversion postings"
|
||||||
,flagReq ["round"] (\s opts -> Right $ setopt "round" s opts) "TYPE" $
|
,roundFlag
|
||||||
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)"
|
|
||||||
]
|
|
||||||
,flagNone ["new"] (setboolopt "new")
|
,flagNone ["new"] (setboolopt "new")
|
||||||
"show only newer-dated transactions added in each file since last run"
|
"show only newer-dated transactions added in each file since last run"
|
||||||
,let arg = "DESC" in
|
,let arg = "DESC" in
|
||||||
@ -63,6 +55,19 @@ printmode = hledgerCommandMode
|
|||||||
hiddenflags
|
hiddenflags
|
||||||
([], Just $ argsFlag "[QUERY]")
|
([], 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.
|
-- | Get the --round option's value, if any. Can fail with a parse error.
|
||||||
roundFromRawOpts :: RawOpts -> Maybe Rounding
|
roundFromRawOpts :: RawOpts -> Maybe Rounding
|
||||||
roundFromRawOpts = lastMay . collectopts roundfromrawopt
|
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
|
| n=="round" = error' $ "--round's value should be none, soft, hard or all; got: "++v
|
||||||
| otherwise = Nothing
|
| 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 journal transactions in standard format.
|
||||||
print' :: CliOpts -> Journal -> IO ()
|
print' :: CliOpts -> Journal -> IO ()
|
||||||
print' opts j = do
|
print' opts j = do
|
||||||
@ -105,12 +118,7 @@ printEntries opts@CliOpts{rawopts_=rawopts, reportspec_=rspec} j =
|
|||||||
writeOutputLazyText opts $ render $ entriesReport rspec j
|
writeOutputLazyText opts $ render $ entriesReport rspec j
|
||||||
where
|
where
|
||||||
-- print does user-specified rounding or (by default) no rounding, in all output formats
|
-- print does user-specified rounding or (by default) no rounding, in all output formats
|
||||||
styles =
|
styles = amountStylesSetRoundingFromRawOpts rawopts $ journalCommodityStyles j
|
||||||
case roundFromRawOpts rawopts of
|
|
||||||
Nothing -> styles0
|
|
||||||
Just NoRounding -> styles0
|
|
||||||
Just r -> amountStylesSetRounding r styles0
|
|
||||||
where styles0 = journalCommodityStyles j
|
|
||||||
|
|
||||||
fmt = outputFormatFromOpts opts
|
fmt = outputFormatFromOpts opts
|
||||||
render | fmt=="txt" = entriesReportAsText . styleAmounts styles . map maybeoriginalamounts
|
render | fmt=="txt" = entriesReportAsText . styleAmounts styles . map maybeoriginalamounts
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
>=
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user