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.27 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.26 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.45 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.25 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.23 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.44 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.22 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.43 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.21 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.42 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.20 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.41 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.19 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.40 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.18 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.39 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.17 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.38 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.5.16 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.37 app/view_models/workarea/admin/insights/product_view_model.rb