diff --git a/CHANGELOG.mdown b/CHANGELOG.mdown index 332cc64..6ab22d7 100644 --- a/CHANGELOG.mdown +++ b/CHANGELOG.mdown @@ -1,5 +1,10 @@ # NEXT release +# 2.0 +- `[REFACTOR]` **DEPRECATED:** `storno?` and related methods `storno_credit`, `storno_debit`. Use `reversal?` and related methods `reversal_credit?`, `reversal_debit?` instead +- `[BUGFIX]` **DEPRECATED:** `funds_code` method returns the `credit_debit_indicator` from the SWIFT definition. Therefore the method is deprecated in favor of `credit_debit_indicator` method +- `[HOUSEKEEPING]` [Replace Travis CI with github actions](https://github.com/railslove/cmxl/pull/57) + # 1.5.0 - `[BUGFIX]` fix potential bug when generation_date is not provided in field 20 and 13 (issue: [#35](https://github.com/railslove/cmxl/issues/35) PR: [#36](https://github.com/railslove/cmxl/pull/36)) diff --git a/README.md b/README.md index 14d5fac..2474f44 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ statements.each do |s| puts t.information puts t.description puts t.entry_date - puts t.funds_code + puts t.credit_debit_indicator puts t.credit? puts t.debit? puts t.sign # -1 if it's a debit; 1 if it's a credit diff --git a/lib/cmxl/fields/transaction.rb b/lib/cmxl/fields/transaction.rb index dd4ba05..4676548 100644 --- a/lib/cmxl/fields/transaction.rb +++ b/lib/cmxl/fields/transaction.rb @@ -2,7 +2,7 @@ module Cmxl module Fields class Transaction < Field self.tag = 61 - self.parser = %r{^(?\d{6})(?\d{4})?(?R?)(?[CD]{1})(?[a-zA-Z])?(?\d{1,12},\d{0,2})(?(?:N|F|S).{3})(?NONREF|(.(?!\/\/)){,16}([^\/]){,1})((?:\/\/)(?[^\n]{,16}))?((?:\n)(?.{,34}))?$} + self.parser = %r{^(?\d{6})(?\d{4})?(?D|C|RD|RC|ED|EC)(?[a-zA-Z])?(?\d{1,12},\d{0,2})(?(?:N|F|S).{3})(?NONREF|(.(?!\/\/)){,16}([^\/]){,1})((?:\/\/)(?[^\n]{,16}))?((?:\n)(?.{,34}))?$} attr_accessor :details @@ -15,31 +15,63 @@ def sha end def credit? - data['funds_code'].to_s.casecmp('C').zero? + credit_debit_indicator.include?('C') end def debit? - data['funds_code'].to_s.casecmp('D').zero? + credit_debit_indicator.include?('D') end def storno_credit? + warn "[DEPRECATION] `storno_credit?` is deprecated. Please use `reversal_credit?` instead. It will be removed in version 3.0." + reversal_credit? + end + + def reversal_credit? credit? && storno? end def storno_debit? + warn "[DEPRECATION] `storno_debit?` is deprecated. Please use `reversal_debit?` instead. It will be removed in version 3.0." + reversal_debit? + end + + def reversal_debit? debit? && storno? end def storno? - !storno_flag.empty? + warn "[DEPRECATION] `storno?` is deprecated. Please use `reversal?` instead. It will be removed in version 3.0." + reversal? + end + + def reversal? + credit_debit_indicator.include?('R') + end + + def expected_credit? + credit? && expected? + end + + def expected_debit? + debit? && expected? + end + + def expected? + credit_debit_indicator.include?('E') + end + + def credit_debit_indicator + data['credit_debit_indicator'].to_s end def funds_code - data.values_at('storno_flag', 'funds_code').join + warn "[DEPRECATION] `funds_code` is deprecated. Please use `credit_debit_indicator` instead. It will be removed in version 3.0." + data['credit_debit_indicator'].to_s end def storno_flag - data['storno_flag'] + reversal? ? 'R' : '' end def sign @@ -140,8 +172,11 @@ def to_h 'sign' => sign, 'debit' => debit?, 'credit' => credit?, - 'storno' => storno?, - 'funds_code' => funds_code, + 'storno' => reversal?, + 'reversal' => reversal?, + 'expected' => expected?, + 'funds_code' => credit_debit_indicator, + 'credit_debit_indicator' => credit_debit_indicator, 'swift_code' => swift_code, 'reference' => reference, 'bank_reference' => bank_reference, diff --git a/lib/cmxl/version.rb b/lib/cmxl/version.rb index e19c655..cf38e5b 100644 --- a/lib/cmxl/version.rb +++ b/lib/cmxl/version.rb @@ -1,3 +1,3 @@ module Cmxl - VERSION = '1.5.0'.freeze + VERSION = '2.0'.freeze end diff --git a/spec/fields/transaction_spec.rb b/spec/fields/transaction_spec.rb index 000bc81..bbb6bbb 100644 --- a/spec/fields/transaction_spec.rb +++ b/spec/fields/transaction_spec.rb @@ -70,6 +70,11 @@ end context 'statement with complex supplementary' do + it 'future reference' do + result = Cmxl::Fields::Transaction.parse(':61:2412121212ED162,57NDDTNONREF//950\n') + expect(result.amount).to eql(162.57) + end + it { expect(complex_supplementary_transaction.initial_amount_in_cents).to eql(nil) } it { expect(complex_supplementary_transaction.initial_currency).to eql(nil) } @@ -107,4 +112,77 @@ it { expect(transaction_type_swift).not_to be_storno } it { expect(transaction_type_swift.sign).to eql(1) } end + + describe '#credit_debit_indicator' do + it 'returns the credit_debit_indicator as debit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902DR000000000001,62NTRF0000549855700010//025498557/000001') + expect(result.credit_debit_indicator).to eql('D') + end + + it 'returns the credit_debit_indicator as credit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902CR000000000001,62NTRF0000549855700010//025498557/000001') + expect(result.credit_debit_indicator).to eql('C') + end + + it 'returns the credit_debit_indicator as reversal credit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902RC000000000001,62NTRF0000549855700010//025498557/000001') + expect(result.credit_debit_indicator).to eql('RC') + end + + it 'returns the credit_debit_indicator as reversal debit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902RD000000000001,62NTRF0000549855700010//025498557/000001') + expect(result.credit_debit_indicator).to eql('RD') + end + + it 'returns the credit_debit_indicator as expected credit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902EC000000000001,62NTRF0000549855700010//025498557/000001') + expect(result.credit_debit_indicator).to eql('EC') + end + end + + describe '#expected?' do + it 'returns true if the transaction is expected' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902EC000000000001,62NTRF0000549855700010//025498557/000001') + expect(result).to be_expected + end + + it 'returns false if the transaction is not expected' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902RD000000000001,62NTRF0000549855700010//025498557/000001') + expect(result).not_to be_expected + end + end + + describe '#expected_credit?' do + it 'returns true if the transaction is expected and credit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902EC000000000001,62NTRF0000549855700010//025498557/000001') + expect(result).to be_expected_credit + end + + it 'returns false if the transaction is not expected and credit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902RC000000000001,62NTRF0000549855700010//025498557/000001') + expect(result).not_to be_expected_credit + end + + it 'returns false if the transaction is expected and debit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902ED000000000001,62NTRF0000549855700010//025498557/000001') + expect(result).not_to be_expected_credit + end + end + + describe '#expected_debit?' do + it 'returns true if the transaction is expected and debit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902ED000000000001,62NTRF0000549855700010//025498557/000001') + expect(result).to be_expected_debit + end + + it 'returns false if the transaction is not expected and debit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902RD000000000001,62NTRF0000549855700010//025498557/000001') + expect(result).not_to be_expected_debit + end + + it 'returns false if the transaction is expected and credit' do + result = Cmxl::Fields::Transaction.parse(':61:1409010902EC000000000001,62NTRF0000549855700010//025498557/000001') + expect(result).not_to be_expected_debit + end + end end diff --git a/spec/statement_spec.rb b/spec/statement_spec.rb index a232278..c701591 100644 --- a/spec/statement_spec.rb +++ b/spec/statement_spec.rb @@ -27,9 +27,12 @@ 'bank_reference' => '025498557/000001', 'amount_in_cents' => 162, 'sign' => -1, + 'credit_debit_indicator' => 'D', 'debit' => true, 'credit' => false, 'storno' => false, + 'reversal' => false, + 'expected' => false, 'bic' => 'HYVEDEMMXXX', 'iban' => 'HUkkbbbsssskcccccccccccccccx', 'name' => 'Peter Pan', @@ -81,6 +84,7 @@ 'sha' => '3c5e65aa3d3878b06b58b6f1ae2f3693004dfb04e3ab7119a1c1244e612293da', 'entry_date' => Date.new(2014, 9, 2), 'funds_code' => 'D', + 'credit_debit_indicator' => 'D', 'currency_letter' => 'R', 'amount' => 1.62, 'swift_code' => 'NTRF', @@ -90,7 +94,9 @@ 'sign' => -1, 'debit' => true, 'credit' => false, - 'storno' => false + 'storno' => false, + 'reversal' => false, + 'expected' => false, ) end end