diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 0d0310a13..626f63f0c 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -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 diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index 900c1157f..e18d223d4 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -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 diff --git a/hledger/Hledger/Cli/CliOptions.hs b/hledger/Hledger/Cli/CliOptions.hs index 7a0a368a3..63cd53747 100644 --- a/hledger/Hledger/Cli/CliOptions.hs +++ b/hledger/Hledger/Cli/CliOptions.hs @@ -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") diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index f2657ae80..3674339e5 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -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 ] diff --git a/hledger/Hledger/Cli/Commands/Balance.md b/hledger/Hledger/Cli/Commands/Balance.md index bdf360468..a8e57acf3 100644 --- a/hledger/Hledger/Cli/Commands/Balance.md +++ b/hledger/Hledger/Cli/Commands/Balance.md @@ -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 diff --git a/hledger/test/balance/balance.test b/hledger/test/balance/balance.test index 2972437ad..3574294f4 100644 --- a/hledger/test/balance/balance.test +++ b/hledger/test/balance/balance.test @@ -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