Sha256: 4998cb7aeb674f519996f7740f0d7ae397d1f28d9bcb1ffcb338086e03c37fb6

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Spree
  class CartAdditionsReport < Spree::Report
    DEFAULT_SORTABLE_ATTRIBUTE = :product_name
    HEADERS                    = { sku: :string, product_name: :string, additions: :integer, quantity_change: :integer }
    SEARCH_ATTRIBUTES          = { start_date: :product_added_from, end_date: :product_added_to }
    SORTABLE_ATTRIBUTES        = [:product_name, :sku, :additions, :quantity_change]

    deeplink product_name: { template: %Q{<a href="/admin/products/{%# o.product_slug %}" target="_blank">{%# o.product_name %}</a>} }

    class Result < Spree::Report::Result
      class Observation < Spree::Report::Observation
        observation_fields [:product_name, :product_slug, :additions, :quantity_change, :sku]

        def sku
          @sku.presence || @product_name
        end
      end
    end

    def report_query
      Spree::CartEvent
        .added
        .joins(:variant)
        .joins(:product)
        .where(created_at: reporting_period)
        .group('product_name', 'product_slug', 'spree_variants.sku')
        .select(
          'spree_products.name             as product_name',
          'spree_products.slug             as product_slug',
          'spree_variants.sku              as sku',
          'count(spree_products.name)      as additions',
          'sum(spree_cart_events.quantity) as quantity_change'
        )
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spree_admin_insights-3.2.0 app/reports/spree/cart_additions_report.rb
solidus_admin_insights-2.1.1 app/reports/spree/cart_additions_report.rb
solidus_admin_insights-2.1.0 app/reports/spree/cart_additions_report.rb