fix: txn balancing: fix logic for the 1.50 message [#2402]

and add more tests.
This commit is contained in:
Simon Michael 2025-06-13 08:51:33 -10:00
parent 3713a1b77b
commit bd2d8728d1
2 changed files with 36 additions and 14 deletions

View File

@ -119,12 +119,12 @@ transactionCheckBalanced BalancingOpts{commodity_styles_=_mglobalstyles, txn_bal
lookszeroatglobaldisplayprecision = mixedAmountLooksZero . maybe id styleAmounts _mglobalstyles
-- check that the sum looks like zero
(rsumcost, bvsumcost) = (foldMap postingBalancingAmount rps, foldMap postingBalancingAmount bvps)
(rsumok, bvsumok) = (lookszero rsumcost, lookszero bvsumcost)
(rsumcost, bvsumcost) = (foldMap postingBalancingAmount rps, foldMap postingBalancingAmount bvps)
(rsumok, bvsumok) = (lookszero rsumcost, lookszero bvsumcost)
(rsumokold, bvsumokold) = (lookszeroatglobaldisplayprecision rsumcost, lookszeroatglobaldisplayprecision bvsumcost)
-- when there's multiple non-zeros, check they do not all have the same sign
(rsignsok, bvsignsok) = (signsOk rps, signsOk bvps)
(rsignsok, bvsignsok) = (signsOk rps, signsOk bvps)
where
signsOk ps = length nonzeros < 2 || length nonzerosigns > 1
where
@ -141,7 +141,7 @@ transactionCheckBalanced BalancingOpts{commodity_styles_=_mglobalstyles, txn_bal
(showMixedAmountWith oneLineNoCostFmt{displayCost=True, displayZeroCommodity=True} $
mixedAmountSetFullPrecisionUpTo Nothing $ mixedAmountSetFullPrecision
rsumcost)
++ if rsumokold then "" else oldbalancingmsg
++ if rsumokold then oldbalancingmsg else ""
bvmsg
| bvsumok = ""
| not bvsignsok = "The balanced virtual postings all have the same sign. Consider negating some of them."
@ -149,7 +149,7 @@ transactionCheckBalanced BalancingOpts{commodity_styles_=_mglobalstyles, txn_bal
(showMixedAmountWith oneLineNoCostFmt{displayCost=True, displayZeroCommodity=True} $
mixedAmountSetFullPrecisionUpTo Nothing $ mixedAmountSetFullPrecision
bvsumcost)
++ if bvsumokold then "" else oldbalancingmsg
++ if bvsumokold then oldbalancingmsg else ""
oldbalancingmsg = unlines [
-- -------------------------------------------------------------------------------
"\nNote, hledger <1.50 accepted this entry because of the global display precision,"

View File

@ -189,10 +189,10 @@ $ hledger -f - bal -O csv
"c","1000 JPY"
"Total:","1002 JPY"
# ** 13. Before hledger 1.44, an entry like this (generated by bean-report) did not balance,
# ** 13. Before hledger 1.50, an entry like this (generated by bean-report) did not balance,
# because the P amount influenced the commodity's display (and balancing) precision.
# From 1.44 transaction balancing uses the transaction's local precisions,
# so CNY's precision is 2 and this does balance.
# From 1.50 transaction balancing uses the transaction's local precisions,
# so CNY's precision is 2 and this does balance. (#2402)
<
P 2025-01-01 USD 7.147224669603524229074889868 CNY
@ -219,10 +219,11 @@ $ hledger -f - bal -N
-113.50 USD a
0.000000000000000000000000018 CNY z
# ** 16. Before hledger 1.44, an inexactly balanced entry like this could be accepted
# ** 16. Before hledger 1.50, an inexactly balanced entry like this could be accepted,
# because of a commodity directive reducing the display/balance-checking precision.
# From 1.44, transaction balancing uses the transaction's local precisions by default,
# making the balance checking more strict in this case.
# From 1.50, transaction balancing uses the transaction's local precisions by default.
# Also when balance checking fails for this reason, an extra note is shown.
# (#2402)
<
commodity $1.00
@ -231,13 +232,33 @@ commodity $1.00
a -$1.120
$ hledger -f - check
>2 /unbalanced/
>2 /Note, hledger <1.50 accepted this entry/
>= 1
# ** 17. --txn-balancing=old can be used to restore the pre-1.44 behaviour.
# ** 17. When balance checking would have failed with hledger <1.50 too, the extra note is not shown. (#2402)
<
2025-01-01
a 1 C @ $1.1206
a -$1.120
$ hledger -f - check
>2 !/Note, hledger <1.50 accepted this entry/
>= 1
# ** 18. When balance checking fails because cost inference is not allowed, the extra note is not shown. (#2402)
<
2025-01-01
a 1 A
b -1 USD
$ hledger -f - check balanced
>2 !/Note, hledger <1.50 accepted this entry/
>=1
# ** 19. --txn-balancing=old can be used to restore the pre-1.50 behaviour. (#2402)
$ hledger -f - check --txn-balancing=old
# ** 18. Note that a cost amount can affect display precision, by affecting an implicit posting amount.
# ** 20. A cost amount can affect display precision, by affecting an implicit posting amount.
# Here the z posting amount is inferred as -2.000 USD, so USD's display precision is inferred as 3.
<
2025-01-01
@ -248,3 +269,4 @@ $ hledger -f - check --txn-balancing=old
$ hledger -f - reg cur:USD
2025-01-01 a 1.000 USD 1.000 USD
z -2.000 USD -1.000 USD