diff --git a/lib/cmxl/fields/transaction.rb b/lib/cmxl/fields/transaction.rb index bce207e..d533c54 100644 --- a/lib/cmxl/fields/transaction.rb +++ b/lib/cmxl/fields/transaction.rb @@ -59,13 +59,19 @@ def date end def entry_date - if data['entry_date'] && date - if date.month == 1 && date.month < to_date(data['entry_date'], date.year).month - to_date(data['entry_date'], date.year - 1) - else - to_date(data['entry_date'], date.year) - end - end + return if !date || !data['entry_date'] + + e_date = to_date(data['entry_date'], date.year) + # we assume that valuta (date) and entry_date have a close connection. so valuta and entry_date should not be + # further apart than one month. this leads to some edge cases + + # valuta is in january while entry_date is in december => entry_date was done the year before + e_date = to_date(data['entry_date'], date.year - 1) if date.month == 1 && e_date.month == 12 + + # valuta is in december but entry_date is in january => entry_date is actually in the year after valuta + e_date = to_date(data['entry_date'], date.year + 1) if date.month == 12 && e_date.month == 1 + + e_date end def supplementary diff --git a/spec/fields/transaction_spec.rb b/spec/fields/transaction_spec.rb index 56b1369..75c26c8 100644 --- a/spec/fields/transaction_spec.rb +++ b/spec/fields/transaction_spec.rb @@ -1,12 +1,14 @@ require 'spec_helper' describe Cmxl::Fields::Transaction do - subject(:debit_transaction) { Cmxl::Fields::Transaction.parse(fixture.first) } - subject(:storno_credit_transaction) { Cmxl::Fields::Transaction.parse(fixture.last) } + subject(:debit_transaction) { Cmxl::Fields::Transaction.parse(fixture[0]) } + subject(:storno_credit_transaction) { Cmxl::Fields::Transaction.parse(fixture[1]) } subject(:ocmt_transaction) { Cmxl::Fields::Transaction.parse(fixture_line(:statement_ocmt)) } subject(:ocmt_cghs_transaction) { Cmxl::Fields::Transaction.parse(fixture_line(:statement_ocmt_chgs)) } subject(:supplementary_transaction) { Cmxl::Fields::Transaction.parse(fixture_line(:statement_supplementary_plain)) } subject(:complex_supplementary_transaction) { Cmxl::Fields::Transaction.parse(fixture_line(:statement_supplementary_complex)) } + subject(:valuta_after_enty_date) { Cmxl::Fields::Transaction.parse(fixture[3]) } + subject(:entry_before_valuta_transaction) { Cmxl::Fields::Transaction.parse(fixture[2]) } let(:fixture) { fixture_line(:statement_line).split(/\n/) } @@ -77,4 +79,16 @@ it { expect(complex_supplementary_transaction.bank_reference).to eql('HERP-DERP-REF') } it { expect(complex_supplementary_transaction.supplementary.source).to eql('random text / and stuff') } end + + context 'valuta and entry-date assumptions' do + it 'entry_date before valuta is recognized correclty when including year-change' do + expect(entry_before_valuta_transaction.date).to eql(Date.new(2014, 1, 10)) + expect(entry_before_valuta_transaction.entry_date).to eql(Date.new(2013, 12, 24)) + end + + it 'entry_date after valuta is recognized correctly when including year-change' do + expect(valuta_after_enty_date.date).to eql(Date.new(2014, 12, 24)) + expect(valuta_after_enty_date.entry_date).to eql(Date.new(2015, 1, 2)) + end + end end diff --git a/spec/fixtures/lines/statement_line.txt b/spec/fixtures/lines/statement_line.txt index a196d1c..e2a201f 100644 --- a/spec/fixtures/lines/statement_line.txt +++ b/spec/fixtures/lines/statement_line.txt @@ -1,2 +1,4 @@ :61:1409010902DR000000000001,62NTRF0000549855700010//025498557/000001 :61:1409010902RCR000000000001,62NTRF0000549855700010//025498557/000001 +:61:1401101224CR000000000001,62NTRF0000549855700010//025498557/000001 +:61:1412240102CR000000000001,62NTRF0000549855700010//025498557/000001