diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index 03a47f878..c04282848 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -63,6 +63,7 @@ module Hledger.Data.Posting ( -- * arithmetic sumPostings, + postingNegate, postingNegateMainAmount, -- * rendering showPosting, @@ -381,6 +382,13 @@ accountNamesFromPostings = S.toList . S.fromList . map paccount sumPostings :: [Posting] -> MixedAmount sumPostings = foldl' (\amt p -> maPlus amt $ pamount p) nullmixedamt +-- | Negate the posting's main amount and balance assertion amount if any. +postingNegate :: Posting -> Posting +postingNegate p@Posting{pamount=a, pbalanceassertion=mb} = + p{pamount=negate a, pbalanceassertion=fmap balanceAssertionNegate mb} + where + balanceAssertionNegate b@BalanceAssertion{baamount=ba} = b{baamount=negate ba} + -- | Negate the posting's main amount but not the balance assertion amount. postingNegateMainAmount :: Posting -> Posting postingNegateMainAmount p@Posting{pamount=a} = p{pamount=negate a}