lib: Hledger.Data.Journal: transactionAddPricesFromEquity -> transactionInferCostsFromEquity

This commit is contained in:
Simon Michael 2023-01-19 09:22:55 -10:00
parent dfea6eaee2
commit 402b4265f8
2 changed files with 12 additions and 12 deletions

View File

@ -880,10 +880,11 @@ journalAddInferredEquityPostings j = journalMapTransactions (transactionAddInfer
where where
equityAcct = journalConversionAccount j equityAcct = journalConversionAccount j
-- | Add inferred transaction prices from equity postings. -- | Add costs inferred from equity postings, where needed and possible.
-- See hledger manual > Inferring cost from equity postings.
journalInferCosts :: Journal -> Either String Journal journalInferCosts :: Journal -> Either String Journal
journalInferCosts j = do journalInferCosts j = do
ts <- mapM (transactionAddPricesFromEquity $ jaccounttypes j) $ jtxns j ts <- mapM (transactionInferCostsFromEquity $ jaccounttypes j) $ jtxns j
return j{jtxns=ts} return j{jtxns=ts}
-- -- | Get this journal's unique, display-preference-canonicalised commodities, by symbol. -- -- | Get this journal's unique, display-preference-canonicalised commodities, by symbol.

View File

@ -28,7 +28,7 @@ module Hledger.Data.Transaction
, transactionApplyValuation , transactionApplyValuation
, transactionToCost , transactionToCost
, transactionAddInferredEquityPostings , transactionAddInferredEquityPostings
, transactionAddPricesFromEquity , transactionInferCostsFromEquity
, transactionApplyAliases , transactionApplyAliases
, transactionMapPostings , transactionMapPostings
, transactionMapPostingAmounts , transactionMapPostingAmounts
@ -225,16 +225,15 @@ transactionAddInferredEquityPostings :: AccountName -> Transaction -> Transactio
transactionAddInferredEquityPostings equityAcct t = transactionAddInferredEquityPostings equityAcct t =
t{tpostings=concatMap (postingAddInferredEquityPostings equityAcct) $ tpostings t} t{tpostings=concatMap (postingAddInferredEquityPostings equityAcct) $ tpostings t}
-- | Add inferred transaction prices from equity postings. For every adjacent -- | Add costs inferred from equity postings in this transaction.
-- pair of conversion postings, it will first search the postings with -- For every adjacent pair of conversion postings, it will first search the postings
-- transaction prices to see if any match. If so, it will tag it as matched. -- with costs to see if any match. If so, it will tag these as matched.
-- If no postings with transaction prices match, it will then search the -- If no postings with costs match, it will then search the postings without costs,
-- postings without transaction prices, and will match the first such posting -- and will match the first such posting which matches one of the conversion amounts.
-- which matches one of the conversion amounts. If it finds a match, it will -- If it finds a match, it will add a cost and then tag it.
-- add a transaction price and then tag it.
type IdxPosting = (Int, Posting) type IdxPosting = (Int, Posting)
transactionAddPricesFromEquity :: M.Map AccountName AccountType -> Transaction -> Either String Transaction transactionInferCostsFromEquity :: M.Map AccountName AccountType -> Transaction -> Either String Transaction
transactionAddPricesFromEquity acctTypes t = first (annotateErrorWithTransaction t . T.unpack) $ do transactionInferCostsFromEquity acctTypes t = first (annotateErrorWithTransaction t . T.unpack) $ do
(conversionPairs, stateps) <- partitionPs npostings (conversionPairs, stateps) <- partitionPs npostings
f <- transformIndexedPostingsF addPricesToPostings conversionPairs stateps f <- transformIndexedPostingsF addPricesToPostings conversionPairs stateps
return t{tpostings = map (snd . f) npostings} return t{tpostings = map (snd . f) npostings}