Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added account and balance classes #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mt940.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'mt940/bic_codes'
require 'mt940/parser'
require 'mt940/base'
require 'mt940/account'
require 'mt940/transaction'
require 'mt940/banks/ing'
require 'mt940/banks/rabobank'
Expand Down
20 changes: 20 additions & 0 deletions lib/mt940/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module MT940

class Account

attr_accessor :number, :currency, :opening_balance, :closing_balance

def initialize
@opening_balance = MT940::Balance.new
@closing_balance = MT940::Balance.new
end
end

class Balance

attr_accessor :date, :amount

end


end
20 changes: 15 additions & 5 deletions lib/mt940/banks/ing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@ class MT940::Ing < MT940::Base
private

def parse_line_before_sepa
pattern = Regexp.new "(#{MT940::BBAN_PATTERN})(.+)"
pattern = Regexp.new "(#{MT940::BBAN_PATTERN}|#{MT940::IBAN_PATTERN}|\s)(.+)"
if @line.match(pattern)
@description = $2.strip
@contra_account = $1[/[^0+]\d*/]
@contra_account = $1 #[/[^0+]\d*/]
end
end

def parse_line_after_sepa
if @line.match(MT940::SEPA_PATTERN)
@contra_account = $2
@description = $4.strip
pattern = Regexp.new "(#{MT940::SEPA_PATTERN})(.+)"
if @line.match(pattern)
@description = $2.strip
@contra_account = $1
end
end

def parse_tag_86
if @line.match(/^:86:(.*)$/)
@line = $1.strip
sepa? ? parse_line_after_sepa : parse_line_before_sepa
@transaction.contra_account = @contra_account
@transaction.description = @description
end
end

def sepa?
@line.match(MT940::SEPA_PATTERN)
end
Expand Down
1 change: 1 addition & 0 deletions lib/mt940/banks/rabobank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def parse_tag_25
else
@bank_account = $1.gsub(/^0/,'') if @line.match(/^:\d{2}:[^\d]*(\d*)/)
end
@account.number = @bank_account
end

def parse_tag_61
Expand Down
34 changes: 27 additions & 7 deletions lib/mt940/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ module MT940

class Base

attr_accessor :bank, :transactions
attr_accessor :bank, :transactions, :account

def initialize(file)
@transactions, @lines = [], []
@bank = self.class.to_s.split('::').last
@account = MT940::Account.new

file.readlines.each do |line|
begin_of_line?(line) ? @lines << line : @lines[-1] += line
end
Expand All @@ -32,7 +34,7 @@ def parse
when '86'
parse_tag_86 if @transaction
when '62F'
@transaction = nil #Ignore 'eindsaldo'
parse_tag_62F
end
end
end
Expand All @@ -47,12 +49,27 @@ def begin_of_line?(line)
def parse_tag_25
@line.gsub!('.','')
@bank_account = $1.gsub(/^0/,'') if @line.match(/^:\d{2}:[^\d]*(\d*)/)
@account.number = @bank_account
end

# opening balance
def parse_tag_60F
@currency = @line[12..14]
debet_credit = @line[5].eql?("D") ? -1 : 1
@account.opening_balance.date = Date.strptime(@line[6..11], "%y%m%d")
@account.opening_balance.amount = debet_credit * @line[15..-1].gsub(',', '.').to_f
@account.currency = @line[12..14]
@currency = @account.currency
end

# closing balance
def parse_tag_62F
debet_credit = @line[5].eql?("D") ? -1 : 1
@account.closing_balance.date = Date.strptime(@line[6..11], "%y%m%d")
@account.closing_balance.amount = debet_credit * @line[15..-1].gsub(',', '.').to_f
@account.currency = @line[12..14]
@currency = @account.currency
end

def parse_tag_61(pattern = nil)
pattern = pattern || /^:61:(\d{6})(C|D)(\d+),(\d{0,2})/
match = @line.match(pattern)
Expand All @@ -72,6 +89,7 @@ def parse_tag_86
end
end


def hashify_description(description)
hash = {}
description.gsub!(/[^A-Z]\/[^A-Z]/,' ') #Remove single forward slashes '/', which are not part of a swift code
Expand All @@ -83,10 +101,12 @@ def hashify_description(description)

def create_transaction(match)
type = match[2] == 'D' ? -1 : 1
MT940::Transaction.new(:bank_account => @bank_account,
:amount => type * (match[3] + '.' + match[4]).to_f,
:bank => @bank,
:currency => @currency)
MT940::Transaction.new(
:bank_account => @bank_account,
:amount => type * (match[3] + '.' + match[4]).to_f,
:bank => @bank,
:currency => @currency
)
end

def parse_date(string)
Expand Down
3 changes: 2 additions & 1 deletion lib/mt940/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UnknownBank < Exception; end

class Parser

attr_accessor :transactions
attr_accessor :transactions, :account

def initialize(file)
file = File.open(file) if file.is_a?(String)
Expand All @@ -25,6 +25,7 @@ def process(file)
instance = bank_class.new(file)
instance.parse
@transactions = instance.transactions
@account = instance.account
rescue NoMethodError => exception
if exception.message == "undefined method `new' for nil:NilClass"
raise UnknownBank.new('Could not determine bank!')
Expand Down
35 changes: 35 additions & 0 deletions test/mt940_base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,40 @@ class TestMt940Base < Test::Unit::TestCase
end
end
end


context 'MT940::Account' do

setup do
# Fixture contains a mixture of transactions with and without iban numbers
file_name = File.dirname(__FILE__) + '/fixtures/ing_sepa.txt'
@parser = MT940::Parser.new(file_name)
@transactions = @parser.transactions
end

should 'have the correct accountnumber' do
assert_equal "654321789", @parser.account.number
end

should 'have the correct currency' do
assert_equal "EUR", @parser.account.currency
end

should 'have the correct opening balance' do
assert_equal "2012-08-10", @parser.account.opening_balance.date.to_s
assert_equal 68.20 , @parser.account.opening_balance.amount
end

should 'have the correct closing balance' do
assert_equal "2012-08-11", @parser.account.closing_balance.date.to_s
assert_equal 1005.83 , @parser.account.closing_balance.amount
end

# the difference between the opening and closing balance should equal the net change of transactions
should 'calculate the correct difference between the opening and closing balance' do
assert_equal @transactions.map(&:amount).reduce(:+), @parser.account.closing_balance.amount - @parser.account.opening_balance.amount
end

end

end