-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
Balancesheet.hs
73 lines (62 loc) · 2.38 KB
/
Balancesheet.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{-# LANGUAGE QuasiQuotes, RecordWildCards, NoCPP #-}
{-|
The @balancesheet@ command prints a simple balance sheet.
-}
module Hledger.Cli.Balancesheet (
balancesheetmode
,balancesheet
,tests_Hledger_Cli_Balancesheet
) where
import Data.Maybe (fromMaybe)
import qualified Data.Text.Lazy.IO as LT
import System.Console.CmdArgs.Explicit
import Test.HUnit
import Text.Shakespeare.Text
import Hledger
import Hledger.Cli.CliOptions
import Hledger.Cli.Balance
balancesheetmode :: Mode RawOpts
balancesheetmode = (defCommandMode $ ["balancesheet"]++aliases) {
modeHelp = "show a balance sheet" `withAliases` aliases
,modeGroupFlags = Group {
groupUnnamed = [
flagNone ["flat"] (\opts -> setboolopt "flat" opts) "show accounts as a list"
,flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "flat mode: omit N leading account name parts"
,flagNone ["value","V"] (setboolopt "value") "show amounts as their current market value in their default valuation commodity"
]
,groupHidden = []
,groupNamed = [generalflagsgroup1]
}
}
where aliases = ["bs"]
-- | Print a simple balance sheet.
balancesheet :: CliOpts -> Journal -> IO ()
balancesheet CliOpts{reportopts_=ropts} j = do
-- let lines = case lineFormatFromOpts ropts of Left err, Right ...
d <- getCurrentDay
let q = queryFromOpts d (withoutBeginDate ropts)
valuedate = fromMaybe d $ queryEndDate False $ queryFromOpts d ropts
assetreport@(_,assets) = balanceReport ropts (And [q, journalAssetAccountQuery j]) j
liabilityreport@(_,liabilities) = balanceReport ropts (And [q, journalLiabilityAccountQuery j]) j
total = assets + liabilities
convertReport | value_ ropts = balanceReportValue j valuedate
| otherwise = id
convertTotal | value_ ropts = mixedAmountValue j valuedate
| otherwise = id
LT.putStr $ [lt|Balance Sheet
Assets:
#{balanceReportAsText ropts (convertReport assetreport)}
Liabilities:
#{balanceReportAsText ropts (convertReport liabilityreport)}
Total:
--------------------
#{padleft 20 $ showMixedAmountWithoutPrice (convertTotal total)}
|]
withoutBeginDate :: ReportOpts -> ReportOpts
withoutBeginDate ropts@ReportOpts{..} = ropts{period_=p}
where
p = dateSpanAsPeriod $ DateSpan Nothing (periodEnd period_)
tests_Hledger_Cli_Balancesheet :: Test
tests_Hledger_Cli_Balancesheet = TestList
[
]