{-# LANGUAGE OverloadedStrings, RecordWildCards, LambdaCase #-}
module Hledger.Cli.CompoundBalanceCommand (
CompoundBalanceCommandSpec(..)
,CBCSubreportSpec(..)
,compoundBalanceCommandMode
,compoundBalanceCommand
) where
import Data.List (foldl')
import Data.Maybe
import qualified Data.Text as TS
import qualified Data.Text.Lazy as TL
import Data.Time.Calendar
import System.Console.CmdArgs.Explicit as C
import Hledger.Read.CsvReader (CSV, printCSV)
import Lucid as L hiding (value_)
import Text.Tabular as T
import Hledger
import Hledger.Cli.Commands.Balance
import Hledger.Cli.CliOptions
import Hledger.Cli.Utils (unsupportedOutputFormatError, writeOutput)
data CompoundBalanceCommandSpec = CompoundBalanceCommandSpec {
CompoundBalanceCommandSpec -> CommandDoc
cbcdoc :: CommandDoc,
CompoundBalanceCommandSpec -> CommandDoc
cbctitle :: String,
CompoundBalanceCommandSpec -> [CBCSubreportSpec]
cbcqueries :: [CBCSubreportSpec],
CompoundBalanceCommandSpec -> BalanceType
cbctype :: BalanceType
}
data CBCSubreportSpec = CBCSubreportSpec {
CBCSubreportSpec -> CommandDoc
cbcsubreporttitle :: String
,CBCSubreportSpec -> Journal -> Query
cbcsubreportquery :: Journal -> Query
,CBCSubreportSpec -> NormalSign
cbcsubreportnormalsign :: NormalSign
,CBCSubreportSpec -> Bool
cbcsubreportincreasestotal :: Bool
}
type CompoundBalanceReport =
( String
, [DateSpan]
, [(String, MultiBalanceReport, Bool)]
, ([MixedAmount], MixedAmount, MixedAmount)
)
compoundBalanceCommandMode :: CompoundBalanceCommandSpec -> Mode RawOpts
compoundBalanceCommandMode :: CompoundBalanceCommandSpec -> Mode RawOpts
compoundBalanceCommandMode CompoundBalanceCommandSpec{..} =
CommandDoc
-> [Flag RawOpts]
-> [(CommandDoc, [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
CommandDoc
cbcdoc
[[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["change"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "change")
("show balance change in each period" CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ BalanceType -> CommandDoc
defType BalanceType
PeriodChange)
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["cumulative"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "cumulative")
("show balance change accumulated across periods (in multicolumn reports)"
CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ BalanceType -> CommandDoc
defType BalanceType
CumulativeChange
)
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["historical","H"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "historical")
("show historical ending balance in each period (includes postings before report start date)"
CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ BalanceType -> CommandDoc
defType BalanceType
HistoricalBalance
)
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["flat"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "flat") "show accounts as a list"
,[CommandDoc]
-> Update RawOpts -> CommandDoc -> CommandDoc -> Flag RawOpts
forall a.
[CommandDoc] -> Update a -> CommandDoc -> CommandDoc -> Flag a
flagReq ["drop"] (\s :: CommandDoc
s opts :: RawOpts
opts -> RawOpts -> Either CommandDoc RawOpts
forall a b. b -> Either a b
Right (RawOpts -> Either CommandDoc RawOpts)
-> RawOpts -> Either CommandDoc RawOpts
forall a b. (a -> b) -> a -> b
$ CommandDoc -> CommandDoc -> RawOpts -> RawOpts
setopt "drop" CommandDoc
s RawOpts
opts) "N" "flat mode: omit N leading account name parts"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["no-total","N"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "no-total") "omit the final total row"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["tree"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "tree") "show accounts as a tree; amounts include subaccounts (default in simple reports)"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["average","A"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "average") "show a row average column (in multicolumn reports)"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["row-total","T"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "row-total") "show a row total column (in multicolumn reports)"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["no-elide"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "no-elide") "don't squash boring parent accounts (in tree mode)"
,[CommandDoc]
-> Update RawOpts -> CommandDoc -> CommandDoc -> Flag RawOpts
forall a.
[CommandDoc] -> Update a -> CommandDoc -> CommandDoc -> Flag a
flagReq ["format"] (\s :: CommandDoc
s opts :: RawOpts
opts -> RawOpts -> Either CommandDoc RawOpts
forall a b. b -> Either a b
Right (RawOpts -> Either CommandDoc RawOpts)
-> RawOpts -> Either CommandDoc RawOpts
forall a b. (a -> b) -> a -> b
$ CommandDoc -> CommandDoc -> RawOpts -> RawOpts
setopt "format" CommandDoc
s RawOpts
opts) "FORMATSTR" "use this custom line format (in simple reports)"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["pretty-tables"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "pretty-tables") "use unicode when displaying tables"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["sort-amount","S"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "sort-amount") "sort by amount instead of account code/name"
,[CommandDoc] -> (RawOpts -> RawOpts) -> CommandDoc -> Flag RawOpts
forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone ["percent", "%"] (CommandDoc -> RawOpts -> RawOpts
setboolopt "percent") "express values in percentage of each column's total"
,[CommandDoc] -> Flag RawOpts
outputFormatFlag ["txt","html","csv","json"]
,Flag RawOpts
outputFileFlag
]
[(CommandDoc, [Flag RawOpts])
generalflagsgroup1]
[Flag RawOpts]
hiddenflags
([], Arg RawOpts -> Maybe (Arg RawOpts)
forall a. a -> Maybe a
Just (Arg RawOpts -> Maybe (Arg RawOpts))
-> Arg RawOpts -> Maybe (Arg RawOpts)
forall a b. (a -> b) -> a -> b
$ CommandDoc -> Arg RawOpts
argsFlag "[QUERY]")
where
defType :: BalanceType -> String
defType :: BalanceType -> CommandDoc
defType bt :: BalanceType
bt | BalanceType
bt BalanceType -> BalanceType -> Bool
forall a. Eq a => a -> a -> Bool
== BalanceType
cbctype = " (default)"
| Bool
otherwise = ""
compoundBalanceCommand :: CompoundBalanceCommandSpec -> (CliOpts -> Journal -> IO ())
compoundBalanceCommand :: CompoundBalanceCommandSpec -> CliOpts -> Journal -> IO ()
compoundBalanceCommand CompoundBalanceCommandSpec{..} opts :: CliOpts
opts@CliOpts{reportopts_ :: CliOpts -> ReportOpts
reportopts_=ropts :: ReportOpts
ropts@ReportOpts{..}, rawopts_ :: CliOpts -> RawOpts
rawopts_=RawOpts
rawopts} j :: Journal
j = do
Day
d <- IO Day
getCurrentDay
let
mBalanceTypeOverride :: Maybe BalanceType
mBalanceTypeOverride =
(CommandDoc -> Maybe BalanceType) -> RawOpts -> Maybe BalanceType
forall a. (CommandDoc -> Maybe a) -> RawOpts -> Maybe a
choiceopt CommandDoc -> Maybe BalanceType
parse RawOpts
rawopts where
parse :: CommandDoc -> Maybe BalanceType
parse = \case
"historical" -> BalanceType -> Maybe BalanceType
forall a. a -> Maybe a
Just BalanceType
HistoricalBalance
"cumulative" -> BalanceType -> Maybe BalanceType
forall a. a -> Maybe a
Just BalanceType
CumulativeChange
"change" -> BalanceType -> Maybe BalanceType
forall a. a -> Maybe a
Just BalanceType
PeriodChange
_ -> Maybe BalanceType
forall a. Maybe a
Nothing
balancetype :: BalanceType
balancetype = BalanceType -> Maybe BalanceType -> BalanceType
forall a. a -> Maybe a -> a
fromMaybe BalanceType
cbctype Maybe BalanceType
mBalanceTypeOverride
ropts' :: ReportOpts
ropts' = ReportOpts
ropts{
balancetype_ :: BalanceType
balancetype_=BalanceType
balancetype,
accountlistmode_ :: AccountListMode
accountlistmode_=if Bool -> Bool
not (ReportOpts -> Bool
flat_ ReportOpts
ropts) Bool -> Bool -> Bool
&& Interval
interval_Interval -> Interval -> Bool
forall a. Eq a => a -> a -> Bool
==Interval
NoInterval Bool -> Bool -> Bool
&& BalanceType
balancetype BalanceType -> [BalanceType] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [BalanceType
CumulativeChange, BalanceType
HistoricalBalance] then AccountListMode
ALTree else AccountListMode
accountlistmode_,
no_total_ :: Bool
no_total_=if Bool
percent_ Bool -> Bool -> Bool
&& [CBCSubreportSpec] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [CBCSubreportSpec]
cbcqueries Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 1 then Bool
True else Bool
no_total_
}
userq :: Query
userq = Day -> ReportOpts -> Query
queryFromOpts Day
d ReportOpts
ropts'
fmt :: CommandDoc
fmt = CliOpts -> CommandDoc
outputFormatFromOpts CliOpts
opts
priceoracle :: PriceOracle
priceoracle = Bool -> Journal -> PriceOracle
journalPriceOracle Bool
infer_value_ Journal
j
subreports :: [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports =
(CBCSubreportSpec
-> (CommandDoc, PeriodicReport AccountName MixedAmount, Bool))
-> [CBCSubreportSpec]
-> [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
forall a b. (a -> b) -> [a] -> [b]
map (\CBCSubreportSpec{..} ->
(CommandDoc
cbcsubreporttitle
,NormalSign
-> PeriodicReport AccountName MixedAmount
-> PeriodicReport AccountName MixedAmount
forall b a.
Num b =>
NormalSign -> PeriodicReport a b -> PeriodicReport a b
prNormaliseSign NormalSign
cbcsubreportnormalsign (PeriodicReport AccountName MixedAmount
-> PeriodicReport AccountName MixedAmount)
-> PeriodicReport AccountName MixedAmount
-> PeriodicReport AccountName MixedAmount
forall a b. (a -> b) -> a -> b
$
ReportOpts
-> Query
-> Journal
-> PriceOracle
-> (Journal -> Query)
-> NormalSign
-> PeriodicReport AccountName MixedAmount
compoundBalanceSubreport ReportOpts
ropts' Query
userq Journal
j PriceOracle
priceoracle Journal -> Query
cbcsubreportquery NormalSign
cbcsubreportnormalsign
,Bool
cbcsubreportincreasestotal
))
[CBCSubreportSpec]
cbcqueries
subtotalrows :: [([MixedAmount], Bool)]
subtotalrows =
[(PeriodicReportRow () MixedAmount -> [MixedAmount]
forall a b. PeriodicReportRow a b -> [b]
prrAmounts (PeriodicReportRow () MixedAmount -> [MixedAmount])
-> PeriodicReportRow () MixedAmount -> [MixedAmount]
forall a b. (a -> b) -> a -> b
$ PeriodicReport AccountName MixedAmount
-> PeriodicReportRow () MixedAmount
forall a b. PeriodicReport a b -> PeriodicReportRow () b
prTotals PeriodicReport AccountName MixedAmount
report, Bool
increasesoveralltotal)
| (_, report :: PeriodicReport AccountName MixedAmount
report, increasesoveralltotal :: Bool
increasesoveralltotal) <- [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports
]
overalltotals :: ([MixedAmount], MixedAmount, MixedAmount)
overalltotals = case [([MixedAmount], Bool)]
subtotalrows of
[] -> ([], MixedAmount
nullmixedamt, MixedAmount
nullmixedamt)
rs :: [([MixedAmount], Bool)]
rs ->
let
numcols :: Int
numcols = [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (([MixedAmount], Bool) -> Int) -> [([MixedAmount], Bool)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([MixedAmount] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length([MixedAmount] -> Int)
-> (([MixedAmount], Bool) -> [MixedAmount])
-> ([MixedAmount], Bool)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.([MixedAmount], Bool) -> [MixedAmount]
forall a b. (a, b) -> a
fst) [([MixedAmount], Bool)]
rs
paddedsignedsubtotalrows :: [[MixedAmount]]
paddedsignedsubtotalrows =
[(MixedAmount -> MixedAmount) -> [MixedAmount] -> [MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map (if Bool
increasesoveralltotal then MixedAmount -> MixedAmount
forall a. a -> a
id else MixedAmount -> MixedAmount
forall a. Num a => a -> a
negate) ([MixedAmount] -> [MixedAmount]) -> [MixedAmount] -> [MixedAmount]
forall a b. (a -> b) -> a -> b
$
Int -> [MixedAmount] -> [MixedAmount]
forall a. Int -> [a] -> [a]
take Int
numcols ([MixedAmount] -> [MixedAmount]) -> [MixedAmount] -> [MixedAmount]
forall a b. (a -> b) -> a -> b
$ [MixedAmount]
as [MixedAmount] -> [MixedAmount] -> [MixedAmount]
forall a. [a] -> [a] -> [a]
++ MixedAmount -> [MixedAmount]
forall a. a -> [a]
repeat MixedAmount
nullmixedamt
| (as :: [MixedAmount]
as,increasesoveralltotal :: Bool
increasesoveralltotal) <- [([MixedAmount], Bool)]
rs
]
coltotals :: [MixedAmount]
coltotals = ([MixedAmount] -> [MixedAmount] -> [MixedAmount])
-> [MixedAmount] -> [[MixedAmount]] -> [MixedAmount]
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((MixedAmount -> MixedAmount -> MixedAmount)
-> [MixedAmount] -> [MixedAmount] -> [MixedAmount]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith MixedAmount -> MixedAmount -> MixedAmount
forall a. Num a => a -> a -> a
(+)) [MixedAmount]
zeros [[MixedAmount]]
paddedsignedsubtotalrows
where zeros :: [MixedAmount]
zeros = Int -> MixedAmount -> [MixedAmount]
forall a. Int -> a -> [a]
replicate Int
numcols MixedAmount
nullmixedamt
grandtotal :: MixedAmount
grandtotal = [MixedAmount] -> MixedAmount
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [MixedAmount]
coltotals
grandavg :: MixedAmount
grandavg | [MixedAmount] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [MixedAmount]
coltotals = MixedAmount
nullmixedamt
| Bool
otherwise = Int -> Quantity
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([MixedAmount] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [MixedAmount]
coltotals) Quantity -> MixedAmount -> MixedAmount
`divideMixedAmount` MixedAmount
grandtotal
in
([MixedAmount]
coltotals, MixedAmount
grandtotal, MixedAmount
grandavg)
colspans :: [DateSpan]
colspans =
case [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports of
(_, PeriodicReport ds :: [DateSpan]
ds _ _, _):_ -> [DateSpan]
ds
[] -> []
title :: CommandDoc
title =
CommandDoc
cbctitle
CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ " "
CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ CommandDoc
titledatestr
CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ CommandDoc
-> (CommandDoc -> CommandDoc) -> Maybe CommandDoc -> CommandDoc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (' 'Char -> CommandDoc -> CommandDoc
forall a. a -> [a] -> [a]
:) Maybe CommandDoc
mtitleclarification
CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ CommandDoc
valuationdesc
where
titledatestr :: CommandDoc
titledatestr
| BalanceType
balancetype BalanceType -> BalanceType -> Bool
forall a. Eq a => a -> a -> Bool
== BalanceType
HistoricalBalance = [Day] -> CommandDoc
showEndDates [Day]
enddates
| Bool
otherwise = DateSpan -> CommandDoc
showDateSpan DateSpan
requestedspan
where
enddates :: [Day]
enddates = (Day -> Day) -> [Day] -> [Day]
forall a b. (a -> b) -> [a] -> [b]
map (Integer -> Day -> Day
addDays (-1)) ([Day] -> [Day]) -> [Day] -> [Day]
forall a b. (a -> b) -> a -> b
$ [Maybe Day] -> [Day]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Day] -> [Day]) -> [Maybe Day] -> [Day]
forall a b. (a -> b) -> a -> b
$ (DateSpan -> Maybe Day) -> [DateSpan] -> [Maybe Day]
forall a b. (a -> b) -> [a] -> [b]
map DateSpan -> Maybe Day
spanEnd [DateSpan]
colspans
requestedspan :: DateSpan
requestedspan = Bool -> Query -> DateSpan
queryDateSpan Bool
date2_ Query
userq DateSpan -> DateSpan -> DateSpan
`spanDefaultsFrom` Bool -> Journal -> DateSpan
journalDateSpan Bool
date2_ Journal
j
mtitleclarification :: Maybe CommandDoc
mtitleclarification = ((BalanceType -> CommandDoc)
-> Maybe BalanceType -> Maybe CommandDoc)
-> Maybe BalanceType
-> (BalanceType -> CommandDoc)
-> Maybe CommandDoc
forall a b c. (a -> b -> c) -> b -> a -> c
flip (BalanceType -> CommandDoc)
-> Maybe BalanceType -> Maybe CommandDoc
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe BalanceType
mBalanceTypeOverride ((BalanceType -> CommandDoc) -> Maybe CommandDoc)
-> (BalanceType -> CommandDoc) -> Maybe CommandDoc
forall a b. (a -> b) -> a -> b
$ \t :: BalanceType
t ->
case BalanceType
t of
PeriodChange -> "(Balance Changes)"
CumulativeChange -> "(Cumulative Ending Balances)"
HistoricalBalance -> "(Historical Ending Balances)"
valuationdesc :: CommandDoc
valuationdesc = case Maybe ValuationType
value_ of
Just (AtCost _mc :: Maybe AccountName
_mc) -> ", valued at cost"
Just (AtThen _mc :: Maybe AccountName
_mc) -> CommandDoc -> CommandDoc
forall a. CommandDoc -> a
error' CommandDoc
unsupportedValueThenError
Just (AtEnd _mc :: Maybe AccountName
_mc) -> ", valued at period ends"
Just (AtNow _mc :: Maybe AccountName
_mc) -> ", current value"
Just (AtDefault _mc :: Maybe AccountName
_mc) | Bool
multiperiod -> ", valued at period ends"
Just (AtDefault _mc :: Maybe AccountName
_mc) -> ", current value"
Just (AtDate d :: Day
d _mc :: Maybe AccountName
_mc) -> ", valued at "CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++Day -> CommandDoc
showDate Day
d
Nothing -> ""
where
multiperiod :: Bool
multiperiod = Interval
interval_ Interval -> Interval -> Bool
forall a. Eq a => a -> a -> Bool
/= Interval
NoInterval
cbr :: (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
cbr =
(CommandDoc
title
,[DateSpan]
colspans
,[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports
,([MixedAmount], MixedAmount, MixedAmount)
overalltotals
)
CliOpts -> CommandDoc -> IO ()
writeOutput CliOpts
opts (CommandDoc -> IO ()) -> CommandDoc -> IO ()
forall a b. (a -> b) -> a -> b
$
case CommandDoc
fmt of
"txt" -> ReportOpts
-> (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
-> CommandDoc
compoundBalanceReportAsText ReportOpts
ropts' (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
cbr
"csv" -> CSV -> CommandDoc
printCSV (ReportOpts
-> (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
-> CSV
compoundBalanceReportAsCsv ReportOpts
ropts (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
cbr) CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ "\n"
"html" -> (CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++"\n") (CommandDoc -> CommandDoc) -> CommandDoc -> CommandDoc
forall a b. (a -> b) -> a -> b
$ Text -> CommandDoc
TL.unpack (Text -> CommandDoc) -> Text -> CommandDoc
forall a b. (a -> b) -> a -> b
$ Html () -> Text
forall a. Html a -> Text
L.renderText (Html () -> Text) -> Html () -> Text
forall a b. (a -> b) -> a -> b
$ ReportOpts
-> (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
-> Html ()
compoundBalanceReportAsHtml ReportOpts
ropts (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
cbr
"json" -> (CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++"\n") (CommandDoc -> CommandDoc) -> CommandDoc -> CommandDoc
forall a b. (a -> b) -> a -> b
$ Text -> CommandDoc
TL.unpack (Text -> CommandDoc) -> Text -> CommandDoc
forall a b. (a -> b) -> a -> b
$ (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
-> Text
forall a. ToJSON a => a -> Text
toJsonText (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
cbr
_ -> CommandDoc -> CommandDoc
forall a. CommandDoc -> a
error' (CommandDoc -> CommandDoc) -> CommandDoc -> CommandDoc
forall a b. (a -> b) -> a -> b
$ CommandDoc -> CommandDoc
unsupportedOutputFormatError CommandDoc
fmt
showEndDates :: [Day] -> String
showEndDates :: [Day] -> CommandDoc
showEndDates es :: [Day]
es = case [Day]
es of
(e :: Day
e:_:_) -> Day -> CommandDoc
showdate Day
e CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ ".." CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ Day -> CommandDoc
showdate ([Day] -> Day
forall a. [a] -> a
last [Day]
es)
[e :: Day
e] -> Day -> CommandDoc
showdate Day
e
[] -> ""
where
showdate :: Day -> CommandDoc
showdate = Day -> CommandDoc
forall a. Show a => a -> CommandDoc
show
compoundBalanceSubreport :: ReportOpts -> Query -> Journal -> PriceOracle -> (Journal -> Query) -> NormalSign -> MultiBalanceReport
compoundBalanceSubreport :: ReportOpts
-> Query
-> Journal
-> PriceOracle
-> (Journal -> Query)
-> NormalSign
-> PeriodicReport AccountName MixedAmount
compoundBalanceSubreport ropts :: ReportOpts
ropts@ReportOpts{..} userq :: Query
userq j :: Journal
j priceoracle :: PriceOracle
priceoracle subreportqfn :: Journal -> Query
subreportqfn subreportnormalsign :: NormalSign
subreportnormalsign = PeriodicReport AccountName MixedAmount
r'
where
ropts' :: ReportOpts
ropts' = ReportOpts
ropts { empty_ :: Bool
empty_=Bool
True, normalbalance_ :: Maybe NormalSign
normalbalance_=NormalSign -> Maybe NormalSign
forall a. a -> Maybe a
Just NormalSign
subreportnormalsign }
q :: Query
q = [Query] -> Query
And [Journal -> Query
subreportqfn Journal
j, Query
userq]
r :: PeriodicReport AccountName MixedAmount
r@(PeriodicReport dates :: [DateSpan]
dates rows :: [PeriodicReportRow AccountName MixedAmount]
rows totals :: PeriodicReportRow () MixedAmount
totals) = ReportOpts
-> Query
-> Journal
-> PriceOracle
-> PeriodicReport AccountName MixedAmount
multiBalanceReportWith ReportOpts
ropts' Query
q Journal
j PriceOracle
priceoracle
r' :: PeriodicReport AccountName MixedAmount
r' | Bool
empty_ = PeriodicReport AccountName MixedAmount
r
| Bool
otherwise = [DateSpan]
-> [PeriodicReportRow AccountName MixedAmount]
-> PeriodicReportRow () MixedAmount
-> PeriodicReport AccountName MixedAmount
forall a b.
[DateSpan]
-> [PeriodicReportRow a b]
-> PeriodicReportRow () b
-> PeriodicReport a b
PeriodicReport [DateSpan]
dates [PeriodicReportRow AccountName MixedAmount]
rows' PeriodicReportRow () MixedAmount
totals
where
nonzeroaccounts :: [AccountName]
nonzeroaccounts =
CommandDoc -> [AccountName] -> [AccountName]
forall a. Show a => CommandDoc -> a -> a
dbg5 "nonzeroaccounts" ([AccountName] -> [AccountName]) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> a -> b
$
(PeriodicReportRow AccountName MixedAmount -> Maybe AccountName)
-> [PeriodicReportRow AccountName MixedAmount] -> [AccountName]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\(PeriodicReportRow act :: AccountName
act _ amts :: [MixedAmount]
amts _ _) ->
if Bool -> Bool
not ((MixedAmount -> Bool) -> [MixedAmount] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all MixedAmount -> Bool
mixedAmountLooksZero [MixedAmount]
amts) then AccountName -> Maybe AccountName
forall a. a -> Maybe a
Just AccountName
act else Maybe AccountName
forall a. Maybe a
Nothing) [PeriodicReportRow AccountName MixedAmount]
rows
rows' :: [PeriodicReportRow AccountName MixedAmount]
rows' = (PeriodicReportRow AccountName MixedAmount -> Bool)
-> [PeriodicReportRow AccountName MixedAmount]
-> [PeriodicReportRow AccountName MixedAmount]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> (PeriodicReportRow AccountName MixedAmount -> Bool)
-> PeriodicReportRow AccountName MixedAmount
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PeriodicReportRow AccountName MixedAmount -> Bool
emptyRow) [PeriodicReportRow AccountName MixedAmount]
rows
where
emptyRow :: PeriodicReportRow AccountName MixedAmount -> Bool
emptyRow (PeriodicReportRow act :: AccountName
act _ amts :: [MixedAmount]
amts _ _) =
(MixedAmount -> Bool) -> [MixedAmount] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all MixedAmount -> Bool
mixedAmountLooksZero [MixedAmount]
amts Bool -> Bool -> Bool
&& Bool -> Bool
not ((AccountName -> Bool) -> [AccountName] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (AccountName
act AccountName -> AccountName -> Bool
`isAccountNamePrefixOf`) [AccountName]
nonzeroaccounts)
compoundBalanceReportAsText :: ReportOpts -> CompoundBalanceReport -> String
compoundBalanceReportAsText :: ReportOpts
-> (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
-> CommandDoc
compoundBalanceReportAsText ropts :: ReportOpts
ropts (title :: CommandDoc
title, _colspans :: [DateSpan]
_colspans, subreports :: [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports, (coltotals :: [MixedAmount]
coltotals, grandtotal :: MixedAmount
grandtotal, grandavg :: MixedAmount
grandavg)) =
CommandDoc
title CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++ "\n\n" CommandDoc -> CommandDoc -> CommandDoc
forall a. [a] -> [a] -> [a]
++
ReportOpts -> Table CommandDoc CommandDoc MixedAmount -> CommandDoc
balanceReportTableAsText ReportOpts
ropts Table CommandDoc CommandDoc MixedAmount
bigtable'
where
singlesubreport :: Bool
singlesubreport = [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1
bigtable :: Table CommandDoc CommandDoc MixedAmount
bigtable =
case ((CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> Table CommandDoc CommandDoc MixedAmount)
-> [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
-> [Table CommandDoc CommandDoc MixedAmount]
forall a b. (a -> b) -> [a] -> [b]
map (ReportOpts
-> Bool
-> (CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> Table CommandDoc CommandDoc MixedAmount
forall c.
ReportOpts
-> Bool
-> (CommandDoc, PeriodicReport AccountName MixedAmount, c)
-> Table CommandDoc CommandDoc MixedAmount
subreportAsTable ReportOpts
ropts Bool
singlesubreport) [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports of
[] -> Table CommandDoc CommandDoc MixedAmount
forall rh ch a. Table rh ch a
T.empty
r :: Table CommandDoc CommandDoc MixedAmount
r:rs :: [Table CommandDoc CommandDoc MixedAmount]
rs -> (Table CommandDoc CommandDoc MixedAmount
-> Table CommandDoc CommandDoc MixedAmount
-> Table CommandDoc CommandDoc MixedAmount)
-> Table CommandDoc CommandDoc MixedAmount
-> [Table CommandDoc CommandDoc MixedAmount]
-> Table CommandDoc CommandDoc MixedAmount
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Table CommandDoc CommandDoc MixedAmount
-> Table CommandDoc CommandDoc MixedAmount
-> Table CommandDoc CommandDoc MixedAmount
forall rh ch a ch. Table rh ch a -> Table rh ch a -> Table rh ch a
concatTables Table CommandDoc CommandDoc MixedAmount
r [Table CommandDoc CommandDoc MixedAmount]
rs
bigtable' :: Table CommandDoc CommandDoc MixedAmount
bigtable'
| ReportOpts -> Bool
no_total_ ReportOpts
ropts Bool -> Bool -> Bool
|| Bool
singlesubreport =
Table CommandDoc CommandDoc MixedAmount
bigtable
| Bool
otherwise =
Table CommandDoc CommandDoc MixedAmount
bigtable
Table CommandDoc CommandDoc MixedAmount
-> SemiTable CommandDoc MixedAmount
-> Table CommandDoc CommandDoc MixedAmount
forall rh ch a. Table rh ch a -> SemiTable rh a -> Table rh ch a
+====+
CommandDoc -> [MixedAmount] -> SemiTable CommandDoc MixedAmount
forall rh a. rh -> [a] -> SemiTable rh a
row "Net:" (
[MixedAmount]
coltotals
[MixedAmount] -> [MixedAmount] -> [MixedAmount]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
row_total_ ReportOpts
ropts then [MixedAmount
grandtotal] else [])
[MixedAmount] -> [MixedAmount] -> [MixedAmount]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
average_ ReportOpts
ropts then [MixedAmount
grandavg] else [])
)
subreportAsTable :: ReportOpts
-> Bool
-> (CommandDoc, PeriodicReport AccountName MixedAmount, c)
-> Table CommandDoc CommandDoc MixedAmount
subreportAsTable ropts :: ReportOpts
ropts singlesubreport :: Bool
singlesubreport (title :: CommandDoc
title, r :: PeriodicReport AccountName MixedAmount
r, _) = Table CommandDoc CommandDoc MixedAmount
t
where
ropts' :: ReportOpts
ropts' | Bool
singlesubreport = ReportOpts
ropts
| Bool
otherwise = ReportOpts
ropts{ no_total_ :: Bool
no_total_=Bool
False }
Table lefthdrs :: Header CommandDoc
lefthdrs tophdrs :: Header CommandDoc
tophdrs cells :: [[MixedAmount]]
cells = ReportOpts
-> PeriodicReport AccountName MixedAmount
-> Table CommandDoc CommandDoc MixedAmount
balanceReportAsTable ReportOpts
ropts' PeriodicReport AccountName MixedAmount
r
t :: Table CommandDoc CommandDoc MixedAmount
t = Header CommandDoc
-> Header CommandDoc
-> [[MixedAmount]]
-> Table CommandDoc CommandDoc MixedAmount
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table (Properties -> [Header CommandDoc] -> Header CommandDoc
forall h. Properties -> [Header h] -> Header h
T.Group Properties
SingleLine [CommandDoc -> Header CommandDoc
forall h. h -> Header h
Header CommandDoc
title, Header CommandDoc
lefthdrs]) Header CommandDoc
tophdrs ([][MixedAmount] -> [[MixedAmount]] -> [[MixedAmount]]
forall a. a -> [a] -> [a]
:[[MixedAmount]]
cells)
concatTables :: Table rh ch a -> Table rh ch a -> Table rh ch a
concatTables (Table hLeft :: Header rh
hLeft hTop :: Header ch
hTop dat :: [[a]]
dat) (Table hLeft' :: Header rh
hLeft' _ dat' :: [[a]]
dat') =
Header rh -> Header ch -> [[a]] -> Table rh ch a
forall rh ch a. Header rh -> Header ch -> [[a]] -> Table rh ch a
Table (Properties -> [Header rh] -> Header rh
forall h. Properties -> [Header h] -> Header h
T.Group Properties
DoubleLine [Header rh
hLeft, Header rh
hLeft']) Header ch
hTop ([[a]]
dat [[a]] -> [[a]] -> [[a]]
forall a. [a] -> [a] -> [a]
++ [[a]]
dat')
compoundBalanceReportAsCsv :: ReportOpts -> CompoundBalanceReport -> CSV
compoundBalanceReportAsCsv :: ReportOpts
-> (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
-> CSV
compoundBalanceReportAsCsv ropts :: ReportOpts
ropts (title :: CommandDoc
title, colspans :: [DateSpan]
colspans, subreports :: [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports, (coltotals :: [MixedAmount]
coltotals, grandtotal :: MixedAmount
grandtotal, grandavg :: MixedAmount
grandavg)) =
CSV -> CSV
addtotals (CSV -> CSV) -> CSV -> CSV
forall a b. (a -> b) -> a -> b
$
CommandDoc -> [CommandDoc]
forall a. IsString a => a -> [a]
padRow CommandDoc
title [CommandDoc] -> CSV -> CSV
forall a. a -> [a] -> [a]
:
("Account" CommandDoc -> [CommandDoc] -> [CommandDoc]
forall a. a -> [a] -> [a]
:
(DateSpan -> CommandDoc) -> [DateSpan] -> [CommandDoc]
forall a b. (a -> b) -> [a] -> [b]
map DateSpan -> CommandDoc
showDateSpanMonthAbbrev [DateSpan]
colspans
[CommandDoc] -> [CommandDoc] -> [CommandDoc]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
row_total_ ReportOpts
ropts then ["Total"] else [])
[CommandDoc] -> [CommandDoc] -> [CommandDoc]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
average_ ReportOpts
ropts then ["Average"] else [])
) [CommandDoc] -> CSV -> CSV
forall a. a -> [a] -> [a]
:
((CommandDoc, PeriodicReport AccountName MixedAmount, Bool) -> CSV)
-> [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
-> CSV
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ReportOpts
-> Bool
-> (CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> CSV
forall c.
ReportOpts
-> Bool
-> (CommandDoc, PeriodicReport AccountName MixedAmount, c)
-> CSV
subreportAsCsv ReportOpts
ropts Bool
singlesubreport) [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports
where
singlesubreport :: Bool
singlesubreport = [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1
subreportAsCsv :: ReportOpts
-> Bool
-> (CommandDoc, PeriodicReport AccountName MixedAmount, c)
-> CSV
subreportAsCsv ropts :: ReportOpts
ropts singlesubreport :: Bool
singlesubreport (subreporttitle :: CommandDoc
subreporttitle, multibalreport :: PeriodicReport AccountName MixedAmount
multibalreport, _) =
CommandDoc -> [CommandDoc]
forall a. IsString a => a -> [a]
padRow CommandDoc
subreporttitle [CommandDoc] -> CSV -> CSV
forall a. a -> [a] -> [a]
:
CSV -> CSV
forall a. [a] -> [a]
tail (ReportOpts -> PeriodicReport AccountName MixedAmount -> CSV
multiBalanceReportAsCsv ReportOpts
ropts' PeriodicReport AccountName MixedAmount
multibalreport)
where
ropts' :: ReportOpts
ropts' | Bool
singlesubreport = ReportOpts
ropts
| Bool
otherwise = ReportOpts
ropts{ no_total_ :: Bool
no_total_=Bool
False }
padRow :: a -> [a]
padRow s :: a
s = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
numcols ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ a
s a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a]
forall a. a -> [a]
repeat ""
where
numcols :: Int
numcols
| [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
-> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports = 1
| Bool
otherwise =
(1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$
(if ReportOpts -> Bool
row_total_ ReportOpts
ropts then (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+) else Int -> Int
forall a. a -> a
id) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$
(if ReportOpts -> Bool
average_ ReportOpts
ropts then (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+) else Int -> Int
forall a. a -> a
id) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$
[Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$
((CommandDoc, PeriodicReport AccountName MixedAmount, Bool) -> Int)
-> [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
-> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ([DateSpan] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([DateSpan] -> Int)
-> ((CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> [DateSpan])
-> (CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PeriodicReport AccountName MixedAmount -> [DateSpan]
forall a b. PeriodicReport a b -> [DateSpan]
prDates (PeriodicReport AccountName MixedAmount -> [DateSpan])
-> ((CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> PeriodicReport AccountName MixedAmount)
-> (CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> [DateSpan]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> PeriodicReport AccountName MixedAmount
forall a b c. (a, b, c) -> b
second3) [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports
addtotals :: CSV -> CSV
addtotals
| ReportOpts -> Bool
no_total_ ReportOpts
ropts Bool -> Bool -> Bool
|| [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = CSV -> CSV
forall a. a -> a
id
| Bool
otherwise = (CSV -> CSV -> CSV
forall a. [a] -> [a] -> [a]
++
["Net:" CommandDoc -> [CommandDoc] -> [CommandDoc]
forall a. a -> [a] -> [a]
:
(MixedAmount -> CommandDoc) -> [MixedAmount] -> [CommandDoc]
forall a b. (a -> b) -> [a] -> [b]
map MixedAmount -> CommandDoc
showMixedAmountOneLineWithoutPrice (
[MixedAmount]
coltotals
[MixedAmount] -> [MixedAmount] -> [MixedAmount]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
row_total_ ReportOpts
ropts then [MixedAmount
grandtotal] else [])
[MixedAmount] -> [MixedAmount] -> [MixedAmount]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
average_ ReportOpts
ropts then [MixedAmount
grandavg] else [])
)
])
compoundBalanceReportAsHtml :: ReportOpts -> CompoundBalanceReport -> Html ()
compoundBalanceReportAsHtml :: ReportOpts
-> (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
-> Html ()
compoundBalanceReportAsHtml ropts :: ReportOpts
ropts cbr :: (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
cbr =
let
(title :: CommandDoc
title, colspans :: [DateSpan]
colspans, subreports :: [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports, (coltotals :: [MixedAmount]
coltotals, grandtotal :: MixedAmount
grandtotal, grandavg :: MixedAmount
grandavg)) = (CommandDoc, [DateSpan],
[(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)],
([MixedAmount], MixedAmount, MixedAmount))
cbr
colspanattr :: Attribute
colspanattr = AccountName -> Attribute
colspan_ (AccountName -> Attribute) -> AccountName -> Attribute
forall a b. (a -> b) -> a -> b
$ CommandDoc -> AccountName
TS.pack (CommandDoc -> AccountName) -> CommandDoc -> AccountName
forall a b. (a -> b) -> a -> b
$ Int -> CommandDoc
forall a. Show a => a -> CommandDoc
show (Int -> CommandDoc) -> Int -> CommandDoc
forall a b. (a -> b) -> a -> b
$
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [DateSpan] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [DateSpan]
colspans Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (if ReportOpts -> Bool
row_total_ ReportOpts
ropts then 1 else 0) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (if ReportOpts -> Bool
average_ ReportOpts
ropts then 1 else 0)
leftattr :: Attribute
leftattr = AccountName -> Attribute
forall arg result. TermRaw arg result => arg -> result
style_ "text-align:left"
blankrow :: Html ()
blankrow = Html () -> Html ()
forall arg result. Term arg result => arg -> result
tr_ (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ [Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
td_ [Attribute
colspanattr] (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ CommandDoc -> Html ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtmlRaw (" "::String)
titlerows :: [Html ()]
titlerows =
[Html () -> Html ()
forall arg result. Term arg result => arg -> result
tr_ (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ [Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
th_ [Attribute
colspanattr, Attribute
leftattr] (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ Html () -> Html ()
forall arg result. Term arg result => arg -> result
h2_ (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ CommandDoc -> Html ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtml CommandDoc
title]
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ [[CommandDoc] -> Html ()
thRow ([CommandDoc] -> Html ()) -> [CommandDoc] -> Html ()
forall a b. (a -> b) -> a -> b
$
"" CommandDoc -> [CommandDoc] -> [CommandDoc]
forall a. a -> [a] -> [a]
:
(DateSpan -> CommandDoc) -> [DateSpan] -> [CommandDoc]
forall a b. (a -> b) -> [a] -> [b]
map DateSpan -> CommandDoc
showDateSpanMonthAbbrev [DateSpan]
colspans
[CommandDoc] -> [CommandDoc] -> [CommandDoc]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
row_total_ ReportOpts
ropts then ["Total"] else [])
[CommandDoc] -> [CommandDoc] -> [CommandDoc]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
average_ ReportOpts
ropts then ["Average"] else [])
]
thRow :: [String] -> Html ()
thRow :: [CommandDoc] -> Html ()
thRow = Html () -> Html ()
forall arg result. Term arg result => arg -> result
tr_ (Html () -> Html ())
-> ([CommandDoc] -> Html ()) -> [CommandDoc] -> Html ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Html ()] -> Html ()
forall a. Monoid a => [a] -> a
mconcat ([Html ()] -> Html ())
-> ([CommandDoc] -> [Html ()]) -> [CommandDoc] -> Html ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CommandDoc -> Html ()) -> [CommandDoc] -> [Html ()]
forall a b. (a -> b) -> [a] -> [b]
map (Html () -> Html ()
forall arg result. Term arg result => arg -> result
th_ (Html () -> Html ())
-> (CommandDoc -> Html ()) -> CommandDoc -> Html ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommandDoc -> Html ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtml)
subreportrows :: (String, MultiBalanceReport, Bool) -> [Html ()]
subreportrows :: (CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> [Html ()]
subreportrows (subreporttitle :: CommandDoc
subreporttitle, mbr :: PeriodicReport AccountName MixedAmount
mbr, _increasestotal :: Bool
_increasestotal) =
let
(_,bodyrows :: [Html ()]
bodyrows,mtotalsrow :: Maybe (Html ())
mtotalsrow) = ReportOpts
-> PeriodicReport AccountName MixedAmount
-> (Html (), [Html ()], Maybe (Html ()))
multiBalanceReportHtmlRows ReportOpts
ropts PeriodicReport AccountName MixedAmount
mbr
in
[Html () -> Html ()
forall arg result. Term arg result => arg -> result
tr_ (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ [Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
th_ [Attribute
colspanattr, Attribute
leftattr] (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ CommandDoc -> Html ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtml CommandDoc
subreporttitle]
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ [Html ()]
bodyrows
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ [Html ()] -> (Html () -> [Html ()]) -> Maybe (Html ()) -> [Html ()]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (Html () -> [Html ()] -> [Html ()]
forall a. a -> [a] -> [a]
:[]) Maybe (Html ())
mtotalsrow
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ [Html ()
blankrow]
totalrows :: [Html ()]
totalrows | ReportOpts -> Bool
no_total_ ReportOpts
ropts Bool -> Bool -> Bool
|| [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = []
| Bool
otherwise =
let defstyle :: Attribute
defstyle = AccountName -> Attribute
forall arg result. TermRaw arg result => arg -> result
style_ "text-align:right"
in
[Html () -> Html ()
forall arg result. Term arg result => arg -> result
tr_ (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ [Html ()] -> Html ()
forall a. Monoid a => [a] -> a
mconcat ([Html ()] -> Html ()) -> [Html ()] -> Html ()
forall a b. (a -> b) -> a -> b
$
[Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
th_ [AccountName -> Attribute
class_ "", AccountName -> Attribute
forall arg result. TermRaw arg result => arg -> result
style_ "text-align:left"] "Net:"
Html () -> [Html ()] -> [Html ()]
forall a. a -> [a] -> [a]
: [[Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
th_ [AccountName -> Attribute
class_ "amount coltotal", Attribute
defstyle] (CommandDoc -> Html ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtml (CommandDoc -> Html ()) -> CommandDoc -> Html ()
forall a b. (a -> b) -> a -> b
$ MixedAmount -> CommandDoc
showMixedAmountOneLineWithoutPrice MixedAmount
a) | MixedAmount
a <- [MixedAmount]
coltotals]
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
row_total_ ReportOpts
ropts then [[Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
th_ [AccountName -> Attribute
class_ "amount coltotal", Attribute
defstyle] (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ CommandDoc -> Html ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtml (CommandDoc -> Html ()) -> CommandDoc -> Html ()
forall a b. (a -> b) -> a -> b
$ MixedAmount -> CommandDoc
showMixedAmountOneLineWithoutPrice (MixedAmount -> CommandDoc) -> MixedAmount -> CommandDoc
forall a b. (a -> b) -> a -> b
$ MixedAmount
grandtotal] else [])
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ (if ReportOpts -> Bool
average_ ReportOpts
ropts then [[Attribute] -> Html () -> Html ()
forall arg result. Term arg result => arg -> result
th_ [AccountName -> Attribute
class_ "amount colaverage", Attribute
defstyle] (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ CommandDoc -> Html ()
forall a (m :: * -> *). (ToHtml a, Monad m) => a -> HtmlT m ()
toHtml (CommandDoc -> Html ()) -> CommandDoc -> Html ()
forall a b. (a -> b) -> a -> b
$ MixedAmount -> CommandDoc
showMixedAmountOneLineWithoutPrice (MixedAmount -> CommandDoc) -> MixedAmount -> CommandDoc
forall a b. (a -> b) -> a -> b
$ MixedAmount
grandavg] else [])
]
in do
AccountName -> Html ()
forall arg result. TermRaw arg result => arg -> result
style_ ([AccountName] -> AccountName
TS.unlines [""
,"td { padding:0 0.5em; }"
,"td:nth-child(1) { white-space:nowrap; }"
,"tr:nth-child(even) td { background-color:#eee; }"
])
[Attribute] -> Html ()
forall (m :: * -> *). Applicative m => [Attribute] -> HtmlT m ()
link_ [AccountName -> Attribute
rel_ "stylesheet", AccountName -> Attribute
href_ "hledger.css"]
Html () -> Html ()
forall arg result. Term arg result => arg -> result
table_ (Html () -> Html ()) -> Html () -> Html ()
forall a b. (a -> b) -> a -> b
$ [Html ()] -> Html ()
forall a. Monoid a => [a] -> a
mconcat ([Html ()] -> Html ()) -> [Html ()] -> Html ()
forall a b. (a -> b) -> a -> b
$
[Html ()]
titlerows
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ [Html ()
blankrow]
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ ((CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> [Html ()])
-> [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
-> [Html ()]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (CommandDoc, PeriodicReport AccountName MixedAmount, Bool)
-> [Html ()]
subreportrows [(CommandDoc, PeriodicReport AccountName MixedAmount, Bool)]
subreports
[Html ()] -> [Html ()] -> [Html ()]
forall a. [a] -> [a] -> [a]
++ [Html ()]
totalrows