lib: Groundwork allowing multi-commodity assertions

This commit is contained in:
Samuel May 2018-10-12 21:28:46 -07:00 committed by Simon Michael
parent cde91fc5f4
commit 6c31393dd3

View File

@ -717,9 +717,21 @@ checkInferAndRegisterAmounts (Right oldTx) = do
(fmap void . addToBalance) styles oldTx { tpostings = newPostings } (fmap void . addToBalance) styles oldTx { tpostings = newPostings }
where where
inferFromAssignment :: Posting -> CurrentBalancesModifier s Posting inferFromAssignment :: Posting -> CurrentBalancesModifier s Posting
inferFromAssignment p = maybe (return p) inferFromAssignment p = do
(fmap (\a -> p { pamount = a, porigin = Just $ originalPosting p }) . setBalance (paccount p) . baamount) let acc = paccount p
$ pbalanceassertion p case pbalanceassertion p of
Just ba -> do
old <- liftModifier $ \Env{ eBalances = bals } -> HT.lookup bals acc
let amt = baamount ba
assertedcomm = acommodity amt
diff <- setMixedBalance acc $
Mixed [amt] + filterMixedAmount (\a -> acommodity a /= assertedcomm) (fromMaybe nullmixedamt old)
fullPosting diff p
Nothing -> return p
fullPosting amt p = return p
{ pamount = amt
, porigin = Just $ originalPosting p
}
-- | Adds a posting's amount to the posting's account balance and -- | Adds a posting's amount to the posting's account balance and
-- checks a possible balance assertion. Or if there is no amount, -- checks a possible balance assertion. Or if there is no amount,
@ -735,15 +747,13 @@ addAmountAndCheckBalance _ p | hasAmount p = do
return p return p
addAmountAndCheckBalance fallback p = fallback p addAmountAndCheckBalance fallback p = fallback p
-- | Sets an account's balance to a given amount and returns the -- | Sets all commodities comprising an account's balance to the given
-- difference of new and old amount. -- amounts and returns the difference from the previous balance.
setBalance :: AccountName -> Amount -> CurrentBalancesModifier s MixedAmount setMixedBalance :: AccountName -> MixedAmount -> CurrentBalancesModifier s MixedAmount
setBalance acc amt = liftModifier $ \Env{ eBalances = bals } -> do setMixedBalance acc amt = liftModifier $ \Env{ eBalances = bals } -> do
old <- HT.lookup bals acc old <- HT.lookup bals acc
let new = Mixed $ (amt :) $ maybe [] HT.insert bals acc amt
(filter ((/= acommodity amt) . acommodity) . amounts) old return $ maybe amt (amt -) old
HT.insert bals acc new
return $ maybe new (new -) old
-- | Adds an amount to an account's balance and returns the resulting balance. -- | Adds an amount to an account's balance and returns the resulting balance.
addToBalance :: AccountName -> MixedAmount -> CurrentBalancesModifier s MixedAmount addToBalance :: AccountName -> MixedAmount -> CurrentBalancesModifier s MixedAmount