diff --git a/hledger-lib/Hledger/Data/TransactionModifier.hs b/hledger-lib/Hledger/Data/TransactionModifier.hs index 1b071bec6..6eb956338 100644 --- a/hledger-lib/Hledger/Data/TransactionModifier.hs +++ b/hledger-lib/Hledger/Data/TransactionModifier.hs @@ -92,12 +92,14 @@ tmPostingRuleToFunction pr = Nothing -> const $ pamount pr Just n -> \p -> -- Multiply the old posting's amount by the posting rule's multiplier. - -- Its display precision will be increased if needed to show all digits. let pramount = dbg6 "pramount" $ head $ amounts $ pamount pr matchedamount = dbg6 "matchedamount" $ pamount p - unitpricedmatchedamount = dbg6 "unitpricedmatchedamount" $ mixedAmountTotalPriceToUnitPrice matchedamount - Mixed as = dbg6 "scaledmatchedamount" $ n `multiplyMixedAmount` unitpricedmatchedamount + -- Handle a matched amount with a total price carefully so as to keep the transaction balanced (#928). + -- Approach 1: convert to a unit price and increase the display precision slightly + -- Mixed as = dbg6 "multipliedamount" $ n `multiplyMixedAmount` mixedAmountTotalPriceToUnitPrice matchedamount + -- Approach 2: multiply the total price (keeping it positive) as well as the quantity + Mixed as = dbg6 "multipliedamount" $ n `multiplyMixedAmountAndPrice` matchedamount in case acommodity pramount of "" -> Mixed as diff --git a/tests/journal/modifiers-928.test b/tests/journal/modifiers-928.test index d58e0d7af..5d4dc11a1 100644 --- a/tests/journal/modifiers-928.test +++ b/tests/journal/modifiers-928.test @@ -35,14 +35,21 @@ $ hledger -f- print --auto $ hledger -f- print --auto 2018/01/01 Expenses:Joint:Widgets $100.00 @@ £50 - Expenses:Joint $-100.00 @ £0.5 - Liabilities:Joint:Bob $50.00 @ £0.5 - Liabilities:Joint:Bill $50.00 @ £0.5 + Expenses:Joint $-100.00 @@ £50 + Liabilities:Joint:Bob $50.00 @@ £25 + Liabilities:Joint:Bill $50.00 @@ £25 Assets:Joint:Bank £-50.00 >=0 +# alternate approach: convert to unit prices +#2018/01/01 +# Expenses:Joint:Widgets $100.00 @@ £50 +# Expenses:Joint $-100.00 @ £0.5 +# Liabilities:Joint:Bob $50.00 @ £0.5 +# Liabilities:Joint:Bill $50.00 @ £0.5 +# Assets:Joint:Bank £-50.00 -# Generating auto postings from an implicitly-priced amount. Same as above. +# Generating auto postings from an implicitly-priced amount. Should work like the above. < = ^Expenses:Joint Expenses:Joint *-1 @@ -56,9 +63,9 @@ $ hledger -f- print --auto $ hledger -f- print --auto 2018/01/01 Expenses:Joint:Widgets $100.00 - Expenses:Joint $-100.00 @ £0.5 - Liabilities:Joint:Bob $50.00 @ £0.5 - Liabilities:Joint:Bill $50.00 @ £0.5 + Expenses:Joint $-100.00 @@ £50 + Liabilities:Joint:Bob $50.00 @@ £25 + Liabilities:Joint:Bill $50.00 @@ £25 Assets:Joint:Bank £-50.00 >=0