hledger icon indicating copy to clipboard operation
hledger copied to clipboard

[💰$100 bounty] Auto postings: Allow referring to the matched account

Open lukasbestle opened this issue 2 years ago • 8 comments

Feature

In an auto posting rule, it should be possible to refer to the account that matched the rule.

There are two common use cases for this:

  • It allows to modify the amount of the original posting (see my example below).
  • Another different use case I've seen is for budgeting. E.g. you could use (Budget:$account) to create virtual postings that contain the account name of the original expense.

Prior work

ledger supports the special $account string for that. This string can be used anywhere within the account part of the auto posting(s) and will automatically be replaced with the original account name.

Use case description

For tax reporting (German VAT "Umsatzsteuer" in my case), I have built auto posting rules that react on a tax tag (defined directly in the transaction or as a default in the account definition). A simple transaction could look like this:

2023-01-06 * A client
  Income:Revenue  -1190 €
    ; tax: 19%
  Assets:Bank

The client pays me 1000 € + 19 % VAT = 1190 €. But of course the VAT is not really income, so it needs to be separated. That's where the auto posting rule comes in (using the special $account string that is supported by ledger):

= acct:Income tag:tax=19%
  $account        *-0,15966387 ; 1-100/119
  Liabilities:VAT  *0,15966387 ; 1-100/119

The expected generated result is this:

2023-01-06 * A client
  Income:Revenue   -1000 €
  Liabilities:VAT  -190 €
  Assets:Bank

In this example I could have created a separate auto posting rule for each income account (there are not that many that would apply). But the issue gets more pronounced with expense accounts. There are a lot of expense accounts and it's not viable to create a separate auto posting rule for each one. So having a way to refer to the matched account is really vital.

Alternatives?

Maybe there is another way to implement the auto posting rule so that it modifies the amount of the original posting. If you have an idea, I'd be very grateful to hear it.

Bounty

I am ready to pay $20 to the person who implements this feature. Should multiple people be involved, the bounty will go to whoever's contribution is largest. The bounty will be due on a stable hledger release that includes this feature.

The hledger project will round up the bounty to $100.

lukasbestle avatar Jan 06 '23 20:01 lukasbestle

This would also contribute to increased ledger file support (https://github.com/simonmichael/hledger/issues/1962).

lukasbestle avatar Jan 07 '23 16:01 lukasbestle

That sounds very helpful! 👍

nobodyinperson avatar Jan 17 '23 12:01 nobodyinperson

assign me this

vinfinity7 avatar Nov 09 '23 17:11 vinfinity7

The hledger project will match and round up this bounty to $100.

simonmichael avatar Nov 09 '23 20:11 simonmichael

Design input is welcome from others interested in this.

We'll "never" implement Ledger's value expression language in full. Instead, as with * we can provide intuitive syntax that covers the common needs. If it's similar to Ledger's, like $account, I suppose that makes sense, though that can create wrong expectations, an uncanny valley effect, english bias, and/or a confusing inconsistent style (we don't use $ like this anywhere else). In other contexts (account aliases, and recently CSV if conditions) we use standard regexp replacement pattern syntax like \1. In CSV assignment rules we use %1 or %fieldname. What other items might people want to reference here ? Amount, comment, tags, tag values ?

simonmichael avatar Nov 09 '23 20:11 simonmichael

Thanks for your reply.

I like the idea to use the %fieldname syntax as it brings consistency. The auto posting and CSV features are independent and separate, but in the end they do similar things.

Accessing other metadata of the posting could indeed be useful. A few examples that come to my mind:

# copy over a specific tag value to the auto posting
= acct:Expenses tag:refundable
  (Budget:%acct)  *1 ; refundable: %tag:refundable

# prefix the comment of the same posting
= acct:Expenses amt:<100
  %acct  0 ; Small expense, %comment

# include the amount and description in a comment
# (here, budget is a counter, the comment allows to
# keep track of individual transactions)
= acct:Expenses
  (Budget:%acct)  C1 ; %desc (%amt)

In the examples I have used the field names for queries (again, for consistency). Except %comment, which doesn't seem to be a valid query (yet).

lukasbestle avatar Nov 10 '23 20:11 lukasbestle

Hi , currently busy due to my exams , would start the work within a week

vinfinity7 avatar Nov 19 '23 15:11 vinfinity7

No hurry. Good luck!

simonmichael avatar Nov 19 '23 17:11 simonmichael