Skip to content

Commit

Permalink
Valuta & entry date assumptions (#32)
Browse files Browse the repository at this point in the history
* updates valuta date & entry date calculation for transations
* adds specs for valuta and entry_date assumptions
  • Loading branch information
Uepsilon authored Jun 18, 2019
1 parent 19a5e44 commit ae8694f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
20 changes: 13 additions & 7 deletions lib/cmxl/fields/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions spec/fields/transaction_spec.rb
Original file line number Diff line number Diff line change
@@ -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/) }

Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions spec/fixtures/lines/statement_line.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
:61:1409010902DR000000000001,62NTRF0000549855700010//025498557/000001
:61:1409010902RCR000000000001,62NTRF0000549855700010//025498557/000001
:61:1401101224CR000000000001,62NTRF0000549855700010//025498557/000001
:61:1412240102CR000000000001,62NTRF0000549855700010//025498557/000001

0 comments on commit ae8694f

Please sign in to comment.