Sha256: 37df20ec738d4881dde45f3984fd6a94c383ad8f4daa9eb8f223efca05ba487b

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 KB

Contents

module Workarea
  module Reports
    class AverageOrderValue
      include Report
      include GroupByTime

      self.reporting_class = Metrics::SalesByDay
      self.sort_fields = %w(_id revenue orders average_order_value)

      def aggregation
        [filter_date_range_and_zeroes, group_by_time, add_average_order_value]
      end

      def filter_date_range_and_zeroes
        {
          '$match' => {
            'reporting_on' => { '$gte' => starts_at, '$lte' => ends_at },
            'orders' => { '$gt' => 0 },
            'revenue' => { '$gt' => 0 }
          }
        }
      end

      def group_by_time
        {
          '$group' => {
            '_id' => time_group_id,
            'starts_at' => { '$min' => '$reporting_on' },
            'orders' => { '$sum' => '$orders' },
            'revenue' => { '$sum' => '$revenue' }
          }
        }
      end

      def add_average_order_value
        {
          '$addFields' => {
            'average_order_value' => { '$divide' => ['$revenue', '$orders'] }
          }
        }
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
workarea-core-3.4.22 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.0 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.21 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.5.0.beta.1 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.20 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.19 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.18 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.17 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.16 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.15 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.14 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.13 app/queries/workarea/reports/average_order_value.rb
workarea-core-3.4.12 app/queries/workarea/reports/average_order_value.rb