lib: Use last column of historical balance as total. Use totals for balanceReport.

This commit is contained in:
Stephen Morgan 2020-07-07 15:20:43 +10:00 committed by Simon Michael
parent 5ffe1d7656
commit bde4d7e2e4
3 changed files with 21 additions and 14 deletions

View File

@ -18,7 +18,6 @@ module Hledger.Reports.BalanceReport (
where where
import Data.Time.Calendar import Data.Time.Calendar
import Safe (headDef)
import Hledger.Data import Hledger.Data
import Hledger.Read (mamountp') import Hledger.Read (mamountp')
@ -69,12 +68,9 @@ balanceReport ropts q j = (rows, total)
rows = [( prrFullName row rows = [( prrFullName row
, prrDisplayName row , prrDisplayName row
, prrDepth row - 1 -- BalanceReport uses 0-based account depths , prrDepth row - 1 -- BalanceReport uses 0-based account depths
, headAmt row , prrTotal row
) | row <- prRows report] ) | row <- prRows report]
total = headAmt $ prTotals report total = prrTotal $ prTotals report
headAmt = headDef nullmixedamt . prrAmounts
-- tests -- tests
@ -110,12 +106,12 @@ tests_BalanceReport = tests "BalanceReport" [
(aitems, atotal) = balanceReport opts (queryFromOpts nulldate opts) journal (aitems, atotal) = balanceReport opts (queryFromOpts nulldate opts) journal
showw (acct,acct',indent,amt) = (acct, acct', indent, showMixedAmountDebug amt) showw (acct,acct',indent,amt) = (acct, acct', indent, showMixedAmountDebug amt)
(map showw aitems) @?= (map showw eitems) (map showw aitems) @?= (map showw eitems)
(showMixedAmountDebug etotal) @?= (showMixedAmountDebug atotal) (showMixedAmountDebug atotal) @?= (showMixedAmountDebug etotal)
in in
tests "balanceReport" [ tests "balanceReport" [
test "no args, null journal" $ test "no args, null journal" $
(defreportopts, nulljournal) `gives` ([], Mixed []) (defreportopts, nulljournal) `gives` ([], 0)
,test "no args, sample journal" $ ,test "no args, sample journal" $
(defreportopts, samplejournal) `gives` (defreportopts, samplejournal) `gives`
@ -165,7 +161,7 @@ tests_BalanceReport = tests "BalanceReport" [
,test "with date:" $ ,test "with date:" $
(defreportopts{query_="date:'in 2009'"}, samplejournal2) `gives` (defreportopts{query_="date:'in 2009'"}, samplejournal2) `gives`
([], Mixed [num 0]) ([], 0)
,test "with date2:" $ ,test "with date2:" $
(defreportopts{query_="date2:'in 2009'"}, samplejournal2) `gives` (defreportopts{query_="date2:'in 2009'"}, samplejournal2) `gives`
@ -205,7 +201,7 @@ tests_BalanceReport = tests "BalanceReport" [
,test "with period on an unpopulated period" $ ,test "with period on an unpopulated period" $
(defreportopts{period_= PeriodBetween (fromGregorian 2008 1 2) (fromGregorian 2008 1 3)}, samplejournal) `gives` (defreportopts{period_= PeriodBetween (fromGregorian 2008 1 2) (fromGregorian 2008 1 3)}, samplejournal) `gives`
([], Mixed [num 0]) ([], 0)

View File

@ -44,7 +44,7 @@ import Data.Semigroup ((<>))
#endif #endif
import Data.Semigroup (sconcat) import Data.Semigroup (sconcat)
import Data.Time.Calendar (Day, addDays, fromGregorian) import Data.Time.Calendar (Day, addDays, fromGregorian)
import Safe (headMay, lastMay) import Safe (headMay, lastDef, lastMay)
import Text.Tabular as T import Text.Tabular as T
import Text.Tabular.AsciiWide (render) import Text.Tabular.AsciiWide (render)
@ -403,7 +403,7 @@ buildReportRows ropts acctvalues =
-- The total and average for the row. -- The total and average for the row.
-- These are always simply the sum/average of the displayed row amounts. -- These are always simply the sum/average of the displayed row amounts.
-- Total for a cumulative/historical report is always zero. -- 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 , let rowavg = averageMixedAmounts rowbals
] ]
where balance = case accountlistmode_ ropts of ALTree -> aibalance; ALFlat -> aebalance 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" $ ,test "with -H on a populated period" $
(defreportopts{period_= PeriodBetween (fromGregorian 2008 1 1) (fromGregorian 2008 1 2), balancetype_=HistoricalBalance}, samplejournal) `gives` (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 "assets:bank:checking") [mamountp' "$1.00"] (mamountp' "$1.00") (Mixed [amt0 {aquantity=1}])
, PeriodicReportRow (flatDisplayName "income:salary") [mamountp' "$-1.00"] (Mixed [nullamt]) (Mixed [amt0 {aquantity=(-1)}]) , PeriodicReportRow (flatDisplayName "income:salary") [mamountp' "$-1.00"] (mamountp' "$-1.00") (Mixed [amt0 {aquantity=(-1)}])
], ],
Mixed [nullamt]) Mixed [nullamt])

View File

@ -364,3 +364,14 @@ $ hledger -f- bal -N -S --tree
1 aa 1 aa
2 b 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
>=