bal: Sort amounts after negating when using invert_ (#1283, #1379)

This commit is contained in:
Stephen Morgan 2020-10-26 16:58:11 +11:00 committed by Simon Michael
parent cd0c76eb4a
commit 166951dc69

View File

@ -388,7 +388,9 @@ generateMultiBalanceReport rspec@ReportSpec{rsOpts=ropts} j valuation colspans c
displaynames = dbg'' "displaynames" $ displayedAccounts rspec accumvalued displaynames = dbg'' "displaynames" $ displayedAccounts rspec accumvalued
-- All the rows of the report. -- All the rows of the report.
rows = dbg'' "rows" $ buildReportRows ropts displaynames accumvalued rows = dbg'' "rows"
. (if invert_ ropts then map (fmap negate) else id) -- Negate amounts if applicable
$ buildReportRows ropts displaynames accumvalued
-- Calculate column totals -- Calculate column totals
totalsrow = dbg' "totalsrow" $ calculateTotalsRow ropts rows totalsrow = dbg' "totalsrow" $ calculateTotalsRow ropts rows
@ -396,8 +398,8 @@ generateMultiBalanceReport rspec@ReportSpec{rsOpts=ropts} j valuation colspans c
-- Sorted report rows. -- Sorted report rows.
sortedrows = dbg' "sortedrows" $ sortRows ropts j rows sortedrows = dbg' "sortedrows" $ sortRows ropts j rows
-- Postprocess the report, negating balances and taking percentages if needed -- Take percentages if needed
report = postprocessReport ropts $ PeriodicReport colspans sortedrows totalsrow report = reportPercent ropts $ PeriodicReport colspans sortedrows totalsrow
-- | Build the report rows. -- | Build the report rows.
-- --
@ -528,15 +530,11 @@ calculateTotalsRow ropts rows =
_ -> lastDef 0 coltotals _ -> lastDef 0 coltotals
grandaverage = averageMixedAmounts coltotals grandaverage = averageMixedAmounts coltotals
-- | Map the report rows to percentages and negate if needed -- | Map the report rows to percentages if needed
postprocessReport :: ReportOpts -> MultiBalanceReport -> MultiBalanceReport reportPercent :: ReportOpts -> MultiBalanceReport -> MultiBalanceReport
postprocessReport ropts = maybePercent . maybeInvert reportPercent ropts report@(PeriodicReport spans rows totalrow)
where | percent_ ropts = PeriodicReport spans (map percentRow rows) (percentRow totalrow)
maybeInvert = if invert_ ropts then fmap negate else id | otherwise = report
maybePercent = if percent_ ropts then prPercent else id
prPercent (PeriodicReport spans rows totalrow) =
PeriodicReport spans (map percentRow rows) (percentRow totalrow)
where where
percentRow (PeriodicReportRow name rowvals rowtotal rowavg) = percentRow (PeriodicReportRow name rowvals rowtotal rowavg) =
PeriodicReportRow name PeriodicReportRow name