ledger / vim-ledger

Vim plugin for Ledger
GNU General Public License v2.0
372 stars 55 forks source link

add action to calculate balance of transaction #127

Open zanona opened 2 years ago

zanona commented 2 years ago

Hello, Just following up my previous comment with a suggestion for adding an action to allow calculating the balance for an specific transaction. An good use case would be on automated transactions, which need to be explicitly balanced.

= income:salary
  bank:foo         $1.234
  savings:bar      $1.293
  investment:baz   $1.939
  bank:busines    $-0.000   ; i.e we could have `:call ledger#balance_transaction()`

2022-04-05 Salary
  bank:business    $5.000
  income:salary

Originally posted by @zanona in https://github.com/ledger/vim-ledger/issues/109#issuecomment-1086889068

alerque commented 2 years ago

Pardon my ignorance here because I use hledger and this is a ledger specific syntax. I'm a little confused what would actually be calculated here. It seems to be a far more common use case for automated transactions would be using multipliers, not fixed amounts:

= income:salary
  bank:foo         0.8
  savings:bar      0.2
  bank:business   -1.0

2022-04-05 Salary
  bank:business    $5,000
  income:salary

In the case you posted, it seems like the automation rule would only work for fixed amounts, in which case it isn't clear to me what you would expect VIM to be calculating. Do you expect it to add up the postings in just the automated transaction and come up with $-4.466, or what? Wouldn't that then only work as long as your following transactions relating to salary were always the same amount?

zanona commented 2 years ago

Hey, @alerque I believe this is a nice example:


= income:salary
    expense:gym                                         $75
        expense:bar                                         $50
    assets:bank                                       -$125

~ monthly
    assets:bank                                      $1,500
    income:salary                                   -$1,500

It's not always that I would to declare values in percentile, so it is more readable (I know straight how much I pay for gym as $75 rather than 0.05), but not only that, sometimes there are a group of transactions which happen simultaneously, in which case it could become quite annoying to calculate every time, like the one below.

= income:salary
    fund:electricity                                ($800/12)
    fund:tv-licence                                 ($150/12)
    fund:damages-insurance                          ($125/12)
    fund:rent-insurance                             ($150/12)
    fund:recycling-tax                               ($65/12)
        assets:bank                                      ....

So, at least in my case, those transactions don't always revert to -1.0 spending the full amount of the original transaction. i.e: salary

However, perhaps this is far too outside the scope of this plugin and a possible edge-case scenario, in which case we can certainly close this issue and I will find another ways to automate these calculations otherwise :wink:

alerque commented 2 years ago

If I'm following along correctly here, you just want to balance postings in a single transaction. The fact that it is an automatic transaction is nearly irrelevant. That seems like something we may be able to do, although currency handling is likely to make this excessively complicated to actually pull off. In any case it's possible that as long as what you want is scoped to a single transaction (not referencing a value from another transaction) that me may be able to cover that in this plugin.

I personally don't have much interest in making this happen because I use hledger which doesn't have the same balance requirements and also I normalize translations by printing them with hledger itself which automatically calculates and fills in elided values.

Given the parameters above that it was scoped to a transaction and makes reasonably attempt at handling different currencies and formats properly I'd be willing to facilitate a PR if somebody else jumps in to contribute this.