balance, etc: fix amount style loss (fixes #230, #276)

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.
This commit is contained in:
Simon Michael 2015-09-02 16:09:49 -07:00
parent 921a874e72
commit b8d75b7728

View File

@ -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 $