class DoubleEntry::Reporting::AggregateArray

Attributes

account[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

code[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

currency[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

filter[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

finish[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

function[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

partner_account[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

range_type[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

start[R]

An AggregateArray is awesome It is useful for making reports It is basically an array of aggregate results, representing a column of data in a report.

For example, you could request all sales broken down by month and it would return an array of values

Public Class Methods

new(function:, account:, code:, partner_account: nil, filter: nil, range_type: nil, start: nil, finish: nil) click to toggle source
# File lib/double_entry/reporting/aggregate_array.rb, line 14
def initialize(function:, account:, code:, partner_account: nil, filter: nil, range_type: nil, start: nil, finish: nil)
  @function        = function.to_s
  @account         = account
  @code            = code
  @partner_account = partner_account
  @filter          = filter
  @range_type      = range_type
  @start           = start
  @finish          = finish
  @currency        = DoubleEntry::Account.currency(account)

  retrieve_aggregates
  fill_in_missing_aggregates
  populate_self
end

Private Instance Methods

all_periods() click to toggle source
# File lib/double_entry/reporting/aggregate_array.rb, line 65
def all_periods
  TimeRangeArray.make(range_type, start, finish)
end
fill_in_missing_aggregates() click to toggle source
# File lib/double_entry/reporting/aggregate_array.rb, line 38
def fill_in_missing_aggregates
  # some aggregates may not have been previously calculated, so we can request them now
  # (this includes aggregates for the still-running period)
  all_periods.each do |period|
    unless @aggregates[period.key]
      @aggregates[period.key] = Aggregate.formatted_amount(function: function, account: account, code: code,
                                                           range: period, partner_account: partner_account, filter: filter)
    end
  end
end
formatted_amount(amount) click to toggle source
# File lib/double_entry/reporting/aggregate_array.rb, line 69
def formatted_amount(amount)
  amount ||= 0
  if function == 'count'
    amount
  else
    Money.new(amount, currency)
  end
end
populate_self() click to toggle source
# File lib/double_entry/reporting/aggregate_array.rb, line 32
def populate_self
  all_periods.each do |period|
    self << @aggregates[period.key]
  end
end
retrieve_aggregates() click to toggle source

get any previously calculated aggregates

# File lib/double_entry/reporting/aggregate_array.rb, line 50
def retrieve_aggregates
  fail ArgumentError, "Invalid range type '#{range_type}'" unless %w(year month week day hour).include? range_type
  scope = LineAggregate.
          where(:function => function).
          where(:range_type => 'normal').
          where(:account => account.try(:to_s)).
          where(:partner_account => partner_account.try(:to_s)).
          where(:code => code.try(:to_s)).
          where(:filter => filter.inspect).
          where(LineAggregate.arel_table[range_type].not_eq(nil))
  @aggregates = scope.each_with_object({}) do |result, hash|
    hash[result.key] = formatted_amount(result.amount)
  end
end