cli: "close" can print only opening or closing transaction

This commit is contained in:
Dmitry Astapov 2018-09-22 13:56:44 +01:00 committed by Simon Michael
parent 855bd54d19
commit bda202e4d4
2 changed files with 106 additions and 4 deletions

View File

@ -7,11 +7,13 @@ module Hledger.Cli.Commands.Close (
) )
where where
import Control.Monad (when)
import Data.Maybe import Data.Maybe
import Data.String.Here import Data.String.Here
import Data.Time.Calendar import Data.Time.Calendar
import Hledger import Hledger
import Hledger.Cli.CliOptions import Hledger.Cli.CliOptions
import System.Console.CmdArgs.Explicit as C
closemode = hledgerCommandMode closemode = hledgerCommandMode
[here| close equity [here| close equity
@ -40,6 +42,9 @@ calculated as of end of yesterday, and the opening transaction is dated today.
To close on some other date, use: `hledger close -e OPENINGDATE ...`. To close on some other date, use: `hledger close -e OPENINGDATE ...`.
(-p or date: can also be used, the begin date is ignored.) (-p or date: can also be used, the begin date is ignored.)
You can chose to print just one of the transactions with `--opening`
or `--closing`.
For example, carrying asset/liability balances into a new file for 2018: For example, carrying asset/liability balances into a new file for 2018:
``` ```
$ hledger close -f 2017.journal -e 2018/1/1 ^assets ^liab >>2017.journal $ hledger close -f 2017.journal -e 2018/1/1 ^assets ^liab >>2017.journal
@ -58,14 +63,20 @@ closing at end of 2017:
assets:bank:checking -1 ; date:2018/1/1 assets:bank:checking -1 ; date:2018/1/1
``` ```
|] |]
[] [flagNone ["opening"] (\opts -> setboolopt "opening" opts) "show just opening transaction"
,flagNone ["closing"] (\opts -> setboolopt "closing" opts) "show just closing transaction"
]
[generalflagsgroup1] [generalflagsgroup1]
[] []
([], Just $ argsFlag "[QUERY]") ([], Just $ argsFlag "[QUERY]")
close CliOpts{reportopts_=ropts} j = do close CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do
today <- getCurrentDay today <- getCurrentDay
let let
(opening, closing) =
case (boolopt "opening" rawopts, boolopt "closing" rawopts) of
(False, False) -> (True, True) -- by default show both opening and closing
(o, c) -> (o, c)
ropts_ = ropts{balancetype_=HistoricalBalance, accountlistmode_=ALFlat} ropts_ = ropts{balancetype_=HistoricalBalance, accountlistmode_=ALFlat}
q = queryFromOpts today ropts_ q = queryFromOpts today ropts_
openingdate = fromMaybe today $ queryEndDate False q openingdate = fromMaybe today $ queryEndDate False q
@ -88,5 +99,6 @@ close CliOpts{reportopts_=ropts} j = do
,b <- amounts $ normaliseMixedAmountSquashPricesForDisplay mb ,b <- amounts $ normaliseMixedAmountSquashPricesForDisplay mb
] ]
++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}] ++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}]
putStr $ showTransaction (nulltransaction{tdate=closingdate, tdescription="closing balances", tpostings=nps}) when closing $ putStr $ showTransaction (nulltransaction{tdate=closingdate, tdescription="closing balances", tpostings=nps})
putStr $ showTransaction (nulltransaction{tdate=openingdate, tdescription="opening balances", tpostings=ps}) when opening $ putStr $ showTransaction (nulltransaction{tdate=openingdate, tdescription="opening balances", tpostings=ps})

View File

@ -84,3 +84,93 @@ hledger close -f- -b2017/6/1 -e2018
equity:opening balances equity:opening balances
>>>=0 >>>=0
# Print just the opening transaction
hledger close -f- -p 2016 assets liabilities --opening
<<<
2016/1/1 open
assets:bank $100
assets:cash $20
equity:opening
2016/1/15 spend
expenses:sweets $5
assets:cash
2016/1/19 withdraw
assets:cash $20
assets:bank
2016/1/20 lend
liabilities $25
assets:cash
>>>
2017/01/01 opening balances
assets:bank $80 = $80
assets:cash $10 = $10
liabilities $25 = $25
equity:opening balances
>>>=0
# Print just the closing transaction
hledger close -f- -p 2016 assets liabilities --closing
<<<
2016/1/1 open
assets:bank $100
assets:cash $20
equity:opening
2016/1/15 spend
expenses:sweets $5
assets:cash
2016/1/19 withdraw
assets:cash $20
assets:bank
2016/1/20 lend
liabilities $25
assets:cash
>>>
2016/12/31 closing balances
assets:bank $-80 = $0
assets:cash $-10 = $0
liabilities $-25 = $0
equity:closing balances
>>>=0
# Supplying --opening --closing is the same as just "close"
hledger close -f- -p 2016 assets liabilities --opening --closing
<<<
2016/1/1 open
assets:bank $100
assets:cash $20
equity:opening
2016/1/15 spend
expenses:sweets $5
assets:cash
2016/1/19 withdraw
assets:cash $20
assets:bank
2016/1/20 lend
liabilities $25
assets:cash
>>>
2016/12/31 closing balances
assets:bank $-80 = $0
assets:cash $-10 = $0
liabilities $-25 = $0
equity:closing balances
2017/01/01 opening balances
assets:bank $80 = $80
assets:cash $10 = $10
liabilities $25 = $25
equity:opening balances
>>>=0