feat: bal: A new --count report type counts postings instead of amounts.

This commit is contained in:
Simon Michael 2023-04-20 14:31:55 -10:00
parent 40d10bc8c5
commit 1be06c87c4
6 changed files with 38 additions and 11 deletions

View File

@ -322,10 +322,11 @@ calculateReportMatrix rspec@ReportSpec{_rsReportOpts=ropts} j priceoracle startb
-- changes to report on: usually just the valued changes themselves, but use the
-- differences in the valued historical amount for CalcValueChange and CalcGain.
changes = case balancecalc_ ropts of
CalcChange -> M.mapWithKey avalue unvaluedChanges
CalcBudget -> M.mapWithKey avalue unvaluedChanges
CalcValueChange -> periodChanges valuedStart historical
CalcGain -> periodChanges valuedStart historical
CalcChange -> M.mapWithKey avalue unvaluedChanges
CalcBudget -> M.mapWithKey avalue unvaluedChanges
CalcValueChange -> periodChanges valuedStart historical
CalcGain -> periodChanges valuedStart historical
CalcPostingsCount -> M.mapWithKey avalue unvaluedChanges
-- the historical balance is the valued cumulative sum of all unvalued changes
historical = M.mapWithKey avalue $ cumulativeSum startingBalance unvaluedChanges
-- since this is a cumulative sum of valued amounts, it should not be valued again
@ -353,9 +354,15 @@ calculateReportMatrix rspec@ReportSpec{_rsReportOpts=ropts} j priceoracle startb
generateMultiBalanceReport :: ReportSpec -> Journal -> PriceOracle -> Set AccountName
-> [(DateSpan, [Posting])] -> HashMap AccountName Account
-> MultiBalanceReport
generateMultiBalanceReport rspec@ReportSpec{_rsReportOpts=ropts} j priceoracle unelidableaccts colps startbals =
generateMultiBalanceReport rspec@ReportSpec{_rsReportOpts=ropts} j priceoracle unelidableaccts colps0 startbals =
report
where
-- If doing --count, set all posting amounts to "1".
colps =
if balancecalc_ ropts == CalcPostingsCount
then map (second (map (postingTransformAmount (const $ mixed [num 1])))) colps0
else colps0
-- Process changes into normal, cumulative, or historical amounts, plus value them
matrix = calculateReportMatrix rspec j priceoracle startbals colps

View File

@ -88,10 +88,11 @@ import Hledger.Utils
-- | What to calculate for each cell in a balance report.
-- "Balance report types -> Calculation type" in the hledger manual.
data BalanceCalculation =
CalcChange -- ^ Sum of posting amounts in the period.
| CalcBudget -- ^ Sum of posting amounts and the goal for the period.
| CalcValueChange -- ^ Change from previous period's historical end value to this period's historical end value.
| CalcGain -- ^ Change from previous period's gain, i.e. valuation minus cost basis.
CalcChange -- ^ Sum of posting amounts in the period.
| CalcBudget -- ^ Sum of posting amounts and the goal for the period.
| CalcValueChange -- ^ Change from previous period's historical end value to this period's historical end value.
| CalcGain -- ^ Change from previous period's gain, i.e. valuation minus cost basis.
| CalcPostingsCount -- ^ Number of postings in the period.
deriving (Eq, Show)
instance Default BalanceCalculation where def = CalcChange
@ -318,6 +319,7 @@ balancecalcopt =
"valuechange" -> Just CalcValueChange
"gain" -> Just CalcGain
"budget" -> Just CalcBudget
"count" -> Just CalcPostingsCount
_ -> Nothing
balanceaccumopt :: RawOpts -> BalanceAccumulation

View File

@ -162,7 +162,7 @@ reportflags = [
,flagReq ["depth"] (\s opts -> Right $ setopt "depth" s opts) "NUM" "(or -NUM): hide accounts/postings deeper than this"
,flagNone ["empty","E"] (setboolopt "empty") "show items with zero amount, normally hidden (and vice-versa in hledger-ui/hledger-web)"
-- valuation
-- valuation, including https://hledger.org/dev/hledger.html#valuation-type :
,flagNone ["B","cost"] (setboolopt "B")
"show amounts converted to their cost/selling amount, using the transaction price."
,flagNone ["V","market"] (setboolopt "V")

View File

@ -284,6 +284,7 @@ import Hledger.Read.CsvReader (CSV, printCSV)
balancemode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Balance.txt")
(
-- https://hledger.org/dev/hledger.html#calculation-type :
[flagNone ["sum"] (setboolopt "sum")
"show sum of posting amounts (default)"
-- XXX --budget[=DESCPAT], --forecast[=PERIODEXP], could be more consistent
@ -298,6 +299,8 @@ balancemode = hledgerCommandMode
"show total change of value of period-end historical balances (caused by deposits, withdrawals, market price fluctuations)"
,flagNone ["gain"] (setboolopt "gain")
"show unrealised capital gain/loss (historical balance value minus cost basis)"
,flagNone ["count"] (setboolopt "count") "show the count of postings"
-- https://hledger.org/dev/hledger.html#accumulation-type :
,flagNone ["change"] (setboolopt "change")
"accumulate amounts from column start to column end (in multicolumn reports, default)"
,flagNone ["cumulative"] (setboolopt "cumulative")
@ -305,6 +308,7 @@ balancemode = hledgerCommandMode
,flagNone ["historical","H"] (setboolopt "historical")
"accumulate amounts from journal start to column end (includes postings before report start date)\n "
]
-- other options specific to this command:
++ flattreeflags True ++
[flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "omit N leading account name parts (in flat mode)"
,flagNone ["declared"] (setboolopt "declared") "include non-parent declared accounts (best used with -E)"
@ -326,6 +330,7 @@ balancemode = hledgerCommandMode
,"'bare' : commodity symbols in one column"
,"'tidy' : every attribute in its own column"
])
-- output:
,outputFormatFlag ["txt","html","csv","json"]
,outputFileFlag
]

View File

@ -39,6 +39,7 @@ Many of these work with the higher-level commands as well.
- or value of balance changes ([`-V`](#valuation-type))
- or change of balance values ([`--valuechange`](#balance-report-types))
- or unrealised capital gain/loss ([`--gain`](#balance-report-types))
- or postings count ([`--count`](#balance-report-types))
..in..
@ -418,7 +419,7 @@ To see accurate historical end balances:
The balance command is quite flexible; here is the full detail on how to control what it reports.
If the following seems complicated, don't worry - this is for advanced reporting,
and it does typically take some time and experimentation to get clear on all these report modes.
and it does take time and experimentation to get familiar with all the report modes.
There are three important option groups:
@ -435,6 +436,7 @@ It is one of:
(caused by deposits, withdrawals, and/or market price fluctuations)
- `--gain` : show the unrealised capital gain/loss, (the current valued balance
minus each amount's original cost)
- `--count` : show the count of postings
#### Accumulation type

View File

@ -205,3 +205,14 @@ $ hledger -f- bal -M -O html
$ hledger -f- bal -MT -O html
> //
# 15. --count counts postings.
<
2023-01-01
a 10
a:aa 20
b
$ hledger -f- bal --count -1
2 a
1 b
--------------------
3