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
|
{-# LANGUAGE QuasiQuotes, RecordWildCards, NoCPP #-}
{-|
The @balancesheet@ command prints a simple balance sheet.
-}
module Hledger.Cli.Balancesheet (
balancesheetmode
,balancesheet
,tests_Hledger_Cli_Balancesheet
) where
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.Options
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"
]
,groupHidden = []
,groupNamed = [generalflagsgroup1]
}
}
where aliases = ["bs"]
-- | Print a simple balance sheet.
balancesheet :: CliOpts -> Journal -> IO ()
balancesheet CliOpts{reportopts_=ropts} j = do
-- let lines = case formatFromOpts ropts of Left err, Right ...
d <- getCurrentDay
let q = queryFromOpts d (withoutBeginDate ropts)
assetreport@(_,assets) = balanceReport ropts (And [q, journalAssetAccountQuery j]) j
liabilityreport@(_,liabilities) = balanceReport ropts (And [q, journalLiabilityAccountQuery j]) j
total = assets + liabilities
LT.putStr $ [lt|Balance Sheet
Assets:
#{unlines $ balanceReportAsText ropts assetreport}
Liabilities:
#{unlines $ balanceReportAsText ropts liabilityreport}
Total:
--------------------
#{padleft 20 $ showMixedAmountWithoutPrice total}
|]
withoutBeginDate :: ReportOpts -> ReportOpts
withoutBeginDate ropts@ReportOpts{..} = ropts{begin_=Nothing, period_=p}
where p = case period_ of Nothing -> Nothing
Just (i, DateSpan _ e) -> Just (i, DateSpan Nothing e)
tests_Hledger_Cli_Balancesheet :: Test
tests_Hledger_Cli_Balancesheet = TestList
[
]
|