Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #416 by allowing subtraction of zero-amount mismatched commodities #417

Merged
merged 2 commits into from
Apr 2, 2024

Conversation

bcc32
Copy link
Collaborator

@bcc32 bcc32 commented Mar 26, 2024

This PR changes ledger-{add,subtract}-commodity to allow the second argument's
commodity string not to match the first argument's, as long as the second amount
is zero. Since subtracting a zero amount is a no-op anyway, this should be fine
in terms of correctness, and alleviates some spurious errors.

Fix #416

@bcc32 bcc32 requested a review from purcell March 26, 2024 20:06
@purcell
Copy link
Member

purcell commented Mar 27, 2024

This feels like an indirect and unexpected fix to functions not directly related to ledger-reconcile. It's still a logical error to subtract/add mismatching commoditised values, whatever the numbers. I suspect there may be a better, more local, fix for #416.

@bcc32
Copy link
Collaborator Author

bcc32 commented Mar 31, 2024

I did think of that, but I think this change is still "justified", in that:

  1. These functions are only used by reconcile (in fact, ledger-add-commodity is not used at all)
  2. ledger itself makes this simplification:
$ ledger eval '0 USD'
0

$ ledger eval '0 USD - 0'
0

$ ledger eval '0 USD - 0 GBP'
0

FWIW, the commodity representation in ledger-mode is not quite accurate anyway; ledger itself can represent multiple commodities in the same amount just fine, although the printing and reading syntax then becomes more complex:

$ ledger eval '1 USD - 1 GBP'
-1 GBP
1 USD

(so I suppose it is reasonable not to support it directly in ledger-mode)

If you think this change should be more localized to reconciliation, I can make that change too, although since it's ledger itself that's printing 0 instead of 0 USD (or whatever commodity), it feels a bit more hacky to me than just changing the subtraction code.

@purcell
Copy link
Member

purcell commented Apr 1, 2024

That makes sense. Happy for this change to be merged as-is, then.

@bcc32
Copy link
Collaborator Author

bcc32 commented Apr 2, 2024

Will do, thanks for reviewing!

@bcc32 bcc32 merged commit 194aca2 into ledger:master Apr 2, 2024
24 checks passed
@bcc32 bcc32 deleted the fix-416 branch April 2, 2024 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ledger-reconcile raises an error if current balance is zero
2 participants