2016-06-30 01:01:24 +03:00
|
|
|
#!/usr/bin/env stack
|
|
|
|
{- stack runghc --verbosity info
|
|
|
|
--package hledger-lib
|
|
|
|
--package hledger
|
|
|
|
--package time
|
|
|
|
-}
|
2013-09-20 20:38:54 +04:00
|
|
|
{-
|
2017-01-08 21:24:33 +03:00
|
|
|
|
2016-06-30 01:01:24 +03:00
|
|
|
hledger-equity [HLEDGEROPTS] [QUERY]
|
|
|
|
|
|
|
|
Show a "closing balances" transaction that brings the balance of the
|
|
|
|
accounts matched by QUERY (or all accounts) to zero, and an opposite
|
|
|
|
"opening balances" transaction that restores the balances from zero.
|
|
|
|
|
|
|
|
The opening balances transaction is useful to carry over
|
|
|
|
asset/liability balances if you choose to start a new journal file,
|
|
|
|
eg at the beginning of the year.
|
|
|
|
|
|
|
|
The closing balances transaction is useful to zero out balances in
|
|
|
|
the old file, which gives you the option of reporting on both files
|
|
|
|
at once while still seeing correct balances.
|
2013-09-20 20:38:54 +04:00
|
|
|
|
2016-06-30 01:01:24 +03:00
|
|
|
Balances are calculated, and the opening transaction is dated, as of
|
|
|
|
the report end date, which you should specify with -e or date: (and
|
|
|
|
the closing transaction is dated one day earlier). If a report end
|
|
|
|
date is not specified, it defaults to today.
|
2013-09-20 20:38:54 +04:00
|
|
|
|
2016-06-30 01:01:24 +03:00
|
|
|
Example:
|
|
|
|
$ hledger equity -f 2015.journal -e 2016/1/1 assets liabilities >>2015.journal
|
|
|
|
move opening balances txn to 2016.journal
|
|
|
|
|
|
|
|
Open question: how to handle txns spanning a file boundary ? Eg:
|
|
|
|
2015/12/30 * food
|
|
|
|
expenses:food:dining $10
|
|
|
|
assets:bank:checking -$10 ; date:2016/1/4
|
|
|
|
|
|
|
|
This might or might not have some connection to the concept of
|
|
|
|
"closing the books" in accounting.
|
2013-09-20 20:38:54 +04:00
|
|
|
|
|
|
|
-}
|
2014-02-08 02:05:13 +04:00
|
|
|
|
2016-06-30 01:01:24 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2014-07-15 18:02:14 +04:00
|
|
|
import Data.Maybe (fromMaybe)
|
2016-06-30 01:01:24 +03:00
|
|
|
import Data.Time.Calendar (addDays)
|
2013-09-20 20:38:54 +04:00
|
|
|
import Hledger.Cli
|
|
|
|
|
2014-04-14 23:03:52 +04:00
|
|
|
argsmode :: Mode RawOpts
|
|
|
|
argsmode = (defCommandMode ["equity"])
|
|
|
|
{ modeHelp = "print a journal entry posting the total balance of all accounts"
|
|
|
|
++ " (or the specified account and its subaccounts)"
|
|
|
|
, modeGroupFlags = Group
|
|
|
|
{ groupNamed =
|
2016-06-30 01:01:24 +03:00
|
|
|
-- XXX update to match hledger
|
2014-04-14 23:03:52 +04:00
|
|
|
[ ("Input",inputflags)
|
|
|
|
, ("Reporting",reportflags)
|
|
|
|
, ("Misc",helpflags)
|
|
|
|
]
|
|
|
|
, groupUnnamed = []
|
|
|
|
, groupHidden = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main :: IO ()
|
2013-09-20 20:38:54 +04:00
|
|
|
main = do
|
2014-04-14 23:03:52 +04:00
|
|
|
opts <- getCliOpts argsmode
|
|
|
|
withJournalDo opts $
|
|
|
|
\CliOpts{reportopts_=ropts} j -> do
|
2014-07-15 18:02:14 +04:00
|
|
|
today <- getCurrentDay
|
2015-05-25 18:39:50 +03:00
|
|
|
let ropts_ = ropts{accountlistmode_=ALFlat}
|
2014-07-15 18:02:14 +04:00
|
|
|
q = queryFromOpts today ropts_
|
|
|
|
(acctbals,_) = balanceReport ropts_ q j
|
2016-12-31 18:31:01 +03:00
|
|
|
balancingamt = negate $ sum $ map (\(_,_,_,b) -> b) acctbals
|
2017-01-11 07:44:10 +03:00
|
|
|
ps = [posting{paccount=a, pamount=b} | (a,_,_,b) <- acctbals]
|
2014-04-14 23:03:52 +04:00
|
|
|
++ [posting{paccount="equity:opening balances", pamount=balancingamt}]
|
2014-07-15 18:02:14 +04:00
|
|
|
enddate = fromMaybe today $ queryEndDate (date2_ ropts_) q
|
2017-01-11 07:44:10 +03:00
|
|
|
nps = [posting{paccount=a, pamount=negate b} | (a,_,_,b) <- acctbals]
|
2014-07-24 04:16:13 +04:00
|
|
|
++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}]
|
2016-06-30 01:01:24 +03:00
|
|
|
putStr $ showTransaction (nulltransaction{tdate=addDays (-1) enddate, tdescription="closing balances", tpostings=nps})
|
|
|
|
putStr $ showTransaction (nulltransaction{tdate=enddate, tdescription="opening balances", tpostings=ps})
|