purcell / flycheck-ledger

A flychecker for checking ledger files
GNU General Public License v3.0
29 stars 5 forks source link

Balance assertion error when using include ledgers #10

Closed mikeonly closed 6 months ago

mikeonly commented 6 months ago

Hi, I get an flycheck-ledger error for balance assertions in one of my files, let's say A.journal, when it is included in main.journal as include A.journal together with some other transactions and includes that make the balance assertions in A.journal in total correct. I am missing something I should specify to flycheck-ledger or is it not an intended functionality?

Running both hledger check --strict and ledger reg --strict gives no errors in the command line. They are both ran against $LEDGER_FILE which is an entry main.journal. I suspected that errors from flycheck-ledger would correspond to those from ledger executable, but it is not the case.

What variables should I check to fix that, or what am I missing?

Thanks. :)

purcell commented 6 months ago

I don't quite follow how to reproduce this, sorry, there's not enough information in the first sentence. Like, do you get the flycheck errors when editing A.journal or main.journal? Steps to reproduce would make this clearer.

mikeonly commented 6 months ago

Yes, sorry for not explaining it better. I get the errors when editing the A.journal.

purcell commented 6 months ago

Alright, so A.journal does not balance locally (ie. just within that file), but when included from main.journal it should balance? Errors when editing A.journal would be expected in that case: flycheck operates on buffers even when they're not saved to disk, so there'd be no way to run ledger on main.journal so that the un-saved changes from A.journal would be included in the overall balancing.

However, I think you can (and probably should) arrange for the individual files to balance too. IIRC I had a similar situation to you, and found that I could make everything line up nicely by using additional account names and opening balances:

Additional account names

For example, let's say you have a ledger file per bank account. Often you need to account for moving money between them, so the naive solution is to directly reference the account names for the other file. Instead, you can introduce a "virtual" account name. As an example:

credit-card.ledger:

2012-05-03 5261: EXAMPLE TRANSACTION
    Personal:Expenses:Groceries               £10.58
    Personal:Liabilities:Mastercard

2012-05-05 : PAYMENT DIRECT DEBIT FROM CURRENT ACCOUNT
    Personal:Liabilities:Mastercard        £1,198.43
    Personal:Transfers:Mastercard

and in current-account.ledger:

2012-05-08 (D/D) 'CREDIT CARD PAYMENT
    Personal:Transfers:Mastercard          £1,198.43
    Personal:Assets:Bank:Current          £-1,198.43

while main.ledger includes both files:

include current-account.ledger
include credit-card.ledger

In this scheme Personal:Transfers:Mastercard represents a flow of funds between the credit card account and other sources. All the ledger files balance on their own, and when combined in main.ledger the flows from the Transfers account get balanced up against each other.

Opening balances

By using an opening balance account, you can usually make individual files balance internally, e.g. at the top of my credit-card.ledger you might have:

2012-04-06 Personal Opening Balance
    Personal:Liabilities:Mastercard         £-176.97
    Personal:Equity:Opening Balances
mikeonly commented 6 months ago

Thanks for the detailed clarification. Transfer accounts trick is very helpful, and I can make the opening balance work too. I would just expect a different behavior given that there is a local variable that points to the main .ledger file, but good to know that the observed error is expected. That issue is resolved as you have kindly explained your own solution, thank you.

purcell commented 5 months ago

Great, glad that helped!