Skip to content

Commit

Permalink
Merge pull request #17 from railslove/feature/storno-transactions
Browse files Browse the repository at this point in the history
Storno Transaction
  • Loading branch information
Uepsilon authored Mar 29, 2018
2 parents 930e2a3 + 93c6792 commit 1bf4e3e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 25 deletions.
1 change: 1 addition & 0 deletions cmxl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", '~>3.0'
spec.add_development_dependency "simplecov"
spec.add_development_dependency "pry"

spec.add_dependency "rchardet19"
end
43 changes: 32 additions & 11 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 = /^(?<date>\d{6})(?<entry_date>\d{4})?(?<funds_code>[a-zA-Z])(?<currency_letter>[a-zA-Z])?(?<amount>\d{1,12},\d{0,2})(?<swift_code>(?:N|F).{3})(?<reference>NONREF|.{0,16})(?:$|\/\/)(?<bank_reference>.*)/i
self.parser = /^(?<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).{3})(?<reference>NONREF|.{0,16})(?:$|\/\/)(?<bank_reference>.*)/i

attr_accessor :details

Expand All @@ -15,35 +15,55 @@ def sha
end

def credit?
self.data['funds_code'].to_s.upcase == 'C'
data['funds_code'].to_s.casecmp('C').zero?
end

def debit?
!credit?
data['funds_code'].to_s.casecmp('D').zero?
end

def storno_credit?
credit? && storno?
end

def storno_debit?
debit? && storno?
end

def storno?
!storno_flag.empty?
end

def funds_code
data.values_at('storno_flag', 'funds_code').join
end

def storno_flag
data['storno_flag']
end

def sign
self.credit? ? 1 : -1
credit? ? 1 : -1
end

def amount
to_amount(self.data['amount'])
to_amount(data['amount'])
end

def amount_in_cents
to_amount_in_cents(self.data['amount'])
to_amount_in_cents(data['amount'])
end

def date
to_date(self.data['date'])
to_date(data['date'])
end

def entry_date
if self.data['entry_date'] && self.date
if date.month == 1 && date.month < to_date(self.data['entry_date'], self.date.year).month
to_date(self.data['entry_date'], self.date.year - 1)
if data['entry_date'] && date
if date.month == 1 && date.month < to_date(data['entry_date'], date.year).month
to_date(data['entry_date'], date.year - 1)
else
to_date(self.data['entry_date'], self.date.year)
to_date(data['entry_date'], date.year)
end
end
end
Expand Down Expand Up @@ -82,6 +102,7 @@ def to_h
'sign' => sign,
'debit' => debit?,
'credit' => credit?,
'storno' => storno?,
'funds_code' => funds_code,
'swift_code' => swift_code,
'reference' => reference,
Expand Down
48 changes: 35 additions & 13 deletions spec/fields/transaction_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
require 'spec_helper'

describe Cmxl::Fields::Transaction do
subject(:debit_transaction) { Cmxl::Fields::Transaction.parse(fixture.first) }
subject(:storno_credit_transaction) { Cmxl::Fields::Transaction.parse(fixture.last) }

subject { Cmxl::Fields::Transaction.parse(fixture_line(:statement_line)) }
let(:fixture) { fixture_line(:statement_line).split(/\n/) }

it { expect(subject.date).to eql(Date.new(2014,9,1)) }
it { expect(subject.entry_date).to eql(Date.new(2014,9,2)) }
it { expect(subject.funds_code).to eql('D') }
it { expect(subject.currency_letter).to eql('R') }
it { expect(subject.amount).to eql(1.62) }
it { expect(subject.amount_in_cents).to eql(162) }
it { expect(subject.swift_code).to eql('NTRF') }
it { expect(subject.reference).to eql('0000549855700010') }
it { expect(subject.bank_reference).to eql('025498557/000001') }
it { expect(subject).to_not be_credit }
it { expect(subject).to be_debit }
it { expect(subject.sign).to eql(-1) }
context 'debit' do
it { expect(debit_transaction.date).to eql(Date.new(2014,9,1)) }
it { expect(debit_transaction.entry_date).to eql(Date.new(2014,9,2)) }
it { expect(debit_transaction.funds_code).to eql('D') }
it { expect(debit_transaction.currency_letter).to eql('R') }
it { expect(debit_transaction.amount).to eql(1.62) }
it { expect(debit_transaction.amount_in_cents).to eql(162) }
it { expect(debit_transaction.swift_code).to eql('NTRF') }
it { expect(debit_transaction.reference).to eql('0000549855700010') }
it { expect(debit_transaction.bank_reference).to eql('025498557/000001') }
it { expect(debit_transaction).to_not be_credit }
it { expect(debit_transaction).to be_debit }
it { expect(debit_transaction).not_to be_storno }
it { expect(debit_transaction.sign).to eql(-1) }
end

context 'storno credit' do
it { expect(storno_credit_transaction.date).to eql(Date.new(2014,9,1)) }
it { expect(storno_credit_transaction.entry_date).to eql(Date.new(2014,9,2)) }
it { expect(storno_credit_transaction.funds_code).to eql('RC') }
it { expect(storno_credit_transaction.currency_letter).to eql('R') }
it { expect(storno_credit_transaction.amount).to eql(1.62) }
it { expect(storno_credit_transaction.amount_in_cents).to eql(162) }
it { expect(storno_credit_transaction.swift_code).to eql('NTRF') }
it { expect(storno_credit_transaction.reference).to eql('0000549855700010') }
it { expect(storno_credit_transaction.bank_reference).to eql('025498557/000001') }
it { expect(storno_credit_transaction).to be_credit }
it { expect(storno_credit_transaction).not_to be_debit }
it { expect(storno_credit_transaction).not_to be_storno_debit }
it { expect(storno_credit_transaction).to be_storno }
it { expect(storno_credit_transaction.sign).to eql(1) }
end
end
1 change: 1 addition & 0 deletions spec/fixtures/lines/statement_line.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
:61:1409010902DR000000000001,62NTRF0000549855700010//025498557/000001
:61:1409010902RCR000000000001,62NTRF0000549855700010//025498557/000001
4 changes: 3 additions & 1 deletion spec/statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"sign" => -1,
"debit" => true,
"credit" => false,
"storno" => false,
"bic" => "HYVEDEMMXXX",
"iban" => "HUkkbbbsssskcccccccccccccccx",
"name" => "Peter Pan",
Expand Down Expand Up @@ -85,7 +86,8 @@
"amount_in_cents" => 162,
"sign" => -1,
"debit" => true,
"credit" => false
"credit" => false,
"storno" => false
})
end
end
Expand Down

0 comments on commit 1bf4e3e

Please sign in to comment.