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

AccountFixer not fix account which scope is nil #206

Closed elct9620 closed 1 year ago

elct9620 commented 3 years ago

Hi, the LineCheck can find the accounts which scope: nil but the AccountFixer didn't fix it correctly.

https://github.com/envato/double_entry/blob/37ea766a8f87ab72e9f4b131abd61fec85d10d62/lib/double_entry/validation/account_fixer.rb#L23-L28

The line scope: account.scope_identity.to_s always convert nil to "" (empty string) that it unable find correct account lines to update balance.

Possible Options:

  1. Change scope: account.scope_identity.to_s to scope: account.scope_identity&.to_s
  2. Set scope column default to "" instead accept nil

We may consider to the unscoped account in this case if we set all scope to "" instead of nil

Updated

Due to the LineCheck SQL, option 1 will unable to find any Line in this case

https://github.com/envato/double_entry/blob/37ea766a8f87ab72e9f4b131abd61fec85d10d62/lib/double_entry/validation/line_check.rb#L63-L70

In this case, the scope = NULL is not working correctly.

My database is PostgreSQL and not sure how MySQL works in this case.

Note

Sometimes one of my accounts which without scope will reset the balance to 0 after the transfer (eg. from 100.0 to 1.0)

But if the LineCheck works correctly, the balance can go back correctly value.

I am not sure the relationship between the above two issues, but it seems we have some problems when using scope if it is nil?