From 90787c38f60850ed225e1919a6ca033f95bf69be Mon Sep 17 00:00:00 2001 From: Viktor Date: Thu, 9 Nov 2023 12:07:11 +0200 Subject: [PATCH 1/4] Add `exchange_rate` to operation --- cfonb.gemspec | 2 +- lib/cfonb/operation_detail/mmo.rb | 4 +++- spec/cfonb/operation_spec.rb | 5 +++-- spec/cfonb/parser_spec.rb | 21 +++++++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cfonb.gemspec b/cfonb.gemspec index bb45117..1e54b0e 100644 --- a/cfonb.gemspec +++ b/cfonb.gemspec @@ -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' diff --git a/lib/cfonb/operation_detail/mmo.rb b/lib/cfonb/operation_detail/mmo.rb index 54a44ad..5f6db82 100644 --- a/lib/cfonb/operation_detail/mmo.rb +++ b/lib/cfonb/operation_detail/mmo.rb @@ -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] @@ -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) + + operation.exchange_rate = BigDecimal(line.detail[-4..-1]) / 1000 end CFONB::OperationDetail.register('MMO', self) diff --git a/spec/cfonb/operation_spec.rb b/spec/cfonb/operation_spec.rb index 52b14d6..6a92d36 100644 --- a/spec/cfonb/operation_spec.rb +++ b/spec/cfonb/operation_spec.rb @@ -81,14 +81,15 @@ end context 'with a MMO detail' do - let(:detail) { OpenStruct.new(body: '', detail_code: 'MMO', detail: 'USD200000000001234') } + 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: -12.34, + original_amount: -8358, + exchange_rate: 1.077, ) end end diff --git a/spec/cfonb/parser_spec.rb b/spec/cfonb/parser_spec.rb index 3030f9a..f025846 100644 --- a/spec/cfonb/parser_spec.rb +++ b/spec/cfonb/parser_spec.rb @@ -44,6 +44,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'INTERNET SFR', ) @@ -62,6 +63,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'ELEC ERDF', ) @@ -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( @@ -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( @@ -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( @@ -144,6 +149,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, ) end end @@ -234,6 +240,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'INTERNET SFR', ) @@ -252,6 +259,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'ELEC ERDF', ) @@ -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( @@ -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( @@ -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( @@ -334,6 +345,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, ) end end @@ -359,6 +371,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'ELEC ERDF', ) end @@ -385,6 +398,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'ELEC ERDF', ) end @@ -411,6 +425,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'ELEC ERDF', ) end @@ -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( @@ -454,6 +470,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, ) end end @@ -494,6 +511,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'INTERNET SFR', ) end @@ -553,6 +571,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'INTERNET SFR', ) end @@ -577,6 +596,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'INTERNET SFR', ) end @@ -609,6 +629,7 @@ value_date: Date.new(2019, 5, 16), original_currency: nil, original_amount: nil, + exchange_rate: nil, debtor: 'ELEC ERDF', ) end From 1f7b6b96bf817add3aeb8085c714a5440dccad63 Mon Sep 17 00:00:00 2001 From: Viktor Date: Thu, 9 Nov 2023 12:07:11 +0200 Subject: [PATCH 2/4] Add `exchange_rate` to operation --- lib/cfonb/operation_detail/mmo.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/cfonb/operation_detail/mmo.rb b/lib/cfonb/operation_detail/mmo.rb index 5f6db82..07412bc 100644 --- a/lib/cfonb/operation_detail/mmo.rb +++ b/lib/cfonb/operation_detail/mmo.rb @@ -14,7 +14,6 @@ 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) - operation.exchange_rate = BigDecimal(line.detail[-4..-1]) / 1000 end From 4c26d8055bf804d55f0a382240817ddf09585e64 Mon Sep 17 00:00:00 2001 From: Viktor Date: Thu, 9 Nov 2023 12:54:23 +0200 Subject: [PATCH 3/4] Fix code and specs --- lib/cfonb/operation_detail/mmo.rb | 3 ++- spec/cfonb/operation_spec.rb | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/cfonb/operation_detail/mmo.rb b/lib/cfonb/operation_detail/mmo.rb index 07412bc..d461df1 100644 --- a/lib/cfonb/operation_detail/mmo.rb +++ b/lib/cfonb/operation_detail/mmo.rb @@ -14,7 +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) - operation.exchange_rate = BigDecimal(line.detail[-4..-1]) / 1000 + exchange_rate_value = line.detail[26..29] + operation.exchange_rate = BigDecimal(exchange_rate_value) / 1000 if exchange_rate_value end CFONB::OperationDetail.register('MMO', self) diff --git a/spec/cfonb/operation_spec.rb b/spec/cfonb/operation_spec.rb index 6a92d36..24d3ab0 100644 --- a/spec/cfonb/operation_spec.rb +++ b/spec/cfonb/operation_spec.rb @@ -81,17 +81,31 @@ end context 'with a MMO detail' do - let(:detail) { OpenStruct.new(body: '', detail_code: 'MMO', detail: 'USD000000000008358300000001077') } + let(:detail) { OpenStruct.new(body: '', detail_code: 'MMO', detail: 'USD200000000001234') } 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, + 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 From b5ec726155b3b1f2f7f709dacccfa19f9a81effe Mon Sep 17 00:00:00 2001 From: Viktor Date: Fri, 10 Nov 2023 11:08:57 +0200 Subject: [PATCH 4/4] Add `exchange_rate_scale` --- lib/cfonb/operation_detail/mmo.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cfonb/operation_detail/mmo.rb b/lib/cfonb/operation_detail/mmo.rb index d461df1..92560fe 100644 --- a/lib/cfonb/operation_detail/mmo.rb +++ b/lib/cfonb/operation_detail/mmo.rb @@ -15,7 +15,10 @@ def self.apply(operation, line) operation.original_amount = sign * BigDecimal(line.detail[4..17]) / (10**scale) exchange_rate_value = line.detail[26..29] - operation.exchange_rate = BigDecimal(exchange_rate_value) / 1000 if exchange_rate_value + 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)