refactor compoundBalanceCommandMultiColumnReport etc., more correct docs

This commit is contained in:
Simon Michael 2017-07-25 14:28:52 -07:00
parent 5d93c39922
commit 48d909c695

View File

@ -114,7 +114,8 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} CliOpts{command_=cmd, repo
-- single-column report -- single-column report
NoInterval -> do NoInterval -> do
let let
-- concatenate the rendering and sum the totals from each subreport
(subreportstr, total) = (subreportstr, total) =
foldMap (uncurry (compoundBalanceCommandSingleColumnReport ropts' userq j)) cbcqueries foldMap (uncurry (compoundBalanceCommandSingleColumnReport ropts' userq j)) cbcqueries
putStrLn $ title ++ "\n" putStrLn $ title ++ "\n"
@ -132,8 +133,9 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} CliOpts{command_=cmd, repo
-- multi-column report -- multi-column report
_ -> do _ -> do
let let
-- list the tables, list the totals rows, and sum the totals from each subreport
(subreporttables, subreporttotals, Sum overalltotal) = (subreporttables, subreporttotals, Sum overalltotal) =
foldMap (uncurry (compoundBalanceCommandMultiColumnReports ropts' userq j)) cbcqueries foldMap (uncurry (compoundBalanceCommandMultiColumnReport ropts' userq j)) cbcqueries
overalltable = case subreporttables of overalltable = case subreporttables of
t1:ts -> foldl' concatTables t1 ts t1:ts -> foldl' concatTables t1 ts
[] -> T.empty [] -> T.empty
@ -173,9 +175,9 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} CliOpts{command_=cmd, repo
concatTables (Table hLeft hTop dat) (Table hLeft' _ dat') = concatTables (Table hLeft hTop dat) (Table hLeft' _ dat') =
Table (T.Group DoubleLine [hLeft, hLeft']) hTop (dat ++ dat') Table (T.Group DoubleLine [hLeft, hLeft']) hTop (dat ++ dat')
-- | Run one subreport for a single-column compound balance command. -- | Run one subreport for a compound balance command in single-column mode.
-- Currently this returns the plain text rendering of the subreport, -- Currently this returns the plain text rendering of the subreport, and its total.
-- and its total. -- The latter is wrapped in a Sum for easy monoidal combining.
compoundBalanceCommandSingleColumnReport compoundBalanceCommandSingleColumnReport
:: ReportOpts :: ReportOpts
-> Query -> Query
@ -183,40 +185,47 @@ compoundBalanceCommandSingleColumnReport
-> String -> String
-> (Journal -> Query) -> (Journal -> Query)
-> ([String], Sum MixedAmount) -> ([String], Sum MixedAmount)
compoundBalanceCommandSingleColumnReport ropts userq j t subreportq = ([subreportstr], Sum amt) compoundBalanceCommandSingleColumnReport ropts userq j subreporttitle subreportq =
where ([subreportstr], Sum total)
q' = And [userq, subreportq j] where
rep@(_ , amt) q = And [subreportq j, userq]
-- XXX For --historical/--cumulative, we must use singleBalanceReport r@(_,total)
-- (which also forces --no-elide); otherwise we use balanceReport -- XXX For --historical/--cumulative, we must use singleBalanceReport;
-- because it supports eliding boring parents. -- otherwise we use balanceReport -- because it supports eliding boring parents.
-- See also compoundBalanceCommand, Balance.hs -> balance. -- See also compoundBalanceCommand, Balance.hs -> balance.
| balancetype_ ropts `elem` [CumulativeChange, HistoricalBalance] = singleBalanceReport ropts q' j | balancetype_ ropts `elem` [CumulativeChange, HistoricalBalance] = singleBalanceReport ropts q j
| otherwise = balanceReport ropts q' j | otherwise = balanceReport ropts q j
subreportstr = intercalate "\n" [t <> ":", balanceReportAsText ropts rep] subreportstr = intercalate "\n" [subreporttitle <> ":", balanceReportAsText ropts r]
-- | Run all the subreports for a multi-column compound balance command. -- | Run one subreport for a compound balance command in multi-column mode.
-- Currently this returns a table of rendered balance amounts for each -- Currently this returns the table of rendered balance amounts, including the
-- subreport (including a totals row), the totals row for each subreport -- totals row; the totals row again, as mixedamounts; and the grand total.
-- (again, as mixedamounts), and the grand total. -- The first two are wrapped in a list and the third in a Sum, for easy
compoundBalanceCommandMultiColumnReports -- monoidal combining.
compoundBalanceCommandMultiColumnReport
:: ReportOpts :: ReportOpts
-> Query -> Query
-> Journal -> Journal
-> String -> String
-> (Journal -> Query) -> (Journal -> Query)
-> ([Table String String MixedAmount], [[MixedAmount]], Sum MixedAmount) -> ([Table String String MixedAmount], [[MixedAmount]], Sum MixedAmount)
compoundBalanceCommandMultiColumnReports ropts q0 j t q = ([tabl], [coltotals], Sum tot) compoundBalanceCommandMultiColumnReport ropts userq j subreporttitle subreportq =
where ([tabl], [coltotals], Sum tot)
singlesection = "Cash" `isPrefixOf` t -- TODO temp where
ropts' = ropts { no_total_ = singlesection && no_total_ ropts, empty_ = True } ropts' = ropts { no_total_ = singlesection && no_total_ ropts, empty_ = True }
q' = And [q0, q j] where
MultiBalanceReport (dates, rows, (coltotals,tot,avg)) = multiBalanceReport ropts' q' j singlesection = "Cash" `isPrefixOf` subreporttitle -- TODO temp
rows' | empty_ ropts = rows q = And [subreportq j, userq]
| otherwise = filter (not . emptyRow) rows -- run the report
where MultiBalanceReport (dates, rows, (coltotals,tot,avg)) = multiBalanceReport ropts' q j
emptyRow (_,_,_,amts,_,_) = all isZeroMixedAmount amts -- maybe filter all-zero rows from the report
r = MultiBalanceReport (dates, rows', (coltotals, tot, avg)) r = MultiBalanceReport (dates, rows', (coltotals, tot, avg))
Table hLeft hTop dat = balanceReportAsTable ropts' r where
tabl = Table (T.Group SingleLine [Header t, hLeft]) hTop ([]:dat) rows' | empty_ ropts = rows
| otherwise = filter (not . emptyRow) rows
where
emptyRow (_,_,_,amts,_,_) = all isZeroMixedAmount amts
-- convert to a table for rendering
Table hLeft hTop dat = balanceReportAsTable ropts' r
-- tweak the table layout
tabl = Table (T.Group SingleLine [Header subreporttitle, hLeft]) hTop ([]:dat)