Skip to content

Commit

Permalink
added code line to payment slip, refs #425
Browse files Browse the repository at this point in the history
  • Loading branch information
bihorco36 committed Dec 15, 2017
1 parent b901ca3 commit 217da63
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 45 deletions.
11 changes: 10 additions & 1 deletion app/domain/export/pdf/invoice/payment_slip.rb
Expand Up @@ -15,6 +15,7 @@ def render
amount if invoice.invoice_items.present?
invoice.with_reference? ? esr_number : payment_purpose
receiver_address
code_line
end

private
Expand Down Expand Up @@ -63,7 +64,7 @@ def amount
table amount_data(0), cell_style: { padding: [2, 3.7, 1, 3.7], borders: [] }
end

bounding_box([x+131, 98], width: 36) do
bounding_box([x + 131, 98], width: 36) do
table amount_data(1), cell_style: { padding: [2, 3.7, 1, 3.7], borders: [] }
end
end
Expand Down Expand Up @@ -92,6 +93,14 @@ def receiver_address
end
end

def code_line
bounding_box([150, 0], width: 380, height: 11) do
pdf.font('Courier', size: 11) do
text Invoice::PaymentSlip.new(invoice).code_line
end
end
end

def amount_data(i)
numbers = helper.number_to_currency(invoice.calculated[:total],
format: '%n',
Expand Down
35 changes: 0 additions & 35 deletions app/domain/invoice/esr_number.rb

This file was deleted.

64 changes: 64 additions & 0 deletions app/domain/invoice/payment_slip.rb
@@ -0,0 +1,64 @@
# encoding: utf-8

# Copyright (c) 2017, Jungwacht Blauring Schweiz. This file is part of
# hitobito and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito.

class Invoice::PaymentSlip

# Defined at http://www.pruefziffernberechnung.de/E/Einzahlungsschein-CH.shtml
ESR9_TABLE = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5].freeze
BCS = { esr: '01', esr_plus: '04' }.freeze

attr_reader :invoice

def initialize(invoice = nil)
@invoice = invoice
end

def esr_number
esr_number = invoice.sequence_number.split('-').map { |nr| format('%013d', nr.to_i) }.join
esr_number = "#{esr_number}#{check_digit(esr_number)}"
esr_number.reverse.gsub(/(.{5})/, '\1 ').reverse
end

def code_line
code_line = ''
code_line << first_code_line_block
code_line << "#{invoice.esr_number.delete(' ')}+ "
code_line << last_code_line_block
end

def check_digit(number)
digit = 0
number.each_char.each do |char|
current_digit = digit + char.to_i
digit = ESR9_TABLE[current_digit % 10]
end
(10 - digit) % 10
end

private

def first_code_line_block
block = ''
block << BCS[calculate_bc.to_sym]
block << format('%010d', invoice.total.to_s.delete('.').to_i) if calculate_bc == 'esr'
block << "#{check_digit(block)}>"
end

def last_code_line_block
block = ''
invoice.account_number.split('-').each_with_index do |nr, i|
next block << nr if i != 1
block << format('%06d', nr.to_i)
end
"#{block}>"
end

def calculate_bc
invoice.invoice_items.present? ? 'esr' : 'esr_plus'
end

end
2 changes: 1 addition & 1 deletion app/models/invoice.rb
Expand Up @@ -170,7 +170,7 @@ def set_sequence_number
end

def set_esr_number
self.esr_number = EsrNumber.new(sequence_number).generate
self.esr_number = Invoice::PaymentSlip.new(self).esr_number
end

def set_payment_attributes
Expand Down
Expand Up @@ -7,33 +7,53 @@

require 'spec_helper'

describe Invoice::EsrNumber do
describe Invoice::PaymentSlip do
let(:group) { groups(:top_layer) }
let(:person) { people(:top_leader) }

context 'check digit' do
let(:esr_number) { Invoice::EsrNumber.new }
let(:esr_number) { Invoice::PaymentSlip.new }

it 'calculates' do
numbers = ['1236', '1237', '1230', '1232', '1239', '1235', '1238', '1234', '1231', '1233']
numbers.each_with_index do |nr, i|
expect(esr_number.send(:check_digit, nr)).to be(i)
expect(esr_number.check_digit(nr)).to be(i)
end
end
end

context 'esr number' do
it 'creates and formats esr number based on sequence_number and check digit' do
it 'creates and formats esr number based on invoice and check digit' do
invoice = create_invoice
esr_number = invoice.sequence_number.split('-').map { |nr| format('%013d', nr.to_i) }.join
check_digit = Invoice::EsrNumber.new.send(:check_digit, esr_number)
check_digit = Invoice::PaymentSlip.new.check_digit(esr_number)
esr_number = "#{esr_number}#{check_digit}"
esr_number = esr_number.reverse.gsub(/(.{5})/, '\1 ').reverse

expect(invoice.esr_number).to eq(esr_number)
end
end

context 'code line' do
it 'creates code line with amount' do
invoice = create_invoice
invoice.invoice_items.create
invoice.total = 32.32
code_line = Invoice::PaymentSlip.new(invoice).code_line

expected_code_line = "0100000032320>#{invoice.esr_number.delete(' ')}+ 100053185>"
expect(code_line).to eq(expected_code_line)
end

it 'creates code line without amount' do
invoice = create_invoice
code_line = Invoice::PaymentSlip.new(invoice).code_line

expected_code_line = "042>#{invoice.esr_number.delete(' ')}+ 100053185>"
expect(code_line).to eq(expected_code_line)
end
end

private

def create_invoice(attrs = {})
Expand Down
7 changes: 4 additions & 3 deletions spec/fixtures/invoice_configs.yml
Expand Up @@ -18,7 +18,8 @@ top_layer:
payee: 'Hans Gerber'
address: 'top layer address'
iban: 'CH93 0076 2011 6238 5295 7'
account_number: '10-5318-01'
account_number: '10-5318-5'
payment_slip: 'ch_esr'

bottom_layer_one:
group: bottom_layer_one
Expand All @@ -27,7 +28,7 @@ bottom_layer_one:
payee: 'Fritz von Gunten'
address: 'bottom layer one address'
iban: 'CH93 0076 2011 6238 5295 8'
account_number: '10-3991-803'
account_number: '01-162-8'

bottom_layer_two:
group: bottom_layer_two
Expand All @@ -36,4 +37,4 @@ bottom_layer_two:
payee: 'Anna Streit'
address: 'bottom layer two address'
iban: 'CH84 0221 1981 6169 5329 8'
account_number: '10-3991-803'
account_number: '03-162-5'
2 changes: 2 additions & 0 deletions spec/fixtures/invoices.yml
Expand Up @@ -4,6 +4,7 @@ invoice:
recipient: top_leader
sequence_number: <%= ActiveRecord::FixtureSet.identify(:bottom_layer_one) %>-2
esr_number: <%= ActiveRecord::FixtureSet.identify(:bottom_layer_one) %>-2
account_number: <%= ActiveRecord::FixtureSet.identify(:bottom_layer_one) %>-3
total: 2

sent:
Expand All @@ -14,5 +15,6 @@ sent:
esr_number: <%= ActiveRecord::FixtureSet.identify(:bottom_layer_one) %>-3
sent_at: <%= 10.days.ago.to_date %>
due_at: <%= 20.days.from_now.to_date %>
account_number: <%= ActiveRecord::FixtureSet.identify(:bottom_layer_one) %>-3
state: sent
total: 2

0 comments on commit 217da63

Please sign in to comment.