zero amounts have no sign or commodity, and amounts with different prices are aggregated, like ledger

This commit is contained in:
Simon Michael 2011-04-22 13:50:05 +00:00
parent d9ee8b23a6
commit b159f74a4c
10 changed files with 42 additions and 40 deletions

View File

@ -238,8 +238,7 @@ isNegativeAmount Amount{quantity=q} = q < 0
amounts :: MixedAmount -> [Amount]
amounts (Mixed as) = as
-- | Does this mixed amount appear to be zero - empty, or
-- containing only simple amounts which appear to be zero ?
-- | Does this mixed amount appear to be zero when displayed with its given precision ?
isZeroMixedAmount :: MixedAmount -> Bool
isZeroMixedAmount = all isZeroAmount . amounts . normaliseMixedAmount
@ -313,21 +312,19 @@ showMixedAmountOrZeroWithoutPrice a
| isZeroMixedAmount a = "0"
| otherwise = showMixedAmountWithoutPrice a
-- | Simplify a mixed amount by combining any component amounts which have
-- the same commodity and the same price. Also removes zero amounts,
-- or adds a single zero amount if there are no amounts at all.
-- | Simplify a mixed amount by removing redundancy in its component amounts, as follows:
-- 1. sum amounts which have the same commodity (ignoring their price)
-- 2. remove zero amounts
-- 3. if there are no amounts at all, add a single zero amount
normaliseMixedAmount :: MixedAmount -> MixedAmount
normaliseMixedAmount (Mixed as) = Mixed as''
where
as'' = map sumSamePricedAmountsPreservingPrice $ group $ sort as'
sort = sortBy cmpsymbolandprice
cmpsymbolandprice a1 a2 = compare (sym a1,price a1) (sym a2,price a2)
group = groupBy samesymbolandprice
samesymbolandprice a1 a2 = (sym a1 == sym a2) && (price a1 == price a2)
as'' = if null nonzeros then [nullamt] else nonzeros
(_,nonzeros) = partition (\a -> isReallyZeroAmount a && Mixed [a] /= missingamt) as'
as' = map sumSamePricedAmountsPreservingPrice $ group $ sort as
sort = sortBy (\a1 a2 -> compare (sym a1) (sym a2))
group = groupBy (\a1 a2 -> sym a1 == sym a2)
sym = symbol . commodity
as' | null nonzeros = [head $ zeros ++ [nullamt]]
| otherwise = nonzeros
(zeros,nonzeros) = partition isReallyZeroAmount as
-- | Set a mixed amount's commodity to the canonicalised commodity from
-- the provided commodity map.
@ -427,8 +424,11 @@ missingamt = Mixed [Amount unknown{symbol="AUTO"} 0 Nothing]
tests_Hledger_Data_Amount = TestList [
"showMixedAmount" ~: do
showMixedAmount (Mixed [Amount dollar 0 Nothing]) `is` "$0.00"
"showAmount" ~: do
showAmount (dollars 0 + pounds 0) `is` "0"
,"showMixedAmount" ~: do
showMixedAmount (Mixed [Amount dollar 0 Nothing]) `is` "0"
showMixedAmount (Mixed []) `is` "0"
showMixedAmount missingamt `is` ""
@ -458,10 +458,15 @@ tests_Hledger_Data_Amount = TestList [
[Amount dollar 1.25 Nothing,
Amount dollar0 (-1) Nothing,
Amount dollar (-0.25) Nothing])
`is` Mixed [Amount dollar 0 Nothing]
`is` Mixed [Amount unknown 0 Nothing]
,"normaliseMixedAmount" ~: do
normaliseMixedAmount (Mixed []) ~?= Mixed [nullamt]
normaliseMixedAmount (Mixed []) `is` Mixed [nullamt]
assertBool "" $ isZeroMixedAmount $ normaliseMixedAmount (Mixed [Amount {commodity=dollar, quantity=10, price=Nothing}
,Amount {commodity=dollar, quantity=10, price=Just (TotalPrice (Mixed [Amount {commodity=euro, quantity=7, price=Nothing}]))}
,Amount {commodity=dollar, quantity=(-10), price=Nothing}
,Amount {commodity=dollar, quantity=(-10), price=Just (TotalPrice (Mixed [Amount {commodity=euro, quantity=7, price=Nothing}]))}
])
,"punctuatethousands 1" ~: punctuatethousands "" `is` ""

View File

@ -112,7 +112,7 @@ tests_Hledger_Cli = TestList
," $-1 salary"
," $1 liabilities:debts"
,"--------------------"
," $0"
," 0"
]
,"balance report can be limited with --depth" ~:
@ -122,7 +122,7 @@ tests_Hledger_Cli = TestList
," $-2 income"
," $1 liabilities"
,"--------------------"
," $0"
," 0"
]
,"balance report with account pattern o" ~:
@ -167,7 +167,7 @@ tests_Hledger_Cli = TestList
," $-1 salary"
," $1 liabilities:debts"
,"--------------------"
," $0"
," 0"
]
,"balance report with unmatched parent of two matched subaccounts" ~:
@ -216,7 +216,7 @@ tests_Hledger_Cli = TestList
([SubTotal,Empty], ["assets"]) `gives`
[" $-1 assets"
," $1 bank"
," $0 checking"
," 0 checking"
," $1 saving"
," $-2 cash"
,"--------------------"
@ -236,7 +236,7 @@ tests_Hledger_Cli = TestList
[" $500 a:b"
," $-500 c:d"
,"--------------------"
," $0"
," 0"
]
,"balance report elides zero-balance root account(s)" ~: do

View File

@ -43,7 +43,7 @@ bin/hledger -f - balance
EUR -1
USD -1 c
--------------------
EUR 0
0
>>>=0
# 4. mixed amounts with prices

View File

@ -11,12 +11,12 @@ bin/hledger -f- print
bin/hledger -f- print
<<<
2010-04-05 x
a 10 "DE0002635307"
a 10 "DE 0002 635307"
b
>>>
2010/04/05 x
a 10 "DE0002635307"
b -10 "DE0002635307"
a 10 "DE 0002 635307"
b -10 "DE 0002 635307"
>>>=0
@ -30,5 +30,5 @@ bin/hledger -f- balance
10 "DE0002635307" a
-10 "DE0002635307" b
--------------------
0 "DE0002635307"
0
>>>=0

View File

@ -16,15 +16,13 @@ bin/hledger -f - balance
# 2. Two commodities. As above, and the final total should be a single commodityless zero.
bin/hledger -f - balance
<<<
2010/04/01 tr1
2010/1/1
a 16$ @@ 10€
b -10€
2010/04/02 tr2
2010/1/2
a -16$
b 10€ @@ 16$
>>>
--------------------
0
>>>=0

View File

@ -49,7 +49,7 @@ bin/hledger -f - balance --no-total --cost --empty
a 1C @ $1.0049
a $-1.00
>>>
$0.00 a
0 a
>>>=0
# 5. avamk's 2011/1/19 example
@ -62,7 +62,7 @@ bin/hledger -f - -B bal
$3266.32 assets:investment:ACME
$-3266.32 equity:opening balances
--------------------
$0.00
0
>>>=0
# hledger 0.14pre: precision=2, presumably from price
# $3266.32 assets:investment:ACME
@ -91,7 +91,7 @@ D $1000.0
$3266.32 assets:investment:ACME
$-3266.32 equity:opening balances
--------------------
$0.00
0
>>>=0
### hledger 0.14pre: precision=2, presumably from price, ignores D
### $3266.32 assets:investment:ACME

View File

@ -132,7 +132,7 @@ bin/hledger -f - balance -B
$-135 assets
$135 expenses:foreign currency
--------------------
$0
0
>>>=0
# 10. transaction in two commodities should balance out properly
bin/hledger -f - balance --basis
@ -144,6 +144,5 @@ bin/hledger -f - balance --basis
16$ a
-16$ b
--------------------
0$
0
>>>=0

View File

@ -5,4 +5,4 @@ bin/hledger -f data/sample.journal balance --depth 1
$-2 income
$1 liabilities
--------------------
$0
0

View File

@ -11,4 +11,4 @@ bin/hledger -f data/sample.journal balance
$-1 salary
$1 liabilities:debts
--------------------
$0
0

View File

@ -7,4 +7,4 @@ bin/hledger -f - balance
10 руб τράπεζα
-10 руб नकद
--------------------
0 руб
0