-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathynab_to_ledger.rb
46 lines (34 loc) · 938 Bytes
/
ynab_to_ledger.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
require "csv"
def main
csv = File.read(ARGV.first, encoding: "bom|utf-8")
output = process(csv)
File.open("ynab_ledger.dat", "w") { |f| f.write(output) }
end
def process(csv)
entries = CSV.parse(csv, headers: true).map do |row|
ledger_entry(row)
end
entries.compact.reverse.join("\n")
end
def ledger_entry(row)
inflow = blank_if_zero(row["Inflow"])
outflow = blank_if_zero(row["Outflow"])
return if inflow == "" && outflow == ""
month, day, year = row["Date"].split("/")
if row["Payee"].include?("Transfer :")
return if outflow == ""
source = row["Payee"].split(":").last.strip
else
source = row["Category Group/Category"]
end
return if source == ""
<<END
#{year}/#{month}/#{day} #{row["Payee"]}#{row["Memo"]}
#{source} #{outflow}
#{row["Account"]} #{inflow}
END
end
def blank_if_zero(amount)
amount =~ /\A(\$|€)?0(\.0+)?\z/ ? "" : amount
end
main if __FILE__ == $0