Sha256: 0c3624ae5828db4c6ed269445d9e83ad20d009db681f89bd6ac9e3085c3bea40
Contents?: true
Size: 1.45 KB
Versions: 3
Compression:
Stored size: 1.45 KB
Contents
module Spree class BestSellingProductsReport < Spree::Report DEFAULT_SORTABLE_ATTRIBUTE = :sold_count HEADERS = { sku: :string, product_name: :string, sold_count: :integer } SEARCH_ATTRIBUTES = { start_date: :orders_completed_from, end_date: :orders_completed_to } SORTABLE_ATTRIBUTES = [:product_name, :sku, :sold_count] 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, :sku, :sold_count] def sku @sku.presence || @product_name end end end def report_query Spree::LineItem .joins(:order) .joins(:variant) .joins(:product) .where(Spree::Product.arel_table[:name].matches(search_name)) .where(spree_orders: { state: 'complete' }) .where(spree_orders: { completed_at: reporting_period }) .group(:variant_id, :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', 'sum(quantity) as sold_count' ) end private def search_name search[:name].present? ? "%#{ search[:name] }%" : '%' end end end
Version data entries
3 entries across 3 versions & 2 rubygems