Skip to content

Commit

Permalink
fix: accept entries with unmatched equity conversion postings (#2045)
Browse files Browse the repository at this point in the history
Since 1.29, we unconditionally run part of the --infer-cost logic to
identify redundant costs/equity postings. This was too strict, raising
an error whenever it could not find postings matching the equity
postings.  Now we do this (and also the explicit --infer-costs
operation) as a best effort, leaving transactions unchanged if we
can't detect matching postings. This is consistent with
--infer-equity, --infer-market-prices, -B and -V.
  • Loading branch information
simonmichael committed Jun 7, 2023
1 parent 9d61e81 commit bdac0da
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
7 changes: 5 additions & 2 deletions hledger-lib/Hledger/Data/Transaction.hs
Expand Up @@ -320,8 +320,11 @@ transactionInferCostsFromEquity dryrun acctTypes t = first (annotateErrorWithTra
, Just newotherps <- deleteIdx np otherps
-> Right (transformPostingF np costp, (costps, if dryrun' then otherps else newotherps))

-- Otherwise it's too ambiguous to make a guess, so return an error.
| otherwise -> Left "There is not a unique posting which matches the conversion posting pair:"
-- Otherwise, do nothing, leaving the transaction unchanged.
-- We don't want to be over-zealous reporting problems here
-- since this is always called at least in dry run mode by
-- journalFinalise > journalMarkRedundantCosts. (#2045)
| otherwise -> Right (id, (costps, otherps))

-- If a posting with cost matches both the conversion amounts, return it along
-- with the matching amount which must be present in another non-conversion posting.
Expand Down
105 changes: 64 additions & 41 deletions hledger/test/journal/costs.test
Expand Up @@ -507,51 +507,74 @@ $ hledger -f- print --infer-costs

>=0

# 39. If a conversion pair matches several postings it should throw an error
<
2011/01/01
expenses:foreign currency €100 @@ $135
expenses:foreign trades €100 @@ $135
equity:conversion €-100
equity:conversion $135
assets €-100
assets $-135
# We now tend to just skip cost inference if it doesn't succeed. (#2045)

$ hledger -f- print --infer-costs
>2 /There is not a unique posting which matches the conversion posting pair/
>=1

# 42. If a conversion pair does not match it should throw an error
<
2011/01/01
expenses:foreign currency €120
equity:conversion €-100
equity:conversion $135
assets:extra $20
assets €-20
assets $-155

$ hledger -f- print --infer-costs
>2 /There is not a unique posting which matches the conversion posting pair/
>=1

# 43. Multiple conversion pairs which match a single posting should cause an
# error, and should not match both ‘sides’ of the conversion
# # 39. If a conversion pair matches several postings it should throw an error
# <
# 2011/01/01
# expenses:foreign currency €100 @@ $135
# expenses:foreign trades €100 @@ $135
# equity:conversion €-100
# equity:conversion $135
# assets €-100
# assets $-135

# $ hledger -f- print --infer-costs
# >2 /There is not a unique posting which matches the conversion posting pair/
# >=1

# # 40. If a conversion pair does not match it should throw an error
# <
# 2011/01/01
# expenses:foreign currency €120
# equity:conversion €-100
# equity:conversion $135
# assets:extra $20
# assets €-20
# assets $-155

# $ hledger -f- print --infer-costs
# >2 /There is not a unique posting which matches the conversion posting pair/
# >=1

# # 41. Multiple conversion pairs which match a single posting should cause an
# # error, and should not match both ‘sides’ of the conversion
# <
# 2011/01/01
# expenses:foreign currency €100
# equity:conversion €-100
# equity:conversion $135
# equity:conversion €-100
# equity:conversion $135
# assets $-270
# assets €100

# $ hledger -f- print --infer-costs
# >2 /There is not a unique posting which matches the conversion posting pair/
# >=1

# 39. A multicommodity transaction with conversion postings that we can't
# automatically match up with the corresponding other postings should be ignored,
# not an error. (#2045)
<
2011/01/01
expenses:foreign currency €100
equity:conversion €-100
equity:conversion $135
equity:conversion €-100
equity:conversion $135
assets $-270
assets €100
2023-01-01
Expenses:Gift HKD 118.00
Expenses:Personal HKD 118.00
Equity:Trading:Currency:INR-HKD:HKD HKD -236.00
Equity:Trading:Currency:INR-HKD:INR ₹2,150.77
Liabilities:Credit-Card ₹-2,150.77

$ hledger -f- print
2023-01-01
Expenses:Gift HKD 118.00
Expenses:Personal HKD 118.00
Equity:Trading:Currency:INR-HKD:HKD HKD -236.00
Equity:Trading:Currency:INR-HKD:INR ₹2,150.77
Liabilities:Credit-Card ₹-2,150.77

$ hledger -f- print --infer-costs
>2 /There is not a unique posting which matches the conversion posting pair/
>=1
>=

# 44. We can combine ‘other’ amounts into one posting, if they still match up.
# 40. We can combine ‘other’ amounts into one posting, if they still match up.
<
2011-01-01
expenses:foreign currency €100 @ $1.35
Expand Down

0 comments on commit bdac0da

Please sign in to comment.