egh / ledger-autosync

Synchronize your ledger-cli files with your bank.
GNU General Public License v3.0
273 stars 42 forks source link

Apply ofxid tags to posting scope instead of transaction scope #22

Closed 501st-alpha1 closed 7 years ago

501st-alpha1 commented 7 years ago

A transaction output by ledger-autosync looks something like this:

2017/02/28 Some Merchant
    ; ofxid: someid.123456789.etc
    Assets:Checking                       -$20.00
    Expenses:Misc                          $20.00

Based on my understanding of the ledger file format, the above ofxid tag is scoped to the entire transaction, rather than just to the posting of Assets:Checking. In this example it is still clear enough which posting it applies to, but I often have transactions that transfer money from one bank account to another, and I run ledger-autosync on both. In such a case, I might have a transaction like this:

2017/02/28 Transfer
    Assets:Checking                       -$20.00
    Assets:Savings                         $20.00

If I run ledger-autosync for both accounts, I will have 2 ofxids, which should obviously not both be applied to the same transaction, but to their separate postings, like so:

2017/02/28 Transfer
    Assets:Checking                       -$20.00
    ; ofxid: someid.123456789.etc
    Assets:Savings                         $20.00
    ; ofxid: someid.987654321.etc

To make this case easier to support, and to better structure the ledger file, can the ofxid tags be applied with posting scope rather than transaction scope?

egh commented 7 years ago

Here is what I do:

2017/02/28 Transfer
    ; ofxid: someid.123456789.etc
    Assets:Checking                       -$20.00
    Transfer:Checking_Savings

2017/02/28 Transfer
    ; ofxid: someid.987654321.etc
    Assets:Savings                         $20.00
    Transfer:Checking_Savings

I think if I change this it should probably be an optional behavior. I'm not sure how the syncing code would work if I changed this, though.

501st-alpha1 commented 7 years ago

Apologies for the delayed response; I read your message and didn't respond right away, then it fell off my radar. :flushed:

To clarify, I don't mind combining the two transactions myself; I understand that it would be hard to correlate such transactions. The purpose of this issue is only to request an adjustment to the position of the ofxid tag. Going back to my first example:

2017/02/28 Some Merchant
    ; ofxid: someid.123456789.etc
    Assets:Checking                       -$20.00
    Expenses:Misc                          $20.00

ofxid is only relevant to the Assets:Checking account, not to Expenses:Misc. My understanding of the ledger file format is it works like this:

2017/02/28 Some Merchant
    ; tags and comments here apply to entire transaction
    Assets:Checking                                 -$20.00
    ; tags and comments here apply only to Assets:Checking
    Expenses:Misc                                    $20.00
    ; tags and comments here apply only to Expenses:Misc

Thus, a better way to structure this transaction would be like this:

2017/02/28 Some Merchant
    Assets:Checking                       -$20.00
    ; ofxid: someid.123456789.etc
    Expenses:Misc                          $20.00

I currently cut and paste the ofxid line to the proper location, but I think it'd be better if the output of ledger-autosync was structured that way in the first place.

egh commented 7 years ago

Ah, I get it! You're right, I've been doing it wrong the whole time. I'm pretty sure that I even have code to work around issues this caused. :cry: I think I never quite wrapped my head around the idea that the metadata would come after the posting. That makes a lot more sense now.

I'll fix this ASAP!

egh commented 7 years ago

@501st-alpha1 I have filed a PR #27 for this issue which includes a script to fix existing files.