Sha256: 5fd09a80005ddc3f0b99eb0b612d919855c61d5ad6368f6f5ec0af3f118644ae

Contents?: true

Size: 1.4 KB

Versions: 11

Compression:

Stored size: 1.4 KB

Contents

module Workarea
  module Reports
    class SalesByCountry
      include Report

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

      def aggregation
        [filter, project_used_fields, group_by_country]
      end

      def filter
        {
          '$match' => {
            'reporting_on' => { '$gte' => starts_at, '$lte' => ends_at },
            '$or' => [
              { 'orders' => { '$gt' => 0 } },
              { 'units_sold' => { '$gt' => 0 } }
            ]
          }
        }
      end

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

      def group_by_country
        {
          '$group' => {
            '_id' => '$country',
            'orders' => { '$sum' => '$orders' },
            'units_sold' => { '$sum' => '$units_sold' },
            'merchandise' => { '$sum' => '$merchandise' },
            'shipping' => { '$sum' => '$shipping' },
            'discounts' => { '$sum' => '$discounts' },
            'tax' => { '$sum' => '$tax' },
            'revenue' => { '$sum' => '$revenue' }
          }
        }
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

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