envato / double_entry

A double-entry accounting system for Ruby applications.
https://rubygems.org/gems/double_entry
MIT License
429 stars 68 forks source link

Don't fix balance errors automatically by default #160

Closed orien closed 5 years ago

orien commented 5 years ago

Context

The DoubleEntry::Validation::LineCheck::perform! process checks the running balance (in the double_entry_lines table) and cached balance (in the double_entry_account_balances table). If this process finds a calculation error in the running balance, it corrects the problem by locking the account in question (preventing new transactions on an incorrect balance) and then processing each line recorded, one-by-one recalculating the running balance. Considering that this recalculation process may take considerable time for accounts with millions of associated double_entry_lines rows, for some business cases it maybe unacceptable to prevent new transactions for the duration of the correction process.

Change

Don't perform corrections by default. Instead just log the balance issues in the database. Teams can then plan for any required balance corrections using the new AccountFixer class:

DoubleEntry::Validation::AccountFixer.new.recalculate_account(account)

If desired, the original automatic correction behaviour can be obtained by providing an AccountFixer instance to the line check process:

DoubleEntry::Validation::LineCheck.perform!(
  fixer: DoubleEntry::Validation::AccountFixer.new
)