Skip to content

Commit

Permalink
feat: add expected info
Browse files Browse the repository at this point in the history
- parse transactions line that include upcoming transactions
- deprecate the `strono` method in favor of the `reversal` method to
  match the swift naming pattern
- deprecate the `funds_code` method because the method logic reflects
  the `credit_debit_indicator` logic
  • Loading branch information
janz93 committed Jan 6, 2025
1 parent 2be8345 commit e5809bf
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.mdown
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 43 additions & 8 deletions lib/cmxl/fields/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Cmxl
module Fields
class Transaction < Field
self.tag = 61
self.parser = %r{^(?<date>\d{6})(?<entry_date>\d{4})?(?<storno_flag>R?)(?<funds_code>[CD]{1})(?<currency_letter>[a-zA-Z])?(?<amount>\d{1,12},\d{0,2})(?<swift_code>(?:N|F|S).{3})(?<reference>NONREF|(.(?!\/\/)){,16}([^\/]){,1})((?:\/\/)(?<bank_reference>[^\n]{,16}))?((?:\n)(?<supplementary>.{,34}))?$}
self.parser = %r{^(?<date>\d{6})(?<entry_date>\d{4})?(?<credit_debit_indicator>D|C|RD|RC|ED|EC)(?<currency_letter>[a-zA-Z])?(?<amount>\d{1,12},\d{0,2})(?<swift_code>(?:N|F|S).{3})(?<reference>NONREF|(.(?!\/\/)){,16}([^\/]){,1})((?:\/\/)(?<bank_reference>[^\n]{,16}))?((?:\n)(?<supplementary>.{,34}))?$}

attr_accessor :details

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/cmxl/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Cmxl
VERSION = '1.5.0'.freeze
VERSION = '2.0'.freeze
end
78 changes: 78 additions & 0 deletions spec/fields/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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
8 changes: 7 additions & 1 deletion spec/statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -90,7 +94,9 @@
'sign' => -1,
'debit' => true,
'credit' => false,
'storno' => false
'storno' => false,
'reversal' => false,
'expected' => false,
)
end
end
Expand Down

0 comments on commit e5809bf

Please sign in to comment.