class Keepr::Account

Public Class Methods

merged_with_sums(options = {}) click to toggle source
# File lib/keepr/account.rb, line 50
def self.merged_with_sums(options = {})
  accounts = with_sums(options).to_a

  # Sum up child accounts to parent
  position = 0
  while (account = accounts[position])
    if account.parent_id && account.sum_amount
      parent_account = accounts.find { |a| a.id == account.parent_id }
      raise RuntimeError unless parent_account

      parent_account.sum_amount ||= 0
      parent_account.sum_amount += account.sum_amount
      accounts.delete_at(position)
    else
      position += 1
    end
  end

  accounts
end
with_sums(options = {}) click to toggle source
# File lib/keepr/account.rb, line 24
def self.with_sums(options = {})
  raise ArgumentError unless options.is_a?(Hash)

  subquery = Keepr::Posting
             .select('SUM(keepr_postings.amount)')
             .joins(:keepr_journal)
             .where('keepr_postings.keepr_account_id = keepr_accounts.id')

  date           = options[:date]
  permanent_only = options[:permanent_only]

  case date
  when nil
  when Date
    subquery = subquery.where('keepr_journals.date <= ?', date)
  when Range
    subquery = subquery.where(keepr_journals: { date: date.first..date.last })
  else
    raise ArgumentError
  end

  subquery = subquery.where(keepr_journals: { permanent: true }) if permanent_only

  select "keepr_accounts.*, (#{subquery.to_sql}) AS sum_amount"
end

Public Instance Methods

balance(date = nil) click to toggle source
# File lib/keepr/account.rb, line 81
def balance(date = nil)
  scope = case date
          when nil
            keepr_postings
          when Date
            keepr_postings
          .joins(:keepr_journal)
          .where('keepr_journals.date <= ?', date)
          when Range
            keepr_postings
          .joins(:keepr_journal)
          .where(keepr_journals: { date: date.first..date.last })
          else
            raise ArgumentError
          end

  scope.sum(:amount)
end
keepr_postings() click to toggle source
# File lib/keepr/account.rb, line 75
def keepr_postings
  Keepr::Posting
    .joins(:keepr_account)
    .merge(subtree)
end
number_as_string() click to toggle source
# File lib/keepr/account.rb, line 100
def number_as_string
  if number < 1000
    format('%04d', number)
  else
    number.to_s
  end
end
profit_and_loss?() click to toggle source
# File lib/keepr/account.rb, line 71
def profit_and_loss?
  revenue? || expense?
end
to_s() click to toggle source
# File lib/keepr/account.rb, line 108
def to_s
  "#{number_as_string} (#{name})"
end

Private Instance Methods

group_validation() click to toggle source
# File lib/keepr/account.rb, line 114
def group_validation
  return unless keepr_group.present?

  if asset?
    errors.add(:kind, :group_mismatch) unless keepr_group.asset?
  elsif liability?
    errors.add(:kind, :group_mismatch) unless keepr_group.liability?
  elsif profit_and_loss?
    errors.add(:kind, :group_mismatch) unless keepr_group.profit_and_loss?
  else
    errors.add(:kind, :group_conflict)
  end

  errors.add(:keepr_group_id, :no_group_allowed_for_result) if keepr_group.is_result
end
tax_validation() click to toggle source
# File lib/keepr/account.rb, line 130
def tax_validation
  errors.add(:keepr_tax_id, :circular_reference) if keepr_tax && keepr_tax.keepr_account == self
end