Skip to content

Instantly share code, notes, and snippets.

@jayschab
Created June 28, 2017 17:13
Show Gist options
  • Save jayschab/f904e36fca0b91ede50c3ba6b5c21b6c to your computer and use it in GitHub Desktop.
Save jayschab/f904e36fca0b91ede50c3ba6b5c21b6c to your computer and use it in GitHub Desktop.
Convert Itaú CSV extrato to OFX
# Modified from https://github.com/fgrehm/itau_to_ofx
def write_transaction(txn)
type = txn[:amount] > 0 ? 'CREDIT' : 'DEBIT'
date = txn[:date].strftime('%Y%m%d100000[-03:EST]')
id = txn[:date].strftime("%Y%m%d#{txn[:inc].to_s.rjust(3, '0')}")
memo = txn[:description]
amount = '%.2f' % txn[:amount]
"<STMTTRN>
<TRNTYPE>#{type}
<DTPOSTED>#{date}
<TRNAMT>#{amount}
<FITID>#{id}
<CHECKNUM>#{id}
<MEMO>#{memo}
</STMTTRN>"
end
require 'csv'
csv = CSV.read("./dump.csv")
keys = csv.shift
keys = keys.map(&:to_sym)
txns = csv.map {|arr| keys.zip(arr).to_h}
txns.each {|a| a[:amount] = a[:amount].to_f}
txns.each {|a| a[:date] = Date.strptime(a[:date], "%d/%m") }
gs = txns.group_by{|x| x[:date]}
ordered_txns = gs.values.each {|group| inc = 1; group.each {|y| y[:inc] = inc; inc+=1} }.flatten
puts ordered_txns.map {|a| write_transaction(a) }.join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment