module DoubleEntry::Reporting

Constants

VERSION

Public Instance Methods

aggregate(function:, account:, code:, range:, partner_account: nil, filter: nil) click to toggle source

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.

@example 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'
    },
  ]
)

@param [Symbol] function The function to perform on the set of transfers.

Valid functions are :sum, :count, and :average

@param [Symbol] account The symbol identifying the account to perform

the aggregate calculation on. As specified in the account configuration.

@param [Symbol] code The application specific code for the type of

transfer to perform an aggregate calculation on. As specified in the
transfer configuration.

@param [DoubleEntry::Reporting::TimeRange] range Only include transfers in

the given time range in the calculation.

@param [Symbol] partner_account The symbol identifying the partner account

to perform the aggregate calculatoin on.  As specified in the account
configuration.

@param [Array<Hash<Symbol,Hash<Symbol,Object>>>] filter

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.

@return [Money, Integer] Returns a Money object for :sum and :average

calculations, or a Integer for :count calculations.

@raise [Reporting::AggregateFunctionNotSupported] The provided function

is not supported.
# File lib/double_entry/reporting.rb, line 85
def aggregate(function:, account:, code:, range:, partner_account: nil, filter: nil)
  Aggregate.formatted_amount(function: function, account: account, code: code, range: range,
                             partner_account: partner_account, filter: filter)
end
aggregate_array(function:, account:, code:, partner_account: nil, filter: nil, range_type: nil, start: nil, finish: nil) click to toggle source

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.

@example 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',
)

@param [Symbol] function The function to perform on the set of transfers.

Valid functions are :sum, :count, and :average

@param [Symbol] account The symbol identifying the account to perform

the aggregate calculation on. As specified in the account configuration.

@param [Symbol] code The application specific code for the type of

transfer to perform an aggregate calculation on. As specified in the
transfer configuration.

@param [Symbol] partner_account The symbol identifying the partner account

to perform the aggregative calculation on.  As specified in the account
configuration.

@param [Array<Symbol>, Array<Hash<Symbol, Object>>] filter

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.

@param [String] range_type 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'

@param [String] start 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'

@param [String] finish 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'

@return [Array<Money, Integer>] Returns an array of Money objects for :sum

and :average calculations, or an array of Integer 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.

@raise [Reporting::AggregateFunctionNotSupported] The provided function

is not supported.
# File lib/double_entry/reporting.rb, line 139
def aggregate_array(function:, account:, code:, partner_account: nil, filter: nil,
                    range_type: nil, start: nil, finish: nil)
  AggregateArray.new(function: function, account: account, code: code, partner_account: partner_account,
                     filter: filter, range_type: range_type, start: start, finish: finish)
end

Private Instance Methods

sanitize_sql_array(sql_array) click to toggle source
# File lib/double_entry/reporting.rb, line 150
def sanitize_sql_array(sql_array)
  ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
end