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

Handle multiple ing accounts in single file #46

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
2 changes: 2 additions & 0 deletions lib/mt940_structured/file_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def group_lines
grouped_lines[-1] = [grouped_lines.last, line].join(@join_lines_by)
elsif get_header.parser.bank === 'Sns' && line.strip.empty?
grouped_lines[-1] = [grouped_lines.last, line].join(@join_lines_by)
elsif get_header.parser.bank === 'Ing' && line.match(/^({1:|{2:|{4:|-})/)
# ignore this line
else
grouped_lines[-1] = [grouped_lines.last, line].join
end
Expand Down
18 changes: 16 additions & 2 deletions lib/mt940_structured/parsers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module MT940Structured::Parsers
'86' => ['61', '62']
}

BANK_SPECIFIC_NEXT_LINES = {
'ing' => {
'86' => ['20'],
}
}

NO_NEXT_LINES = Set.new(['62', '64', '65'])

class Base
Expand Down Expand Up @@ -41,13 +47,21 @@ def validate_grouped_lines(lines)
next_line_type = next_line[1, 2]
current_line_type = current_line[1, 2]
next if NO_NEXT_LINES.include?(current_line_type)
possible_next_line_starts = @next_lines_for[current_line_type]
raise MT940Structured::InvalidFileContentError.new(%Q{Expected line of type #{possible_next_line_starts} after a #{current_line_type}, but was #{next_line_type}}) unless possible_next_line_starts.include?(next_line_type)
possible_next_line_starts = @next_lines_for[current_line_type] + bank_specific_next_lines(current_line_type)

raise MT940Structured::InvalidFileContentError.new(<<~EOS) unless possible_next_line_starts.include?(next_line_type)
Expected line of type #{possible_next_line_starts} after a #{current_line_type}, but was #{next_line_type}
Lines around:
#{lines.slice([0, index - 2].max, 5).join("\n")}
EOS
end
end
end

private
def bank_specific_next_lines(current_line_type)
BANK_SPECIFIC_NEXT_LINES.dig(bank.downcase, current_line_type) || []
end

def group_lines_by_tag(lines)
result = []
Expand Down
32 changes: 32 additions & 0 deletions spec/fixtures/ing/multiple_accounts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{1:F01INGBNL2ABXXX0000000000}
{2:I940INGBNL2AXXXN}
{4:
:20:P210916000000001
:25:V88888888EUR
:28C:00000
:60F:C191231EUR0,00
:61:2011271127D30000,00NTRFNONREF
/TRCD/00370/
:86:/CNTP/NL27INGB0003333333/////REMI/USTD//Overboeking naar beta
alrekening NL27INGB0003333333/
:61:2011271127D50000,00NTRFNONREF
/TRCD/00370/
:86:/CNTP/NL27INGB0003333333/////REMI/USTD//Overboeking naar beta
alrekening NL27INGB0003333333/
:62F:C201231EUR20000,00
:86:/SUM/2/2/80000,00/100000,00/
-}
{1:F01INGBNL2ABXXX0000000000}
{2:I940INGBNL2AXXXN}
{4:
:20:P210916000000001
:25:A12345678EUR
:28C:00000
:60F:C191231EUR10,00
:61:2011271127D15,00NTRFNONREF
/TRCD/00370/
:86:/CNTP/NL27INGB0003333333/////REMI/USTD//Overboeking naar beta
alrekening NL27INGB0003333333/
:62F:C201231EUR25,00
:86:/SUM/1/1/10,00/25,00/
-}
24 changes: 24 additions & 0 deletions spec/mt940_ing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -597,5 +597,29 @@
STRING
end

end

context 'multiple bank accounts in single file' do
before :each do
@file_name = File.dirname(__FILE__) + '/fixtures/ing/multiple_accounts.txt'
@bank_statements = MT940Structured::Parser.parse_mt940(@file_name)[account_number]
@transactions = @bank_statements.flat_map(&:transactions)
@transaction = @transactions.first
end

context 'account V88888888' do
let(:account_number) {'V88888888'}
it 'has the correct amount of transactions' do
expect(@transactions).to have(2).items
end
end

context 'account A12345678' do
let(:account_number) {'A12345678'}
it 'has the correct amount of transactions' do
expect(@transactions).to have(1).items
end
end

end
end