Normalize amount in infereBalancingAmount (#469)

This fixes issue exposed by a fix for simonmichael/hledger#465
This commit is contained in:
Nikolay Orlyuk 2017-01-11 22:09:07 +02:00 committed by Simon Michael
parent ea7a7d78aa
commit c0d12f5d1c
2 changed files with 49 additions and 1 deletions

View File

@ -244,6 +244,38 @@ tests_postingAsLines = [
]
]
tests_inference = [
"inferBalancingAmount" ~: do
let p `gives` p' = assertEqual (show p) (Right p') $ inferTransaction p
inferTransaction :: Transaction -> Either String Transaction
inferTransaction = runIdentity . runExceptT . inferBalancingAmount (\_ _ -> return ())
nulltransaction `gives` nulltransaction
nulltransaction{
tpostings=[
"a" `post` usd (-5),
"b" `post` missingamt
]}
`gives`
nulltransaction{
tpostings=[
"a" `post` usd (-5),
"b" `post` usd 5
]}
nulltransaction{
tpostings=[
"a" `post` usd (-5),
"b" `post` (eur 3 @@ usd 4),
"c" `post` missingamt
]}
`gives`
nulltransaction{
tpostings=[
"a" `post` usd (-5),
"b" `post` (eur 3 @@ usd 4),
"c" `post` usd 1
]}
]
indent :: String -> String
indent = (" "++)
@ -371,7 +403,7 @@ inferBalancingAmount update t@Transaction{tpostings=ps}
| not (hasAmount p) = updateAmount p bvsum
inferamount p = return p
updateAmount p amt = update (paccount p) amt' >> return p { pamount=amt' }
where amt' = costOfMixedAmount (-amt)
where amt' = normaliseMixedAmount $ costOfMixedAmount (-amt)
-- | Infer prices for this transaction's posting amounts, if needed to make
-- the postings balance, and if possible. This is done once for the real
@ -471,6 +503,7 @@ postingSetTransaction t p = p{ptransaction=Just t}
tests_Hledger_Data_Transaction = TestList $ concat [
tests_postingAsLines,
tests_showTransactionUnelided,
tests_inference,
[
"showTransaction" ~: do
assertEqual "show a balanced transaction, eliding last amount"

View File

@ -32,3 +32,18 @@ hledger -f- balance
--------------------
0
>>>=0
# 4. autobalance with prices
hledger -f- print
<<<
2016/1/1
saving-card $-105
snacks 95 EUR @@ $100
Equity:Unbalanced
>>>
2016/01/01
saving-card $-105
snacks 95 EUR @@ $100
Equity:Unbalanced $5
>>>=0