From 88ef586480e5352c74bd47e2e07daf5f4d25fa50 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 23 Jan 2018 11:32:24 -0800 Subject: [PATCH] lib: rename, clarify purpose of balanceReportFromMultiBalanceReport --- bin/_hledger-chart.hs | 2 +- hledger-lib/Hledger/Reports/BalanceReport.hs | 2 +- .../Hledger/Reports/MultiBalanceReports.hs | 35 ++++++++++--------- hledger-lib/Hledger/Reports/ReportOptions.hs | 2 +- hledger-ui/Hledger/UI/AccountsScreen.hs | 6 ++-- hledger/Hledger/Cli/Commands/Balance.hs | 12 ++----- hledger/Hledger/Cli/Commands/Close.hs | 2 +- hledger/Hledger/Cli/CompoundBalanceCommand.hs | 6 ++-- 8 files changed, 32 insertions(+), 35 deletions(-) diff --git a/bin/_hledger-chart.hs b/bin/_hledger-chart.hs index cc0f57e6e..e5446ff44 100755 --- a/bin/_hledger-chart.hs +++ b/bin/_hledger-chart.hs @@ -97,7 +97,7 @@ main = do d <- getCurrentDay j <- defaultJournal let ropts = (reportopts_ $ cliopts_ chopts) - let balreport = singleBalanceReport ropts (queryFromOpts d ropts) j + let balreport = balanceReportFromMultiBalanceReport ropts (queryFromOpts d ropts) j let go -- | "--help" `elem` (rawopts_ $ cliopts_ chopts) = putStr (showModeHelp chartmode) >> exitSuccess -- | "--version" `elem` (rawopts_ $ cliopts_ chopts) = putStrLn progversion >> exitSuccess | otherwise = withJournalAndChartOptsDo chopts (writeChart balreport) diff --git a/hledger-lib/Hledger/Reports/BalanceReport.hs b/hledger-lib/Hledger/Reports/BalanceReport.hs index bb7b2fb2a..9044c8d5a 100644 --- a/hledger-lib/Hledger/Reports/BalanceReport.hs +++ b/hledger-lib/Hledger/Reports/BalanceReport.hs @@ -43,7 +43,7 @@ import Hledger.Reports.ReportOptions --- | A simple single-column balance report. It has: +-- | A simple balance report. It has: -- -- 1. a list of items, one per account, each containing: -- diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs index c73c6c41f..eb01569c7 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs @@ -9,7 +9,7 @@ module Hledger.Reports.MultiBalanceReports ( MultiBalanceReport(..), MultiBalanceReportRow, multiBalanceReport, - singleBalanceReport, + balanceReportFromMultiBalanceReport, -- mbrNegate, -- mbrNormaliseSign, @@ -74,23 +74,11 @@ instance Show MultiBalanceReport where -- type alias just to remind us which AccountNames might be depth-clipped, below. type ClippedAccountName = AccountName --- | Generates a single column BalanceReport like balanceReport, but uses --- multiBalanceReport, so supports --historical. --- TODO Does not support boring parent eliding or --flat yet. -singleBalanceReport :: ReportOpts -> Query -> Journal -> BalanceReport -singleBalanceReport opts q j = (rows', total) - where - MultiBalanceReport (_, rows, (totals, _, _)) = multiBalanceReport opts q j - rows' = [(a - ,if flat_ opts then a else a' -- BalanceReport expects full account name here with --flat - ,if tree_ opts then d-1 else 0 -- BalanceReport uses 0-based account depths - , headDef nullmixedamt amts -- 0 columns is illegal, should not happen, return zeroes if it does - ) | (a,a',d, amts, _, _) <- rows] - total = headDef nullmixedamt totals - -- | Generate a multicolumn balance report for the matched accounts, -- showing the change of balance, accumulated balance, or historical balance --- in each of the specified periods. +-- in each of the specified periods. Does not support tree-mode boring parent eliding. +-- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts +-- (see ReportOpts and CompoundBalanceCommand). multiBalanceReport :: ReportOpts -> Query -> Journal -> MultiBalanceReport multiBalanceReport opts q j = MultiBalanceReport (displayspans, sorteditems, totalsrow) where @@ -235,6 +223,21 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, sorteditems, tot dbg1 s = let p = "multiBalanceReport" in Hledger.Utils.dbg1 (p++" "++s) -- add prefix in this function's debug output -- dbg1 = const id -- exclude this function from debug output +-- | Generates a simple non-columnar BalanceReport, but using multiBalanceReport, +-- in order to support --historical. Does not support tree-mode boring parent eliding. +-- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts +-- (see ReportOpts and CompoundBalanceCommand). +balanceReportFromMultiBalanceReport :: ReportOpts -> Query -> Journal -> BalanceReport +balanceReportFromMultiBalanceReport opts q j = (rows', total) + where + MultiBalanceReport (_, rows, (totals, _, _)) = multiBalanceReport opts q j + rows' = [(a + ,if flat_ opts then a else a' -- BalanceReport expects full account name here with --flat + ,if tree_ opts then d-1 else 0 -- BalanceReport uses 0-based account depths + , headDef nullmixedamt amts -- 0 columns is illegal, should not happen, return zeroes if it does + ) | (a,a',d, amts, _, _) <- rows] + total = headDef nullmixedamt totals + tests_multiBalanceReport = let diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index 86f6fc7fb..08167acbb 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -104,7 +104,7 @@ data ReportOpts = ReportOpts { ,sort_amount_ :: Bool ,normalbalance_ :: Maybe NormalSign -- ^ This can be set when running balance reports on a set of accounts - -- with the same normal balance type (eg all assets, or all incomes). + -- with the same normal balance type (eg all assets, or all incomes). -- - It helps --sort-amount know how to sort negative numbers -- (eg in the income section of an income statement) -- - It helps compound balance report commands (is, bs etc.) do diff --git a/hledger-ui/Hledger/UI/AccountsScreen.hs b/hledger-ui/Hledger/UI/AccountsScreen.hs index 4e293f668..91ba8d391 100644 --- a/hledger-ui/Hledger/UI/AccountsScreen.hs +++ b/hledger-ui/Hledger/UI/AccountsScreen.hs @@ -85,10 +85,10 @@ asInit d reset ui@UIState{ -- run the report (items,_total) = report ropts' q j where - -- still using the old balanceReport for change reports as it - -- does not include every account from before the report period - report | balancetype_ ropts == HistoricalBalance = singleBalanceReport + report | balancetype_ ropts == HistoricalBalance = balanceReportFromMultiBalanceReport | otherwise = balanceReport + -- still using the old balanceReport for change reports as it + -- does not include every account from before the report period -- pre-render the list items diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 0179b1962..3723c3446 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -309,21 +309,15 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts} j = do Right _ -> do let format = outputFormatFromOpts opts interval = interval_ ropts - -- XXX shenanigans: use singleBalanceReport or multiBalanceReport when we must, - -- ie when there's a report interval, or when --historical or --cumulative - -- are used (balanceReport doesn't handle those). - -- Otherwise prefer the older balanceReport since it can elide boring parents. - -- See also singleBalanceReport etc. case interval of NoInterval -> do let report - -- For --historical/--cumulative, we must use multiBalanceReport. - -- (This forces --no-elide.) | balancetype_ ropts `elem` [HistoricalBalance, CumulativeChange] = let ropts' | flat_ ropts = ropts | otherwise = ropts{accountlistmode_=ALTree} - in singleBalanceReport ropts' (queryFromOpts d ropts) j - | otherwise = balanceReport ropts (queryFromOpts d ropts) j + in balanceReportFromMultiBalanceReport ropts' (queryFromOpts d ropts) j + -- for historical balances we must use balanceReportFromMultiBalanceReport (also forces --no-elide) + | otherwise = balanceReport ropts (queryFromOpts d ropts) j -- simple Ledger-style balance report render = case format of "csv" -> \ropts r -> (++ "\n") $ printCSV $ balanceReportAsCsv ropts r "html" -> \_ _ -> error' "Sorry, HTML output is not yet implemented for this kind of report." -- TODO diff --git a/hledger/Hledger/Cli/Commands/Close.hs b/hledger/Hledger/Cli/Commands/Close.hs index 2d6b01fe2..28f6c41ed 100755 --- a/hledger/Hledger/Cli/Commands/Close.hs +++ b/hledger/Hledger/Cli/Commands/Close.hs @@ -70,7 +70,7 @@ close CliOpts{reportopts_=ropts} j = do q = queryFromOpts today ropts_ openingdate = fromMaybe today $ queryEndDate False q closingdate = addDays (-1) openingdate - (acctbals,_) = singleBalanceReport ropts_ q j + (acctbals,_) = balanceReportFromMultiBalanceReport ropts_ q j balancingamt = negate $ sum $ map (\(_,_,_,b) -> normaliseMixedAmountSquashPricesForDisplay b) acctbals ps = [posting{paccount=a ,pamount=mixed [b] diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index ff547ae5d..c3ee49f1b 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -142,10 +142,10 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r CumulativeChange -> "(Cumulative Ending Balances)" HistoricalBalance -> "(Historical Ending Balances)" -- Set balance type in the report options. - -- XXX Also, use tree mode (by default, at least?) if --cumulative/--historical + -- Also, use tree mode (by default, at least?) if --cumulative/--historical -- are used in single column mode, since in that situation we will be using - -- singleBalanceReport which does not support eliding boring parents, - -- and tree mode hides this.. or something.. + -- balanceReportFromMultiBalanceReport which does not support eliding boring parents, + -- and tree mode hides this.. or something.. XXX ropts' | not (flat_ ropts) && interval_ ropts==NoInterval &&