Sha256: 369aa5146f220967e98acfae53a8eae28242b3dc60aa67035899ec9744c35d67

Contents?: true

Size: 1.3 KB

Versions: 26

Compression:

Stored size: 1.3 KB

Contents

module Spree
  module UserReporting
    extend DisplayMoney
    money_methods :lifetime_value, :average_order_value

    def report_values_for(report_name, store)
      store ||= Store.default

      completed_orders(store).pluck(:currency).uniq.each_with_object([]) do |currency, arr|
        arr << send("display_#{report_name}", store: store, currency: currency)
      end
    end

    def lifetime_value(**args)
      order_calculate(operation: :sum,
                      column: :total,
                      **args)
    end

    def average_order_value(**args)
      order_calculate(operation: :average,
                      column: :total,
                      **args)
    end

    def order_count(store = nil)
      store ||= Store.default
      order_calculate(store: store,
                      currency: store.supported_currencies.split(','),
                      operation: :count,
                      column: :all)
    end

    private

    def order_calculate(operation:, column:, store: nil, currency: nil)
      store ||= Store.default
      currency ||= store.default_currency

      completed_orders(store).where(currency: currency).calculate(operation, column) || BigDecimal('0.00')
    end

    def completed_orders(store)
      orders.for_store(store).complete.order(currency: :desc)
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/models/concerns/spree/user_reporting.rb
spree_core-4.10.0 app/models/concerns/spree/user_reporting.rb
spree_core-4.9.0 app/models/concerns/spree/user_reporting.rb
spree_core-4.8.3 app/models/concerns/spree/user_reporting.rb
spree_core-4.8.2 app/models/concerns/spree/user_reporting.rb
spree_core-4.7.3 app/models/concerns/spree/user_reporting.rb
spree_core-4.6.6 app/models/concerns/spree/user_reporting.rb
spree_core-4.5.5 app/models/concerns/spree/user_reporting.rb
spree_core-4.4.1 app/models/concerns/spree/user_reporting.rb
spree_core-4.5.4 app/models/concerns/spree/user_reporting.rb
spree_core-4.6.5 app/models/concerns/spree/user_reporting.rb
spree_core-4.7.2 app/models/concerns/spree/user_reporting.rb
spree_core-4.7.1 app/models/concerns/spree/user_reporting.rb
spree_core-4.6.4 app/models/concerns/spree/user_reporting.rb
spree_core-4.7.0 app/models/concerns/spree/user_reporting.rb
spree_core-4.6.3 app/models/concerns/spree/user_reporting.rb
spree_core-4.6.2 app/models/concerns/spree/user_reporting.rb
spree_core-4.6.1 app/models/concerns/spree/user_reporting.rb
spree_core-4.6.0 app/models/concerns/spree/user_reporting.rb
spree_core-4.5.3 app/models/concerns/spree/user_reporting.rb