bal: with same-date market prices, use the last parsed not the highest price #403 #453

This commit is contained in:
Simon Michael 2016-12-30 11:41:12 -08:00
parent a57c1bde08
commit 1866d2375d

View File

@ -26,7 +26,8 @@ module Hledger.Reports.BalanceReport (
) )
where where
import Data.List (sort) import Data.List
import Data.Ord
import Data.Maybe import Data.Maybe
import Data.Time.Calendar import Data.Time.Calendar
import Test.HUnit import Test.HUnit
@ -169,17 +170,23 @@ amountValue j d a =
} }
Nothing -> a Nothing -> a
-- | Find the market value, if known, of one unit of this commodity on -- | Find the market value, if known, of one unit of this commodity (A) on
-- the given date, in the commodity in which it has most recently been -- the given valuation date, in the commodity (B) mentioned in the latest
-- market-priced (ie the commodity mentioned in the most recent -- applicable market price. The latest applicable market price is the market
-- applicable market price directive before this date). -- price directive for commodity A with the latest date that is on or before
-- the valuation date; or if there are multiple such prices with the same date,
-- the last parsed.
commodityValue :: Journal -> Day -> CommoditySymbol -> Maybe Amount commodityValue :: Journal -> Day -> CommoditySymbol -> Maybe Amount
commodityValue j d c commodityValue j valuationdate c
| null applicableprices = dbg Nothing | null applicableprices = dbg Nothing
| otherwise = dbg $ Just $ mpamount $ last applicableprices | otherwise = dbg $ Just $ mpamount $ last applicableprices
where where
applicableprices = [p | p <- sort $ jmarketprices j, mpcommodity p == c, mpdate p <= d]
dbg = dbg8 ("using market price for "++T.unpack c) dbg = dbg8 ("using market price for "++T.unpack c)
applicableprices =
[p | p <- sortBy (comparing mpdate) $ jmarketprices j
, mpcommodity p == c
, mpdate p <= valuationdate
]