Sha256: dcdb209eca02060f7d4d11f3686b11758519de9acc7d34c7f3f9826898b8ac0b

Contents?: true

Size: 1.54 KB

Versions: 39

Compression:

Stored size: 1.54 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) / units_sold
        end

        def previous_average_price
          return nil if previous_orders.zero?
          (previous_merchandise - previous_discounts) / previous_units_sold
        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

39 entries across 39 versions & 1 rubygems

Version Path
workarea-admin-3.5.15 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.36 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.14 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.35 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.13 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.34 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.12 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.33 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.11 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.10 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.32 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.9 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.31 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.8 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.30 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.7 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.29 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.6 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.28 app/view_models/workarea/admin/insights/product_view_model.rb