;lib: transaction valuation helpers
This commit is contained in:
parent
de0a6b1e62
commit
905149df86
@ -30,6 +30,9 @@ module Hledger.Data.Transaction (
|
|||||||
isTransactionBalanced,
|
isTransactionBalanced,
|
||||||
balanceTransaction,
|
balanceTransaction,
|
||||||
balanceTransactionHelper,
|
balanceTransactionHelper,
|
||||||
|
transactionTransformPostings,
|
||||||
|
transactionApplyValuation,
|
||||||
|
transactionToCost,
|
||||||
-- nonzerobalanceerror,
|
-- nonzerobalanceerror,
|
||||||
-- * date operations
|
-- * date operations
|
||||||
transactionDate2,
|
transactionDate2,
|
||||||
@ -67,6 +70,7 @@ import Hledger.Data.Types
|
|||||||
import Hledger.Data.Dates
|
import Hledger.Data.Dates
|
||||||
import Hledger.Data.Posting
|
import Hledger.Data.Posting
|
||||||
import Hledger.Data.Amount
|
import Hledger.Data.Amount
|
||||||
|
import Hledger.Data.Valuation
|
||||||
|
|
||||||
sourceFilePath :: GenericSourcePos -> FilePath
|
sourceFilePath :: GenericSourcePos -> FilePath
|
||||||
sourceFilePath = \case
|
sourceFilePath = \case
|
||||||
@ -553,6 +557,22 @@ txnUntieKnot t@Transaction{tpostings=ps} = t{tpostings=map (\p -> p{ptransaction
|
|||||||
postingSetTransaction :: Transaction -> Posting -> Posting
|
postingSetTransaction :: Transaction -> Posting -> Posting
|
||||||
postingSetTransaction t p = p{ptransaction=Just t}
|
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
|
||||||
|
|
||||||
tests_Transaction =
|
tests_Transaction =
|
||||||
|
|||||||
@ -38,6 +38,7 @@ module Hledger.Reports.ReportOptions (
|
|||||||
reportPeriodLastDay,
|
reportPeriodLastDay,
|
||||||
reportPeriodOrJournalLastDay,
|
reportPeriodOrJournalLastDay,
|
||||||
valuationTypeIsCost,
|
valuationTypeIsCost,
|
||||||
|
valuationTypeIsDefaultValue,
|
||||||
|
|
||||||
tests_ReportOptions
|
tests_ReportOptions
|
||||||
)
|
)
|
||||||
@ -372,6 +373,12 @@ valuationTypeIsCost ropts =
|
|||||||
Just (AtCost _) -> True
|
Just (AtCost _) -> True
|
||||||
_ -> False
|
_ -> False
|
||||||
|
|
||||||
|
valuationTypeIsDefaultValue :: ReportOpts -> Bool
|
||||||
|
valuationTypeIsDefaultValue ropts =
|
||||||
|
case value_ ropts of
|
||||||
|
Just (AtDefault _) -> True
|
||||||
|
_ -> False
|
||||||
|
|
||||||
type DisplayExp = String
|
type DisplayExp = String
|
||||||
|
|
||||||
maybedisplayopt :: Day -> RawOpts -> Maybe DisplayExp
|
maybedisplayopt :: Day -> RawOpts -> Maybe DisplayExp
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user