lib: rename, clarify purpose of balanceReportFromMultiBalanceReport
This commit is contained in:
		
							parent
							
								
									6c60e4a97b
								
							
						
					
					
						commit
						88ef586480
					
				| @ -97,7 +97,7 @@ main = do | |||||||
|   d <- getCurrentDay |   d <- getCurrentDay | ||||||
|   j <- defaultJournal |   j <- defaultJournal | ||||||
|   let ropts = (reportopts_ $ cliopts_ chopts) |   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 |   let go -- | "--help" `elem` (rawopts_ $ cliopts_ chopts)    = putStr (showModeHelp chartmode) >> exitSuccess | ||||||
|          -- | "--version" `elem` (rawopts_ $ cliopts_ chopts) = putStrLn progversion >> exitSuccess |          -- | "--version" `elem` (rawopts_ $ cliopts_ chopts) = putStrLn progversion >> exitSuccess | ||||||
|          | otherwise                                       = withJournalAndChartOptsDo chopts (writeChart balreport) |          | otherwise                                       = withJournalAndChartOptsDo chopts (writeChart balreport) | ||||||
|  | |||||||
| @ -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: | -- 1. a list of items, one per account, each containing: | ||||||
| -- | -- | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ module Hledger.Reports.MultiBalanceReports ( | |||||||
|   MultiBalanceReport(..), |   MultiBalanceReport(..), | ||||||
|   MultiBalanceReportRow, |   MultiBalanceReportRow, | ||||||
|   multiBalanceReport, |   multiBalanceReport, | ||||||
|   singleBalanceReport, |   balanceReportFromMultiBalanceReport, | ||||||
| --  mbrNegate, | --  mbrNegate, | ||||||
| --  mbrNormaliseSign, | --  mbrNormaliseSign, | ||||||
| 
 | 
 | ||||||
| @ -74,23 +74,11 @@ instance Show MultiBalanceReport where | |||||||
| -- type alias just to remind us which AccountNames might be depth-clipped, below. | -- type alias just to remind us which AccountNames might be depth-clipped, below. | ||||||
| type ClippedAccountName = AccountName | 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, | -- | Generate a multicolumn balance report for the matched accounts, | ||||||
| -- showing the change of balance, accumulated balance, or historical balance | -- 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 :: ReportOpts -> Query -> Journal -> MultiBalanceReport | ||||||
| multiBalanceReport opts q j = MultiBalanceReport (displayspans, sorteditems, totalsrow) | multiBalanceReport opts q j = MultiBalanceReport (displayspans, sorteditems, totalsrow) | ||||||
|     where |     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 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 |       -- 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 = | tests_multiBalanceReport = | ||||||
|   let |   let | ||||||
|  | |||||||
| @ -104,7 +104,7 @@ data ReportOpts = ReportOpts { | |||||||
|     ,sort_amount_    :: Bool |     ,sort_amount_    :: Bool | ||||||
|     ,normalbalance_  :: Maybe NormalSign |     ,normalbalance_  :: Maybe NormalSign | ||||||
|       -- ^ This can be set when running balance reports on a set of accounts |       -- ^ 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 |       -- - It helps --sort-amount know how to sort negative numbers | ||||||
|       --   (eg in the income section of an income statement)  |       --   (eg in the income section of an income statement)  | ||||||
|       -- - It helps compound balance report commands (is, bs etc.) do   |       -- - It helps compound balance report commands (is, bs etc.) do   | ||||||
|  | |||||||
| @ -85,10 +85,10 @@ asInit d reset ui@UIState{ | |||||||
|     -- run the report |     -- run the report | ||||||
|     (items,_total) = report ropts' q j |     (items,_total) = report ropts' q j | ||||||
|       where |       where | ||||||
|         -- still using the old balanceReport for change reports as it |         report | balancetype_ ropts == HistoricalBalance = balanceReportFromMultiBalanceReport | ||||||
|         -- does not include every account from before the report period |  | ||||||
|         report | balancetype_ ropts == HistoricalBalance = singleBalanceReport |  | ||||||
|                | otherwise                               = balanceReport |                | 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 |     -- pre-render the list items | ||||||
|  | |||||||
| @ -309,21 +309,15 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts} j = do | |||||||
|     Right _ -> do |     Right _ -> do | ||||||
|       let format   = outputFormatFromOpts opts |       let format   = outputFormatFromOpts opts | ||||||
|           interval = interval_ ropts |           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 |       case interval of | ||||||
|         NoInterval -> do |         NoInterval -> do | ||||||
|           let report |           let report | ||||||
|                 -- For --historical/--cumulative, we must use multiBalanceReport. |  | ||||||
|                 -- (This forces --no-elide.) |  | ||||||
|                 | balancetype_ ropts `elem` [HistoricalBalance, CumulativeChange] |                 | balancetype_ ropts `elem` [HistoricalBalance, CumulativeChange] | ||||||
|                   = let ropts' | flat_ ropts = ropts |                   = let ropts' | flat_ ropts = ropts | ||||||
|                                | otherwise   = ropts{accountlistmode_=ALTree} |                                | otherwise   = ropts{accountlistmode_=ALTree} | ||||||
|                     in singleBalanceReport ropts' (queryFromOpts d ropts) j |                     in balanceReportFromMultiBalanceReport ropts' (queryFromOpts d ropts) j | ||||||
|                 | otherwise = balanceReport 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 |               render = case format of | ||||||
|                 "csv"  -> \ropts r -> (++ "\n") $ printCSV $ balanceReportAsCsv ropts r |                 "csv"  -> \ropts r -> (++ "\n") $ printCSV $ balanceReportAsCsv ropts r | ||||||
|                 "html" -> \_ _ -> error' "Sorry, HTML output is not yet implemented for this kind of report."  -- TODO |                 "html" -> \_ _ -> error' "Sorry, HTML output is not yet implemented for this kind of report."  -- TODO | ||||||
|  | |||||||
| @ -70,7 +70,7 @@ close CliOpts{reportopts_=ropts} j = do | |||||||
|       q = queryFromOpts today ropts_ |       q = queryFromOpts today ropts_ | ||||||
|       openingdate = fromMaybe today $ queryEndDate False q |       openingdate = fromMaybe today $ queryEndDate False q | ||||||
|       closingdate = addDays (-1) openingdate |       closingdate = addDays (-1) openingdate | ||||||
|       (acctbals,_) = singleBalanceReport ropts_ q j |       (acctbals,_) = balanceReportFromMultiBalanceReport ropts_ q j | ||||||
|       balancingamt = negate $ sum $ map (\(_,_,_,b) -> normaliseMixedAmountSquashPricesForDisplay b) acctbals |       balancingamt = negate $ sum $ map (\(_,_,_,b) -> normaliseMixedAmountSquashPricesForDisplay b) acctbals | ||||||
|       ps = [posting{paccount=a |       ps = [posting{paccount=a | ||||||
|                    ,pamount=mixed [b] |                    ,pamount=mixed [b] | ||||||
|  | |||||||
| @ -142,10 +142,10 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r | |||||||
|               CumulativeChange  -> "(Cumulative Ending Balances)" |               CumulativeChange  -> "(Cumulative Ending Balances)" | ||||||
|               HistoricalBalance -> "(Historical Ending Balances)" |               HistoricalBalance -> "(Historical Ending Balances)" | ||||||
|       -- Set balance type in the report options. |       -- 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  |       -- are used in single column mode, since in that situation we will be using  | ||||||
|       -- singleBalanceReport which does not support eliding boring parents, |       -- balanceReportFromMultiBalanceReport which does not support eliding boring parents, | ||||||
|       -- and tree mode hides this.. or something..  |       -- and tree mode hides this.. or something.. XXX  | ||||||
|       ropts' |       ropts' | ||||||
|         | not (flat_ ropts) &&  |         | not (flat_ ropts) &&  | ||||||
|           interval_ ropts==NoInterval &&  |           interval_ ropts==NoInterval &&  | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user