feat: bal: A new --count report type counts postings instead of amounts.
This commit is contained in:
parent
40d10bc8c5
commit
1be06c87c4
@ -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
|
-- changes to report on: usually just the valued changes themselves, but use the
|
||||||
-- differences in the valued historical amount for CalcValueChange and CalcGain.
|
-- differences in the valued historical amount for CalcValueChange and CalcGain.
|
||||||
changes = case balancecalc_ ropts of
|
changes = case balancecalc_ ropts of
|
||||||
CalcChange -> M.mapWithKey avalue unvaluedChanges
|
CalcChange -> M.mapWithKey avalue unvaluedChanges
|
||||||
CalcBudget -> M.mapWithKey avalue unvaluedChanges
|
CalcBudget -> M.mapWithKey avalue unvaluedChanges
|
||||||
CalcValueChange -> periodChanges valuedStart historical
|
CalcValueChange -> periodChanges valuedStart historical
|
||||||
CalcGain -> periodChanges valuedStart historical
|
CalcGain -> periodChanges valuedStart historical
|
||||||
|
CalcPostingsCount -> M.mapWithKey avalue unvaluedChanges
|
||||||
-- the historical balance is the valued cumulative sum of all unvalued changes
|
-- the historical balance is the valued cumulative sum of all unvalued changes
|
||||||
historical = M.mapWithKey avalue $ cumulativeSum startingBalance unvaluedChanges
|
historical = M.mapWithKey avalue $ cumulativeSum startingBalance unvaluedChanges
|
||||||
-- since this is a cumulative sum of valued amounts, it should not be valued again
|
-- 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
|
generateMultiBalanceReport :: ReportSpec -> Journal -> PriceOracle -> Set AccountName
|
||||||
-> [(DateSpan, [Posting])] -> HashMap AccountName Account
|
-> [(DateSpan, [Posting])] -> HashMap AccountName Account
|
||||||
-> MultiBalanceReport
|
-> MultiBalanceReport
|
||||||
generateMultiBalanceReport rspec@ReportSpec{_rsReportOpts=ropts} j priceoracle unelidableaccts colps startbals =
|
generateMultiBalanceReport rspec@ReportSpec{_rsReportOpts=ropts} j priceoracle unelidableaccts colps0 startbals =
|
||||||
report
|
report
|
||||||
where
|
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
|
-- Process changes into normal, cumulative, or historical amounts, plus value them
|
||||||
matrix = calculateReportMatrix rspec j priceoracle startbals colps
|
matrix = calculateReportMatrix rspec j priceoracle startbals colps
|
||||||
|
|
||||||
|
|||||||
@ -88,10 +88,11 @@ import Hledger.Utils
|
|||||||
-- | What to calculate for each cell in a balance report.
|
-- | What to calculate for each cell in a balance report.
|
||||||
-- "Balance report types -> Calculation type" in the hledger manual.
|
-- "Balance report types -> Calculation type" in the hledger manual.
|
||||||
data BalanceCalculation =
|
data BalanceCalculation =
|
||||||
CalcChange -- ^ Sum of posting amounts in the period.
|
CalcChange -- ^ Sum of posting amounts in the period.
|
||||||
| CalcBudget -- ^ Sum of posting amounts and the goal for 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.
|
| 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.
|
| CalcGain -- ^ Change from previous period's gain, i.e. valuation minus cost basis.
|
||||||
|
| CalcPostingsCount -- ^ Number of postings in the period.
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
instance Default BalanceCalculation where def = CalcChange
|
instance Default BalanceCalculation where def = CalcChange
|
||||||
@ -318,6 +319,7 @@ balancecalcopt =
|
|||||||
"valuechange" -> Just CalcValueChange
|
"valuechange" -> Just CalcValueChange
|
||||||
"gain" -> Just CalcGain
|
"gain" -> Just CalcGain
|
||||||
"budget" -> Just CalcBudget
|
"budget" -> Just CalcBudget
|
||||||
|
"count" -> Just CalcPostingsCount
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
balanceaccumopt :: RawOpts -> BalanceAccumulation
|
balanceaccumopt :: RawOpts -> BalanceAccumulation
|
||||||
|
|||||||
@ -162,7 +162,7 @@ reportflags = [
|
|||||||
,flagReq ["depth"] (\s opts -> Right $ setopt "depth" s opts) "NUM" "(or -NUM): hide accounts/postings deeper than this"
|
,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)"
|
,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")
|
,flagNone ["B","cost"] (setboolopt "B")
|
||||||
"show amounts converted to their cost/selling amount, using the transaction price."
|
"show amounts converted to their cost/selling amount, using the transaction price."
|
||||||
,flagNone ["V","market"] (setboolopt "V")
|
,flagNone ["V","market"] (setboolopt "V")
|
||||||
|
|||||||
@ -284,6 +284,7 @@ import Hledger.Read.CsvReader (CSV, printCSV)
|
|||||||
balancemode = hledgerCommandMode
|
balancemode = hledgerCommandMode
|
||||||
$(embedFileRelative "Hledger/Cli/Commands/Balance.txt")
|
$(embedFileRelative "Hledger/Cli/Commands/Balance.txt")
|
||||||
(
|
(
|
||||||
|
-- https://hledger.org/dev/hledger.html#calculation-type :
|
||||||
[flagNone ["sum"] (setboolopt "sum")
|
[flagNone ["sum"] (setboolopt "sum")
|
||||||
"show sum of posting amounts (default)"
|
"show sum of posting amounts (default)"
|
||||||
-- XXX --budget[=DESCPAT], --forecast[=PERIODEXP], could be more consistent
|
-- 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)"
|
"show total change of value of period-end historical balances (caused by deposits, withdrawals, market price fluctuations)"
|
||||||
,flagNone ["gain"] (setboolopt "gain")
|
,flagNone ["gain"] (setboolopt "gain")
|
||||||
"show unrealised capital gain/loss (historical balance value minus cost basis)"
|
"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")
|
,flagNone ["change"] (setboolopt "change")
|
||||||
"accumulate amounts from column start to column end (in multicolumn reports, default)"
|
"accumulate amounts from column start to column end (in multicolumn reports, default)"
|
||||||
,flagNone ["cumulative"] (setboolopt "cumulative")
|
,flagNone ["cumulative"] (setboolopt "cumulative")
|
||||||
@ -305,6 +308,7 @@ balancemode = hledgerCommandMode
|
|||||||
,flagNone ["historical","H"] (setboolopt "historical")
|
,flagNone ["historical","H"] (setboolopt "historical")
|
||||||
"accumulate amounts from journal start to column end (includes postings before report start date)\n "
|
"accumulate amounts from journal start to column end (includes postings before report start date)\n "
|
||||||
]
|
]
|
||||||
|
-- other options specific to this command:
|
||||||
++ flattreeflags True ++
|
++ flattreeflags True ++
|
||||||
[flagReq ["drop"] (\s opts -> Right $ setopt "drop" s opts) "N" "omit N leading account name parts (in flat mode)"
|
[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)"
|
,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"
|
,"'bare' : commodity symbols in one column"
|
||||||
,"'tidy' : every attribute in its own column"
|
,"'tidy' : every attribute in its own column"
|
||||||
])
|
])
|
||||||
|
-- output:
|
||||||
,outputFormatFlag ["txt","html","csv","json"]
|
,outputFormatFlag ["txt","html","csv","json"]
|
||||||
,outputFileFlag
|
,outputFileFlag
|
||||||
]
|
]
|
||||||
|
|||||||
@ -39,6 +39,7 @@ Many of these work with the higher-level commands as well.
|
|||||||
- or value of balance changes ([`-V`](#valuation-type))
|
- or value of balance changes ([`-V`](#valuation-type))
|
||||||
- or change of balance values ([`--valuechange`](#balance-report-types))
|
- or change of balance values ([`--valuechange`](#balance-report-types))
|
||||||
- or unrealised capital gain/loss ([`--gain`](#balance-report-types))
|
- or unrealised capital gain/loss ([`--gain`](#balance-report-types))
|
||||||
|
- or postings count ([`--count`](#balance-report-types))
|
||||||
|
|
||||||
..in..
|
..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.
|
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,
|
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:
|
There are three important option groups:
|
||||||
|
|
||||||
@ -435,6 +436,7 @@ It is one of:
|
|||||||
(caused by deposits, withdrawals, and/or market price fluctuations)
|
(caused by deposits, withdrawals, and/or market price fluctuations)
|
||||||
- `--gain` : show the unrealised capital gain/loss, (the current valued balance
|
- `--gain` : show the unrealised capital gain/loss, (the current valued balance
|
||||||
minus each amount's original cost)
|
minus each amount's original cost)
|
||||||
|
- `--count` : show the count of postings
|
||||||
|
|
||||||
#### Accumulation type
|
#### Accumulation type
|
||||||
|
|
||||||
|
|||||||
@ -205,3 +205,14 @@ $ hledger -f- bal -M -O html
|
|||||||
$ hledger -f- bal -MT -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user