From b00aa32d7b1078e14308d050eeef45049977255c Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 13 Nov 2018 17:50:30 -0800 Subject: [PATCH] journal: txn modifier multipliers multiply total-priced amounts (#928) A different approach: instead of converting to unit prices and fiddling with the display precision, just multiply the total prices by the same multiplier (and keep them positive). This seems a little more natural. I'm not sure if one of these will be more robust than the other. --- .../Hledger/Data/TransactionModifier.hs | 8 ++++--- tests/journal/modifiers-928.test | 21 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) 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