Class: StripeIIFToQBO::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/stripe-iiftoqbo.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Converter

Returns a new instance of Converter.


7
8
9
10
11
12
13
14
15
# File 'lib/stripe-iiftoqbo.rb', line 7

def initialize( options={} )
  @account_id = options[:account_id] if options[:account_id]
  @iif_file = options[:iif_file] if options[:iif_file]
  @payments_file = options[:payments_file] if options[:payments_file]
  @server_time = options[:server_time] || Date.today

  load_payments_file(@payments_file)
  load_iif_file(@iif_file)
end

Instance Method Details

#convert_iif_entry_to_ofx(iif_entry) ⇒ Object


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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/stripe-iiftoqbo.rb', line 44

def convert_iif_entry_to_ofx(iif_entry)
  ofx_entry = {}
  
  ofx_entry[:date] = iif_entry.date
  ofx_entry[:fitid] = iif_entry.memo
  ofx_entry[:accnt] = iif_entry.accnt
  ofx_entry[:trnstype] = iif_entry.trnstype
  ofx_entry[:memo] = iif_entry.memo

  case iif_entry.accnt

  when "Stripe Third-party Account"
    ofx_entry[:amount] = -iif_entry.amount
    ofx_entry[:name] = iif_entry.name
  when "Stripe Payment Processing Fees"
    ofx_entry[:amount] = -iif_entry.amount
    ofx_entry[:name] = "Stripe"
  when "Stripe Checking Account"
    ofx_entry[:amount] = -iif_entry.amount
    ofx_entry[:name] = "Transfer to #{iif_entry.accnt}"
  when "Stripe Sales"
    ofx_entry[:amount] = -iif_entry.amount
    
    if iif_entry.memo =~ /Stripe Connect fee/
      ofx_entry[:name] = "Stripe Connect Charge"
    elsif iif_entry.memo =~ /Charge/
      ofx_entry[:name] = "Credit Card Charge"
    else
      ofx_entry[:name] = iif_entry.accnt
    end
    
    ofx_entry[:memo] =~ /Charge ID: (.*)/
    charge_id = $1
    
    if @payments[charge_id]
      ofx_entry[:memo] = "#{@payments[charge_id]} #{iif_entry.memo}"
    end
    
  when "Stripe Returns"
    ofx_entry[:amount] = -iif_entry.amount
    ofx_entry[:name] = "Credit Card Refund"
  when "Stripe Account"
    return nil
  end

  return ofx_entry
end

#load_iif_file(iif_file) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stripe-iiftoqbo.rb', line 27

def load_iif_file(iif_file)
  @ofx_entries = []
      
  if iif_file
    IIF(iif_file) do |iif|
      iif.transactions.each do |transaction|
        transaction.entries.each do |iif_entry|
          ofx_entry = convert_iif_entry_to_ofx(iif_entry)
          if ofx_entry
            @ofx_entries.push( ofx_entry )
          end
        end
      end
    end
  end
end

#load_payments_file(payments_file) ⇒ Object


17
18
19
20
21
22
23
24
25
# File 'lib/stripe-iiftoqbo.rb', line 17

def load_payments_file(payments_file)
  @payments = {}
  
  if payments_file
    CSV.foreach(payments_file, :headers => true, :encoding => 'windows-1251:utf-8') do |row|
      @payments[row["id"]] = row["Description"] || ""
    end
  end
end

#to_csvObject


92
93
94
95
96
97
98
99
# File 'lib/stripe-iiftoqbo.rb', line 92

def to_csv
  rows = []
  rows.push(["Date", "Name", "Account", "Memo", "Amount"].to_csv)
  @ofx_entries.each do |ofx_entry|
    rows.push([ ofx_entry[:date].strftime("%m/%d/%Y"), ofx_entry[:name], ofx_entry[:accnt], "#{ofx_entry[:trnstype]} #{ofx_entry[:memo]}", ofx_entry[:amount].to_s('F') ].to_csv)
  end
  return rows.join
end

#to_qboObject


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/stripe-iiftoqbo.rb', line 101

def to_qbo
  min_date = nil
  max_date = nil
  
  @ofx_entries.each do |e|
    if e[:date]
      min_date = e[:date] if min_date.nil? or e[:date] < min_date
      max_date = e[:date] if max_date.nil? or e[:date] > max_date
    end
  end
  
  ofx_builder = OFX::Builder.new do |ofx|
    ofx.dtserver = @server_time
    ofx.fi_org = "Stripe"
    ofx.fi_fid = "0"
    ofx.bank_id = "123456789"
    ofx.acct_id = @account_id
    ofx.acct_type = "CHECKING"
    ofx.dtstart = min_date
    ofx.dtend = max_date
    ofx.bal_amt = 0
    ofx.dtasof = max_date
  end
  
  @ofx_entries.each do |ofx_entry|
    ofx_builder.transaction do |ofx_tr|
      ofx_tr.dtposted = ofx_entry[:date]
      ofx_tr.trnamt = ofx_entry[:amount]
      ofx_tr.fitid = ofx_entry[:fitid]
      ofx_tr.name = ofx_entry[:name]
      ofx_tr.memo = ofx_entry[:memo]
    end
  end
  
  return ofx_builder.to_ofx
end