bal: --invert flag to flip all signs
This commit is contained in:
		
							parent
							
								
									a26647f49d
								
							
						
					
					
						commit
						d3fde29b36
					
				| @ -77,7 +77,9 @@ 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 = (items, total) | balanceReport opts q j =  | ||||||
|  |   (if invert_ opts then brNegate else id) $  | ||||||
|  |   (items, 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 | ||||||
| @ -148,6 +150,11 @@ balanceReportItem opts q a | |||||||
| --     items = [(a,a',n, headDef 0 bs) | ((a,a',n), bs) <- mbrrows] | --     items = [(a,a',n, headDef 0 bs) | ((a,a',n), bs) <- mbrrows] | ||||||
| --     total = headDef 0 mbrtotals | --     total = headDef 0 mbrtotals | ||||||
| 
 | 
 | ||||||
|  | -- | Flip the sign of all amounts in a BalanceReport. | ||||||
|  | brNegate :: BalanceReport -> BalanceReport | ||||||
|  | brNegate (is, tot) = (map brItemNegate is, -tot)  | ||||||
|  |   where | ||||||
|  |     brItemNegate (a, a', d, amt) = (a, a', d, -amt) | ||||||
| 
 | 
 | ||||||
| tests_balanceReport = | tests_balanceReport = | ||||||
|   let |   let | ||||||
|  | |||||||
| @ -10,8 +10,8 @@ module Hledger.Reports.MultiBalanceReports ( | |||||||
|   MultiBalanceReportRow, |   MultiBalanceReportRow, | ||||||
|   multiBalanceReport, |   multiBalanceReport, | ||||||
|   balanceReportFromMultiBalanceReport, |   balanceReportFromMultiBalanceReport, | ||||||
| --  mbrNegate, |   mbrNegate, | ||||||
| --  mbrNormaliseSign, |   mbrNormaliseSign, | ||||||
| 
 | 
 | ||||||
|   -- -- * Tests |   -- -- * Tests | ||||||
|   tests_Hledger_Reports_MultiBalanceReport |   tests_Hledger_Reports_MultiBalanceReport | ||||||
| @ -80,7 +80,9 @@ type ClippedAccountName = AccountName | |||||||
| -- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts  | -- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts  | ||||||
| -- (see ReportOpts and CompoundBalanceCommand). | -- (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 = | ||||||
|  |   (if invert_ opts then mbrNegate else id) $  | ||||||
|  |   MultiBalanceReport (displayspans, sorteditems, totalsrow) | ||||||
|     where |     where | ||||||
|       symq       = dbg1 "symq"   $ filterQuery queryIsSym $ dbg1 "requested q" q |       symq       = dbg1 "symq"   $ filterQuery queryIsSym $ dbg1 "requested q" q | ||||||
|       depthq     = dbg1 "depthq" $ filterQuery queryIsDepth q |       depthq     = dbg1 "depthq" $ filterQuery queryIsDepth q | ||||||
| @ -244,6 +246,19 @@ 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 | ||||||
| 
 | 
 | ||||||
|  | -- | Given a MultiBalanceReport and its normal balance sign, | ||||||
|  | -- if it is known to be normally negative, convert it to normally positive. | ||||||
|  | mbrNormaliseSign :: NormalSign -> MultiBalanceReport -> MultiBalanceReport | ||||||
|  | mbrNormaliseSign NormallyNegative = mbrNegate | ||||||
|  | mbrNormaliseSign _ = id | ||||||
|  | 
 | ||||||
|  | -- | Flip the sign of all amounts in a MultiBalanceReport. | ||||||
|  | mbrNegate (MultiBalanceReport (colspans, rows, totalsrow)) = | ||||||
|  |   MultiBalanceReport (colspans, map mbrRowNegate rows, mbrTotalsRowNegate totalsrow) | ||||||
|  |   where | ||||||
|  |     mbrRowNegate (acct,shortacct,indent,amts,tot,avg) = (acct,shortacct,indent,map negate amts,-tot,-avg) | ||||||
|  |     mbrTotalsRowNegate (amts,tot,avg) = (map negate amts,-tot,-avg) | ||||||
|  | 
 | ||||||
| -- | Generates a simple non-columnar BalanceReport, but using multiBalanceReport,  | -- | Generates a simple non-columnar BalanceReport, but using multiBalanceReport,  | ||||||
| -- in order to support --historical. Does not support tree-mode boring parent eliding.  | -- 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  | -- If the normalbalance_ option is set, it adjusts the sorting and sign of amounts  | ||||||
|  | |||||||
| @ -102,6 +102,7 @@ data ReportOpts = ReportOpts { | |||||||
|     ,value_          :: Bool |     ,value_          :: Bool | ||||||
|     ,pretty_tables_  :: Bool |     ,pretty_tables_  :: Bool | ||||||
|     ,sort_amount_    :: Bool |     ,sort_amount_    :: Bool | ||||||
|  |     ,invert_         :: Bool  -- ^ if true, flip all amount signs in reports | ||||||
|     ,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). | ||||||
| @ -145,6 +146,7 @@ defreportopts = ReportOpts | |||||||
|     def |     def | ||||||
|     def |     def | ||||||
|     def |     def | ||||||
|  |     def | ||||||
| 
 | 
 | ||||||
| rawOptsToReportOpts :: RawOpts -> IO ReportOpts | rawOptsToReportOpts :: RawOpts -> IO ReportOpts | ||||||
| rawOptsToReportOpts rawopts = checkReportOpts <$> do | rawOptsToReportOpts rawopts = checkReportOpts <$> do | ||||||
| @ -173,6 +175,7 @@ rawOptsToReportOpts rawopts = checkReportOpts <$> do | |||||||
|     ,no_total_    = boolopt "no-total" rawopts' |     ,no_total_    = boolopt "no-total" rawopts' | ||||||
|     ,value_       = boolopt "value" rawopts' |     ,value_       = boolopt "value" rawopts' | ||||||
|     ,sort_amount_ = boolopt "sort-amount" rawopts' |     ,sort_amount_ = boolopt "sort-amount" rawopts' | ||||||
|  |     ,invert_      = boolopt "invert" rawopts' | ||||||
|     ,pretty_tables_ = boolopt "pretty-tables" rawopts' |     ,pretty_tables_ = boolopt "pretty-tables" rawopts' | ||||||
|     ,color_       = color |     ,color_       = color | ||||||
|     ,forecast_    = boolopt "forecast" rawopts' |     ,forecast_    = boolopt "forecast" rawopts' | ||||||
|  | |||||||
| @ -292,6 +292,7 @@ balancemode = (defCommandMode $ ["balance"] ++ aliases) { -- also accept but don | |||||||
|      ,flagNone ["sort-amount","S"] (\opts -> setboolopt "sort-amount" opts) "sort by amount instead of account code/name (in flat mode). With multiple columns, sorts by the row total, or by row average if that is displayed." |      ,flagNone ["sort-amount","S"] (\opts -> setboolopt "sort-amount" opts) "sort by amount instead of account code/name (in flat mode). With multiple columns, sorts by the row total, or by row average if that is displayed." | ||||||
|      ,flagNone ["budget"] (setboolopt "budget") "show performance compared to budget goals defined by periodic transactions" |      ,flagNone ["budget"] (setboolopt "budget") "show performance compared to budget goals defined by periodic transactions" | ||||||
|      ,flagNone ["show-unbudgeted"] (setboolopt "show-unbudgeted") "with --budget, show unbudgeted accounts also" |      ,flagNone ["show-unbudgeted"] (setboolopt "show-unbudgeted") "with --budget, show unbudgeted accounts also" | ||||||
|  |      ,flagNone ["invert"] (setboolopt "invert") "display all amounts with reversed sign" | ||||||
|      ] |      ] | ||||||
|      ++ outputflags |      ++ outputflags | ||||||
|     ,groupHidden = [] |     ,groupHidden = [] | ||||||
| @ -701,11 +702,11 @@ renderBalanceReportTable ropts = | |||||||
|             | otherwise    = showMixedAmountOneLineWithoutPrice |             | otherwise    = showMixedAmountOneLineWithoutPrice | ||||||
| 
 | 
 | ||||||
| renderBalanceReportTable' :: ReportOpts -> (a -> String) -> Table String String a -> String | renderBalanceReportTable' :: ReportOpts -> (a -> String) -> Table String String a -> String | ||||||
| renderBalanceReportTable' (ReportOpts { pretty_tables_ = pretty}) showCell = | renderBalanceReportTable' (ReportOpts { pretty_tables_ = pretty}) showamt = | ||||||
|   unlines |   unlines | ||||||
|   . trimborder |   . trimborder | ||||||
|   . lines |   . lines | ||||||
|   . render pretty id id showCell |   . render pretty id id showamt | ||||||
|   . align |   . align | ||||||
|   where |   where | ||||||
|     trimborder = drop 1 . init . map (drop 1 . init) |     trimborder = drop 1 . init . map (drop 1 . init) | ||||||
|  | |||||||
| @ -209,19 +209,6 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r | |||||||
|         "html" -> (++ "\n") $ TL.unpack $ L.renderText $ compoundBalanceReportAsHtml ropts cbr |         "html" -> (++ "\n") $ TL.unpack $ L.renderText $ compoundBalanceReportAsHtml ropts cbr | ||||||
|         _      -> compoundBalanceReportAsText ropts' cbr |         _      -> compoundBalanceReportAsText ropts' cbr | ||||||
| 
 | 
 | ||||||
| -- | Given a MultiBalanceReport and its normal balance sign, |  | ||||||
| -- if it is known to be normally negative, convert it to normally positive. |  | ||||||
| mbrNormaliseSign :: NormalSign -> MultiBalanceReport -> MultiBalanceReport |  | ||||||
| mbrNormaliseSign NormallyNegative = mbrNegate |  | ||||||
| mbrNormaliseSign _ = id |  | ||||||
| 
 |  | ||||||
| -- | Flip the sign of all amounts in a MultiBalanceReport. |  | ||||||
| mbrNegate (MultiBalanceReport (colspans, rows, totalsrow)) = |  | ||||||
|   MultiBalanceReport (colspans, map mbrRowNegate rows, mbrTotalsRowNegate totalsrow) |  | ||||||
|   where |  | ||||||
|     mbrRowNegate (acct,shortacct,indent,amts,tot,avg) = (acct,shortacct,indent,map negate amts,-tot,-avg) |  | ||||||
|     mbrTotalsRowNegate (amts,tot,avg) = (map negate amts,-tot,-avg) |  | ||||||
| 
 |  | ||||||
| -- | Run one subreport for a compound balance command in multi-column mode. | -- | Run one subreport for a compound balance command in multi-column mode. | ||||||
| -- This returns a MultiBalanceReport. | -- This returns a MultiBalanceReport. | ||||||
| compoundBalanceSubreport :: ReportOpts -> Query -> Journal -> (Journal -> Query) -> NormalSign -> MultiBalanceReport | compoundBalanceSubreport :: ReportOpts -> Query -> Journal -> (Journal -> Query) -> NormalSign -> MultiBalanceReport | ||||||
|  | |||||||
| @ -47,6 +47,9 @@ txt, csv, html. | |||||||
| `--sort-amount` | `--sort-amount` | ||||||
| : sort by amount instead of account code/name (in flat mode). With multiple columns, sorts by the row total, or by row average if that is displayed.  | : sort by amount instead of account code/name (in flat mode). With multiple columns, sorts by the row total, or by row average if that is displayed.  | ||||||
| 
 | 
 | ||||||
|  | `--invert` | ||||||
|  | : display all amounts with reversed sign  | ||||||
|  | 
 | ||||||
| `--budget` | `--budget` | ||||||
| : show performance compared to budget goals defined by [periodic transactions](journal.html#periodic-transactions) | : show performance compared to budget goals defined by [periodic transactions](journal.html#periodic-transactions) | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user