Class: Spree::AdvancedReport::TransactionReport

Inherits:
Spree::AdvancedReport show all
Includes:
ActionView::Helpers::NumberHelper, ActionView::Helpers::UrlHelper
Defined in:
lib/spree/advanced_report/transaction_report.rb

Instance Attribute Summary

Attributes inherited from Spree::AdvancedReport

#data, #date_text, #orders, #params, #product, #product_in_taxon, #product_text, #ruportdata, #search, #taxon, #taxon_text, #unfiltered_params

Instance Method Summary collapse

Methods inherited from Spree::AdvancedReport

#date_range, #download_url, #order_count, #profit, #revenue, #units

Constructor Details

#initialize(params) ⇒ TransactionReport

Returns a new instance of TransactionReport.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/spree/advanced_report/transaction_report.rb', line 13

def initialize(params)
  super(params)

  self.ruportdata = Ruport::Data::Group.new({
    :name => "#{self.name} #{self.date_range}",
    :column_names => %w[date type id total state]
  })

  card_listing = {}

  orders.each do |order|
    order.payments.each do |payment|
      # create a direct link for easy inspection
      gateway_link = payment.response_code

      if payment.payment_method.type == "Spree::Gateway::AuthorizeNetCim" and payment.response_code.present?
        gateway_link = link_to(payment.response_code, "https://account.authorize.net/UI/themes/anet/transaction/transactiondetail.aspx?transID=#{payment.response_code}", target: '_blank')
      end

      if payment.source.class == Spree::Creditcard
        credit_cart_type = payment.source.cc_type
      else
        # it seems as though a previous payment can be used a payment type
        # links to the previous payment's credit card
        credit_cart_type = payment.source.source.cc_type
      end

      (card_listing[credit_cart_type] ||= []) << {
        "date" => payment.source.created_at.to_formatted_s(:db),
        "type" => credit_cart_type.humanize.titlecase,
        "id" => gateway_link,
        "total" => payment.amount,
        "state" => payment.state,
      }
    end
  end

  @sales_total = 0

  card_listing.keys.sort.each do |card_name|
    card_total = card_listing[card_name].map { |c| c["total"] * (c["state"] == 'void' ? 0 : 1) }.sum
    @sales_total += card_total

    ruportdata << {
      "date" => "<b>#{card_name.humanize.titlecase} (#{card_listing[card_name].count}): #{number_to_currency(card_total)}</b>"
    }

    card_listing[card_name].each do |transaction|
      transaction["total"] = number_to_currency(transaction["total"])
      ruportdata << transaction
    end
  end
  
  ruportdata << { "date" => "<b>Sales Total: #{number_to_currency(@sales_total)}</b>" }

  # spaces don't seem to work in column names (ruport is old...)
  ruportdata.rename_column("date", "Transaction Date")
  ruportdata.rename_column("type", "Card Type")
  ruportdata.rename_column("id", "Transaction ID")
  ruportdata.rename_column("total", "Payment Total")
  ruportdata.rename_column("state", "Payment State")
end

Instance Method Details

#descriptionObject



9
10
11
# File 'lib/spree/advanced_report/transaction_report.rb', line 9

def description
  "Top purchasing customers, calculated by revenue"
end

#nameObject



5
6
7
# File 'lib/spree/advanced_report/transaction_report.rb', line 5

def name
  "Transaction Report"
end

#sales_totalObject



76
77
78
# File 'lib/spree/advanced_report/transaction_report.rb', line 76

def sales_total
  @sales_total
end