From 2645b76ff024bfedd1915881ede20ba507126d42 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 22 Jan 2020 12:00:56 -0800 Subject: [PATCH] close: hide prices by default, show them with --show-costs (#1165) close no longer preserves costs, generating huge entries when there are many foreign currency/investment transactions, unless you ask it to. --- hledger/Hledger/Cli/Commands/Close.hs | 26 ++++++---- hledger/Hledger/Cli/Commands/Close.md | 14 ++++-- tests/close.test | 69 ++++++++++++++++++++++----- 3 files changed, 82 insertions(+), 27 deletions(-) diff --git a/hledger/Hledger/Cli/Commands/Close.hs b/hledger/Hledger/Cli/Commands/Close.hs index c366f3a27..c7c483877 100755 --- a/hledger/Hledger/Cli/Commands/Close.hs +++ b/hledger/Hledger/Cli/Commands/Close.hs @@ -28,6 +28,7 @@ closemode = hledgerCommandMode ,flagReq ["close-to"] (\s opts -> Right $ setopt "close-to" s opts) "ACCT" ("account to transfer closing balances to (default: "++defclosingacct++")") ,flagReq ["open-from"] (\s opts -> Right $ setopt "open-from" s opts) "ACCT" ("account to transfer opening balances from (default: "++defopeningacct++")") ,flagNone ["interleaved"] (setboolopt "interleaved") "keep equity and non-equity postings adjacent" + ,flagNone ["show-costs"] (setboolopt "show-costs") "keep balances with different costs separate" ] [generalflagsgroup1] hiddenflags @@ -54,22 +55,27 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do (Nothing, Just o) -> (o, o) (Nothing, Nothing) -> (T.pack defclosingacct, T.pack defopeningacct) - -- interleave equity postings next to the corresponding closing posting, or put them all at the end ? - interleaved = boolopt "interleaved" rawopts - - -- since balance assertion amounts are required to be exact, the - -- amounts in opening/closing transactions should be too (#941, #1137) - precise = setFullPrecision - -- dates of the closing and opening transactions ropts_ = ropts{balancetype_=HistoricalBalance, accountlistmode_=ALFlat} q = queryFromOpts today ropts_ openingdate = fromMaybe today $ queryEndDate False q closingdate = addDays (-1) openingdate + -- should we preserve cost information ? + normalise = case boolopt "show-costs" rawopts of + True -> normaliseMixedAmount + False -> normaliseMixedAmount . mixedAmountStripPrices + -- the balances to close (acctbals,_) = balanceReportFromMultiBalanceReport ropts_ q j - totalamt = sum $ map (\(_,_,_,b) -> normaliseMixedAmount b) acctbals + totalamt = sum $ map (\(_,_,_,b) -> normalise b) acctbals + + -- since balance assertion amounts are required to be exact, the + -- amounts in opening/closing transactions should be too (#941, #1137) + precise = setFullPrecision + + -- interleave equity postings next to the corresponding closing posting, or put them all at the end ? + interleaved = boolopt "interleaved" rawopts -- the closing transaction closingtxn = nulltransaction{tdate=closingdate, tdescription="closing balances", tpostings=closingps} @@ -90,7 +96,7 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do | -- get the balances for each commodity and transaction price (a,_,_,mb) <- acctbals - , let bs = amounts $ normaliseMixedAmount mb + , let bs = amounts $ normalise mb -- mark the last balance in each commodity with True , let bs' = concat [reverse $ zip (reverse bs) (True : repeat False) | bs <- groupBy ((==) `on` acommodity) bs] @@ -116,7 +122,7 @@ close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do ++ [posting{paccount=openingacct, pamount=Mixed [precise $ negate b]} | interleaved] | (a,_,_,mb) <- acctbals - , let bs = amounts $ normaliseMixedAmount mb + , let bs = amounts $ normalise mb -- mark the last balance in each commodity with the unpriced sum in that commodity (for a balance assertion) , let bs' = concat [reverse $ zip (reverse bs) (Just commoditysum : repeat Nothing) | bs <- groupBy ((==) `on` acommodity) bs diff --git a/hledger/Hledger/Cli/Commands/Close.md b/hledger/Hledger/Cli/Commands/Close.md index bd01eba78..ee7117688 100644 --- a/hledger/Hledger/Cli/Commands/Close.md +++ b/hledger/Hledger/Cli/Commands/Close.md @@ -19,7 +19,15 @@ these, it is used for both. The "equity" postings are shown at the end of the transaction by default (and are combined when possible). With `--interleaved`, they are shown next to each posting they -balance, instead (better for troubleshooting). +balance, which is better for troubleshooting (and may generate more postings). + +When the balances being closed have cost information (because +transaction prices were recorded), `--show-costs` will preserve it. +Balances with different costs are closed/reopened separately, +and balance -B reports will be unchanged after the transition. +Note this can generate many postings, +if you have a lot of foreign currency or investment transactions. +By default, transaction prices are ignored. ### close usage @@ -53,10 +61,6 @@ the generated balance assertions will depend on these flags. Likewise, if you run this command with --auto, the balance assertions will probably always require --auto. -When account balances have cost information (transaction prices), the -closing/opening transactions will preserve it, so that eg balance -B reports -will not be affected. - Examples: Carrying asset/liability balances into a new file for 2019, all from command line: diff --git a/tests/close.test b/tests/close.test index 9b0a03859..ae4979245 100644 --- a/tests/close.test +++ b/tests/close.test @@ -139,9 +139,7 @@ $ hledger close -f- -p 2016 assets liabilities --opening --closing >=0 -# 6. Closing a multi-priced balance. The "lot" prices are preserved. -# Only the last posting in each commodity gets a balance assertion (#1035). -# Balance assertion amounts do not have a price. +# 6. Closing a multi-priced balance. By default the transaction prices are ignored. < 2019/01/01 assets 1A @ 1B @@ -149,6 +147,20 @@ $ hledger close -f- -p 2016 assets liabilities --opening --closing equity $ hledger -f- close assets -p 2019 +2019-12-31 closing balances + assets -2A = 0A + equity:closing balances 2A + +2020-01-01 opening balances + assets 2A = 2A + equity:opening balances -2A + +>=0 + +# 7. With --show-costs, the transaction prices are preserved. +# Only the last posting in each commodity gets a balance assertion (#1035). +# Balance assertion amounts do not have a price. +$ hledger -f- close assets -p 2019 --show-costs 2019-12-31 closing balances assets -1A @ 1B assets -1A @ 1C = 0A @@ -163,9 +175,8 @@ $ hledger -f- close assets -p 2019 >=0 -# 7. Closing a multi-priced balance, slightly more complex -# (different price in each transaction). Hopefully -# equivalent to 8. +# 8. Closing a multi-priced balance, slightly more complex +# (different price in each transaction). < 2019/01/01 (assets) 1A @ 1B @@ -174,6 +185,25 @@ $ hledger -f- close assets -p 2019 (assets) 1A @ 2B $ hledger -f- close assets -p 2019 +2019-12-31 closing balances + assets -2A = 0A + equity:closing balances 2A + +2020-01-01 opening balances + assets 2A = 2A + equity:opening balances -2A + +>=0 + +# 9. The same, with costs. +< +2019/01/01 + (assets) 1A @ 1B + +2019/01/02 + (assets) 1A @ 2B + +$ hledger -f- close assets -p 2019 --show-costs 2019-12-31 closing balances assets -1A @ 1B assets -1A @ 2B = 0A @@ -188,7 +218,7 @@ $ hledger -f- close assets -p 2019 >=0 -# 8. Closing a multi-priced balance, a more complex example. +# 10. Closing a multi-priced balance, a more complex example. # Decimal places specified by the amount display style should not be stripped # even if they are zeros (#1137). < @@ -212,7 +242,7 @@ $ hledger -f- close assets -p 2019 assets:bank 2,836.00 EUR liabilities:employer -$ hledger -f- close -p 2016 assets liabilities +$ hledger -f- close -p 2016 assets liabilities --show-costs 2016-12-31 closing balances assets:bank -5,733.00 EUR = 0.00 EUR liabilities:employer $-10,000.00 @@ -237,10 +267,10 @@ $ hledger -f- close -p 2016 assets liabilities >=0 -# 9. With --interleaved, each transfer's postings are adjacent. +# 11. With --interleaved, each transfer's postings are adjacent. # (And balances with the same cost are not necessarily combined into # a single posting. Eg the 5734 EUR above is 5733 EUR and 1 EUR below.) -$ hledger -f- close -p 2016 assets liabilities --interleaved +$ hledger -f- close -p 2016 assets liabilities --interleaved --show-costs 2016-12-31 closing balances assets:bank -5,733.00 EUR = 0.00 EUR equity:closing balances 5,733.00 EUR @@ -267,7 +297,7 @@ $ hledger -f- close -p 2016 assets liabilities --interleaved >=0 -# 10. A tricky case where a closing posting was rounded and failed to balance (#1164) +# 12. A tricky case where a closing posting was rounded and failed to balance (#1164) < commodity $0.00 commodity AAA 0.00000000 @@ -281,7 +311,7 @@ commodity AAA 0.00000000 assets:usd -$0.1280810 expenses:banking $0.1280810 -$ hledger -f- close -p 2019 assets +$ hledger -f- close -p 2019 assets --show-costs 2019-12-31 closing balances assets:aaa AAA -510.00000000 = AAA 0.00000000 assets:usd $-49.50 @@ -300,3 +330,18 @@ $ hledger -f- close -p 2019 assets >=0 +# 13. The same, without costs and with --interleaved. +$ hledger -f- close -p 2019 assets --interleaved +2019-12-31 closing balances + assets:aaa AAA -510.00000000 = AAA 0.00000000 + equity:closing balances AAA 510.00000000 + assets:usd $-0.109999 = $0.00 + equity:closing balances $0.109999 + +2020-01-01 opening balances + assets:aaa AAA 510.00000000 = AAA 510.00000000 + equity:opening balances AAA -510.00000000 + assets:usd $0.109999 = $0.109999 + equity:opening balances $-0.109999 + +>=0