From cd72c62858affc68071f7ad6231db70ffd40bc1d Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 14 Jan 2024 16:15:28 -1000 Subject: [PATCH] imp:close: exclude E by default; and always exclude the balancing account [#2151] The first of some changes discussed on the issue. A more useful default ? --- hledger/Hledger/Cli/Commands/Close.hs | 19 +++++++++++-------- hledger/Hledger/Cli/Commands/Close.md | 10 ++++++---- hledger/test/close.test | 8 +------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/hledger/Hledger/Cli/Commands/Close.hs b/hledger/Hledger/Cli/Commands/Close.hs index f29ea4487..3b93a7f1c 100644 --- a/hledger/Hledger/Cli/Commands/Close.hs +++ b/hledger/Hledger/Cli/Commands/Close.hs @@ -62,11 +62,11 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do -- currently only one of the six mode flags takes effect at a time (hledger close --close --open only does --open). (close_, open_, assert_, assign_, defclosedesc_, defopendesc_, defcloseacct_, defacctsq_) = if | boolopt "retain" rawopts -> (True, False, False, False, defretaindesc, undefined, defretainacct, Type [Revenue, Expense]) - | boolopt "migrate" rawopts -> (True, True, False, False, defclosedesc, defopendesc, defcloseacct, Type [Asset, Liability, Equity]) - | boolopt "assign" rawopts -> (False, False, False, True, undefined, defopendesc, defcloseacct, Type [Asset, Liability, Equity]) - | boolopt "assert" rawopts -> (False, False, True, False, defclosedesc, undefined, defcloseacct, Type [Asset, Liability, Equity]) - | boolopt "open" rawopts -> (False, True, False, False, undefined, defopendesc, defcloseacct, Type [Asset, Liability, Equity]) - | otherwise -> (True, False, False, False, defclosedesc, undefined, defcloseacct, Type [Asset, Liability, Equity]) -- close + | boolopt "migrate" rawopts -> (True, True, False, False, defclosedesc, defopendesc, defcloseacct, Type [Asset, Liability]) + | boolopt "assign" rawopts -> (False, False, False, True, undefined, defopendesc, defcloseacct, Type [Asset, Liability]) + | boolopt "assert" rawopts -> (False, False, True, False, defclosedesc, undefined, defcloseacct, Type [Asset, Liability]) + | boolopt "open" rawopts -> (False, True, False, False, undefined, defopendesc, defcloseacct, Type [Asset, Liability]) + | otherwise {- close -} -> (True, False, False, False, defclosedesc, undefined, defcloseacct, Type [Asset, Liability]) -- descriptions to use for the closing/opening transactions closedesc = T.pack $ fromMaybe defclosedesc_ $ maybestringopt "close-desc" rawopts @@ -96,11 +96,14 @@ close copts@CliOpts{rawopts_=rawopts, reportspec_=rspec0} j = do -- should we show the amount(s) on the equity posting(s) ? explicit = boolopt "explicit" rawopts || copts ^. infer_costs - -- the balances to close + -- the accounts to close argsacctq = filterQuery (\q -> queryIsAcct q || queryIsType q) argsq q2 = if queryIsNull argsacctq then And [argsq, defacctsq_] else argsq - rspec2 = rspec1{_rsQuery=q2} - (acctbals',_) = balanceReport rspec2 j + -- always exclude the balancing equity account + q3 = And [q2, Not $ Acct $ accountNameToAccountOnlyRegex closeacct] + -- the balances to close + rspec3 = rspec1{_rsQuery=q3} + (acctbals',_) = balanceReport rspec3 j acctbals = map (\(a,_,_,b) -> (a, if show_costs_ ropts then b else mixedAmountStripPrices b)) acctbals' totalamt = maSum $ map snd acctbals diff --git a/hledger/Hledger/Cli/Commands/Close.md b/hledger/Hledger/Cli/Commands/Close.md index a7f20fae2..311fbe2ab 100644 --- a/hledger/Hledger/Cli/Commands/Close.md +++ b/hledger/Hledger/Cli/Commands/Close.md @@ -17,14 +17,16 @@ or as a sort of generic mover of balances from any group of accounts to some oth So it currently has six modes, selected by a mode flag. Use only one of these flags at a time: 1. With `--close` (or no mode flag) it prints a "closing balances" transaction -that zeroes out all the asset, liability, and equity account balances, by default -(this requires inferred or declared [account types](hledger.md#account-types)). -Or, it will zero out the accounts matched by any ACCTQUERY arguments you provide. +that zeroes out all the asset and liability account balances, by default +(this requires [account types](hledger.md#account-types) to be detected or declared.). All of the balances are transferred to a special "opening/closing balances" equity account. +Or, if it will zero out the accounts matched by query arguments you provide. +Eg you could include assets, liabilities, and equity with `hledger close type:ALE`. +The "opening/closing balances" equity account is always excluded. 2. With `--open`, it prints an opposite "opening balances" transaction that restores the same account balances, starting from zero. -This mode is similar to [Ledger's equity command](https://ledger-cli.org/doc/ledger3.html#The-equity-command). + 3. With `--migrate`, it prints both the closing and opening transactions above. This is a common way to migrate balances to a new file at year end; diff --git a/hledger/test/close.test b/hledger/test/close.test index f1a853eee..a553878f0 100644 --- a/hledger/test/close.test +++ b/hledger/test/close.test @@ -23,7 +23,6 @@ $ hledger close -f- -e 2017 2016-12-31 closing balances assets:bank $-80 = $0 assets:cash $-10 = $0 - equity:opening $120 = $0 liabilities $-25 = $0 equity:opening/closing balances @@ -34,7 +33,6 @@ $ hledger close -f- -e 2017 --close 2016-12-31 closing balances assets:bank $-80 = $0 assets:cash $-10 = $0 - equity:opening $120 = $0 liabilities $-25 = $0 equity:opening/closing balances @@ -53,14 +51,12 @@ $ hledger close -f- -p 2016 --migrate 2016-12-31 closing balances assets:bank $-80 = $0 assets:cash $-10 = $0 - equity:opening $120 = $0 liabilities $-25 = $0 equity:opening/closing balances 2017-01-01 opening balances assets:bank $80 = $80 assets:cash $10 = $10 - equity:opening $-120 = $-120 liabilities $25 = $25 equity:opening/closing balances @@ -71,7 +67,6 @@ $ hledger close -f- -p 2016 --open 2017-01-01 opening balances assets:bank $80 = $80 assets:cash $10 = $10 - equity:opening $-120 = $-120 liabilities $25 = $25 equity:opening/closing balances @@ -82,9 +77,8 @@ $ hledger close -f- -p 2016 -x 2016-12-31 closing balances assets:bank $-80 = $0 assets:cash $-10 = $0 - equity:opening $120 = $0 liabilities $-25 = $0 - equity:opening/closing balances $-5 + equity:opening/closing balances $115 >=0