Sha256: 77be52fbd563bac6b6ee7a23014f13e56bb99ef6cf24bd4d05d4eaad6c530cef

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module Spree
  class PaymentMethodTransactionsReport < Spree::Report
    DEFAULT_SORTABLE_ATTRIBUTE = :payment_method_name
    HEADERS                    = { payment_method_name: :string, payment_amount: :integer }
    SEARCH_ATTRIBUTES          = { start_date: :payments_created_from, end_date: :payments_created_till }
    SORTABLE_ATTRIBUTES        = []

    class Result < Spree::Report::TimedResult
      charts PaymentMethodRevenueDistributionChart

      def build_empty_observations
        super
        @_payment_methods = @results.collect { |result| result['payment_method_name'] }.uniq
        @observations = @_payment_methods.collect do |payment_method_name|
          @observations.collect do |observation|
            _d_observation = observation.dup
            _d_observation.payment_amount = 0
            _d_observation.payment_method_name = payment_method_name
            _d_observation
          end
        end.flatten
      end

      class Observation < Spree::Report::TimedObservation
        observation_fields [:payment_method_name, :payment_amount]

        def describes?(result, time_scale)
          (result['payment_method_name'] == payment_method_name) && super
        end

        def payment_amount
          @payment_amount.to_f
        end
      end
    end

    def report_query
      Spree::Report::QueryFragments
        .from_subquery(payments)
        .group(*time_scale_columns_to_s, 'payment_method_name')
        .order(*time_scale_columns)
        .project(
          *time_scale_columns,
          'payment_method_name',
          'SUM(payment_amount) as payment_amount'
        )
    end

    private def payments
      Spree::PaymentMethod
        .joins(:payments)
        .where(spree_payments: { created_at: reporting_period })
        .select(
          *time_scale_selects('spree_payments'),
          'spree_payment_methods.name  as payment_method_name',
          'spree_payments.amount       as payment_amount',
        )
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spree_admin_insights-3.2.0 app/reports/spree/payment_method_transactions_report.rb
solidus_admin_insights-2.1.1 app/reports/spree/payment_method_transactions_report.rb
solidus_admin_insights-2.1.0 app/reports/spree/payment_method_transactions_report.rb