balance: with --tree and --flat, use the last (fix #219)
This commit is contained in:
		
							parent
							
								
									d352bbedd2
								
							
						
					
					
						commit
						855d4e1131
					
				| @ -122,7 +122,7 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, items, totals) | ||||
|             (startbalanceitems,_) = dbg "starting balance report" $ balanceReport opts' precedingq j | ||||
|                                     where | ||||
|                                       opts' | tree_ opts = opts{no_elide_=True} | ||||
|                                             | otherwise  = opts{flat_=True} | ||||
|                                             | otherwise  = opts{accountlistmode_=ALFlat} | ||||
|       startingBalanceFor a = fromMaybe nullmixedamt $ lookup a startacctbals | ||||
|       startAccts = dbg "startAccts" $ map fst startacctbals | ||||
| 
 | ||||
|  | ||||
| @ -8,9 +8,12 @@ Options common to most hledger reports. | ||||
| module Hledger.Reports.ReportOptions ( | ||||
|   ReportOpts(..), | ||||
|   BalanceType(..), | ||||
|   AccountListMode(..), | ||||
|   FormatStr, | ||||
|   defreportopts, | ||||
|   rawOptsToReportOpts, | ||||
|   flat_, | ||||
|   tree_, | ||||
|   dateSpanFromOpts, | ||||
|   intervalFromOpts, | ||||
|   clearedValueFromOpts, | ||||
| @ -47,6 +50,11 @@ data BalanceType = PeriodBalance     -- ^ The change of balance in each period. | ||||
| 
 | ||||
| instance Default BalanceType where def = PeriodBalance | ||||
| 
 | ||||
| -- | Should accounts be displayed: in the command's default style, hierarchically, or as a flat list ? | ||||
| data AccountListMode = ALDefault | ALTree | ALFlat deriving (Eq, Show, Data, Typeable) | ||||
| 
 | ||||
| instance Default AccountListMode where def = ALDefault | ||||
| 
 | ||||
| -- | Standard options for customising report filtering and output, | ||||
| -- corresponding to hledger's command-line options and query language | ||||
| -- arguments. Used in hledger-lib and above. | ||||
| @ -75,8 +83,7 @@ data ReportOpts = ReportOpts { | ||||
|     ,related_        :: Bool | ||||
|     -- balance | ||||
|     ,balancetype_    :: BalanceType | ||||
|     ,flat_           :: Bool -- mutually | ||||
|     ,tree_           :: Bool -- exclusive | ||||
|     ,accountlistmode_  :: AccountListMode | ||||
|     ,drop_           :: Int | ||||
|     ,no_total_       :: Bool | ||||
|  } deriving (Show, Data, Typeable) | ||||
| @ -110,7 +117,6 @@ defreportopts = ReportOpts | ||||
|     def | ||||
|     def | ||||
|     def | ||||
|     def | ||||
| 
 | ||||
| rawOptsToReportOpts :: RawOpts -> IO ReportOpts | ||||
| rawOptsToReportOpts rawopts = do | ||||
| @ -138,12 +144,18 @@ rawOptsToReportOpts rawopts = do | ||||
|     ,average_     = boolopt "average" rawopts | ||||
|     ,related_     = boolopt "related" rawopts | ||||
|     ,balancetype_ = balancetypeopt rawopts | ||||
|     ,flat_        = boolopt "flat" rawopts | ||||
|     ,tree_        = boolopt "tree" rawopts | ||||
|     ,accountlistmode_ = accountlistmodeopt rawopts | ||||
|     ,drop_        = intopt "drop" rawopts | ||||
|     ,no_total_    = boolopt "no-total" rawopts | ||||
|     } | ||||
| 
 | ||||
| accountlistmodeopt :: RawOpts -> AccountListMode | ||||
| accountlistmodeopt rawopts = | ||||
|   case reverse $ filter (`elem` ["tree","flat"]) $ map fst rawopts of | ||||
|     ("tree":_) -> ALTree | ||||
|     ("flat":_) -> ALFlat | ||||
|     _          -> ALDefault | ||||
| 
 | ||||
| balancetypeopt :: RawOpts -> BalanceType | ||||
| balancetypeopt rawopts | ||||
|     | length [o | o <- ["cumulative","historical"], isset o] > 1 | ||||
| @ -181,6 +193,13 @@ maybeperiodopt d rawopts = | ||||
|                 Just | ||||
|                 $ parsePeriodExpr d s | ||||
| 
 | ||||
| -- | Legacy-compatible convenience aliases for accountlistmode_. | ||||
| tree_ :: ReportOpts -> Bool | ||||
| tree_ = (==ALTree) . accountlistmode_ | ||||
| 
 | ||||
| flat_ :: ReportOpts -> Bool | ||||
| flat_ = (==ALFlat) . accountlistmode_ | ||||
| 
 | ||||
| -- | Figure out the date span we should report on, based on any | ||||
| -- begin/end/period options provided. A period option will cause begin and | ||||
| -- end options to be ignored. | ||||
|  | ||||
| @ -38,7 +38,7 @@ print' opts@CliOpts{reportopts_=ropts} j = do | ||||
|   let q = queryFromOpts d ropts | ||||
|       fmt = outputFormatFromOpts opts | ||||
|       (render, ropts') = case fmt of | ||||
|         "csv" -> ((++"\n") . printCSV . entriesReportAsCsv, ropts{flat_=True}) | ||||
|         "csv" -> ((++"\n") . printCSV . entriesReportAsCsv, ropts{accountlistmode_=ALFlat}) | ||||
|         _     -> (entriesReportAsText,                      ropts) | ||||
|   writeOutput opts $ render $ entriesReport ropts' q j | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										23
									
								
								tests/balance/219.test
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								tests/balance/219.test
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | ||||
| # issue 219, --tree and --flat flags should override each other cleanly | ||||
| # 1. multiple flags ending with --flat, equivalent to --flat | ||||
| hledgerdev -f balance-multicol.journal bal -MEH --no-total date:2013/1 --tree --flat | ||||
| >>> | ||||
| Ending balances (historical) in 2013/01: | ||||
| 
 | ||||
|                  ||  2013/01/31  | ||||
| =================++============= | ||||
|  assets:checking ||          10  | ||||
| 
 | ||||
| >>>= 0 | ||||
| 
 | ||||
| # 2. multiple flags ending with --tree, equivalent to --tree | ||||
| hledgerdev -f balance-multicol.journal bal -MEH --no-total date:2013/1 --flat --tree | ||||
| >>> | ||||
| Ending balances (historical) in 2013/01: | ||||
| 
 | ||||
|             ||  2013/01/31  | ||||
| ============++============= | ||||
|  assets     ||          10  | ||||
|    checking ||          10  | ||||
| 
 | ||||
| >>>= 0 | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user