Skip to content

Commit

Permalink
Add exchange_rate to operation (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnovitskyi authored Nov 13, 2023
1 parent 300a7e2 commit 12488de
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cfonb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'cfonb'
s.version = '0.0.5'
s.version = '0.0.6'
s.required_ruby_version = '>= 3.2.2'
s.summary = 'CFONB parser'
s.description = 'An easy to use CFONB format parser'
Expand Down
7 changes: 6 additions & 1 deletion lib/cfonb/operation_detail/mmo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module CFONB
module OperationDetail
class MMO
ATTRIBUTES = %i[original_currency original_amount].freeze
ATTRIBUTES = %i[original_currency original_amount exchange_rate].freeze

def self.apply(operation, line)
operation.original_currency = line.detail[0..2]
Expand All @@ -14,6 +14,11 @@ def self.apply(operation, line)
sign = operation.amount <=> 0 # the detail amount is unsigned

operation.original_amount = sign * BigDecimal(line.detail[4..17]) / (10**scale)
exchange_rate_value = line.detail[26..29]
return unless exchange_rate_value

exchange_rate_scale = line.detail[18]
operation.exchange_rate = BigDecimal(exchange_rate_value) / (10**BigDecimal(exchange_rate_scale))
end

CFONB::OperationDetail.register('MMO', self)
Expand Down
15 changes: 15 additions & 0 deletions spec/cfonb/operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,23 @@
expect(operation).to have_attributes(
original_currency: 'USD',
original_amount: -12.34,
exchange_rate: nil,
)
end

context 'with exchange rate' do
let(:detail) { OpenStruct.new(body: '', detail_code: 'MMO', detail: 'USD000000000008358300000001077') }

it 'Adds the original currency information' do
operation.merge_detail(detail)

expect(operation).to have_attributes(
original_currency: 'USD',
original_amount: -8358,
exchange_rate: 1.077,
)
end
end
end

context 'with a NPY detail' do
Expand Down
21 changes: 21 additions & 0 deletions spec/cfonb/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'INTERNET SFR',
)

Expand All @@ -62,6 +63,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'ELEC ERDF',
)

Expand All @@ -80,6 +82,7 @@
value_date: Date.new(2019, 5, 15),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)

expect(statements[1]).to have_attributes(
Expand Down Expand Up @@ -110,6 +113,7 @@
value_date: Date.new(2019, 5, 15),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)

expect(statements[1].operations[1]).to have_attributes(
Expand All @@ -127,6 +131,7 @@
value_date: Date.new(2019, 5, 15),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)

expect(statements[1].operations[2]).to have_attributes(
Expand All @@ -144,6 +149,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)
end
end
Expand Down Expand Up @@ -234,6 +240,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'INTERNET SFR',
)

Expand All @@ -252,6 +259,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'ELEC ERDF',
)

Expand All @@ -270,6 +278,7 @@
value_date: Date.new(2019, 5, 15),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)

expect(statements[1]).to have_attributes(
Expand Down Expand Up @@ -300,6 +309,7 @@
value_date: Date.new(2019, 5, 15),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)

expect(statements[1].operations[1]).to have_attributes(
Expand All @@ -317,6 +327,7 @@
value_date: Date.new(2019, 5, 15),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)

expect(statements[1].operations[2]).to have_attributes(
Expand All @@ -334,6 +345,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)
end
end
Expand All @@ -359,6 +371,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'ELEC ERDF',
)
end
Expand All @@ -385,6 +398,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'ELEC ERDF',
)
end
Expand All @@ -411,6 +425,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'ELEC ERDF',
)
end
Expand All @@ -437,6 +452,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'ELEC ERDF',
)
expect(statements[0].operations[1]).to have_attributes(
Expand All @@ -454,6 +470,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
)
end
end
Expand Down Expand Up @@ -494,6 +511,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'INTERNET SFR',
)
end
Expand Down Expand Up @@ -553,6 +571,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'INTERNET SFR',
)
end
Expand All @@ -577,6 +596,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'INTERNET SFR',
)
end
Expand Down Expand Up @@ -609,6 +629,7 @@
value_date: Date.new(2019, 5, 16),
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
debtor: 'ELEC ERDF',
)
end
Expand Down

0 comments on commit 12488de

Please sign in to comment.