ltowarek / budget-supervisor

MIT License
1 stars 0 forks source link

Add account balance field #54

Closed ltowarek closed 3 years ago

ltowarek commented 3 years ago

Balance in synchronized accounts should be read-only and updated during connection refresh. Balance in regular accounts should be updated when transaction is created/updated/deleted.

ltowarek commented 3 years ago

With a current balance approach it will not work well with the reports, because there will be no history of account balance in a given point of time. Better approach seems to be an initial balance per account. This way, current account balance is simply the initial account balance and transactions balance.

ltowarek commented 3 years ago

For imported, just created accounts, initial balance can be taken from the oldest transaction's extra fields - https://docs.saltedge.com/account_information/v5/#transactions-extra - opening_balance or closing_balance - amount.

ltowarek commented 3 years ago

Initial balance should be treated as a regular transaction instead of account field.

ltowarek commented 3 years ago

Approach with the oldest transaction does not work when there are pending transactions. Account balance returns sum of pending and posted transactions.

It seems that the proper way of doing this is:

  1. Download account
  2. Set initial balance to account's current balance
  3. If account has pending transactions:
    1. Download pending transactions
    2. Subtract sum of pending transactions from initial balance
  4. Download transactions
  5. Subtract sum of transactions from initial balance
  6. Return initial balance