;valuation: refactor
This commit is contained in:
		
							parent
							
								
									a384041f7c
								
							
						
					
					
						commit
						6e36ede9aa
					
				| @ -179,18 +179,18 @@ instance Semigroup Journal where | |||||||
|     ,jparseparentaccounts       = jparseparentaccounts       j2 |     ,jparseparentaccounts       = jparseparentaccounts       j2 | ||||||
|     ,jparsealiases              = jparsealiases              j2 |     ,jparsealiases              = jparsealiases              j2 | ||||||
|     -- ,jparsetransactioncount     = jparsetransactioncount     j1 +  jparsetransactioncount     j2 |     -- ,jparsetransactioncount     = jparsetransactioncount     j1 +  jparsetransactioncount     j2 | ||||||
|     ,jparsetimeclockentries     = jparsetimeclockentries j1 <> jparsetimeclockentries j2 |     ,jparsetimeclockentries     = jparsetimeclockentries     j1 <> jparsetimeclockentries     j2 | ||||||
|     ,jincludefilestack          = jincludefilestack          j2 |     ,jincludefilestack          = jincludefilestack j2 | ||||||
|     ,jdeclaredaccounts          = jdeclaredaccounts          j1 <> jdeclaredaccounts          j2 |     ,jdeclaredaccounts          = jdeclaredaccounts          j1 <> jdeclaredaccounts          j2 | ||||||
|     ,jdeclaredaccounttypes      = jdeclaredaccounttypes      j1 <> jdeclaredaccounttypes      j2 |     ,jdeclaredaccounttypes      = jdeclaredaccounttypes      j1 <> jdeclaredaccounttypes      j2 | ||||||
|     ,jcommodities               = jcommodities               j1 <> jcommodities               j2 |     ,jcommodities               = jcommodities               j1 <> jcommodities               j2 | ||||||
|     ,jinferredcommodities       = jinferredcommodities       j1 <> jinferredcommodities       j2 |     ,jinferredcommodities       = jinferredcommodities       j1 <> jinferredcommodities       j2 | ||||||
|     ,jpricedirectives              = jpricedirectives              j1 <> jpricedirectives              j2 |     ,jpricedirectives           = jpricedirectives           j1 <> jpricedirectives           j2 | ||||||
|     ,jtransactionimpliedmarketprices = jtransactionimpliedmarketprices j1 <> jtransactionimpliedmarketprices j2 |     ,jimpliedmarketprices       = jimpliedmarketprices       j1 <> jimpliedmarketprices       j2 | ||||||
|     ,jtxnmodifiers              = jtxnmodifiers              j1 <> jtxnmodifiers              j2 |     ,jtxnmodifiers              = jtxnmodifiers              j1 <> jtxnmodifiers              j2 | ||||||
|     ,jperiodictxns              = jperiodictxns              j1 <> jperiodictxns              j2 |     ,jperiodictxns              = jperiodictxns              j1 <> jperiodictxns              j2 | ||||||
|     ,jtxns                      = jtxns                      j1 <> jtxns                      j2 |     ,jtxns                      = jtxns                      j1 <> jtxns                      j2 | ||||||
|     ,jfinalcommentlines         = jfinalcommentlines         j2  -- XXX discards j1's ? |     ,jfinalcommentlines         = jfinalcommentlines j2  -- XXX discards j1's ? | ||||||
|     ,jfiles                     = jfiles                     j1 <> jfiles                     j2 |     ,jfiles                     = jfiles                     j1 <> jfiles                     j2 | ||||||
|     ,jlastreadtime              = max (jlastreadtime j1) (jlastreadtime j2) |     ,jlastreadtime              = max (jlastreadtime j1) (jlastreadtime j2) | ||||||
|     } |     } | ||||||
| @ -211,8 +211,8 @@ nulljournal = Journal { | |||||||
|   ,jdeclaredaccounttypes      = M.empty |   ,jdeclaredaccounttypes      = M.empty | ||||||
|   ,jcommodities               = M.empty |   ,jcommodities               = M.empty | ||||||
|   ,jinferredcommodities       = M.empty |   ,jinferredcommodities       = M.empty | ||||||
|   ,jpricedirectives              = [] |   ,jpricedirectives           = [] | ||||||
|   ,jtransactionimpliedmarketprices = [] |   ,jimpliedmarketprices       = [] | ||||||
|   ,jtxnmodifiers              = [] |   ,jtxnmodifiers              = [] | ||||||
|   ,jperiodictxns              = [] |   ,jperiodictxns              = [] | ||||||
|   ,jtxns                      = [] |   ,jtxns                      = [] | ||||||
| @ -1044,8 +1044,8 @@ canonicalStyleFrom ss@(s:_) = | |||||||
| -- been balanced and posting amounts have appropriate prices attached. | -- been balanced and posting amounts have appropriate prices attached. | ||||||
| journalInferMarketPricesFromTransactions :: Journal -> Journal | journalInferMarketPricesFromTransactions :: Journal -> Journal | ||||||
| journalInferMarketPricesFromTransactions j = | journalInferMarketPricesFromTransactions j = | ||||||
|   j{jtransactionimpliedmarketprices = |   j{jimpliedmarketprices = | ||||||
|        dbg4 "jtransactionimpliedmarketprices" $ |        dbg4 "jimpliedmarketprices" $ | ||||||
|        mapMaybe postingImpliedMarketPrice $ journalPostings j |        mapMaybe postingImpliedMarketPrice $ journalPostings j | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -472,7 +472,7 @@ data Journal = Journal { | |||||||
|   ,jcommodities           :: M.Map CommoditySymbol Commodity        -- ^ commodities and formats declared by commodity directives |   ,jcommodities           :: M.Map CommoditySymbol Commodity        -- ^ commodities and formats declared by commodity directives | ||||||
|   ,jinferredcommodities   :: M.Map CommoditySymbol AmountStyle      -- ^ commodities and formats inferred from journal amounts  TODO misnamed - jusedstyles |   ,jinferredcommodities   :: M.Map CommoditySymbol AmountStyle      -- ^ commodities and formats inferred from journal amounts  TODO misnamed - jusedstyles | ||||||
|   ,jpricedirectives       :: [PriceDirective]                       -- ^ Declarations of market prices by P directives, in parse order (after journal finalisation) |   ,jpricedirectives       :: [PriceDirective]                       -- ^ Declarations of market prices by P directives, in parse order (after journal finalisation) | ||||||
|   ,jtransactionimpliedmarketprices :: [MarketPrice]                 -- ^ Market prices implied by transactions, in parse order (after journal finalisation) |   ,jimpliedmarketprices   :: [MarketPrice]                          -- ^ Market prices implied by transactions, in parse order (after journal finalisation) | ||||||
|   ,jtxnmodifiers          :: [TransactionModifier] |   ,jtxnmodifiers          :: [TransactionModifier] | ||||||
|   ,jperiodictxns          :: [PeriodicTransaction] |   ,jperiodictxns          :: [PeriodicTransaction] | ||||||
|   ,jtxns                  :: [Transaction] |   ,jtxns                  :: [Transaction] | ||||||
|  | |||||||
| @ -195,12 +195,12 @@ amountValueAtDate priceoracle styles mto d a = | |||||||
| -- you should generate this only once per journal, reusing it across | -- you should generate this only once per journal, reusing it across | ||||||
| -- reports if there are more than one (as in compoundBalanceCommand). | -- reports if there are more than one (as in compoundBalanceCommand). | ||||||
| journalPriceOracle :: Journal -> PriceOracle | journalPriceOracle :: Journal -> PriceOracle | ||||||
| journalPriceOracle Journal{jpricedirectives, jtransactionimpliedmarketprices} = | journalPriceOracle Journal{jpricedirectives, jimpliedmarketprices} = | ||||||
|   -- traceStack "journalPriceOracle" $ |   -- traceStack "journalPriceOracle" $ | ||||||
|   let |   let | ||||||
|     pricesatdate = |     pricesatdate = | ||||||
|       memo $ |       memo $ | ||||||
|       pricesAtDate jpricedirectives jtransactionimpliedmarketprices |       pricesAtDate jpricedirectives jimpliedmarketprices | ||||||
|   in |   in | ||||||
|     memo $ |     memo $ | ||||||
|     uncurry3 $ |     uncurry3 $ | ||||||
| @ -310,13 +310,13 @@ tests_priceLookup = | |||||||
| -- graph of the effective exchange rates between commodity pairs on | -- graph of the effective exchange rates between commodity pairs on | ||||||
| -- the given day. | -- the given day. | ||||||
| pricesAtDate :: [PriceDirective] -> [MarketPrice] -> Day -> PriceGraph | pricesAtDate :: [PriceDirective] -> [MarketPrice] -> Day -> PriceGraph | ||||||
| pricesAtDate pricedirectives transactionimpliedmarketprices d = | pricesAtDate pricedirectives impliedmarketprices d = | ||||||
|   -- trace ("pricesAtDate ("++show d++")") $ |   -- trace ("pricesAtDate ("++show d++")") $ | ||||||
|   PriceGraph{prGraph=g, prNodemap=m, prDefaultValuationCommodities=defaultdests} |   PriceGraph{prGraph=g, prNodemap=m, prDefaultValuationCommodities=defaultdests} | ||||||
|   where |   where | ||||||
|     -- prices in effect on date d, either declared or implied |     -- prices in effect on date d, either declared or implied | ||||||
|     declaredandimpliedprices = dbg5 "declaredandimpliedprices" $ |     declaredandimpliedprices = dbg5 "declaredandimpliedprices" $ | ||||||
|       latestPriceForEachPairOn pricedirectives transactionimpliedmarketprices d |       latestPriceForEachPairOn pricedirectives impliedmarketprices d | ||||||
| 
 | 
 | ||||||
|     -- infer any additional reverse prices not already declared or implied |     -- infer any additional reverse prices not already declared or implied | ||||||
|     reverseprices = |     reverseprices = | ||||||
| @ -345,18 +345,18 @@ pricesAtDate pricedirectives transactionimpliedmarketprices d = | |||||||
| -- latest declared or transaction-implied price dated on or before | -- latest declared or transaction-implied price dated on or before | ||||||
| -- that day, with declared prices taking precedence. | -- that day, with declared prices taking precedence. | ||||||
| latestPriceForEachPairOn :: [PriceDirective] -> [MarketPrice] -> Day -> [MarketPrice] | latestPriceForEachPairOn :: [PriceDirective] -> [MarketPrice] -> Day -> [MarketPrice] | ||||||
| latestPriceForEachPairOn pricedirectives transactionimpliedmarketprices d = | latestPriceForEachPairOn pricedirectives impliedmarketprices d = | ||||||
|   dbg5 "latestPriceForEachPairOn" $ |   dbg5 "latestPriceForEachPairOn" $ | ||||||
|   let |   let | ||||||
|     -- consider only declarations/transactions before the valuation date |     -- consider only declarations/transactions before the valuation date | ||||||
|     declaredprices = map priceDirectiveToMarketPrice $ filter ((<=d).pddate) pricedirectives |     declaredprices = map priceDirectiveToMarketPrice $ filter ((<=d).pddate) pricedirectives | ||||||
|     transactionimpliedmarketprices' = filter ((<=d).mpdate) transactionimpliedmarketprices |     impliedmarketprices' = filter ((<=d).mpdate) impliedmarketprices | ||||||
|     -- label the items with their precedence and then their parse order |     -- label the items with their precedence and then their parse order | ||||||
|     declaredprices'                  = [(1, i, p) | (i,p) <- zip [1..] declaredprices] |     declaredprices'                  = [(1, i, p) | (i,p) <- zip [1..] declaredprices] | ||||||
|     transactionimpliedmarketprices'' = [(0, i, p) | (i,p) <- zip [1..] transactionimpliedmarketprices'] |     impliedmarketprices'' = [(0, i, p) | (i,p) <- zip [1..] impliedmarketprices'] | ||||||
|   in |   in | ||||||
|     -- combine |     -- combine | ||||||
|     declaredprices' ++ transactionimpliedmarketprices'' |     declaredprices' ++ impliedmarketprices'' | ||||||
|     -- sort by newest date then highest precedence then latest parse order |     -- sort by newest date then highest precedence then latest parse order | ||||||
|     & sortBy (flip compare `on` (\(precedence,parseorder,mp)->(mpdate mp,precedence,parseorder))) |     & sortBy (flip compare `on` (\(precedence,parseorder,mp)->(mpdate mp,precedence,parseorder))) | ||||||
|     -- discard the sorting labels |     -- discard the sorting labels | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user