Skip to content

Commit

Permalink
dev: no need to style amounts when converting to cost
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Aug 31, 2023
1 parent 9f7ad57 commit 104ff7a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
7 changes: 2 additions & 5 deletions hledger-lib/Hledger/Data/Journal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -909,12 +909,9 @@ journalInferMarketPricesFromTransactions j =
journalPostings j
}

-- | Convert all this journal's amounts to cost using the transaction prices, if any.
-- The journal's commodity styles are applied to the resulting amounts.
-- | Convert all this journal's amounts to cost using their attached prices, if any.
journalToCost :: ConversionOp -> Journal -> Journal
journalToCost cost j@Journal{jtxns=ts} = j{jtxns=map (transactionToCost styles cost) ts}
where
styles = journalCommodityStyles j
journalToCost cost j@Journal{jtxns=ts} = j{jtxns=map (transactionToCost cost) ts}

-- | Add equity postings inferred from costs, where needed and possible.
-- See hledger manual > Cost reporting.
Expand Down
11 changes: 5 additions & 6 deletions hledger-lib/Hledger/Data/Posting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,13 @@ postingApplyValuation :: PriceOracle -> M.Map CommoditySymbol AmountStyle -> Day
postingApplyValuation priceoracle styles periodlast today v p =
postingTransformAmount (mixedAmountApplyValuation priceoracle styles periodlast today (postingDate p) v) p

-- | Maybe convert this 'Posting's amount to cost, and apply apply appropriate
-- amount styles.
postingToCost :: M.Map CommoditySymbol AmountStyle -> ConversionOp -> Posting -> Maybe Posting
postingToCost _ NoConversionOp p = Just p
postingToCost styles ToCost p
-- | Maybe convert this 'Posting's amount to cost.
postingToCost :: ConversionOp -> Posting -> Maybe Posting
postingToCost NoConversionOp p = Just p
postingToCost ToCost p
-- If this is a conversion posting with a matched transaction price posting, ignore it
| "_conversion-matched" `elem` map fst (ptags p) && noCost = Nothing
| otherwise = Just $ postingTransformAmount (mixedAmountSetStyles styles . mixedAmountCost) p
| otherwise = Just $ postingTransformAmount mixedAmountCost p
where
noCost = (not . any (isJust . aprice) . amountsRaw) $ pamount p

Expand Down
7 changes: 3 additions & 4 deletions hledger-lib/Hledger/Data/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,9 @@ transactionApplyValuation :: PriceOracle -> M.Map CommoditySymbol AmountStyle ->
transactionApplyValuation priceoracle styles periodlast today v =
transactionTransformPostings (postingApplyValuation priceoracle styles periodlast today v)

-- | Maybe convert this 'Transaction's amounts to cost and apply the
-- appropriate amount styles.
transactionToCost :: M.Map CommoditySymbol AmountStyle -> ConversionOp -> Transaction -> Transaction
transactionToCost styles cost t = t{tpostings = mapMaybe (postingToCost styles cost) $ tpostings t}
-- | Maybe convert this 'Transaction's amounts to cost.
transactionToCost :: ConversionOp -> Transaction -> Transaction
transactionToCost cost t = t{tpostings = mapMaybe (postingToCost cost) $ tpostings t}

-- | Add inferred equity postings to a 'Transaction' using transaction prices.
transactionAddInferredEquityPostings :: Bool -> AccountName -> Transaction -> Transaction
Expand Down

0 comments on commit 104ff7a

Please sign in to comment.