From b159f74a4cdbfc0711f0dc92b2e364580cae320b Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 22 Apr 2011 13:50:05 +0000 Subject: [PATCH] zero amounts have no sign or commodity, and amounts with different prices are aggregated, like ledger --- hledger-lib/Hledger/Data/Amount.hs | 39 +++++++++++++++++------------- hledger/Hledger/Cli.hs | 10 ++++---- tests/amount-layout-vertical.test | 2 +- tests/commodities.test | 8 +++--- tests/eliding-balance.test | 6 ++--- tests/precision.test | 6 ++--- tests/prices.test | 5 ++-- tests/sample-balance-depth.test | 2 +- tests/sample-balance.test | 2 +- tests/unicode-balance.test | 2 +- 10 files changed, 42 insertions(+), 40 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 5e3918d56..95f7ada79 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -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` "" diff --git a/hledger/Hledger/Cli.hs b/hledger/Hledger/Cli.hs index 76d798503..21732b37b 100644 --- a/hledger/Hledger/Cli.hs +++ b/hledger/Hledger/Cli.hs @@ -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 diff --git a/tests/amount-layout-vertical.test b/tests/amount-layout-vertical.test index b3bf94bae..6202d2349 100644 --- a/tests/amount-layout-vertical.test +++ b/tests/amount-layout-vertical.test @@ -43,7 +43,7 @@ bin/hledger -f - balance EUR -1 USD -1 c -------------------- - EUR 0 + 0 >>>=0 # 4. mixed amounts with prices diff --git a/tests/commodities.test b/tests/commodities.test index 3c2dc7497..a9a41e40a 100644 --- a/tests/commodities.test +++ b/tests/commodities.test @@ -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 diff --git a/tests/eliding-balance.test b/tests/eliding-balance.test index 0d80f6c00..662db42f2 100644 --- a/tests/eliding-balance.test +++ b/tests/eliding-balance.test @@ -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 - diff --git a/tests/precision.test b/tests/precision.test index 8eff5d90e..dd0537f77 100644 --- a/tests/precision.test +++ b/tests/precision.test @@ -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 diff --git a/tests/prices.test b/tests/prices.test index c587efd5e..a8e4d125e 100644 --- a/tests/prices.test +++ b/tests/prices.test @@ -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 diff --git a/tests/sample-balance-depth.test b/tests/sample-balance-depth.test index ee24e9d2e..217c53690 100644 --- a/tests/sample-balance-depth.test +++ b/tests/sample-balance-depth.test @@ -5,4 +5,4 @@ bin/hledger -f data/sample.journal balance --depth 1 $-2 income $1 liabilities -------------------- - $0 + 0 diff --git a/tests/sample-balance.test b/tests/sample-balance.test index 0b1ba21ce..7208a56f4 100644 --- a/tests/sample-balance.test +++ b/tests/sample-balance.test @@ -11,4 +11,4 @@ bin/hledger -f data/sample.journal balance $-1 salary $1 liabilities:debts -------------------- - $0 + 0 diff --git a/tests/unicode-balance.test b/tests/unicode-balance.test index 24a0d5433..e11f41cbf 100644 --- a/tests/unicode-balance.test +++ b/tests/unicode-balance.test @@ -7,4 +7,4 @@ bin/hledger -f - balance 10 руб τράπεζα -10 руб नकद -------------------- - 0 руб + 0