Sha256: b5f15dfd23d004f272652e016fbe5bcfafd31d3bb80035a04db390f88704e4c4

Contents?: true

Size: 1.45 KB

Versions: 23

Compression:

Stored size: 1.45 KB

Contents

module Workarea
  module Reports
    class SalesBySku
      include Report

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

      def skus
        Array.wrap(params[:skus])
      end

      def aggregation
        [filter, project_used_fields, group_by_sku]
      end

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

        result['$match']['sku'] = { '$in' => skus } if skus.present?
        result
      end

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

      def group_by_sku
        {
          '$group' => {
            '_id' => '$sku',
            'orders' => { '$sum' => '$orders' },
            'units_sold' => { '$sum' => '$units_sold' },
            'merchandise' => { '$sum' => '$merchandise' },
            'discounts' => { '$sum' => '$discounts' },
            'tax' => { '$sum' => '$tax' },
            'revenue' => { '$sum' => '$revenue' }
          }
        }
      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_by_sku.rb
workarea-core-3.4.44 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.43 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.42 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.41 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.40 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.39 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.38 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.37 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.36 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.35 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.34 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.33 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.32 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.31 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.30 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.29 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.28 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.27 app/queries/workarea/reports/sales_by_sku.rb
workarea-core-3.4.26 app/queries/workarea/reports/sales_by_sku.rb