This repository has been archived by the owner on Jun 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank2ynab.rb
executable file
·76 lines (65 loc) · 1.87 KB
/
bank2ynab.rb
1
2
3
4
5
6
7
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env ruby
# Small script which converts exported bank account history to YNAB compatible
# csv format.
#
# Sample usage:
# The following will create a new file called `ynab_compatible.csv` with the
# converted account history from `exported.csv`
#
# $ cat exported.csv | ./bank2ynab.rb > ynab_compatible.csv budapestbank
#
#
# Supported banks:
# * Budapest Bank
#
require 'csv'
require 'bigdecimal'
class BankCsvRow < BasicObject
attr_reader :row
def initialize(csv_row)
@row = csv_row
end
def self.method_missing(m, *args, &blk)
define_method(m, &blk)
end
end
class BudapestBank
def self.convert(io)
new(io)
end
def initialize(io)
@csv_content = io.read.force_encoding("ISO-8859-1").encode("UTF-8")
end
def to_ynab_csv
puts 'Date,Payee,Category,Memo,Outflow,Inflow'
CSV.parse(@csv_content, headers: true) do |row|
print CsvRow.new(row).to_ynab_row
end
end
class CsvRow < BankCsvRow
date { ::Date.parse(row['Értéknap']).strftime('%d/%m/%Y') }
category { row['Tranzakció'].to_s.strip }
withdraw? { amount < 0 }
credit_debit { row['Credit/Debit'] == 'D' }
currency { row['Deviza'] }
amount { ::BigDecimal.new(row['Összeg']) }
refno { row['Referenciaszám']}
memo1 { row['Közlemény 1'].to_s.strip }
memo2 { row['Közlemény 2'].to_s.strip }
memo3 { row['Közlemény 3'].to_s.strip }
memo23 { [memo2, memo3].join(' ') }
outflow { amount.abs.to_s('F') if withdraw? }
inflow { amount.to_s('F') unless withdraw? }
def to_ynab_row
::CSV.generate { |io| io << [date, memo1, category, memo23, outflow, inflow] }
end
end
end
if __FILE__ == $0
case ARGV[0]
when 'budapestbank', 'bb', nil
BudapestBank.convert($stdin).to_ynab_csv
else
puts "Don't know about that format!"
end
end