;lib: balanceReport cleanup

This commit is contained in:
Simon Michael 2019-05-05 09:57:03 -07:00
parent f4b0381043
commit 1ad82d5b77

View File

@ -64,23 +64,23 @@ flatShowsExclusiveBalance = True
-- This is like PeriodChangeReport with a single column (but more mature, -- This is like PeriodChangeReport with a single column (but more mature,
-- eg this can do hierarchical display). -- eg this can do hierarchical display).
balanceReport :: ReportOpts -> Query -> Journal -> BalanceReport balanceReport :: ReportOpts -> Query -> Journal -> BalanceReport
balanceReport opts q j = balanceReport ropts@ReportOpts{..} q j =
(if invert_ opts then brNegate else id) $ (if invert_ then brNegate else id) $
(if value_ opts then brValue opts j else id) $ (if value_ then brValue ropts j else id) $
(sorteditems, total) (sorteditems, total)
where where
-- dbg1 = const id -- exclude from debug output -- dbg1 = const id -- exclude from debug output
dbg1 s = let p = "balanceReport" in Hledger.Utils.dbg1 (p++" "++s) -- add prefix in debug output dbg1 s = let p = "balanceReport" in Hledger.Utils.dbg1 (p++" "++s) -- add prefix in debug output
-- Get all the summed accounts & balances, according to the query, as an account tree. -- Get all the summed accounts & balances, according to the query, as an account tree.
accts = ledgerRootAccount $ ledgerFromJournal q $ journalSelectingAmountFromOpts opts j accts = ledgerRootAccount $ ledgerFromJournal q $ journalSelectingAmountFromOpts ropts j
-- Modify this tree for display - depth limit, boring parents, zeroes - and convert to a list. -- Modify this tree for display - depth limit, boring parents, zeroes - and convert to a list.
displayaccts :: [Account] displayaccts :: [Account]
| queryDepth q == 0 = | queryDepth q == 0 =
dbg1 "accts" $ dbg1 "accts" $
take 1 $ clipAccountsAndAggregate (queryDepth q) $ flattenAccounts accts take 1 $ clipAccountsAndAggregate (queryDepth q) $ flattenAccounts accts
| flat_ opts = dbg1 "accts" $ | flat_ ropts = dbg1 "accts" $
filterzeros $ filterzeros $
filterempty $ filterempty $
drop 1 $ clipAccountsAndAggregate (queryDepth q) $ flattenAccounts accts drop 1 $ clipAccountsAndAggregate (queryDepth q) $ flattenAccounts accts
@ -89,40 +89,40 @@ balanceReport opts q j =
drop 1 $ flattenAccounts $ drop 1 $ flattenAccounts $
markboring $ markboring $
prunezeros $ prunezeros $
sortAccountTreeByAmount (fromMaybe NormallyPositive $ normalbalance_ opts) $ sortAccountTreeByAmount (fromMaybe NormallyPositive normalbalance_) $
clipAccounts (queryDepth q) accts clipAccounts (queryDepth q) accts
where where
balance = if flat_ opts then aebalance else aibalance balance = if flat_ ropts then aebalance else aibalance
filterzeros = if empty_ opts then id else filter (not . isZeroMixedAmount . balance) filterzeros = if empty_ then id else filter (not . isZeroMixedAmount . balance)
filterempty = filter (\a -> anumpostings a > 0 || not (isZeroMixedAmount (balance a))) filterempty = filter (\a -> anumpostings a > 0 || not (isZeroMixedAmount (balance a)))
prunezeros = if empty_ opts then id else fromMaybe nullacct . pruneAccounts (isZeroMixedAmount . balance) prunezeros = if empty_ then id else fromMaybe nullacct . pruneAccounts (isZeroMixedAmount . balance)
markboring = if no_elide_ opts then id else markBoringParentAccounts markboring = if no_elide_ then id else markBoringParentAccounts
-- Make a report row for each account. -- Make a report row for each account.
items = dbg1 "items" $ map (balanceReportItem opts q) displayaccts items = dbg1 "items" $ map (balanceReportItem ropts q) displayaccts
-- Sort report rows (except sorting by amount in tree mode, which was done above). -- Sort report rows (except sorting by amount in tree mode, which was done above).
sorteditems sorteditems
| sort_amount_ opts && tree_ opts = items | sort_amount_ && tree_ ropts = items
| sort_amount_ opts = sortFlatBRByAmount items | sort_amount_ = sortFlatBRByAmount items
| otherwise = sortBRByAccountDeclaration items | otherwise = sortBRByAccountDeclaration items
where where
-- Sort the report rows, representing a flat account list, by row total. -- Sort the report rows, representing a flat account list, by row total.
sortFlatBRByAmount :: [BalanceReportItem] -> [BalanceReportItem] sortFlatBRByAmount :: [BalanceReportItem] -> [BalanceReportItem]
sortFlatBRByAmount = sortBy (maybeflip $ comparing (normaliseMixedAmountSquashPricesForDisplay . fourth4)) sortFlatBRByAmount = sortBy (maybeflip $ comparing (normaliseMixedAmountSquashPricesForDisplay . fourth4))
where where
maybeflip = if normalbalance_ opts == Just NormallyNegative then id else flip maybeflip = if normalbalance_ == Just NormallyNegative then id else flip
-- Sort the report rows by account declaration order then account name. -- Sort the report rows by account declaration order then account name.
sortBRByAccountDeclaration :: [BalanceReportItem] -> [BalanceReportItem] sortBRByAccountDeclaration :: [BalanceReportItem] -> [BalanceReportItem]
sortBRByAccountDeclaration rows = sortedrows sortBRByAccountDeclaration rows = sortedrows
where where
anamesandrows = [(first4 r, r) | r <- rows] anamesandrows = [(first4 r, r) | r <- rows]
anames = map fst anamesandrows anames = map fst anamesandrows
sortedanames = sortAccountNamesByDeclaration j (tree_ opts) anames sortedanames = sortAccountNamesByDeclaration j (tree_ ropts) anames
sortedrows = sortAccountItemsLike sortedanames anamesandrows sortedrows = sortAccountItemsLike sortedanames anamesandrows
-- Calculate the grand total. -- Calculate the grand total.
total | not (flat_ opts) = dbg1 "total" $ sum [amt | (_,_,indent,amt) <- items, indent == 0] total | not (flat_ ropts) = dbg1 "total" $ sum [amt | (_,_,indent,amt) <- items, indent == 0]
| otherwise = dbg1 "total" $ | otherwise = dbg1 "total" $
if flatShowsExclusiveBalance if flatShowsExclusiveBalance
then sum $ map fourth4 items then sum $ map fourth4 items