Skip to content

Commit

Permalink
fix: Fill totals with correct amount of zeroes for empty reports
Browse files Browse the repository at this point in the history
  • Loading branch information
aragaer committed Mar 17, 2024
1 parent f1bd46f commit fa9c9c1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
10 changes: 6 additions & 4 deletions hledger-lib/Hledger/Reports/MultiBalanceReport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ generateMultiBalanceReport rspec@ReportSpec{_rsReportOpts=ropts} j priceoracle u
$ buildReportRows ropts displaynames matrix

-- Calculate column totals
totalsrow = dbg5 "totalsrow" $ calculateTotalsRow ropts rows
totalsrow = dbg5 "totalsrow" $ calculateTotalsRow ropts rows $ length colps

-- Sorted report rows.
sortedrows = dbg5 "sortedrows" $ sortRows ropts j rows
Expand Down Expand Up @@ -501,8 +501,8 @@ sortRows ropts j
-- | Build the report totals row.
--
-- Calculate the column totals. These are always the sum of column amounts.
calculateTotalsRow :: ReportOpts -> [MultiBalanceReportRow] -> PeriodicReportRow () MixedAmount
calculateTotalsRow ropts rows =
calculateTotalsRow :: ReportOpts -> [MultiBalanceReportRow] -> Int -> PeriodicReportRow () MixedAmount
calculateTotalsRow ropts rows colcount =
PeriodicReportRow () coltotals grandtotal grandaverage
where
isTopRow row = flat_ ropts || not (any (`HM.member` rowMap) parents)
Expand All @@ -511,7 +511,9 @@ calculateTotalsRow ropts rows =

colamts = transpose . map prrAmounts $ filter isTopRow rows

coltotals :: [MixedAmount] = dbg5 "coltotals" $ map maSum colamts
coltotals :: [MixedAmount] = dbg5 "coltotals" $ case colamts of
[] -> replicate colcount nullmixedamt
_ -> map maSum colamts

-- Calculate the grand total and average. These are always the sum/average
-- of the column totals.
Expand Down
22 changes: 11 additions & 11 deletions hledger/test/balancesheet.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Balance Sheet 2016-01-01
Liabilities ||
-------------++------------
-------------++------------
||
|| 0
=============++============
Net: || 1

Expand All @@ -42,7 +42,7 @@ Balance Sheet 2016-01-01
Liabilities ||
-------------++------------
-------------++------------
||
|| 0
=============++============
Net: || 1

Expand Down Expand Up @@ -171,7 +171,7 @@ Balance Sheet 2017-01-01
Liabilities ||
-------------++------------
-------------++------------
||
|| 0
=============++============
Net: || 1

Expand All @@ -193,7 +193,7 @@ Balance Sheet 2017-01-01
Liabilities ||
-------------++------------
-------------++------------
||
|| 0
=============++============
Net: || 1

Expand All @@ -215,7 +215,7 @@ Balance Sheet 2017-12-31
Liabilities ||
-------------++---------------------
-------------++---------------------
||
|| 0 0
=============++=====================
Net: || $1 $1

Expand All @@ -238,7 +238,7 @@ Balance Sheet 2016-01-31
Liabilities ║
─────────────╫────────────
─────────────╫────────────
0
═════════════╬════════════
Net: ║ 1

Expand Down Expand Up @@ -276,7 +276,7 @@ Balance Sheet 2018-10-03
Liabilities ||
-------------------------------------++------------
-------------------------------------++------------
||
|| 0
=====================================++============
Net: || $120

Expand All @@ -303,7 +303,7 @@ Balance Sheet 2020-03-25
Liabilities ||
-------------++------------
-------------++------------
||
|| 0
=============++============
Net: || $1

Expand All @@ -315,7 +315,7 @@ $ hledger -f - balancesheet --tree --output-format=csv
"assets:a","$1"
"total","$1"
"Liabilities",""
"total"
"total","0"
"Net:","$1"

# ** 13. CSV output supports --drop.
Expand All @@ -326,7 +326,7 @@ $ hledger -f - balancesheet --tree --output-format=csv --drop 1
"a","$1"
"total","$1"
"Liabilities",""
"total"
"total","0"
"Net:","$1"

<
Expand All @@ -342,7 +342,7 @@ $ hledger -f - balancesheet --output-format=csv
"assets:a","$-10000.00"
"total","$-10000.00"
"Liabilities",""
"total"
"total","0"
"Net:","$-10000.00"

# ** 15. In compound reports like balancesheet, parent accounts might not have
Expand Down
2 changes: 1 addition & 1 deletion hledger/test/cashflow.test
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Cashflow Statement 2016-10
Cash flows ||
------------++-----
------------++-----
||
|| 0
>2
>= 0

Expand Down
2 changes: 1 addition & 1 deletion hledger/test/cli/multiple-files.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Income Statement 2014-01-01..2014-01-02
Revenues ||
--------------------------++------------------------
--------------------------++------------------------
||
|| 0
==========================++========================
Expenses ||
--------------------------++------------------------
Expand Down
10 changes: 5 additions & 5 deletions hledger/test/incomestatement.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Income Statement 2016-01-01
Expenses ||
----------++------------
----------++------------
||
|| 0
==========++============
Net: || -1

Expand Down Expand Up @@ -138,7 +138,7 @@ Income Statement 2015-10
Expenses ||
-------------------++------------
-------------------++------------
||
|| 0
===================++============
Net: || $10,000.00

Expand Down Expand Up @@ -171,14 +171,14 @@ Income Statement 2016-10
Revenues ||
----------++-----
----------++-----
||
|| 0
==========++=====
Expenses ||
----------++-----
----------++-----
||
|| 0
==========++=====
Net: ||
Net: || 0

# ** 6. Multicolumn test
# old (arithmetic sign):
Expand Down

0 comments on commit fa9c9c1

Please sign in to comment.