From 0d531bb24f1d27c2f93bad0c56c6233b1539416b Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 22 Oct 2018 07:23:40 -0700 Subject: [PATCH] lib: showTransaction: fix a case showing multiple missing amounts Noticed by peti: showTransaction could sometimes hide the last posting's amount even if one of the other posting amounts was already implcit, producing invalid transaction output. --- hledger-lib/Hledger/Data/Transaction.hs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index d03e52e73..1babebab0 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -117,7 +117,8 @@ To facilitate this, postings with explicit multi-commodity amounts are displayed as multiple similar postings, one per commodity. (Normally does not happen with this function). -If there are multiple postings and the transaction appears obviously balanced +If there are multiple postings, all with explicit amounts, +and the transaction appears obviously balanced (postings sum to 0, without needing to infer conversion prices), the last posting's amount will not be shown. -} @@ -171,7 +172,8 @@ renderCommentLines t = case lines $ T.unpack t of ("":ls) -> "":map commentpref -- for `print` output. -- -- Explicit amounts are shown, implicit amounts are not. --- If elide is true and there are multiple postings and the transaction appears obviously balanced +-- If elide is true and there are multiple postings, all with explicit amounts, +-- and the transaction appears obviously balanced -- (postings sum to 0, without needing to infer conversion prices), -- the last posting's amount will be made implicit (and not shown). -- @@ -188,7 +190,7 @@ renderCommentLines t = case lines $ T.unpack t of ("":ls) -> "":map commentpref -- postingsAsLines :: Bool -> Bool -> Transaction -> [Posting] -> [String] postingsAsLines elide onelineamounts t ps - | elide && length ps > 1 && isTransactionBalanced Nothing t -- imprecise balanced check + | elide && length ps > 1 && all hasAmount ps && isTransactionBalanced Nothing t -- imprecise balanced check = (concatMap (postingAsLines False onelineamounts ps) $ init ps) ++ postingAsLines True onelineamounts ps (last ps) | otherwise = concatMap (postingAsLines False onelineamounts ps) ps @@ -571,6 +573,12 @@ tests_Transaction = tests "Transaction" [ "a" `post` usd 1, "b" `post` hrs (-1) ]} + -- one missing amount, not the last one + t3 = nulltransaction{tpostings=[ + "a" `post` usd 1 + ,"b" `post` missingamt + ,"c" `post` usd (-1) + ]} in tests "postingsAsLines" [ @@ -619,6 +627,12 @@ tests_Transaction = tests "Transaction" [ ," b -1.00h" -- explicit amount remains explicit since a conversion price would have be inferred to balance ] + ,test "implicit-amount-not-last" $ + let t = t3 in postingsAsLines True False t (tpostings t) `is` [ + " a $1.00" + ," b" + ," c $-1.00" + ] ] ,do