lib: amountToCost, mixedAmountToCost

This commit is contained in:
Simon Michael 2019-05-23 12:15:54 -07:00
parent f999bf78e6
commit e5cb082fb5

View File

@ -57,6 +57,7 @@ module Hledger.Data.Amount (
amountWithCommodity, amountWithCommodity,
-- ** arithmetic -- ** arithmetic
costOfAmount, costOfAmount,
amountToCost,
divideAmount, divideAmount,
multiplyAmount, multiplyAmount,
divideAmountAndPrice, divideAmountAndPrice,
@ -93,6 +94,7 @@ module Hledger.Data.Amount (
normaliseMixedAmount, normaliseMixedAmount,
-- ** arithmetic -- ** arithmetic
costOfMixedAmount, costOfMixedAmount,
mixedAmountToCost,
divideMixedAmount, divideMixedAmount,
multiplyMixedAmount, multiplyMixedAmount,
divideMixedAmountAndPrice, divideMixedAmountAndPrice,
@ -216,6 +218,10 @@ costOfAmount a@Amount{aquantity=q, aprice=price} =
UnitPrice p@Amount{aquantity=pq} -> p{aquantity=pq * q} UnitPrice p@Amount{aquantity=pq} -> p{aquantity=pq * q}
TotalPrice p@Amount{aquantity=pq} -> p{aquantity=pq * signum q} TotalPrice p@Amount{aquantity=pq} -> p{aquantity=pq * signum q}
-- | Convert this amount to cost, and apply the appropriate amount style.
amountToCost :: M.Map CommoditySymbol AmountStyle -> Amount -> Amount
amountToCost styles = styleAmount styles . costOfAmount
-- | Replace an amount's TotalPrice, if it has one, with an equivalent UnitPrice. -- | Replace an amount's TotalPrice, if it has one, with an equivalent UnitPrice.
-- Has no effect on amounts without one. -- Has no effect on amounts without one.
-- Also increases the unit price's display precision to show one extra decimal place, -- Also increases the unit price's display precision to show one extra decimal place,
@ -581,6 +587,10 @@ mapMixedAmount f (Mixed as) = Mixed $ map f as
costOfMixedAmount :: MixedAmount -> MixedAmount costOfMixedAmount :: MixedAmount -> MixedAmount
costOfMixedAmount (Mixed as) = Mixed $ map costOfAmount as costOfMixedAmount (Mixed as) = Mixed $ map costOfAmount as
-- | Convert all component amounts to cost, and apply the appropriate amount styles.
mixedAmountToCost :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
mixedAmountToCost styles (Mixed as) = Mixed $ map (amountToCost styles) as
-- | Divide a mixed amount's quantities by a constant. -- | Divide a mixed amount's quantities by a constant.
divideMixedAmount :: Quantity -> MixedAmount -> MixedAmount divideMixedAmount :: Quantity -> MixedAmount -> MixedAmount
divideMixedAmount n = mapMixedAmount (divideAmount n) divideMixedAmount n = mapMixedAmount (divideAmount n)