class Account

Public Class Methods

total_form(v) click to toggle source
# File lib/africompta/entities/account.rb, line 993
def self.total_form(v)
  (v.to_f * 1000 + 0.5).floor.to_s.tap do |s|
    :go while s.gsub!(/^([^.]*)(\ d)(?=(\ d { 3 })+)/, "\\1\\2,")
  end
end

Public Instance Methods

account() click to toggle source

This is the parent account

# File lib/africompta/entities/account.rb, line 864
def account
  Accounts.match_by_id(self.account_id)
end
account=(a) click to toggle source
# File lib/africompta/entities/account.rb, line 868
def account=(a)
  self.account_id = a.class == Account ? a.id : a
end
accounts() click to toggle source
# File lib/africompta/entities/account.rb, line 850
def accounts
  # Some hand-optimized stuff. This would be written shorter like this:
  # Accounts.matches_by_account_id( self.id )
  # But the code below is 3 times faster for some big data
  ret = []
  Accounts.data.each { |k, v|
    if v[:account_id] == self.id
      ret.push Accounts.get_data_instance(k)
    end
  }
  ret
end
bool_to_s(b) click to toggle source
# File lib/africompta/entities/account.rb, line 811
def bool_to_s(b)
  b ? 'true' : 'false'
end
data_set(f, v) click to toggle source
Calls superclass method
# File lib/africompta/entities/account.rb, line 673
def data_set(f, v)
  if !@proxy.loading
    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
delete(force = false) click to toggle source
# File lib/africompta/entities/account.rb, line 892
def delete(force = false)
  if not is_empty && force
    movements_src.each { |m|
      dputs(3) { "Deleting movement #{m.to_json}" }
      m.delete
    }
  end
  if is_empty
    dputs(2) { "Deleting account #{self.name}-#{self.id}" }
    self.account_id = nil
    self.deleted = true
  else
    dputs(1) { "Refusing to delete account #{name}" }
    return false
  end
  return true
end
dump(mov = false) click to toggle source
# File lib/africompta/entities/account.rb, line 956
def dump(mov = false)
  t = (self.keep_total ? 'K' : '.') + "#{self.multiplier.to_s.rjust(2, '+')}"
  acc_desc = ["**#{t}**#{self.path_id}#{self.deleted ? ' -- deleted' : ''}"]
  dputs(1) { acc_desc.first }
  acc_desc +
      if mov
        movements.collect { |m|
          m_desc = "     #{m.to_json}"
          dputs(1) { m_desc }
          m_desc
        }
      else
        []
      end
end
dump_rec(mov = false) click to toggle source
# File lib/africompta/entities/account.rb, line 972
def dump_rec(mov = false)
  ret = []
  get_tree_depth { |a|
    ret.push a.dump(mov)
  }
  ret.flatten
end
get_archive(year = Date.today.year - 1, month = Date.today.month) click to toggle source
# File lib/africompta/entities/account.rb, line 1007
def get_archive(year = Date.today.year - 1, month = Date.today.month)
  dputs(0) { 'Error: not implemented yet' }
end
get_archives() click to toggle source
# File lib/africompta/entities/account.rb, line 999
def get_archives
  if archive = AccountRoot.archive
    archive.accounts.collect { |arch|
      Accounts.get_by_path("#{arch.path}::#{path.sub(/^Root::/, '')}")
    }.select { |a| a }
  end
end
get_path(sep = '::', p = '', first = true) click to toggle source
# File lib/africompta/entities/account.rb, line 722
def get_path(sep = '::', p = '', first = true)
  path(sep, p, first)
end
get_tree(depth = -1) { |self, depth| ... } click to toggle source

This gets the tree under that account, breadth-first

# File lib/africompta/entities/account.rb, line 684
def get_tree(depth = -1)
  yield self, depth
  return if depth == 0
  accounts.sort { |a, b| a.name <=> b.name }.each { |a|
    a.get_tree(depth - 1) { |b| yield b, depth - 1 }
  }
end
get_tree_debug(ind = '') { |self| ... } click to toggle source
# File lib/africompta/entities/account.rb, line 700
def get_tree_debug(ind = '')
  yield self
  dputs(1) { "get_tree_ #{ind}#{self.name}" }
  accounts.sort { |a, b| a.name <=> b.name }.each { |a|
    a.get_tree_debug("#{ind} ") { |b| yield b }
  }
end
get_tree_depth() { |b| ... } click to toggle source

This gets the tree under that account, depth-first

# File lib/africompta/entities/account.rb, line 693
def get_tree_depth
  accounts.sort { |a, b| a.name <=> b.name }.each { |a|
    a.get_tree_depth { |b| yield b }
  }
  yield self
end
is_empty() click to toggle source
# File lib/africompta/entities/account.rb, line 828
def is_empty
  size = self.movements.select { |m| m.value.to_f != 0.0 }.size
  dputs(2) { "Account #{self.name} has #{size} non-zero elements" }
  dputs(4) { "Non-zero elements: #{movements.inspect}" }
  if size == 0 and self.accounts.size == 0
    return true
  end
  return false
end
listp_path(depth = -1) click to toggle source
# File lib/africompta/entities/account.rb, line 980
def listp_path(depth = -1)
  acc = []
  get_tree(depth) { |a|
    acc.push [a.id, a.path]
  }
  dputs(3) { "Ret is #{acc.inspect}" }
  acc
end
movements(from = nil, to = nil) click to toggle source

Sort first regarding inverse date (newest first), then description, and finally the value

# File lib/africompta/entities/account.rb, line 773
  def movements(from = nil, to = nil)
    dputs(5) { 'Account::movements' }
    movs = (movements_src + movements_dst)
    if (from != nil and to != nil)
      movs.delete_if { |m|
        (m.date < from || m.date > to)
      }
      dputs(3) { 'Rejected some elements' }
    end
    movs.delete_if { |m| m.value == 0 }
    sorted = movs.sort { |a, b|
      ret = 0
      if a.date and b.date
        ret = a.date.to_s <=> b.date.to_s
      end
      if ret == 0
        ret = a.rev_index <=> b.rev_index
=begin
        if a.desc and b.desc
          ret = a.desc <=> b.desc
        end
        if ret == 0
          if a.value and b.value
            ret = a.value.to_f <=> b.value.to_f
          end
        end
=end
      end
      if ret
        ret * -1
      else
        dputs(0) { "Error: Ret shouldn't be nil... #{self.path}" }
        0
      end
    }
    sorted
  end
movements_dst() click to toggle source
# File lib/africompta/entities/account.rb, line 884
def movements_dst
  Movements.matches_by_account_dst_id(self.id)
end
movements_src() click to toggle source
# File lib/africompta/entities/account.rb, line 880
def movements_src
  Movements.matches_by_account_src_id(self.id)
end
multiplier() click to toggle source
# File lib/africompta/entities/account.rb, line 888
def multiplier
  _multiplier.to_i
end
new_index() click to toggle source
# File lib/africompta/entities/account.rb, line 726
def new_index()
  if !u_l = Users.match_by_name('local')
    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 #{rev_index}" }
end
parent() click to toggle source
# File lib/africompta/entities/account.rb, line 872
def parent
  account
end
parent=(a) click to toggle source
# File lib/africompta/entities/account.rb, line 876
def parent=(a)
  self.account = a
end
path(sep = '::', p='', first=true) click to toggle source
# File lib/africompta/entities/account.rb, line 708
def path(sep = '::', p='', first=true)
  if (acc = self.account)
    return acc.path(sep, p, false) + sep + self.name
  else
    return self.name
  end
end
path_id(sep = '::', p='', first=true) click to toggle source
# File lib/africompta/entities/account.rb, line 716
def path_id(sep = '::', p='', first=true)
  (self.account ?
      "#{self.account.path_id(sep, p, false)}#{sep}" : '') +
      "#{self.name}-#{self.id}"
end
print_pdf(file, recursive = false) click to toggle source
print_pdf_document(pdf) click to toggle source
set(name, desc, parent, multiplier = 1, users = [], keep_total = false) click to toggle source
# File lib/africompta/entities/account.rb, line 764
def set(name, desc, parent, multiplier = 1, users = [], keep_total = false)
  dputs(3) { "Going to set #{name}-#{parent}-#{multiplier}" }
  set_nochildmult(name, desc, parent, multiplier, users, keep_total)
  # All descendants shall have the same multiplier
  set_child_multiplier_total(multiplier, total)
end
set_child_multiplier_total(m, t) click to toggle source

Be sure that all descendants have the same multiplier and keep_total

# File lib/africompta/entities/account.rb, line 839
def set_child_multiplier_total(m, t)
  dputs(3) { "Setting multiplier from #{name} to #{m} and keep_total to #{t}" }
  self.multiplier = m
  self.keep_total = t
  return if not accounts
  accounts.each { |acc|
    acc.set_child_multiplier_total(m, t)
  }
  self
end
set_nochildmult(name, desc, parent = nil, multiplier = 1, users = [], keep_total = false) click to toggle source

Sets different new parameters.

# File lib/africompta/entities/account.rb, line 752
def set_nochildmult(name, desc, parent = nil, multiplier = 1, users = [],
                    keep_total = false)
  self.name, self.desc, self.keep_total = name, desc, keep_total
  parent and self.account_id = parent
  # TODO: implement link between user-table and account-table
  # self.users = users ? users.join(":") : ""
  self.multiplier = multiplier
  self.keep_total = keep_total
  update_total
  self
end
to_s(add_path = false) click to toggle source
# File lib/africompta/entities/account.rb, line 815
def to_s(add_path = false)
  if account || true
    dputs(4) { "Account-desc: #{name.to_s}, #{global_id}, #{account_id.inspect}" }
    "#{desc}\r#{global_id}\t" +
        "#{sprintf('%.3f', total.to_f.round(3))}\t#{name.to_s}\t#{multiplier.to_i.to_s}\t" +
        (account_id ? ((account_id > 0) ? account.global_id.to_s : '') : '') +
        "\t#{bool_to_s(self.deleted)}" + "\t#{bool_to_s(self.keep_total)}" +
        (add_path ? "\t#{path}" : '')
  else
    'nope'
  end
end
total_form() click to toggle source
# File lib/africompta/entities/account.rb, line 989
def total_form
  Account.total_form(total)
end
update_total(precision = 3) click to toggle source
# File lib/africompta/entities/account.rb, line 736
def update_total(precision = 3)
  # Recalculate everything.
  dputs(4) { "Calculating total for #{self.path_id} with mult #{self.multiplier}" }
  self.total = (0.0).to_f
  dputs(4) { "Total before update is #{self.total} - #{self.total.class.name}" }
  self.movements.each { |m|
    v = m.get_value(self)
    dputs(5) { "Adding value #{v.inspect} to #{self.total.inspect}" }
    self.total = self.total.to_f + v.to_f
    dputs(5) { "And getting #{self.total.inspect}" }
  }
  self.total = self.total.to_f.round(precision)
  dputs(4) { "Final total is #{self.total} - #{self.total.class.name}" }
end