Skip to content

Commit

Permalink
fix: do not show average column in cumulative balance (fixes #2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoettl committed Dec 1, 2023
1 parent f2a7f71 commit f655adc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
26 changes: 26 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion hledger/Hledger/Cli/Commands/Add.hs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ accountWizard PrevInput{..} EntryState{..} = do
historicalp = fmap ((!! (pnum - 1)) . (++ (repeat nullposting)) . tpostings) esSimilarTransaction
historicalacct = case historicalp of Just p -> showAccountName Nothing (ptype p) (paccount p)
Nothing -> ""
def = headDef (T.unpack historicalacct) esArgs
def = traceShowId $ headDef (T.unpack historicalacct) esArgs
endmsg | canfinish && null def = " (or . or enter to finish this transaction)"
| canfinish = " (or . to finish this transaction)"
| otherwise = ""
Expand Down
15 changes: 10 additions & 5 deletions hledger/Hledger/Cli/Commands/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,12 @@ balanceReportAsTable opts@ReportOpts{summary_only_, average_, row_total_, balanc
(Tab.Group Tab.NoLine $ map Tab.Header colheadings)
(concat rows)
where
totalscolumn = row_total_ && balanceaccum_ `notElem` [Cumulative, Historical]
totalscolumn = hasAggregatedColumn opts row_total_
averagescolumn = hasAggregatedColumn opts average_
colheadings = ["Commodity" | layout_ opts == LayoutBare]
++ (if not summary_only_ then map (reportPeriodName balanceaccum_ spans) spans else [])
++ [" Total" | totalscolumn]
++ ["Average" | average_]
++ ["Average" | averagescolumn]
fullRowAsTexts row =
let rs = multiBalanceRowAsTableText opts row
in (replicate (length rs) (renderacct row), rs)
Expand All @@ -729,7 +730,7 @@ balanceReportAsTable opts@ReportOpts{summary_only_, average_, row_total_, balanc
| otherwise = id

multiBalanceRowAsWbs :: AmountDisplayOpts -> ReportOpts -> [DateSpan] -> PeriodicReportRow a MixedAmount -> [[WideBuilder]]
multiBalanceRowAsWbs bopts ReportOpts{..} colspans (PeriodicReportRow _ as rowtot rowavg) =
multiBalanceRowAsWbs bopts opts@ReportOpts{..} colspans (PeriodicReportRow _ as rowtot rowavg) =
case layout_ of
LayoutWide width -> [fmap (showMixedAmountB bopts{displayMaxWidth=width}) allamts]
LayoutTall -> paddedTranspose mempty
Expand All @@ -746,11 +747,12 @@ multiBalanceRowAsWbs bopts ReportOpts{..} colspans (PeriodicReportRow _ as rowto
$ as -- Do not include totals column or average for tidy output, as this
-- complicates the data representation and can be easily calculated
where
totalscolumn = row_total_ && balanceaccum_ `notElem` [Cumulative, Historical]
totalscolumn = hasAggregatedColumn opts row_total_
averagescolumn = hasAggregatedColumn opts average_
cs = if all mixedAmountLooksZero allamts then [""] else S.toList $ foldMap maCommodities allamts
allamts = (if not summary_only_ then as else []) ++
[rowtot | totalscolumn && not (null as)] ++
[rowavg | average_ && not (null as)]
[rowavg | averagescolumn && not (null as)]
addDateColumns spn@(DateSpan s e) = (wbFromText (showDateSpan spn) :)
. (wbFromText (maybe "" showEFDate s) :)
. (wbFromText (maybe "" (showEFDate . modifyEFDay (addDays (-1))) e) :)
Expand All @@ -775,6 +777,9 @@ multiBalanceRowAsCsvText opts colspans = fmap (fmap wbToText) . multiBalanceRowA
multiBalanceRowAsTableText :: ReportOpts -> PeriodicReportRow a MixedAmount -> [[WideBuilder]]
multiBalanceRowAsTableText opts = multiBalanceRowAsWbs oneLine{displayColour=color_ opts} opts []

hasAggregatedColumn :: ReportOpts -> Bool -> Bool
hasAggregatedColumn ReportOpts{balanceaccum_} flag = flag && balanceaccum_ `notElem` [Cumulative, Historical]

tests_Balance = testGroup "Balance" [

testGroup "balanceReportAsText" [
Expand Down
35 changes: 35 additions & 0 deletions hledger/test/balance/balance-cumulative.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


$ hledger -f sample.journal balance --cumulative -M -AT
Ending balances (cumulative) in 2008:

|| 2008-01-31 2008-02-29 2008-03-31 2008-04-30 2008-05-31 2008-06-30 2008-07-31 2008-08-31 2008-09-30 2008-10-31 2008-11-30 2008-12-31
======================++================================================================================================================================================
assets:bank:checking || $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 0
assets:bank:saving || 0 0 0 0 0 $1 $1 $1 $1 $1 $1 $1
assets:cash || 0 0 0 0 0 $-2 $-2 $-2 $-2 $-2 $-2 $-2
expenses:food || 0 0 0 0 0 $1 $1 $1 $1 $1 $1 $1
expenses:supplies || 0 0 0 0 0 $1 $1 $1 $1 $1 $1 $1
income:gifts || 0 0 0 0 0 $-1 $-1 $-1 $-1 $-1 $-1 $-1
income:salary || $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1
liabilities:debts || 0 0 0 0 0 0 0 0 0 0 0 $1
----------------------++------------------------------------------------------------------------------------------------------------------------------------------------
|| 0 0 0 0 0 0 0 0 0 0 0 0


$ hledger -f sample.journal balance --historical -M -AT
Ending balances (historical) in 2008:

|| 2008-01-31 2008-02-29 2008-03-31 2008-04-30 2008-05-31 2008-06-30 2008-07-31 2008-08-31 2008-09-30 2008-10-31 2008-11-30 2008-12-31
======================++================================================================================================================================================
assets:bank:checking || $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 $1 0
assets:bank:saving || 0 0 0 0 0 $1 $1 $1 $1 $1 $1 $1
assets:cash || 0 0 0 0 0 $-2 $-2 $-2 $-2 $-2 $-2 $-2
expenses:food || 0 0 0 0 0 $1 $1 $1 $1 $1 $1 $1
expenses:supplies || 0 0 0 0 0 $1 $1 $1 $1 $1 $1 $1
income:gifts || 0 0 0 0 0 $-1 $-1 $-1 $-1 $-1 $-1 $-1
income:salary || $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1 $-1
liabilities:debts || 0 0 0 0 0 0 0 0 0 0 0 $1
----------------------++------------------------------------------------------------------------------------------------------------------------------------------------
|| 0 0 0 0 0 0 0 0 0 0 0 0

4 changes: 3 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# stack build plan using GHC 9.6.3

resolver: nightly-2023-10-13
resolver: lts-21.11

packages:
- hledger-lib
Expand All @@ -14,6 +14,8 @@ nix:
pure: false
packages: [perl gmp ncurses zlib]

extra-deps:

# ghc-options:
# "$locals": -fplugin Debug.Breakpoint

Expand Down

0 comments on commit f655adc

Please sign in to comment.