bal/bs/cf/is: --sort-amount sorts by amount instead of account name

This commit is contained in:
Simon Michael 2017-09-25 06:17:46 -10:00
parent 5c85a1dd1c
commit 35e2e94228
7 changed files with 30 additions and 6 deletions

View File

@ -92,6 +92,7 @@ balanceReport opts q j = (items, total)
dbg1 "accts" $
take 1 $ clipAccountsAndAggregate (queryDepth q) $ flattenAccounts accts
| flat_ opts = dbg1 "accts" $
maybesort $
filterzeros $
filterempty $
drop 1 $ clipAccountsAndAggregate (queryDepth q) $ flattenAccounts accts
@ -107,6 +108,7 @@ balanceReport opts q j = (items, total)
filterempty = filter (\a -> anumpostings a > 0 || not (isZeroMixedAmount (balance a)))
prunezeros = if empty_ opts then id else fromMaybe nullacct . pruneAccounts (isZeroMixedAmount . balance)
markboring = if no_elide_ opts then id else markBoringParentAccounts
maybesort = if sort_amount_ opts then sortBy (flip $ comparing balance) else id
items = dbg1 "items" $ map (balanceReportItem opts q) accts'
total | not (flat_ opts) = dbg1 "total" $ sum [amt | (_,_,indent,amt) <- items, indent == 0]
| otherwise = dbg1 "total" $

View File

@ -169,7 +169,13 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, items, totalsrow
[(a, map snd abs) | abs@((a,_):_) <- transpose acctBalChangesPerSpan] -- never null, or used when null...
items :: [MultiBalanceReportRow] =
dbg1 "items"
dbg1 "items" $
(if sort_amount_ opts && accountlistmode_ opts /= ALTree
then sortBy (flip $ comparing $
-- sort by average when that is displayed, instead of total.
-- Usually equivalent, but perhaps not in future.
if average_ opts then sixth6 else fifth6)
else id) $
[(a, accountLeafName a, accountNameLevel a, displayedBals, rowtot, rowavg)
| (a,changes) <- acctBalChanges
, let displayedBals = case balancetype_ opts of

View File

@ -97,6 +97,7 @@ data ReportOpts = ReportOpts {
,no_total_ :: Bool
,value_ :: Bool
,pretty_tables_ :: Bool
,sort_amount_ :: Bool
,color_ :: Bool
} deriving (Show, Data, Typeable)
@ -126,6 +127,7 @@ defreportopts = ReportOpts
def
def
def
def
rawOptsToReportOpts :: RawOpts -> IO ReportOpts
rawOptsToReportOpts rawopts = checkReportOpts <$> do
@ -153,6 +155,7 @@ rawOptsToReportOpts rawopts = checkReportOpts <$> do
,row_total_ = boolopt "row-total" rawopts'
,no_total_ = boolopt "no-total" rawopts'
,value_ = boolopt "value" rawopts'
,sort_amount_ = boolopt "sort-amount" rawopts'
,pretty_tables_ = boolopt "pretty-tables" rawopts'
,color_ = color
}

View File

@ -282,6 +282,7 @@ balancemode = (defCommandMode $ ["balance"] ++ aliases) { -- also accept but don
,flagNone ["no-elide"] (\opts -> setboolopt "no-elide" opts) "don't squash boring parent accounts (in tree mode)"
,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format (in simple reports)"
,flagNone ["pretty-tables"] (\opts -> setboolopt "pretty-tables" opts) "use unicode when displaying tables"
,flagNone ["sort-amount","S"] (\opts -> setboolopt "sort-amount" opts) "sort by amount/total/average (in flat mode)"
]
++ outputflags
,groupHidden = []

View File

@ -65,6 +65,7 @@ compoundBalanceCommandMode CompoundBalanceCommandSpec{..} = (defCommandMode $ cb
,flagNone ["no-elide"] (\opts -> setboolopt "no-elide" opts) "don't squash boring parent accounts (in tree mode)"
,flagReq ["format"] (\s opts -> Right $ setopt "format" s opts) "FORMATSTR" "use this custom line format (in simple reports)"
,flagNone ["pretty-tables"] (\opts -> setboolopt "pretty-tables" opts) "use unicode when displaying tables"
,flagNone ["sort-amount","S"] (\opts -> setboolopt "sort-amount" opts) "sort by amount/total/average (in flat mode)"
,outputFormatFlag
,outputFileFlag
]
@ -175,11 +176,6 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{command_=cmd,
"csv" -> printCSV (compoundBalanceReportAsCsv ropts cbr) ++ "\n"
_ -> compoundBalanceReportAsText ropts' cbr
-- | Render a multi-column balance report as plain text suitable for console output.
-- Add the second table below the first, discarding its column headings.
concatTables (Table hLeft hTop dat) (Table hLeft' _ dat') =
Table (T.Group DoubleLine [hLeft, hLeft']) hTop (dat ++ dat')
-- | Run one subreport for a compound balance command in single-column mode.
-- Currently this returns the plain text rendering of the subreport, and its total.
-- The latter is wrapped in a Sum for easy monoidal combining.
@ -290,6 +286,10 @@ compoundBalanceReportAsText ropts (title, subreports, (coltotals, grandtotal, gr
-- tweak the layout
t = Table (T.Group SingleLine [Header title, lefthdrs]) tophdrs ([]:cells)
-- | Add the second table below the first, discarding its column headings.
concatTables (Table hLeft hTop dat) (Table hLeft' _ dat') =
Table (T.Group DoubleLine [hLeft, hLeft']) hTop (dat ++ dat')
-- | Render a compound balance report as CSV.
{- Eg:
ghci> :main -f examples/sample.journal bs -Y -O csv -AT

View File

@ -44,6 +44,9 @@ txt, csv.
`--pretty-tables`
: Use unicode to display prettier tables.
`--sort-amount`
: Sort by amount (total row amount, or by average if that is displayed), instead of account name (in flat mode)
The balance command displays accounts and balances.
It is hledger's most featureful and versatile command.

View File

@ -206,6 +206,9 @@ Show a balance sheet. Alias: bs.
`--format=LINEFORMAT`
: in single-column balance reports: use this custom line format
`--sort-amount`
: Sort by amount (total row amount, or by average if that is displayed), instead of account name (in flat mode)
This command displays a simple [balance
sheet](http://en.wikipedia.org/wiki/Balance_sheet). It currently assumes that
you have top-level accounts named `asset` and `liability` (plural forms also
@ -313,6 +316,9 @@ Show a cashflow statement. Alias: cf.
`--format=LINEFORMAT`
: in single-column balance reports: use this custom line format
`--sort-amount`
: Sort by amount (total row amount, or by average if that is displayed), instead of account name (in flat mode)
This command displays a simple
[cashflow statement](http://en.wikipedia.org/wiki/Cash_flow_statement)
It shows the change in all "cash" (ie, liquid assets) accounts for the
@ -437,6 +443,9 @@ Show an income statement. Alias: is.
`--format=LINEFORMAT`
: in single-column balance reports: use this custom line format
`--sort-amount`
: Sort by amount (total row amount, or by average if that is displayed), instead of account name (in flat mode)
This command displays a simple
[income statement](http://en.wikipedia.org/wiki/Income_statement). It
currently assumes that you have top-level accounts named `income` (or