Fix for auto-balancing transactions which have negative amount in the first posting

This commit is contained in:
Dmitry Astapov 2011-04-24 22:10:33 +00:00
parent 2daa56dcb3
commit 2c01541de1
2 changed files with 7 additions and 5 deletions

View File

@ -189,9 +189,10 @@ balanceTransaction canonicalcommoditymap t@Transaction{tpostings=ps}
where where
conversionprice c | c == unpricedcommodity conversionprice c | c == unpricedcommodity
-- assign a balancing price. Use @@ for more exact output when possible. -- assign a balancing price. Use @@ for more exact output when possible.
-- invariant: prices should always be positive. Enforced with "abs"
= if length ramountsinunpricedcommodity == 1 = if length ramountsinunpricedcommodity == 1
then Just $ TotalPrice $ Mixed [setAmountPrecision maxprecision $ negate $ targetcommodityamount] then Just $ TotalPrice $ Mixed [setAmountPrecision maxprecision $ abs $ targetcommodityamount]
else Just $ UnitPrice $ Mixed [setAmountPrecision maxprecision $ negate $ targetcommodityamount `divideAmount` (quantity unpricedamount)] else Just $ UnitPrice $ Mixed [setAmountPrecision maxprecision $ abs $ targetcommodityamount `divideAmount` (quantity unpricedamount)]
| otherwise = Nothing | otherwise = Nothing
where where
unpricedcommodity = head $ filter (`elem` (map commodity rsumamounts)) rcommoditiesinorder unpricedcommodity = head $ filter (`elem` (map commodity rsumamounts)) rcommoditiesinorder
@ -214,8 +215,8 @@ balanceTransaction canonicalcommoditymap t@Transaction{tpostings=ps}
where where
conversionprice c | c == unpricedcommodity conversionprice c | c == unpricedcommodity
= if length bvamountsinunpricedcommodity == 1 = if length bvamountsinunpricedcommodity == 1
then Just $ TotalPrice $ Mixed [setAmountPrecision maxprecision $ negate $ targetcommodityamount] then Just $ TotalPrice $ Mixed [setAmountPrecision maxprecision $ abs $ targetcommodityamount]
else Just $ UnitPrice $ Mixed [setAmountPrecision maxprecision $ negate $ targetcommodityamount `divideAmount` (quantity unpricedamount)] else Just $ UnitPrice $ Mixed [setAmountPrecision maxprecision $ abs $ targetcommodityamount `divideAmount` (quantity unpricedamount)]
| otherwise = Nothing | otherwise = Nothing
where where
unpricedcommodity = head $ filter (`elem` (map commodity bvsumamounts)) bvcommoditiesinorder unpricedcommodity = head $ filter (`elem` (map commodity bvsumamounts)) bvcommoditiesinorder

View File

@ -71,7 +71,8 @@ data Commodity = Commodity {
-- | An amount's price may be written as \@ unit price or \@\@ total price. -- | An amount's price may be written as \@ unit price or \@\@ total price.
-- Note although Price has a MixedAmount, it should hold only -- Note although Price has a MixedAmount, it should hold only
-- single-commodity amounts, cf costOfAmount. -- single-commodity amounts, cf costOfAmount. Moreover, price should always
-- be positive, though it is currently not enforced.
data Price = UnitPrice MixedAmount | TotalPrice MixedAmount data Price = UnitPrice MixedAmount | TotalPrice MixedAmount
deriving (Eq,Ord) deriving (Eq,Ord)