From b8d75b7728949bdbf4b6edd9ce55589ed69f1765 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 2 Sep 2015 16:09:49 -0700 Subject: [PATCH] balance, etc: fix amount style loss (fixes #230, #276) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hledger-lib-0.24's "track the commodity of zero amounts when possible (useful eg for hledger-web's multi-commodity charts)" preserved the commodity when normalising a zero mixed amount, but not the amount style. This showed up as occasionally incorrect amount style (commodity symbol placement, decimal point character, etc.) in balance reports with certain journals, like this: $ hledger bal €3000.00 a <------ not using the canonical € style 4000,58€ 1 -1000,58€ D -3000,00€ e -------------------- 0 I thought this would require a big rewrite of amount arithmetic, but it seems that just being a little more careful is enough. When normalising a mixed amount containing multiple zeros in the same commodity, we now preserve the last zero with its amount style, instead of replacing them all with a new one. --- hledger-lib/Hledger/Data/Amount.hs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index b66883641..9398867e0 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -377,9 +377,11 @@ mixed = normaliseMixedAmount . Mixed -- -- * amounts in the same commodity are combined unless they have different prices or total prices -- --- * multiple zero amounts are replaced by just one. If they had the same commodity, it is preserved. +-- * multiple zero amounts, all with the same non-null commodity, are replaced by just the last of them, preserving the commodity and amount style (all but the last zero amount are discarded) -- --- * an empty amount list is replaced with a single commodityless zero +-- * multiple zero amounts with multiple commodities, or no commodities, are replaced by one commodity-less zero amount +-- +-- * an empty amount list is replaced by one commodity-less zero amount -- -- * the special "missing" mixed amount remains unchanged -- @@ -393,7 +395,7 @@ normaliseHelper squashprices (Mixed as) | otherwise = Mixed nonzeros where newzero = case filter (/= "") (map acommodity zeros) of - [c] -> nullamt{acommodity=c} + _:_ -> last zeros _ -> nullamt (zeros, nonzeros) = partition isReallyZeroAmount $ map sumSimilarAmountsUsingFirstPrice $