bal*: support --value-at in multicolumn balance reports

This commit is contained in:
Simon Michael 2019-04-26 07:37:27 -07:00
parent 65934958f9
commit 00975fb226
3 changed files with 78 additions and 33 deletions

View File

@ -1,4 +1,4 @@
{-# LANGUAGE FlexibleInstances, ScopedTypeVariables, OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances, RecordWildCards, ScopedTypeVariables, OverloadedStrings #-}
{-|
Multi-column balance reports, used by the balance command.
@ -20,7 +20,6 @@ module Hledger.Reports.MultiBalanceReports (
)
where
import Control.Applicative ((<|>))
import Data.List
import Data.Maybe
import Data.Ord
@ -276,39 +275,36 @@ multiBalanceReportSpan (MultiBalanceReport (colspans, _, _)) = DateSpan (spanSta
-- | Convert all the posting amounts in a MultiBalanceReport to their
-- default valuation commodities. This means using the Journal's most
-- recent applicable market prices before the valuation date.
-- The valuation date is the specified report end date if any,
-- otherwise the current date, otherwise the journal's end date.
-- The valuation date is set with --value-date and can be:
-- the posting date,
-- the last day in the report subperiod,
-- today's date (gives an error if today_ is not set in ReportOpts),
-- or a custom date.
mbrValue :: ReportOpts -> Journal -> MultiBalanceReport -> MultiBalanceReport
mbrValue ropts j r =
let mvaluationdate = periodEnd (period_ ropts) <|> today_ ropts <|> journalEndDate False j
in case mvaluationdate of
Nothing -> r
Just d -> r'
mbrValue ReportOpts{..} Journal{..} (MultiBalanceReport (spans, rows, (coltotals, rowtotaltotal, rowavgtotal))) =
MultiBalanceReport (
spans
,[(acct, acct', depth, map (uncurry val) $ zip ends rowamts, val end rowtotal, val end rowavg)
| (acct, acct', depth, rowamts, rowtotal, rowavg) <- rows]
,(map (uncurry val) $ zip ends coltotals
,val end rowtotaltotal
,val end rowavgtotal)
)
where
ends = map (fromMaybe (error' "mbrValue: expected all report periods to have an end date") . spanEnd) spans -- XXX shouldn't happen
end = lastDef (error' "mbrValue: expected at least one report subperiod") ends -- XXX shouldn't happen
val periodend amt = mixedAmountValue prices d amt
where
-- prices are in parse order - sort into date then parse order,
-- & reversed for quick lookup of the latest price.
prices = reverse $ sortOn mpdate $ jmarketprices j
MultiBalanceReport (spans, rows, (coltotals, rowtotaltotal, rowavgtotal)) = r
r' = MultiBalanceReport
(spans,
[(acct, acct', depth, map convert rowamts, convert rowtotal, convert rowavg) | (acct, acct', depth, rowamts, rowtotal, rowavg) <- rows],
(map convert coltotals, convert rowtotaltotal, convert rowavgtotal))
convert = mixedAmountValue prices d
-- -- convert to value ?
-- -- first get period end date(s) XXX duplicated from multiBalanceReport
-- -- The date span specified by -b/-e/-p options and query args if any.
-- requestedspan = dbg1 "requestedspan" $ queryDateSpan (date2_ ropts) userq -- XXX userq ok ?
-- -- If the requested span is open-ended, close it using the journal's end dates.
-- -- This can still be the null (open) span if the journal is empty.
-- requestedspan' = dbg1 "requestedspan'" $ requestedspan `spanDefaultsFrom` journalDateSpan (date2_ ropts) j
-- -- The list of interval spans enclosing the requested span.
-- -- This list can be empty if the journal was empty,
-- -- or if hledger-ui has added its special date:-tomorrow to the query
-- -- and all txns are in the future.
-- -- intervalspans = dbg1 "intervalspans" $ splitSpan (interval_ ropts) requestedspan'
prices = reverse $ sortOn mpdate jmarketprices
d = case value_at_ of
AtTransaction -> error' "sorry, --value-at=transaction is not yet supported with multicolumn balance reports" -- XXX
AtPeriod -> periodend
AtNow -> case today_ of
Just d -> d
Nothing -> error' "ReportOpts today_ is unset so could not satisfy --value-at=now"
AtDate d -> d
-- | Generates a simple non-columnar BalanceReport, but using multiBalanceReport,
-- in order to support --historical. Does not support tree-mode boring parent eliding.

View File

@ -511,10 +511,14 @@ The precise effect of the keywords is command-specific, but here is their genera
: Use the prices as of today's date when the report is generated. This is the default.
- `--value-at=YYYY-MM-DD`
: Use the prices as of the given date (must be 8 digits with `-` or `/` or `.` separators).
: Use the prices as of the given date (8 digits with `-` or `/` or `.` separators).
: Eg `--value-at=2019-04-25`.
Currently `--value-at` affects only the [print](/hledger.html#print) command.
Currently `--value-at` affects only some commands
([print](/hledger.html#print),
[multicolumn balance reports](/hledger.html#balance)),
and some of the keywords may not be supported by certain commands.
Here are some examples to show its effect:
```journal

View File

@ -198,3 +198,48 @@ $ hledger -f- print --value-at=2000-01-15
>=0
# 14. multicolumn balance report with default value
$ hledger -f- bal -M -V
Balance changes in 2000q1:
|| Jan Feb Mar
===++===============
a || 4 B 4 B 4 B
---++---------------
|| 4 B 4 B 4 B
# 15. multicolumn balance report valued at transaction is not supported
$ hledger -f- bal -M --value-at=transaction
>2 /--value-at=transaction is not yet supported with multicolumn balance reports/
>=1
# 16. multicolumn balance report valued at period end
$ hledger -f- bal -M --value-at=period
Balance changes in 2000q1:
|| Jan Feb Mar
===++===============
a || 2 B 3 B 4 B
---++---------------
|| 2 B 3 B 4 B
# 17. multicolumn balance report valued at today
$ hledger -f- bal -M --value-at=now
Balance changes in 2000q1:
|| Jan Feb Mar
===++===============
a || 4 B 4 B 4 B
---++---------------
|| 4 B 4 B 4 B
# 18. multicolumn balance report valued at other date
$ hledger -f- bal -M --value-at=2000-01-15
Balance changes in 2000q1:
|| Jan Feb Mar
===++===============
a || 1 B 1 B 1 B
---++---------------
|| 1 B 1 B 1 B