lib/africompta/entities/account.rb in africompta-1.9.10 vs lib/africompta/entities/account.rb in africompta-1.9.11

- old
+ new

@@ -1,108 +1,8 @@ require 'prawn' require 'prawn/measurement_extensions' -class AccountRoot - - def self.actual - self.accounts.find { |a| a.name == 'Root' } - end - - def self.current - AccountRoot.actual - end - - def self.archive - self.accounts.find { |a| a.name == 'Archive' } - end - - def self.accounts - Accounts.matches_by_account_id(0) - end - - def self.clean - count_mov, count_acc = 0, 0 - bad_mov, bad_acc = 0, 0 - log_msg 'Account.clean', 'starting to clean up' - Movements.search_all_.each { |m| - dputs(4) { "Testing movement #{m.inspect}" } - if not m or not m.date or not m.desc or not m.value or - not m.rev_index or not m.account_src or not m.account_dst - if m and m.desc - log_msg 'Account.clean', "Bad movement: #{m.inspect}" - end - m.delete - bad_mov += 1 - end - if m.rev_index - count_mov = [count_mov, m.rev_index].max - end - } - Accounts.search_all_.each { |a| - if (a.account_id and (a.account_id > 0)) and (not a.account) - log_msg 'Account.clean', "Account has unexistent parent: #{a.inspect}" - a.delete - bad_acc += 1 - end - if !(a.account_id or a.deleted) - log_msg 'Account.clean', "Account has undefined parent: #{a.inspect}" - a.delete - bad_acc += 1 - end - if a.account_id == 0 - if !((a.name =~ /(Root|Archive)/) or a.deleted) - log_msg 'Account.clean', 'Account is in root but neither ' + - "'Root' nor 'Archive': #{a.inspect}" - a.delete - bad_acc += 1 - end - end - if !a.rev_index - log_msg 'Account.clean', "Didn't find rev_index for #{a.inspect}" - a.new_index - bad_acc += 1 - end - count_acc = [count_acc, a.rev_index].max - } - - # Check also whether our counters are OK - u_l = Users.match_by_name('local') - dputs(1) { "Movements-index: #{count_mov} - #{u_l.movement_index}" } - dputs(1) { "Accounts-index: #{count_acc} - #{u_l.account_index}" } - @ul_mov, @ul_acc = u_l.movement_index, u_l.account_index - if count_mov > u_l.movement_index - log_msg 'Account.clean', 'Error, there is a bigger movement! Fixing' - u_l.movement_index = count_mov + 1 - end - if count_acc > u_l.account_index - log_msg 'Account.clean', 'Error, there is a bigger account! Fixing' - u_l.account_index = count_acc + 1 - end - return [count_mov, bad_mov, count_acc, bad_acc] - end - - def self.path_id - return '' - end - - def self.id - 0 - end - - def self.mult - 1 - end - - def self.keep_total - false - end - - def self.multiplier - 1 - end -end - class Accounts < Entities self.needs %w(Users Movements) attr_reader :check_state, :check_progress @@ -124,11 +24,11 @@ end def self.create(name, desc = 'Too lazy', parent = nil, global_id = '', mult = nil) dputs(5) { "Parent is #{parent.inspect}" } if parent - if parent.class != Account and parent != AccountRoot + if parent.class != Account && parent != AccountRoot parent = Accounts.matches_by_id(parent).first end mult ||= parent.multiplier a = super(:name => name, :desc => desc, :account_id => parent.id, :global_id => global_id.to_s, :multiplier => mult, @@ -537,24 +437,28 @@ AccountRoot.accounts.each { |a| dputs(1) { "Root-Account: #{a.inspect}" } } end + def init + root = Accounts.create('Root', 'Initialisation') + %w( Income Outcome Lending Cash ).each { |a| + Accounts.create(a, 'Initialisation', root) + } + %w( Lending Cash ).each { |a| + acc = Accounts.match_by_name(a) + acc.multiplier = -1 + acc.keep_total = true + } + Accounts.save + end + def load super if Accounts.search_by_name('Root').count == 0 - dputs(1) { "Didn't find 'Root' in database - creating base" } - root = Accounts.create('Root', 'Initialisation') - %w( Income Outcome Lending Cash ).each { |a| - Accounts.create(a, 'Initialisation', root) - } - %w( Lending Cash ).each { |a| - acc = Accounts.match_by_name(a) - acc.multiplier = -1 - acc.keep_total = true - } - Accounts.save + dputs(0) { "Didn't find 'Root' in database - creating base" } + Accounts.init end end def migration_1(a) dputs(4) { Accounts.storage[:SQLiteAC].db_class.inspect } @@ -650,16 +554,116 @@ @check_state = 'Done' [in_db, diff, in_local] end end +class AccountRoot + + def self.actual + self.accounts.find { |a| a.name == 'Root' } + end + + def self.current + AccountRoot.actual + end + + def self.archive + self.accounts.find { |a| a.name == 'Archive' } + end + + def self.accounts + Accounts.matches_by_account_id(0) + end + + def self.clean + count_mov, count_acc = 0, 0 + bad_mov, bad_acc = 0, 0 + log_msg 'Account.clean', 'starting to clean up' + Movements.search_all_.each { |m| + dputs(4) { "Testing movement #{m.inspect}" } + if not m or not m.date or not m.desc or not m.value or + not m.rev_index or not m.account_src or not m.account_dst + if m and m.desc + log_msg 'Account.clean', "Bad movement: #{m.inspect}" + end + m.delete + bad_mov += 1 + end + if m.rev_index + count_mov = [count_mov, m.rev_index].max + end + } + Accounts.search_all_.each { |a| + if (a.account_id && (a.account_id > 0)) && (!a.account) + log_msg 'Account.clean', "Account has unexistent parent: #{a.inspect}" + a.delete + bad_acc += 1 + end + if !(a.account_id || a.deleted) + log_msg 'Account.clean', "Account has undefined parent: #{a.inspect}" + a.delete + bad_acc += 1 + end + if a.account_id == 0 || a.account_id == nil + if !((a.name =~ /(Root|Archive)/) || a.deleted) + log_msg 'Account.clean', 'Account is in root but neither ' + + "'Root' nor 'Archive': #{a.inspect}" + a.delete + bad_acc += 1 + end + end + if !a.rev_index + log_msg 'Account.clean', "Didn't find rev_index for #{a.inspect}" + a.new_index + bad_acc += 1 + end + count_acc = [count_acc, a.rev_index].max + } + + # Check also whether our counters are OK + u_l = Users.match_by_name('local') + dputs(1) { "Movements-index: #{count_mov} - #{u_l.movement_index}" } + dputs(1) { "Accounts-index: #{count_acc} - #{u_l.account_index}" } + @ul_mov, @ul_acc = u_l.movement_index, u_l.account_index + if count_mov > u_l.movement_index + log_msg 'Account.clean', 'Error, there is a bigger movement! Fixing' + u_l.movement_index = count_mov + 1 + end + if count_acc > u_l.account_index + log_msg 'Account.clean', 'Error, there is a bigger account! Fixing' + u_l.account_index = count_acc + 1 + end + return [count_mov, bad_mov, count_acc, bad_acc] + end + + def self.path_id + return '' + end + + def self.id + 0 + end + + def self.mult + 1 + end + + def self.keep_total + false + end + + def self.multiplier + 1 + end +end + class Account < Entity def data_set(f, v) if !@proxy.loading - if !%w( _total _rev_index ).index(f.to_s) - dputs(4) { "Updating index for field #{f.inspect} - #{@pre_init} - #{@proxy.loading} - #{caller}" } + if !%w( _total _rev_index _global_id ).index(f.to_s) + dputs(4) { "Updating index for field #{f.inspect} - #{@pre_init} - #{@proxy.loading}: #{v}" } new_index end end super(f, v) end @@ -712,11 +716,11 @@ dputs(0) { "Oups - user 'local' was not here: #{caller}" } u_l = Users.create('local') end self.rev_index = u_l.account_index u_l.account_index += 1 - dputs(3) { "Index for account #{name} is #{index}" } + dputs(3) { "Index for account #{name} is #{rev_index}" } end def update_total(precision = 3) # Recalculate everything. dputs(4) { "Calculating total for #{self.path_id} with mult #{self.multiplier}" } @@ -872,10 +876,10 @@ def multiplier _multiplier.to_i end def delete(force = false) - if not is_empty and force + if not is_empty && force movements_src.each { |m| dputs(3) { "Deleting movement #{m.to_json}" } m.delete } end