dev:budget: big refactor, clarify

Big functions like budgetReportAsTable are grouped as much as possible
into more modular sub-scopes.
This commit is contained in:
Simon Michael 2024-02-03 08:27:02 -10:00
parent 3cad760851
commit c701e3a663

View File

@ -49,20 +49,29 @@ import Hledger.Reports.MultiBalanceReport
import Data.Ord (comparing) import Data.Ord (comparing)
import Control.Monad ((>=>)) import Control.Monad ((>=>))
-- All MixedAmounts:
type BudgetGoal = Change type BudgetGoal = Change
type BudgetTotal = Total type BudgetTotal = Total
type BudgetAverage = Average type BudgetAverage = Average
-- | A budget report tracks expected and actual changes per account and subperiod. -- | A budget report tracks expected and actual changes per account and subperiod.
-- Each table cell has an actual change amount and/or a budget goal amount.
type BudgetCell = (Maybe Change, Maybe BudgetGoal) type BudgetCell = (Maybe Change, Maybe BudgetGoal)
-- | A row in a budget report table - account name and data cells.
type BudgetReportRow = PeriodicReportRow DisplayName BudgetCell type BudgetReportRow = PeriodicReportRow DisplayName BudgetCell
-- | A full budget report table.
type BudgetReport = PeriodicReport DisplayName BudgetCell type BudgetReport = PeriodicReport DisplayName BudgetCell
-- A BudgetCell's data values rendered for display - the actual change amount,
-- the budget goal amount if any, and the corresponding goal percentage if possible.
type BudgetDisplayCell = (WideBuilder, Maybe (WideBuilder, Maybe WideBuilder)) type BudgetDisplayCell = (WideBuilder, Maybe (WideBuilder, Maybe WideBuilder))
-- | A row of rendered budget data cells.
type BudgetDisplayRow = [BudgetDisplayCell] type BudgetDisplayRow = [BudgetDisplayCell]
type BudgetShowMixed = MixedAmount -> [WideBuilder]
type BudgetPercBudget = Change -> BudgetGoal -> [Maybe Percentage] -- | An amount render helper for the budget report. Renders each commodity separately.
type BudgetShowAmountsFn = MixedAmount -> [WideBuilder]
-- | A goal percentage calculating helper for the budget report.
type BudgetCalcPercentagesFn = Change -> BudgetGoal -> [Maybe Percentage]
_brrShowDebug :: BudgetReportRow -> String _brrShowDebug :: BudgetReportRow -> String
_brrShowDebug (PeriodicReportRow dname budgetpairs _tot _avg) = _brrShowDebug (PeriodicReportRow dname budgetpairs _tot _avg) =
@ -225,9 +234,9 @@ combineBudgetAndActual ropts j
, let budgetmamts = maybe (Nothing <$ periods) (map Just . first3) mbudgetgoals :: [Maybe BudgetGoal] , let budgetmamts = maybe (Nothing <$ periods) (map Just . first3) mbudgetgoals :: [Maybe BudgetGoal]
, let mbudgettot = second3 <$> mbudgetgoals :: Maybe BudgetTotal , let mbudgettot = second3 <$> mbudgetgoals :: Maybe BudgetTotal
, let mbudgetavg = third3 <$> mbudgetgoals :: Maybe BudgetAverage , let mbudgetavg = third3 <$> mbudgetgoals :: Maybe BudgetAverage
, let acctBudgetByPeriod = Map.fromList [ (p,budgetamt) | (p, Just budgetamt) <- zip budgetperiods budgetmamts ] :: Map DateSpan BudgetGoal , let acctGoalByPeriod = Map.fromList [ (p,budgetamt) | (p, Just budgetamt) <- zip budgetperiods budgetmamts ] :: Map DateSpan BudgetGoal
, let acctActualByPeriod = Map.fromList [ (p,actualamt) | (p, Just actualamt) <- zip actualperiods (map Just actualamts) ] :: Map DateSpan Change , let acctActualByPeriod = Map.fromList [ (p,actualamt) | (p, Just actualamt) <- zip actualperiods (map Just actualamts) ] :: Map DateSpan Change
, let amtandgoals = [ (Map.lookup p acctActualByPeriod, Map.lookup p acctBudgetByPeriod) | p <- periods ] :: [BudgetCell] , let amtandgoals = [ (Map.lookup p acctActualByPeriod, Map.lookup p acctGoalByPeriod) | p <- periods ] :: [BudgetCell]
, let totamtandgoal = (Just actualtot, mbudgettot) , let totamtandgoal = (Just actualtot, mbudgettot)
, let avgamtandgoal = (Just actualavg, mbudgetavg) , let avgamtandgoal = (Just actualavg, mbudgetavg)
] ]
@ -245,8 +254,8 @@ combineBudgetAndActual ropts j
PeriodicReportRow acct amtandgoals totamtandgoal avgamtandgoal PeriodicReportRow acct amtandgoals totamtandgoal avgamtandgoal
| PeriodicReportRow acct budgetgoals budgettot budgetavg <- budgetrows | PeriodicReportRow acct budgetgoals budgettot budgetavg <- budgetrows
, displayFull acct `notElem` map prrFullName actualsplusgoals , displayFull acct `notElem` map prrFullName actualsplusgoals
, let acctBudgetByPeriod = Map.fromList $ zip budgetperiods budgetgoals :: Map DateSpan BudgetGoal , let acctGoalByPeriod = Map.fromList $ zip budgetperiods budgetgoals :: Map DateSpan BudgetGoal
, let amtandgoals = [ (Just 0, Map.lookup p acctBudgetByPeriod) | p <- periods ] :: [BudgetCell] , let amtandgoals = [ (Just 0, Map.lookup p acctGoalByPeriod) | p <- periods ] :: [BudgetCell]
, let totamtandgoal = (Just 0, Just budgettot) , let totamtandgoal = (Just 0, Just budgettot)
, let avgamtandgoal = (Just 0, Just budgetavg) , let avgamtandgoal = (Just 0, Just budgetavg)
] ]
@ -262,11 +271,11 @@ combineBudgetAndActual ropts j
rows = actualsplusgoals ++ othergoals rows = actualsplusgoals ++ othergoals
totalrow = PeriodicReportRow () totalrow = PeriodicReportRow ()
[ (Map.lookup p totActualByPeriod, Map.lookup p totBudgetByPeriod) | p <- periods ] [ (Map.lookup p totActualByPeriod, Map.lookup p totGoalByPeriod) | p <- periods ]
( Just actualgrandtot, budget budgetgrandtot ) ( Just actualgrandtot, budget budgetgrandtot )
( Just actualgrandavg, budget budgetgrandavg ) ( Just actualgrandavg, budget budgetgrandavg )
where where
totBudgetByPeriod = Map.fromList $ zip budgetperiods budgettots :: Map DateSpan BudgetTotal totGoalByPeriod = Map.fromList $ zip budgetperiods budgettots :: Map DateSpan BudgetTotal
totActualByPeriod = Map.fromList $ zip actualperiods actualtots :: Map DateSpan Change totActualByPeriod = Map.fromList $ zip actualperiods actualtots :: Map DateSpan Change
budget b = if mixedAmountLooksZero b then Nothing else Just b budget b = if mixedAmountLooksZero b then Nothing else Just b
@ -290,27 +299,17 @@ budgetReportAsText ropts@ReportOpts{..} budgetr = TB.toLazyText $
-- | Build a 'Table' from a multi-column balance report. -- | Build a 'Table' from a multi-column balance report.
budgetReportAsTable :: ReportOpts -> BudgetReport -> Tab.Table Text Text WideBuilder budgetReportAsTable :: ReportOpts -> BudgetReport -> Tab.Table Text Text WideBuilder
budgetReportAsTable budgetReportAsTable ReportOpts{..} (PeriodicReport spans items totrow) =
ReportOpts{..} maybetransposetable $
(PeriodicReport spans items tr) = addtotalrow $
maybetransposetable $
addtotalrow $
Tab.Table Tab.Table
(Tab.Group Tab.NoLine $ map Tab.Header accts) (Tab.Group Tab.NoLine $ map Tab.Header accts)
(Tab.Group Tab.NoLine $ map Tab.Header colheadings) (Tab.Group Tab.NoLine $ map Tab.Header colheadings)
rows rows
where where
colheadings = ["Commodity" | layout_ == LayoutBare] maybetransposetable
++ map (reportPeriodName balanceaccum_ spans) spans | transpose_ = \(Tab.Table rh ch vals) -> Tab.Table ch rh (transpose vals)
++ [" Total" | row_total_] | otherwise = id
++ ["Average" | average_]
-- FIXME. Have to check explicitly for which to render here, since
-- budgetReport sets accountlistmode to ALTree. Find a principled way to do
-- this.
renderacct row = case accountlistmode_ of
ALTree -> T.replicate ((prrDepth row - 1)*2) " " <> prrDisplayName row
ALFlat -> accountNameDrop (drop_) $ prrFullName row
addtotalrow addtotalrow
| no_total_ = id | no_total_ = id
@ -318,140 +317,180 @@ budgetReportAsTable
ch = Tab.Header [] -- ignored ch = Tab.Header [] -- ignored
in (flip (Tab.concatTables Tab.SingleLine) $ Tab.Table rh ch totalrows) in (flip (Tab.concatTables Tab.SingleLine) $ Tab.Table rh ch totalrows)
maybetranspose colheadings = ["Commodity" | layout_ == LayoutBare]
| transpose_ = transpose ++ map (reportPeriodName balanceaccum_ spans) spans
| otherwise = id ++ [" Total" | row_total_]
++ ["Average" | average_]
maybetransposetable (accts, rows, totalrows) =
| transpose_ = \(Tab.Table rh ch vals) -> Tab.Table ch rh (transpose vals) (accts'
| otherwise = id ,maybecommcol itemscs $ showcells texts
,maybecommcol totrowcs $ showtotrow totrowtexts)
(accts, rows, totalrows) = (accts', prependcs itemscs (padcells texts), prependcs trcs (padtr trtexts))
where where
shownitems :: [[(AccountName, WideBuilder, BudgetDisplayRow)]] -- If --layout=bare, prepend a commodities column.
shownitems = (map (\i -> map (\(cs, cvals) -> (renderacct i, cs, cvals)) . showrow $ rowToBudgetCells i) items) maybecommcol :: [WideBuilder] -> [[WideBuilder]] -> [[WideBuilder]]
(accts', itemscs, texts) = unzip3 $ concat shownitems maybecommcol cs
showntr :: [[(WideBuilder, BudgetDisplayRow)]]
showntr = [showrow $ rowToBudgetCells tr]
(trcs, trtexts) = unzip $ concat showntr
trwidths
| transpose_ = drop (length texts) widths
| otherwise = widths
padcells = maybetranspose . map (map (uncurry paddisplaycell) . zip widths) . maybetranspose
padtr = maybetranspose . map (map (uncurry paddisplaycell) . zip trwidths) . maybetranspose
-- with --layout=bare, begin with a commodity column
prependcs cs
| layout_ == LayoutBare = zipWith (:) cs | layout_ == LayoutBare = zipWith (:) cs
| otherwise = id | otherwise = id
rowToBudgetCells (PeriodicReportRow _ as rowtot rowavg) = as showcells, showtotrow :: [[BudgetDisplayCell]] -> [[WideBuilder]]
++ [rowtot | row_total_ && not (null as)] (showcells, showtotrow) =
++ [rowavg | average_ && not (null as)] (maybetranspose . map (zipWith showBudgetDisplayCell widths) . maybetranspose
,maybetranspose . map (zipWith showBudgetDisplayCell totrowwidths) . maybetranspose)
where
-- | Combine a BudgetDisplayCell's rendered values into a "[PERCENT of GOAL]" rendering,
-- respecting the given widths.
showBudgetDisplayCell :: (Int, Int, Int) -> BudgetDisplayCell -> WideBuilder
showBudgetDisplayCell (actualwidth, budgetwidth, percentwidth) (actual, mbudget) =
flip WideBuilder (actualwidth + totalbudgetwidth) $
toPadded actual <> maybe emptycell showBudgetGoalAndPercentage mbudget
-- functions for displaying budget cells depending on `commodity-layout_` option where
rowfuncs :: [CommoditySymbol] -> (BudgetShowMixed, BudgetPercBudget) toPadded (WideBuilder b w) = (TB.fromText . flip T.replicate " " $ actualwidth - w) <> b
rowfuncs cs = case layout_ of
LayoutWide width ->
( pure . showMixedAmountB oneLineNoCostFmt{displayMaxWidth=width, displayColour=color_}
, \a -> pure . percentage a)
_ -> ( showMixedAmountLinesB noCostFmt{displayCommodity=layout_/=LayoutBare, displayCommodityOrder=Just cs, displayMinWidth=Nothing, displayColour=color_}
, \a b -> map (percentage' a b) cs)
showrow :: [BudgetCell] -> [(WideBuilder, BudgetDisplayRow)] (totalpercentwidth, totalbudgetwidth) =
showrow row = let totalpercentwidth' = if percentwidth == 0 then 0 else percentwidth + 5
let cs = budgetCellsCommodities row in ( totalpercentwidth'
(showmixed, percbudget) = rowfuncs cs , if budgetwidth == 0 then 0 else budgetwidth + totalpercentwidth' + 3
in zip (map wbFromText cs) )
. transpose
. map (showcell showmixed percbudget)
$ row
budgetCellsCommodities = S.toList . foldl' S.union mempty . map budgetCellCommodities emptycell :: TB.Builder
budgetCellCommodities :: BudgetCell -> S.Set CommoditySymbol emptycell = TB.fromText $ T.replicate totalbudgetwidth " "
budgetCellCommodities (am, bm) = f am `S.union` f bm
where f = maybe mempty maCommodities
cellswidth :: [BudgetCell] -> [[(Int, Int, Int)]] showBudgetGoalAndPercentage :: (WideBuilder, Maybe WideBuilder) -> TB.Builder
cellswidth row = showBudgetGoalAndPercentage (goal, perc) =
let cs = budgetCellsCommodities row let perct = case perc of
(showmixed, percbudget) = rowfuncs cs Nothing -> T.replicate totalpercentwidth " "
disp = showcell showmixed percbudget Just pct -> T.replicate (percentwidth - wbWidth pct) " " <> wbToText pct <> "% of "
budgetpercwidth = wbWidth *** maybe 0 wbWidth in TB.fromText $ " [" <> perct <> T.replicate (budgetwidth - wbWidth goal) " " <> wbToText goal <> "]"
cellwidth (am, bm) = let (bw, pw) = maybe (0, 0) budgetpercwidth bm in (wbWidth am, bw, pw)
in map (map cellwidth . disp) row
-- build a list of widths for each column. In the case of transposed budget -- | Build a list of widths for each column.
-- reports, the total 'row' must be included in this list -- When --transpose is used, the totals row must be included in this list.
widths = zip3 actualwidths budgetwidths percentwidths widths :: [(Int, Int, Int)]
where widths = zip3 actualwidths budgetwidths percentwidths
actualwidths = map (maximum' . map first3 ) $ cols where
budgetwidths = map (maximum' . map second3) $ cols actualwidths = map (maximum' . map first3 ) $ cols
percentwidths = map (maximum' . map third3 ) $ cols budgetwidths = map (maximum' . map second3) $ cols
catcolumnwidths = foldl' (zipWith (++)) $ repeat [] percentwidths = map (maximum' . map third3 ) $ cols
cols = maybetranspose $ catcolumnwidths $ map (cellswidth . rowToBudgetCells) items ++ [cellswidth $ rowToBudgetCells tr] catcolumnwidths = foldl' (zipWith (++)) $ repeat []
cols = maybetranspose $ catcolumnwidths $ map (cellswidth . rowToBudgetCells) items ++ [cellswidth $ rowToBudgetCells totrow]
-- split a BudgetCell into BudgetDisplayCell's (one per commodity when applicable) cellswidth :: [BudgetCell] -> [[(Int, Int, Int)]]
showcell :: BudgetShowMixed -> BudgetPercBudget -> BudgetCell -> BudgetDisplayRow cellswidth row =
showcell showmixed percbudget (actual, mbudget) = zip (showmixed actual') full let cs = budgetCellsCommodities row
where (showmixed, percbudget) = mkBudgetDisplayFns cs
actual' = fromMaybe nullmixedamt actual disp = showcell showmixed percbudget
budgetpercwidth = wbWidth *** maybe 0 wbWidth
cellwidth (am, bm) = let (bw, pw) = maybe (0, 0) budgetpercwidth bm in (wbWidth am, bw, pw)
in map (map cellwidth . disp) row
budgetAndPerc b = totrowwidths :: [(Int, Int, Int)]
zip (showmixed b) (fmap (wbFromText . T.pack . show . roundTo 0) <$> percbudget actual' b) totrowwidths
| transpose_ = drop (length texts) widths
| otherwise = widths
full maybetranspose
| Just b <- mbudget = Just <$> budgetAndPerc b | transpose_ = transpose
| otherwise = repeat Nothing | otherwise = id
paddisplaycell :: (Int, Int, Int) -> BudgetDisplayCell -> WideBuilder (accts', itemscs, texts) = unzip3 $ concat shownitems
paddisplaycell (actualwidth, budgetwidth, percentwidth) (actual, mbudget) = full where
where shownitems :: [[(AccountName, WideBuilder, BudgetDisplayRow)]]
toPadded (WideBuilder b w) = shownitems =
(TB.fromText . flip T.replicate " " $ actualwidth - w) <> b map (\i ->
let addacctcolumn = map (\(cs, cvals) -> (renderacct i, cs, cvals))
in addacctcolumn . showrow . rowToBudgetCells $ i)
items
where
-- FIXME. Have to check explicitly for which to render here, since
-- budgetReport sets accountlistmode to ALTree. Find a principled way to do
-- this.
renderacct row = case accountlistmode_ of
ALTree -> T.replicate ((prrDepth row - 1)*2) " " <> prrDisplayName row
ALFlat -> accountNameDrop (drop_) $ prrFullName row
(totalpercentwidth, totalbudgetwidth) = (totrowcs, totrowtexts) = unzip $ concat showntotrow
let totalpercentwidth' = if percentwidth == 0 then 0 else percentwidth + 5 where
in ( totalpercentwidth' showntotrow :: [[(WideBuilder, BudgetDisplayRow)]]
, if budgetwidth == 0 then 0 else budgetwidth + totalpercentwidth' + 3 showntotrow = [showrow $ rowToBudgetCells totrow]
)
-- | Display a padded budget string -- | Get the data cells from a row or totals row, maybe adding
budgetb (budget, perc) = -- the row total and/or row average depending on options.
let perct = case perc of rowToBudgetCells :: PeriodicReportRow a BudgetCell -> [BudgetCell]
Nothing -> T.replicate totalpercentwidth " " rowToBudgetCells (PeriodicReportRow _ as rowtot rowavg) = as
Just pct -> T.replicate (percentwidth - wbWidth pct) " " <> wbToText pct <> "% of " ++ [rowtot | row_total_ && not (null as)]
in TB.fromText $ " [" <> perct <> T.replicate (budgetwidth - wbWidth budget) " " <> wbToText budget <> "]" ++ [rowavg | average_ && not (null as)]
emptyBudget = TB.fromText $ T.replicate totalbudgetwidth " " -- | Render a row's data cells as "BudgetDisplayCell"s, and a rendered list of commodity symbols.
showrow :: [BudgetCell] -> [(WideBuilder, BudgetDisplayRow)]
showrow row =
let cs = budgetCellsCommodities row
(showmixed, percbudget) = mkBudgetDisplayFns cs
in zip (map wbFromText cs)
. transpose
. map (showcell showmixed percbudget)
$ row
full = flip WideBuilder (actualwidth + totalbudgetwidth) $ budgetCellsCommodities :: [BudgetCell] -> [CommoditySymbol]
toPadded actual <> maybe emptyBudget budgetb mbudget budgetCellsCommodities = S.toList . foldl' S.union mempty . map budgetCellCommodities
where
budgetCellCommodities :: BudgetCell -> S.Set CommoditySymbol
budgetCellCommodities (am, bm) = f am `S.union` f bm
where f = maybe mempty maCommodities
-- | Calculate the percentage of actual change to budget goal to show, if any. -- | Render a "BudgetCell"'s amounts as "BudgetDisplayCell"s (one per commodity).
-- If valuing at cost, both amounts are converted to cost before comparing. showcell :: BudgetShowAmountsFn -> BudgetCalcPercentagesFn -> BudgetCell -> BudgetDisplayRow
-- A percentage will not be shown if: showcell showCommodityAmounts calcCommodityPercentages (mactual, mbudget) =
-- - actual or goal are not the same, single, commodity zip actualamts budgetinfos
-- - the goal is zero where
percentage :: Change -> BudgetGoal -> Maybe Percentage actual = fromMaybe nullmixedamt mactual
percentage actual budget = actualamts = showCommodityAmounts actual
case (costedAmounts actual, costedAmounts budget) of budgetinfos =
([a], [b]) | (acommodity a == acommodity b || amountLooksZero a) && not (amountLooksZero b) case mbudget of
-> Just $ 100 * aquantity a / aquantity b Nothing -> repeat Nothing
_ -> -- trace (pshow $ (maybecost actual, maybecost budget)) -- debug missing percentage Just goal -> map Just $ showGoalAmountsAndPercentages goal
Nothing where
where showGoalAmountsAndPercentages :: MixedAmount -> [(WideBuilder, Maybe WideBuilder)]
costedAmounts = case conversionop_ of showGoalAmountsAndPercentages goal = zip amts mpcts
Just ToCost -> amounts . mixedAmountCost where
_ -> amounts amts = showCommodityAmounts goal
mpcts = map (showrounded <$>) $ calcCommodityPercentages actual goal
where showrounded = wbFromText . T.pack . show . roundTo 0
-- | Calculate the percentage of actual change to budget goal for a particular commodity -- | Make budget info display helpers that adapt to --layout=wide.
percentage' :: Change -> BudgetGoal -> CommoditySymbol -> Maybe Percentage mkBudgetDisplayFns :: [CommoditySymbol] -> (BudgetShowAmountsFn, BudgetCalcPercentagesFn)
percentage' am bm c = case ((,) `on` find ((==) c . acommodity) . amounts) am bm of mkBudgetDisplayFns cs = case layout_ of
(Just a, Just b) -> percentage (mixedAmount a) (mixedAmount b) LayoutWide width ->
_ -> Nothing ( pure . showMixedAmountB oneLineNoCostFmt{displayMaxWidth=width, displayColour=color_}
, \a -> pure . percentage a)
_ -> ( showMixedAmountLinesB noCostFmt{displayCommodity=layout_/=LayoutBare, displayCommodityOrder=Just cs, displayMinWidth=Nothing, displayColour=color_}
, \a b -> map (percentage' a b) cs)
where
-- | Calculate the percentage of actual change to budget goal to show, if any.
-- If valuing at cost, both amounts are converted to cost before comparing.
-- A percentage will not be shown if:
--
-- - actual or goal are not the same, single, commodity
--
-- - the goal is zero
--
percentage :: Change -> BudgetGoal -> Maybe Percentage
percentage actual budget =
case (costedAmounts actual, costedAmounts budget) of
([a], [b]) | (acommodity a == acommodity b || amountLooksZero a) && not (amountLooksZero b)
-> Just $ 100 * aquantity a / aquantity b
_ -> Nothing
where
costedAmounts = case conversionop_ of
Just ToCost -> amounts . mixedAmountCost
_ -> amounts
-- | Like percentage, but accept multicommodity actual and budget amounts,
-- and extract the specified commodity from both.
percentage' :: Change -> BudgetGoal -> CommoditySymbol -> Maybe Percentage
percentage' am bm c = case ((,) `on` find ((==) c . acommodity) . amounts) am bm of
(Just a, Just b) -> percentage (mixedAmount a) (mixedAmount b)
_ -> Nothing
-- XXX generalise this with multiBalanceReportAsCsv ? -- XXX generalise this with multiBalanceReportAsCsv ?
-- | Render a budget report as CSV. Like multiBalanceReportAsCsv, -- | Render a budget report as CSV. Like multiBalanceReportAsCsv,
@ -459,12 +498,9 @@ budgetReportAsTable
budgetReportAsCsv :: ReportOpts -> BudgetReport -> [[Text]] budgetReportAsCsv :: ReportOpts -> BudgetReport -> [[Text]]
budgetReportAsCsv budgetReportAsCsv
ReportOpts{..} ReportOpts{..}
(PeriodicReport colspans items tr) (PeriodicReport colspans items totrow)
= (if transpose_ then transpose else id) $ = (if transpose_ then transpose else id) $
-- heading row
-- heading row -- heading row
("Account" : ("Account" :
["Commodity" | layout_ == LayoutBare ] ["Commodity" | layout_ == LayoutBare ]
@ -473,14 +509,11 @@ budgetReportAsCsv
++ concat [["Average","budget"] | average_] ++ concat [["Average","budget"] | average_]
) : ) :
-- account rows
-- account rows -- account rows
concatMap (rowAsTexts prrFullName) items concatMap (rowAsTexts prrFullName) items
-- totals row -- totals row
++ concat [ rowAsTexts (const "Total:") tr | not no_total_ ] ++ concat [ rowAsTexts (const "Total:") totrow | not no_total_ ]
where where
flattentuples tups = concat [[a,b] | (a,b) <- tups] flattentuples tups = concat [[a,b] | (a,b) <- tups]