Sha256: cb1f1481ca6bca077bfef01e9cfbbeda5c8247ac28b136a6504d76490f5ec520

Contents?: true

Size: 1.53 KB

Versions: 23

Compression:

Stored size: 1.53 KB

Contents

module Workarea
  module Admin
    module Insights
      class ProductViewModel < ApplicationViewModel
        include InsightsDetails

        insights_scope -> { Metrics::ProductByDay.by_product(model.id) }

        add_sparkline :orders
        add_summaries :views, :orders, :revenue, :units_sold, :merchandise, :discounts
        add_graph_data :views, :orders, :revenue, :units_sold

        def feed
          @feed ||= InsightViewModel.wrap(
            Workarea::Insights::Base.by_product(model.id).to_a
          )
        end

        def top?
          Workarea::Insights::TopProducts.current.include?(product_id: model.id)
        end

        def trending?
          Workarea::Insights::TrendingProducts.current.include?(product_id: model.id)
        end

        def skus
          @skus ||= skus_report.results.map { |r| OpenStruct.new(r) }
        end

        def skus_report
          @skus_report ||= Workarea::Reports::SalesBySku.new(
            skus: model.skus,
            starts_at: starts_at,
            ends_at: ends_at
          )
        end

        def average_price
          return nil if orders.zero?
          (merchandise - discounts) / orders
        end

        def previous_average_price
          return nil if previous_orders.zero?
          (previous_merchandise - previous_discounts) / previous_orders
        end

        def average_price_percent_change
          return nil if previous_average_price.blank?
          calculate_percent_change(previous_average_price, average_price)
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
workarea-admin-3.5.5 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.27 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.4 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.26 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.3 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.25 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.2 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.24 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.1 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.23 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.22 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.0 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.21 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.0.beta.1 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.20 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.19 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.18 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.17 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.16 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.15 app/view_models/workarea/admin/insights/product_view_model.rb