Module: DoubleEntry::Reporting Private

Extended by:
Reporting
Includes:
Configurable
Included in:
Reporting
Defined in:
lib/double_entry/reporting.rb,
lib/double_entry/reporting/aggregate.rb,
lib/double_entry/reporting/day_range.rb,
lib/double_entry/reporting/hour_range.rb,
lib/double_entry/reporting/time_range.rb,
lib/double_entry/reporting/week_range.rb,
lib/double_entry/reporting/year_range.rb,
lib/double_entry/reporting/month_range.rb,
lib/double_entry/reporting/line_aggregate.rb,
lib/double_entry/reporting/aggregate_array.rb,
lib/double_entry/reporting/time_range_array.rb,
lib/double_entry/reporting/line_aggregate_filter.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: Aggregate, AggregateArray, AggregateFunctionNotSupported, Configuration, DayRange, HourRange, LineAggregate, LineAggregateFilter, MonthRange, TimeRange, TimeRangeArray, WeekRange, YearRange

Instance Method Summary collapse

Methods included from Configurable

included

Instance Method Details

#aggregate(function, account, code, range, options = {}) ⇒ Money, Fixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform an aggregate calculation on a set of transfers for an account.

The transfers included in the calculation can be limited by time range and provided custom filters.

Examples:

Find the sum for all $10 :save transfers in all :checking accounts in the current month, made by Australian users (assume the date is January 30, 2014).

time_range = DoubleEntry::Reporting::TimeRange.make(2014, 1)

DoubleEntry::Line.class_eval do
  scope :specific_transfer_amount, ->(amount) { where(:amount => amount.fractional) }
end

DoubleEntry::Reporting.aggregate(
  :sum,
  :checking,
  :save,
  time_range,
  :filter => [
    :scope    => {
      :name      => :specific_transfer_amount,
      :arguments => [Money.new(10_00)]
    },
    :metadata => {
      :user_location => 'AU'
    },
  ]
)

Parameters:

  • function (Symbol)

    The function to perform on the set of transfers. Valid functions are :sum, :count, and :average

  • account (Symbol)

    The symbol identifying the account to perform the aggregate calculation on. As specified in the account configuration.

  • code (Symbol)

    The application specific code for the type of transfer to perform an aggregate calculation on. As specified in the transfer configuration.

  • Only (DoubleEntry::Reporting::TimeRange)

    include transfers in the given time range in the calculation.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :filter (Array<Hash<Symbol,Hash<Symbol,Object>>>)

    An array of custom filter to apply before performing the aggregate calculation. Filters can be either scope filters, where the name must be specified, or they can be metadata filters, where the key/value pair to match on must be specified. Scope filters must be monkey patched as scopes into the DoubleEntry::Line class, as the example above shows. Scope filters may also take a list of arguments to pass into the monkey patched scope, and, if provided, must be contained within an array.

Returns:

  • (Money, Fixnum)

    Returns a Money object for :sum and :average calculations, or a Fixnum for :count calculations.

Raises:



81
82
83
# File 'lib/double_entry/reporting.rb', line 81

def aggregate(function, , code, range, options = {})
  Aggregate.formatted_amount(function, , code, range, options)
end

#aggregate_array(function, account, code, options = {}) ⇒ Array<Money, Fixnum>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform an aggregate calculation on a set of transfers for an account and return the results in an array partitioned by a time range type.

The transfers included in the calculation can be limited by a time range and provided custom filters.

Examples:

Find the number of all $10 :save transfers in all :checking accounts per month for the entire year (Assume the year is 2014).

DoubleEntry::Reporting.aggregate_array(
  :sum,
  :checking,
  :save,
  :range_type => 'month',
  :start      => '2014-01-01',
  :finish     => '2014-12-31',
)

Parameters:

  • function (Symbol)

    The function to perform on the set of transfers. Valid functions are :sum, :count, and :average

  • account (Symbol)

    The symbol identifying the account to perform the aggregate calculation on. As specified in the account configuration.

  • code (Symbol)

    The application specific code for the type of transfer to perform an aggregate calculation on. As specified in the transfer configuration.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :filter (Array<Symbol>, Array<Hash<Symbol, Object>>)

    A custom filter to apply before performing the aggregate calculation. Currently, filters must be monkey patched as scopes into the DoubleEntry::Line class in order to be used as filters, as the example shows. If the filter requires a parameter, it must be given in a Hash, otherwise pass an array with the symbol names for the defined scopes.

  • :range_type (String)

    The type of time range to return data for. For example, specifying 'month' will return an array of the resulting aggregate calculation for each month. Valid range_types are 'hour', 'day', 'week', 'month', and 'year'

  • :start (String)

    The start date for the time range to perform calculations in. The default start date is the start_of_business (can be specified in configuration). The format of the string must be as follows: 'YYYY-mm-dd'

  • :finish (String)

    The finish (or end) date for the time range to perform calculations in. The default finish date is the current date. The format of the string must be as follows: 'YYYY-mm-dd'

Returns:

  • (Array<Money, Fixnum>)

    Returns an array of Money objects for :sum and :average calculations, or an array of Fixnum for :count calculations. The array is indexed by the range_type. For example, if range_type is specified as 'month', each index in the array will represent a month.

Raises:



131
132
133
# File 'lib/double_entry/reporting.rb', line 131

def aggregate_array(function, , code, options = {})
  AggregateArray.new(function, , code, options)
end

#reconciled?(account) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is used by the concurrency test script.

Returns:

  • (Boolean)

    true if all the amounts for an account add up to the final balance, which they always should.



163
164
165
166
167
168
169
170
# File 'lib/double_entry/reporting.rb', line 163

def reconciled?()
  scoped_lines = Line.where(:account => "#{.identifier}")
  scoped_lines = scoped_lines.where(:scope => "#{.scope_identity}") if .scoped?
  sum_of_amounts = scoped_lines.sum(:amount)
  final_balance  = scoped_lines.order(:id).last[:balance]
  cached_balance = AccountBalance.()[:balance]
  final_balance == sum_of_amounts && final_balance == cached_balance
end

#scopes_with_minimum_balance_for_account(minimum_balance, account_identifier) ⇒ Array<Fixnum>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Identify the scopes with the given account identifier holding at least the provided minimum balance.

Examples:

Find users with at least $1,000,000 in their savings accounts

DoubleEntry::Reporting.(
  1_000_000.dollars,
  :savings,
) # might return the user ids: [ 1423, 12232, 34729 ]

Parameters:

  • minimum_balance (Money)

    Minimum account balance a scope must have to be included in the result set.

  • account_identifier (Symbol)

Returns:

  • (Array<Fixnum>)

    Scopes



148
149
150
151
152
153
154
155
# File 'lib/double_entry/reporting.rb', line 148

def (minimum_balance, )
  select_values(sanitize_sql_array([<<-SQL, , minimum_balance.cents])).map(&:to_i)
    SELECT scope
      FROM #{AccountBalance.table_name}
     WHERE account = ?
       AND balance >= ?
  SQL
end