Class: StripeInvoice::DJTaxReport

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/delayed_jobs/stripe_invoice/dj_tax_report.rb

Instance Method Summary collapse

Constructor Details

#initialize(year = 1.year.ago.year) ⇒ DJTaxReport

Returns a new instance of DJTaxReport.



3
4
5
# File 'app/delayed_jobs/stripe_invoice/dj_tax_report.rb', line 3

def initialize(year = 1.year.ago.year)
  @year = year
end

Instance Method Details

#performObject



8
9
10
11
12
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
# File 'app/delayed_jobs/stripe_invoice/dj_tax_report.rb', line 8

def perform
  puts "[#{self.class.name}##{__method__.to_s}] computing tax report for #{@year}"
  
  sicharges = StripeInvoice::Charge.where(
        "date >= ? and date <= ?", 
        date_to_epoch("#{@year}-01-01"), 
        date_to_epoch("#{@year}-12-31"))
        
  puts "[#{self.class.name}##{__method__.to_s}] number of charges in year: #{sicharges.size}"
  arr_data = []
  sicharges.each do |charge|
    owner = charge.owner
    
    next unless owner # skip if we don't have an owner
    
    country = get_country charge
    
    data = {
      charge: charge,
      country: country,
      tax_number: charge.tax_number,
      billing_address: charge.billing_address,
      bt: charge.balance_transaction_object,
      owner: owner,
    }
    puts "[#{self.class.name}##{__method__.to_s}] aggregating data #{charge.stripe_id}"
    if charge.indifferent_json[:refunds].count > 0
      arr_refunds = []
      charge.indifferent_json[:refunds].each do |refund|
        refd = {
          refund: refund,
          bt: Stripe::BalanceTransaction.retrieve(refund[:balance_transaction])
        }
        arr_refunds << refd
      end
      
      data[:refunds] = arr_refunds
      #puts "[DJTaxReport] charge had refunds: #{arr_refunds}"
    end # has refunds
    
    arr_data << data
  end
  
  puts "[#{self.class.name}##{__method__.to_s}] data collected; rendering view"
  #res = InvoicesController.new.tax_report arr_data
  res = Renderer.render template: "stripe_invoice/invoices/tax_report", 
        locals: {sicharges: arr_data, 
          year: @year, 
          totals: totals(arr_data), 
          volume_per_tax_number: volume_per_tax_number(arr_data)}, 
        formats: [:pdf]
  
  InvoiceMailer.tax_report(res).deliver! #unless ::Rails.env.development?
end