print, register: add --value=then, valuing at each posting's date

Currently this will give an error with other kinds of report.
This commit is contained in:
Simon Michael 2020-02-10 08:09:52 -08:00
parent 50acfc9119
commit aa96b41efe
8 changed files with 107 additions and 69 deletions

View File

@ -322,6 +322,7 @@ postingApplyValuation priceoracle styles periodlast mreportlast today ismultiper
case v of
AtCost Nothing -> postingToCost styles p
AtCost mc -> postingValueAtDate priceoracle styles mc periodlast $ postingToCost styles p
AtThen mc -> postingValueAtDate priceoracle styles mc (postingDate p) p
AtEnd mc -> postingValueAtDate priceoracle styles mc periodlast p
AtNow mc -> postingValueAtDate priceoracle styles mc today p
AtDefault mc | ismultiperiod -> postingValueAtDate priceoracle styles mc periodlast p

View File

@ -79,12 +79,13 @@ instance NFData PriceGraph
type PriceOracle = (Day, CommoditySymbol, Maybe CommoditySymbol) -> Maybe (CommoditySymbol, Quantity)
-- | What kind of value conversion should be done on amounts ?
-- UI: --value=cost|end|now|DATE[,COMM]
-- CLI: --value=cost|then|end|now|DATE[,COMM]
data ValuationType =
AtCost (Maybe CommoditySymbol) -- ^ convert to cost commodity using transaction prices, then optionally to given commodity using market prices at posting date
| AtEnd (Maybe CommoditySymbol) -- ^ convert to default valuation commodity or given commodity, using market prices at period end(s)
| AtNow (Maybe CommoditySymbol) -- ^ convert to default valuation commodity or given commodity, using current market prices
| AtDate Day (Maybe CommoditySymbol) -- ^ convert to default valuation commodity or given commodity, using market prices on some date
| AtThen (Maybe CommoditySymbol) -- ^ convert to default or given valuation commodity, using market prices at each posting's date
| AtEnd (Maybe CommoditySymbol) -- ^ convert to default or given valuation commodity, using market prices at period end(s)
| AtNow (Maybe CommoditySymbol) -- ^ convert to default or given valuation commodity, using current market prices
| AtDate Day (Maybe CommoditySymbol) -- ^ convert to default or given valuation commodity, using market prices on some date
| AtDefault (Maybe CommoditySymbol) -- ^ works like AtNow in single period reports, like AtEnd in multiperiod reports
deriving (Show,Data,Eq) -- Typeable
@ -124,6 +125,9 @@ mixedAmountApplyValuation priceoracle styles periodlast mreportlast today ismult
-- - the provided "today" date - (--value=now, or -V/X with no report
-- end date).
--
-- Note --value=then is not supported by this function, and will cause an error;
-- use postingApplyValuation for that.
--
-- This is all a bit complicated. See the reference doc at
-- https://hledger.org/hledger.html#effect-of-value-on-reports
-- (hledger_options.m4.md "Effect of --value on reports"), and #1083.
@ -133,6 +137,8 @@ amountApplyValuation priceoracle styles periodlast mreportlast today ismultiperi
case v of
AtCost Nothing -> amountToCost styles a
AtCost mc -> amountValueAtDate priceoracle styles mc periodlast $ amountToCost styles a
AtThen _mc -> error' "Sorry, --value=then is not yet implemented for this kind of report." -- TODO
-- amountValueAtDate priceoracle styles mc periodlast a -- posting date unknown, handle like AtEnd
AtEnd mc -> amountValueAtDate priceoracle styles mc periodlast a
AtNow mc -> amountValueAtDate priceoracle styles mc today a
AtDefault mc | ismultiperiod -> amountValueAtDate priceoracle styles mc periodlast a

View File

@ -253,6 +253,7 @@ budgetReportAsText ropts@ReportOpts{..} budgetr =
(showDateSpan $ periodicReportSpan budgetr)
(case value_ of
Just (AtCost _mc) -> ", valued at cost"
Just (AtThen _mc) -> error' "Sorry, --value=then is not yet implemented for this kind of report." -- TODO
Just (AtEnd _mc) -> ", valued at period ends"
Just (AtNow _mc) -> ", current value"
-- XXX duplicates the above

View File

@ -359,6 +359,7 @@ valuationTypeFromRawOpts = lastMay . collectopts valuationfromrawopt
| otherwise = Nothing
valuation v
| t `elem` ["cost","c"] = AtCost mc
| t `elem` ["then" ,"t"] = AtThen mc
| t `elem` ["end" ,"e"] = AtEnd mc
| t `elem` ["now" ,"n"] = AtNow mc
| otherwise =

View File

@ -587,6 +587,7 @@ multiBalanceReportAsText ropts@ReportOpts{..} r =
(showDateSpan $ periodicReportSpan r)
(case value_ of
Just (AtCost _mc) -> ", valued at cost"
Just (AtThen _mc) -> error' "Sorry, --value=then is not yet implemented for this kind of report." -- TODO -- ", valued at period ends" -- handled like AtEnd for now
Just (AtEnd _mc) -> ", valued at period ends"
Just (AtNow _mc) -> ", current value"
-- XXX duplicates the above

View File

@ -217,6 +217,7 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r
valuationdesc = case value_ of
Just (AtCost _mc) -> ", valued at cost"
Just (AtThen _mc) -> error' "Sorry, --value=then is not yet implemented for this kind of report." -- TODO
Just (AtEnd _mc) -> ", valued at period ends"
Just (AtNow _mc) -> ", current value"
Just (AtDefault _mc) | multiperiod -> ", valued at period ends"

View File

@ -8,7 +8,9 @@ m4_dnl Lines beginning with m4_dnl are comments.
m4_dnl Words enclosed in underscores are macros, defined in doc/common.m4.
m4_dnl Macro arguments are enclosed in (). Text literals are enclosed in {{}}.
m4_dnl Macros may depend on command line flags, configured in Shake.hs.
m4_dnl In Emacs markdown-mode S-TAB cycles visibility, TAB toggles one section.
m4_dnl In Emacs:
m4_dnl markdown-mode S-TAB cycles visibility, TAB toggles one section.
m4_dnl C-x n s on a heading narrows to that section (C-x n w to widen again).
m4_dnl Show these first headings only in man pages:
_man_({{
@ -1131,10 +1133,11 @@ It is equivalent to `--value=now,COMM` or `--value=end,COMM`.
`-B`, `-V` and `-X` are special cases of the more general `--value` option:
--value=TYPE[,COMM] TYPE is cost, end, now or YYYY-MM-DD.
--value=TYPE[,COMM] TYPE is cost, then, end, now or YYYY-MM-DD.
COMM is an optional commodity symbol.
Shows amounts converted to:
- cost commodity using transaction prices (then optionally to COMM using market prices at period end(s))
- default valuation commodity (or COMM) using market prices at posting dates
- default valuation commodity (or COMM) using market prices at period end(s)
- default valuation commodity (or COMM) using current market prices
- default valuation commodity (or COMM) using market prices at some date
@ -1144,6 +1147,11 @@ The TYPE part basically selects either "cost", or "market value" plus a valuatio
`--value=cost`
: Convert amounts to cost, using the prices recorded in transactions.
`--value=then`
: Convert amounts to their value in a default valuation commodity, using market prices
on each posting's date. This is currently supported only by the
[print](#print) and [register](#register) commands.
`--value=end`
: Convert amounts to their value in a default valuation commodity, using market prices
on the last day of the report period (or if unspecified, the journal's end date);
@ -1303,28 +1311,28 @@ Related:
[#329](https://github.com/simonmichael/hledger/issues/329),
[#1083](https://github.com/simonmichael/hledger/issues/1083).
| Report type | `-B`, `--value=cost` | `-V`, `-X` | `--value=end` | `--value=DATE`, `--value=now` |
|-------------------------------------------------|-----------------------------------------------|--------------------------------------------------|----------------------------------------------------|-----------------------------------------|
| **print** | | | | |
| posting amounts | cost | value at report end or today | value at report or journal end | value at DATE/today |
| balance assertions / assignments | unchanged | unchanged | unchanged | unchanged |
| <br> | | | | |
| **register** | | | | |
| starting balance (with -H) | cost | value at day before report or journal start | value at day before report or journal start | value at DATE/today |
| posting amounts (no report interval) | cost | value at report end or today | value at report or journal end | value at DATE/today |
| summary posting amounts (with report interval) | summarised cost | value at period ends | value at period ends | value at DATE/today |
| running total/average | sum/average of displayed values | sum/average of displayed values | sum/average of displayed values | sum/average of displayed values |
| <br> | | | | |
| **balance (bs, bse, cf, is..)** | | | | |
| balances (no report interval) | sums of costs | value at report end or today of sums of postings | value at report or journal end of sums of postings | value at DATE/today of sums of postings |
| balances (with report interval) | sums of costs | value at period ends of sums of postings | value at period ends of sums of postings | value at DATE/today of sums of postings |
| starting balances (with report interval and -H) | sums of costs of postings before report start | sums of postings before report start | sums of postings before report start | sums of postings before report start |
| budget amounts with --budget | like balances | like balances | like balances | like balances |
| grand total (no report interval) | sum of displayed values | sum of displayed values | sum of displayed values | sum of displayed values |
| row totals/averages (with report interval) | sums/averages of displayed values | sums/averages of displayed values | sums/averages of displayed values | sums/averages of displayed values |
| column totals | sums of displayed values | sums of displayed values | sums of displayed values | sums of displayed values |
| grand total/average | sum/average of column totals | sum/average of column totals | sum/average of column totals | sum/average of column totals |
| <br> | | | | |
| Report type | `-B`, `--value=cost` | `-V`, `-X` | `--value=then` | `--value=end` | `--value=DATE`, `--value=now` |
|-------------------------------------------------|-----------------------------------------------|--------------------------------------------------|----------------------------------------------------------------------|----------------------------------------------------|-----------------------------------------|
| **print** | | | | | |
| posting amounts | cost | value at report end or today | value at posting date | value at report or journal end | value at DATE/today |
| balance assertions / assignments | unchanged | unchanged | unchanged | unchanged | unchanged |
| <br> | | | | | |
| **register** | | | | | |
| starting balance (with -H) | cost | value at day before report or journal start | sum of contemporaneous values of postings before report start ? TODO | value at day before report or journal start | value at DATE/today |
| posting amounts (no report interval) | cost | value at report end or today | value at posting date | value at report or journal end | value at DATE/today |
| summary posting amounts (with report interval) | summarised cost | value at period ends | sum of contemporaneous values of postings in interval | value at period ends | value at DATE/today |
| running total/average | sum/average of displayed values | sum/average of displayed values | sum/average of displayed values | sum/average of displayed values | sum/average of displayed values |
| <br> | | | | | |
| **balance (bs, bse, cf, is..)** | | | | | |
| balances (no report interval) | sums of costs | value at report end or today of sums of postings | not supported | value at report or journal end of sums of postings | value at DATE/today of sums of postings |
| balances (with report interval) | sums of costs | value at period ends of sums of postings | not supported | value at period ends of sums of postings | value at DATE/today of sums of postings |
| starting balances (with report interval and -H) | sums of costs of postings before report start | sums of postings before report start | not supported | sums of postings before report start | sums of postings before report start |
| budget amounts with --budget | like balances | like balances | not supported | like balances | like balances |
| grand total (no report interval) | sum of displayed values | sum of displayed values | not supported | sum of displayed values | sum of displayed values |
| row totals/averages (with report interval) | sums/averages of displayed values | sums/averages of displayed values | not supported | sums/averages of displayed values | sums/averages of displayed values |
| column totals | sums of displayed values | sums of displayed values | not supported | sums of displayed values | sums of displayed values |
| grand total/average | sum/average of column totals | sum/average of column totals | not supported | sum/average of column totals | sum/average of column totals |
| <br> | | | | | |
**Additional notes**

View File

@ -148,7 +148,20 @@ $ hledger -f- print --value=cost
>=0
# 10. print value using prices on last day of report period (2000-02-29)
# 10. print value using market prices on each transaction's (posting's) date
$ hledger -f- print --value=then
2000-01-01
(a) 1 B
2000-02-01
(a) 2 B
2000-03-01
(a) 3 B
>=0
# 11. print value using prices on last day of report period (2000-02-29)
$ hledger -f- print --value=end date:2000/01-2000/03
2000-01-01
(a) 2 B
@ -158,7 +171,7 @@ $ hledger -f- print --value=end date:2000/01-2000/03
>=0
# 11. print value using prices on last day of report period (no period specified)
# 12. print value using prices on last day of report period (no period specified)
# specified - uses last day of journal (2000-03-01)
$ hledger -f- print --value=end
2000-01-01
@ -172,7 +185,7 @@ $ hledger -f- print --value=end
>=0
# 12. print value using prices on a specified date
# 13. print value using prices on a specified date
$ hledger -f- print --value=2000-01-15
2000-01-01
(a) 5 B
@ -185,7 +198,7 @@ $ hledger -f- print --value=2000-01-15
>=0
# 13. print value using prices today
# 14. print value using prices today
# (assuming today's date is >= 2000-04-01)
$ hledger -f- print --value=now
2000-01-01
@ -201,31 +214,37 @@ $ hledger -f- print --value=now
# register
# 14. register report valued at cost.
# 15. register report valued at cost.
$ hledger -f- reg --value=cost
2000-01-01 (a) 6 B 6 B
2000-02-01 (a) 7 B 13 B
2000-03-01 (a) 8 B 21 B
# 15. register report valued at period end
# 16. register report valued at posting dates
$ hledger -f- reg --value=then
2000-01-01 (a) 1 B 1 B
2000-02-01 (a) 2 B 3 B
2000-03-01 (a) 3 B 6 B
# 17. register report valued at period end
$ hledger -f- reg --value=end
2000-01-01 (a) 3 B 3 B
2000-02-01 (a) 3 B 6 B
2000-03-01 (a) 3 B 9 B
# 16. register report valued at specified date
# 18. register report valued at specified date
$ hledger -f- reg --value=2000-01-15
2000-01-01 (a) 5 B 5 B
2000-02-01 (a) 5 B 10 B
2000-03-01 (a) 5 B 15 B
# 17. register report valued today
# 19. register report valued today
$ hledger -f- reg --value=now
2000-01-01 (a) 4 B 4 B
2000-02-01 (a) 4 B 8 B
2000-03-01 (a) 4 B 12 B
# 18. single-period register report valued at default date (same as --value=now)
# 20. single-period register report valued at default date (same as --value=now)
$ hledger -f- reg -V
2000-01-01 (a) 4 B 4 B
2000-02-01 (a) 4 B 8 B
@ -233,19 +252,19 @@ $ hledger -f- reg -V
# register with -H (starting balance)
# 19. register with starting balance, valued at cost.
# 21. register with starting balance, valued at cost.
$ hledger -f- reg --value=cost -b 200002 -H
2000-02-01 (a) 7 B 13 B
2000-03-01 (a) 8 B 21 B
# 20. register with starting balance, valued at period end.
# 22. register with starting balance, valued at period end.
# That is unspecified so the last posting date is used, ie 2000/3/1, so the price is 3 B.
# Starting balance is 5 B as above.
$ hledger -f- reg --value=end -b 200002 -H
2000-02-01 (a) 3 B 8 B
2000-03-01 (a) 3 B 11 B
# 21. register with starting balance, valued at specified date (when the price is 5 B).
# 23. register with starting balance, valued at specified date (when the price is 5 B).
# Starting balance is 5 B as above.
$ hledger -f- reg --value=2000-01-15 -b 200002 -H
2000-02-01 (a) 5 B 10 B
@ -253,7 +272,7 @@ $ hledger -f- reg --value=2000-01-15 -b 200002 -H
# register, periodic
# 22. periodic register report valued at cost.
# 24. periodic register report valued at cost.
# The total for january is 6 B (1 A valued at 1/1, price 1 B, and 1 A
# valued at 1/20, price 5 B).
# Need an extra transaction for this test:
@ -298,25 +317,25 @@ P 2000/04/01 A 4 B
2000/03/01
(a) 1 A @ 8 B
# 23. periodic register report valued at period end
# 25. periodic register report valued at period end
$ hledger -f- reg --value=end -M
2000/01 a 5 B 5 B
2000/02 a 2 B 7 B
2000/03 a 3 B 10 B
# 24. periodic register report valued at specified date
# 26. periodic register report valued at specified date
$ hledger -f- reg --value=2000-01-15 -M
2000/01 a 5 B 5 B
2000/02 a 5 B 10 B
2000/03 a 5 B 15 B
# 25. periodic register report valued today
# 27. periodic register report valued today
$ hledger -f- reg --value=now -M
2000/01 a 4 B 4 B
2000/02 a 4 B 8 B
2000/03 a 4 B 12 B
# 26. periodic register report valued at default date (same as --value=end)
# 28. periodic register report valued at default date (same as --value=end)
$ hledger -f- reg -V -M
2000/01 a 5 B 5 B
2000/02 a 2 B 7 B
@ -324,29 +343,29 @@ $ hledger -f- reg -V -M
# balance
# 27. single column balance report valued at cost
# 29. single column balance report valued at cost
$ hledger -f- bal -N --value=cost
21 B a
# 28. single column balance report valued at period end
# 30. single column balance report valued at period end
$ hledger -f- bal -N --value=end
9 B a
# 29. single column balance report valued at specified date
# 31. single column balance report valued at specified date
$ hledger -f- bal -N --value=2000-01-15
15 B a
# 30. single column balance report valued today
# 32. single column balance report valued today
$ hledger -f- bal -N --value=now
12 B a
# 31. single column balance report valued at default date (same as --value=now)
# 33. single column balance report valued at default date (same as --value=now)
$ hledger -f- bal -N -V
12 B a
# balance, periodic
# 32. multicolumn balance report valued at cost
# 34. multicolumn balance report valued at cost
$ hledger -f- bal -MTA --value=cost
Balance changes in 2000q1, valued at cost:
@ -356,7 +375,7 @@ Balance changes in 2000q1, valued at cost:
---++---------------------------------
|| 6 B 7 B 8 B 21 B 7 B
# 33. multicolumn balance report valued at period end
# 35. multicolumn balance report valued at period end
$ hledger -f- bal -M --value=end
Balance changes in 2000q1, valued at period ends:
@ -366,7 +385,7 @@ Balance changes in 2000q1, valued at period ends:
---++---------------
|| 5 B 2 B 3 B
# 34. multicolumn balance report valued at period end with -T or -A
# 36. multicolumn balance report valued at period end with -T or -A
$ hledger -f- bal -MTA --value=end
Balance changes in 2000q1, valued at period ends:
@ -376,7 +395,7 @@ Balance changes in 2000q1, valued at period ends:
---++---------------------------------
|| 5 B 2 B 3 B 10 B 3 B
# 35. multicolumn balance report valued at other date
# 37. multicolumn balance report valued at other date
$ hledger -f- bal -MTA --value=2000-01-15
Balance changes in 2000q1, valued at 2000-01-15:
@ -386,7 +405,7 @@ Balance changes in 2000q1, valued at 2000-01-15:
---++---------------------------------
|| 5 B 5 B 5 B 15 B 5 B
# 36. multicolumn balance report valued today (with today >= 2000-04-01)
# 38. multicolumn balance report valued today (with today >= 2000-04-01)
$ hledger -f- bal -M --value=now
Balance changes in 2000q1, current value:
@ -396,7 +415,7 @@ Balance changes in 2000q1, current value:
---++---------------
|| 4 B 4 B 4 B
# 37. multicolumn balance report valued at default date (same as --value=end)
# 39. multicolumn balance report valued at default date (same as --value=end)
$ hledger -f- bal -M -V
Balance changes in 2000q1, valued at period ends:
@ -408,7 +427,7 @@ Balance changes in 2000q1, valued at period ends:
# balance, periodic, with -H (starting balance and accumulating across periods)
# 38. multicolumn balance report with -H, valued at cost.
# 40. multicolumn balance report with -H, valued at cost.
# The starting balance on 2000/01/01 is 6 B (cost of the first 2 A).
# February adds 1 A costing 7 B, making 13 B.
# March adds 1 A costing 8 B, making 21 B.
@ -421,7 +440,7 @@ Ending balances (historical) in 2000-02-01-2000-03-31, valued at cost:
---++------------------------
|| 13 B 21 B
# 39. multicolumn balance report with -H valued at period end.
# 41. multicolumn balance report with -H valued at period end.
# The starting balance is 1 A.
# February adds 1 A making 2 A, which is valued at 2000/02/29 as 4 B.
# March adds 1 A making 3 A, which is valued at 2000/03/31 as 9 B.
@ -434,7 +453,7 @@ Ending balances (historical) in 2000-02-01-2000-03-31, valued at period ends:
---++---------------------------------
|| 4 B 9 B 6 B
# 40. multicolumn balance report with -H valued at other date.
# 42. multicolumn balance report with -H valued at other date.
# The starting balance is 5 B (1 A valued at 2000/1/15).
$ hledger -f- bal -M -H -b 200002 --value=2000-01-15
Ending balances (historical) in 2000-02-01-2000-03-31, valued at 2000-01-15:
@ -445,7 +464,7 @@ Ending balances (historical) in 2000-02-01-2000-03-31, valued at 2000-01-15:
---++------------------------
|| 10 B 15 B
# 41. multicolumn balance report with -H, valuing each period's carried-over balances at cost.
# 43. multicolumn balance report with -H, valuing each period's carried-over balances at cost.
<
P 2000/01/01 A 1 B
P 2000/01/15 A 5 B
@ -465,7 +484,7 @@ Ending balances (historical) in 2000q1, valued at cost:
---++------------------------------------
|| 6 B 6 B 6 B
# 42. multicolumn balance report with -H, valuing each period's carried-over balances at period end.
# 44. multicolumn balance report with -H, valuing each period's carried-over balances at period end.
# Unrelated, also -H always disables -T.
$ hledger -f- bal -META -H -p200001-200004 --value=e
Ending balances (historical) in 2000q1, valued at period ends:
@ -476,7 +495,7 @@ Ending balances (historical) in 2000q1, valued at period ends:
---++---------------------------------------------
|| 5 B 2 B 3 B 3 B
# 43. multicolumn balance report with -H, valuing each period's carried-over balances at other date.
# 45. multicolumn balance report with -H, valuing each period's carried-over balances at other date.
$ hledger -f- bal -ME -H -p200001-200004 --value=2000-01-15
Ending balances (historical) in 2000q1, valued at 2000-01-15:
@ -508,7 +527,7 @@ P 2000/04/01 A 4 B
2000/03/01
(a) 1 A @ 8 B
# 44. budget report, unvalued (for reference).
# 46. budget report, unvalued (for reference).
$ hledger -f- bal -M --budget
Budget performance in 2000q1:
@ -518,7 +537,7 @@ Budget performance in 2000q1:
---++------------------------------------------------------------
|| 1 A [ 50% of 2 A] 1 A [ 50% of 2 A] 1 A [ 50% of 2 A]
# 45. budget report, valued at cost.
# 47. budget report, valued at cost.
$ hledger -f- bal -MTA --budget --value=c
Budget performance in 2000q1, valued at cost:
@ -528,7 +547,7 @@ Budget performance in 2000q1, valued at cost:
---++-----------------------------------------------------------------------------------------------------
|| 6 B [ 300% of 2 B] 7 B [ 350% of 2 B] 8 B [ 400% of 2 B] 21 B [ 350% of 6 B] 7 B [ 350% of 2 B]
# 46. budget report, valued at period ends.
# 48. budget report, valued at period ends.
$ hledger -f- bal -MTA --budget --value=e
Budget performance in 2000q1, valued at period ends:
@ -538,7 +557,7 @@ Budget performance in 2000q1, valued at period ends:
---++----------------------------------------------------------------------------------------------------------
|| 5 B [ 50% of 10 B] 2 B [ 50% of 4 B] 3 B [ 50% of 6 B] 10 B [ 50% of 20 B] 3 B [ 50% of 7 B]
# 47. budget report, valued at other date.
# 49. budget report, valued at other date.
$ hledger -f- bal -MTA --budget --value=2000-01-15
Budget performance in 2000q1, valued at 2000-01-15: