Sha256: e34c1700bf8190f1d7105f3353c2e941e2556f612522289cc742fe3f6f382326

Contents?: true

Size: 1.89 KB

Versions: 23

Compression:

Stored size: 1.89 KB

Contents

module Workarea
  module Reports
    class SalesOverTime
      include Report
      include GroupByTime

      self.reporting_class = Metrics::SalesByDay
      self.sort_fields = %w(_id units_sold orders customers merchandise discounts shipping tax revenue)

      def aggregation
        [filter, project_used_fields, group_by_time, add_aov]
      end

      def filter
        {
          '$match' => {
            'reporting_on' => { '$gte' => starts_at.utc, '$lte' => ends_at.utc },
            'orders' => { '$gt' => 0 }
          }
        }
      end

      def project_used_fields
        {
          '$project' => {
            'reporting_on' => 1,
            'orders' => 1,
            'returning_orders' => 1,
            'customers' => 1,
            'units_sold' => 1,
            'merchandise' => 1,
            'shipping' => 1,
            'discounts' => 1,
            'tax' => 1,
            'revenue' => 1
          }
        }
      end

      def group_by_time
        {
          '$group' => {
            '_id' => time_group_id,
            'starts_at' => { '$min' => '$reporting_on' },
            'orders' => { '$sum' => '$orders' },
            'returning_orders' => { '$sum' => '$returning_orders' },
            'customers' => { '$sum' => '$customers' },
            'units_sold' => { '$sum' => '$units_sold' },
            'merchandise' => { '$sum' => '$merchandise' },
            'shipping' => { '$sum' => '$shipping' },
            'discounts' => { '$sum' => '$discounts' },
            'tax' => { '$sum' => '$tax' },
            'revenue' => { '$sum' => '$revenue' }
          }
        }
      end

      def add_aov
        {
          '$addFields' => {
            'aov' => {
              '$cond' => [
                { '$eq' => ['$orders', 0] },
                0,
                { '$divide' => ['$revenue', '$orders'] }
              ]
            }
          }
        }
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.44 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.43 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.42 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.41 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.40 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.39 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.38 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.37 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.36 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.35 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.34 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.33 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.32 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.31 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.30 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.29 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.28 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.27 app/queries/workarea/reports/sales_over_time.rb
workarea-core-3.4.26 app/queries/workarea/reports/sales_over_time.rb