Skip to content

Commit

Permalink
imp:close: exclude E by default; and always exclude the balancing acc…
Browse files Browse the repository at this point in the history
…ount [#2151]

The first of some changes discussed on the issue. A more useful default ?
  • Loading branch information
simonmichael committed Jan 21, 2024
1 parent 582d6db commit cd72c62
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
19 changes: 11 additions & 8 deletions hledger/Hledger/Cli/Commands/Close.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions hledger/Hledger/Cli/Commands/Close.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
<!-- 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;
Expand Down
8 changes: 1 addition & 7 deletions hledger/test/close.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit cd72c62

Please sign in to comment.