From bde4d7e2e431d61c730b2f2c7c6ee6efac2d9f7a Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Tue, 7 Jul 2020 15:20:43 +1000 Subject: [PATCH] lib: Use last column of historical balance as total. Use totals for balanceReport. --- hledger-lib/Hledger/Reports/BalanceReport.hs | 16 ++++++---------- .../Hledger/Reports/MultiBalanceReport.hs | 8 ++++---- tests/balance/sorting.test | 11 +++++++++++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/hledger-lib/Hledger/Reports/BalanceReport.hs b/hledger-lib/Hledger/Reports/BalanceReport.hs index a8858780c..2ec748451 100644 --- a/hledger-lib/Hledger/Reports/BalanceReport.hs +++ b/hledger-lib/Hledger/Reports/BalanceReport.hs @@ -18,7 +18,6 @@ module Hledger.Reports.BalanceReport ( where import Data.Time.Calendar -import Safe (headDef) import Hledger.Data import Hledger.Read (mamountp') @@ -69,12 +68,9 @@ balanceReport ropts q j = (rows, total) rows = [( prrFullName row , prrDisplayName row , prrDepth row - 1 -- BalanceReport uses 0-based account depths - , headAmt row + , prrTotal row ) | row <- prRows report] - total = headAmt $ prTotals report - headAmt = headDef nullmixedamt . prrAmounts - - + total = prrTotal $ prTotals report -- tests @@ -110,12 +106,12 @@ tests_BalanceReport = tests "BalanceReport" [ (aitems, atotal) = balanceReport opts (queryFromOpts nulldate opts) journal showw (acct,acct',indent,amt) = (acct, acct', indent, showMixedAmountDebug amt) (map showw aitems) @?= (map showw eitems) - (showMixedAmountDebug etotal) @?= (showMixedAmountDebug atotal) + (showMixedAmountDebug atotal) @?= (showMixedAmountDebug etotal) in tests "balanceReport" [ test "no args, null journal" $ - (defreportopts, nulljournal) `gives` ([], Mixed []) + (defreportopts, nulljournal) `gives` ([], 0) ,test "no args, sample journal" $ (defreportopts, samplejournal) `gives` @@ -165,7 +161,7 @@ tests_BalanceReport = tests "BalanceReport" [ ,test "with date:" $ (defreportopts{query_="date:'in 2009'"}, samplejournal2) `gives` - ([], Mixed [num 0]) + ([], 0) ,test "with date2:" $ (defreportopts{query_="date2:'in 2009'"}, samplejournal2) `gives` @@ -205,7 +201,7 @@ tests_BalanceReport = tests "BalanceReport" [ ,test "with period on an unpopulated period" $ (defreportopts{period_= PeriodBetween (fromGregorian 2008 1 2) (fromGregorian 2008 1 3)}, samplejournal) `gives` - ([], Mixed [num 0]) + ([], 0) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index c91cdd3d4..6aa33111c 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -44,7 +44,7 @@ import Data.Semigroup ((<>)) #endif import Data.Semigroup (sconcat) import Data.Time.Calendar (Day, addDays, fromGregorian) -import Safe (headMay, lastMay) +import Safe (headMay, lastDef, lastMay) import Text.Tabular as T import Text.Tabular.AsciiWide (render) @@ -403,7 +403,7 @@ buildReportRows ropts acctvalues = -- The total and average for the row. -- These are always simply the sum/average of the displayed row amounts. -- Total for a cumulative/historical report is always zero. - , let rowtot = if balancetype_ ropts == PeriodChange then sum rowbals else 0 + , let rowtot = if balancetype_ ropts == PeriodChange then sum rowbals else lastDef 0 rowbals , let rowavg = averageMixedAmounts rowbals ] where balance = case accountlistmode_ ropts of ALTree -> aibalance; ALFlat -> aebalance @@ -616,8 +616,8 @@ tests_MultiBalanceReport = tests "MultiBalanceReport" [ ,test "with -H on a populated period" $ (defreportopts{period_= PeriodBetween (fromGregorian 2008 1 1) (fromGregorian 2008 1 2), balancetype_=HistoricalBalance}, samplejournal) `gives` ( - [ PeriodicReportRow (flatDisplayName "assets:bank:checking") [mamountp' "$1.00"] (Mixed [nullamt]) (Mixed [amt0 {aquantity=1}]) - , PeriodicReportRow (flatDisplayName "income:salary") [mamountp' "$-1.00"] (Mixed [nullamt]) (Mixed [amt0 {aquantity=(-1)}]) + [ PeriodicReportRow (flatDisplayName "assets:bank:checking") [mamountp' "$1.00"] (mamountp' "$1.00") (Mixed [amt0 {aquantity=1}]) + , PeriodicReportRow (flatDisplayName "income:salary") [mamountp' "$-1.00"] (mamountp' "$-1.00") (Mixed [amt0 {aquantity=(-1)}]) ], Mixed [nullamt]) diff --git a/tests/balance/sorting.test b/tests/balance/sorting.test index e8aca892a..db1984a2c 100644 --- a/tests/balance/sorting.test +++ b/tests/balance/sorting.test @@ -364,3 +364,14 @@ $ hledger -f- bal -N -S --tree 1 aa 2 b >= + +# 20. Flat mode. --historical (#1287) +$ hledger -f- bal -NY --flat --sort-amount --historical +Ending balances (historical) in 2020: + + || 2020-12-31 +======++============ + a:ab || 3 + b || 2 + a:aa || 1 +>=