diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 5e71d6785..20f839c4d 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -30,6 +30,9 @@ module Hledger.Data.Transaction ( isTransactionBalanced, balanceTransaction, balanceTransactionHelper, + transactionTransformPostings, + transactionApplyValuation, + transactionToCost, -- nonzerobalanceerror, -- * date operations transactionDate2, @@ -67,6 +70,7 @@ import Hledger.Data.Types import Hledger.Data.Dates import Hledger.Data.Posting import Hledger.Data.Amount +import Hledger.Data.Valuation sourceFilePath :: GenericSourcePos -> FilePath sourceFilePath = \case @@ -553,6 +557,22 @@ txnUntieKnot t@Transaction{tpostings=ps} = t{tpostings=map (\p -> p{ptransaction postingSetTransaction :: Transaction -> Posting -> Posting postingSetTransaction t p = p{ptransaction=Just t} +-- | Apply a transform function to this transaction's amounts. +transactionTransformPostings :: (Posting -> Posting) -> Transaction -> Transaction +transactionTransformPostings f t@Transaction{tpostings=ps} = t{tpostings=map f ps} + +-- | Apply a specified valuation to this transaction's amounts, using +-- the provided price oracle, commodity styles, reference dates, and +-- whether this is for a multiperiod report or not. See +-- amountApplyValuation. +transactionApplyValuation :: PriceOracle -> M.Map CommoditySymbol AmountStyle -> Day -> Maybe Day -> Day -> Bool -> Transaction -> ValuationType -> Transaction +transactionApplyValuation priceoracle styles periodlast mreportlast today ismultiperiod t v = + transactionTransformPostings (\p -> postingApplyValuation priceoracle styles periodlast mreportlast today ismultiperiod p v) t + +-- | Convert this transaction's amounts to cost, and apply the appropriate amount styles. +transactionToCost :: M.Map CommoditySymbol AmountStyle -> Transaction -> Transaction +transactionToCost styles t@Transaction{tpostings=ps} = t{tpostings=map (postingToCost styles) ps} + -- tests tests_Transaction = diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index cd95ba171..02103a84d 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -38,6 +38,7 @@ module Hledger.Reports.ReportOptions ( reportPeriodLastDay, reportPeriodOrJournalLastDay, valuationTypeIsCost, + valuationTypeIsDefaultValue, tests_ReportOptions ) @@ -372,6 +373,12 @@ valuationTypeIsCost ropts = Just (AtCost _) -> True _ -> False +valuationTypeIsDefaultValue :: ReportOpts -> Bool +valuationTypeIsDefaultValue ropts = + case value_ ropts of + Just (AtDefault _) -> True + _ -> False + type DisplayExp = String maybedisplayopt :: Day -> RawOpts -> Maybe DisplayExp