Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exchange_rate to operation #12

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 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,8 @@ 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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can still be wrong but ok to keep it like that for now

operation.exchange_rate = BigDecimal(exchange_rate_value) / 1000 if exchange_rate_value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are missing the scale of the exchange rate also

Suggested change
exchange_rate_value = line.detail[26..29]
operation.exchange_rate = BigDecimal(exchange_rate_value) / 1000 if exchange_rate_value
exchange_rate_value = line.detail[26..29]
exchange_rate_scale = line.detail[18]
operation.exchange_rate = BigDecimal(exchange_rate_value) / (10**exchange_rate_scale) if exchange_rate_value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frantisekrokusek make sense. thank
code is updated

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