init
[tool/hledger.git] / hledger-equity.hs
1 #!/usr/bin/env runhaskell
2 {-
3
4 Like ledger's equity command, print a journal entry posting the total
5 balance of all accounts (or the specified account and its subaccounts)
6 in the default journal.
7
8 An entry like this is useful in the transition to a new journal file,
9 to zero out asset/liability balances in the old file and initialise
10 them in the new one. This way you get correct balances when reporting
11 on either file, and when including both files at once.
12
13 Usage: hledger-equity [ACCTPAT]
14 -}
15
16 import Data.Maybe (fromMaybe)
17 import Hledger.Cli
18
19 argsmode :: Mode RawOpts
20 argsmode = (defCommandMode ["equity"])
21 { modeHelp = "print a journal entry posting the total balance of all accounts"
22 ++ " (or the specified account and its subaccounts)"
23 , modeGroupFlags = Group
24 { groupNamed =
25 [ ("Input",inputflags)
26 , ("Reporting",reportflags)
27 , ("Misc",helpflags)
28 ]
29 , groupUnnamed = []
30 , groupHidden = []
31 }
32 }
33
34 main :: IO ()
35 main = do
36 opts <- getCliOpts argsmode
37 withJournalDo opts $
38 \CliOpts{reportopts_=ropts} j -> do
39 today <- getCurrentDay
40 let ropts_ = ropts{flat_=True}
41 q = queryFromOpts today ropts_
42 (acctbals,_) = balanceReport ropts_ q j
43 balancingamt = negate $ sum $ map (\((_,_,_),b) -> b) acctbals
44 ps = [posting{paccount=a, pamount=b} | ((a,_,_),b) <- acctbals]
45 ++ [posting{paccount="equity:opening balances", pamount=balancingamt}]
46 enddate = fromMaybe today $ queryEndDate (date2_ ropts_) q
47 nps = [posting{paccount=a, pamount=negate b} | ((a,_,_),b) <- acctbals]
48 ++ [posting{paccount="equity:closing balances", pamount=negate balancingamt}]
49 putStr $ showTransactionUnelided (nulltransaction{tdate=enddate, tpostings=nps})
50 putStr $ showTransactionUnelided (nulltransaction{tdate=enddate, tpostings=ps})