Sha256: ce865c6ce30be6de639e19350885ef9a04d06c85f74a4fe24f585ad0929ef00c

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

# Revenue: Payment Providers

module Admin
  class ReportPaymentProvidersDatatable < Effective::Datatable
    filters do
      filter :start_date, nil, as: :date
      filter :end_date, nil, as: :date
    end

    datatable do
      length 250

      col :payment_provider

      col :sales, as: :price
      col :returns, as: :price
      col :total, as: :price

      col :start_date, as: :date, search: false, sort: false, visible: false do
        date_range.begin&.strftime('%F')
      end

      col :end_date, as: :date, search: false, sort: false, visible: false do
        date_range.end&.strftime('%F')
      end

      aggregate :total
    end

    collection do
      start_date = date_range.begin&.strftime('%F')
      end_date = date_range.end&.strftime('%F')

      orders = Effective::Order.purchased.where(purchased_at: date_range).where('total != 0')
      order_items = Effective::OrderItem.where(order_id: orders).includes(:purchasable, order: :user)

      payment_providers.map do |provider|
        items = order_items.select { |item| item.order.payment_provider == provider }

        [
          provider,
          items.sum { |item| (item.total > 0 ? item.total : 0) }.to_i,
          items.sum { |item| (item.total < 0 ? item.total : 0) }.to_i,
          items.sum { |item| item.total }.to_i,
          start_date,
          end_date
        ]
      end
    end

    def payment_providers
      @payment_providers ||= EffectiveOrders.payment_providers - ['free', 'pretend']
    end

    def date_range
      @date_range ||= (filters[:start_date].presence)..(filters[:end_date].presence)
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
effective_orders-6.1.5 app/datatables/admin/report_payment_providers_datatable.rb
effective_orders-6.1.4 app/datatables/admin/report_payment_providers_datatable.rb
effective_orders-6.1.3 app/datatables/admin/report_payment_providers_datatable.rb
effective_orders-6.1.2 app/datatables/admin/report_payment_providers_datatable.rb
effective_orders-6.1.1 app/datatables/admin/report_payment_providers_datatable.rb
effective_orders-6.1.0 app/datatables/admin/report_payment_providers_datatable.rb