vesr 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae7e408a671168464173f8f975352930137d89ef
4
- data.tar.gz: b558c3cc6bdd0373587246fb5a6dfe656c707895
3
+ metadata.gz: 812a29300c484ae22567b16acaf0e884a809322f
4
+ data.tar.gz: 34525baa67b7e26c21f3eeb7fd360728411ffcb4
5
5
  SHA512:
6
- metadata.gz: ee0ce7d1edb35a4ec9662dcfc52bf79ba5231190dfbc96650ba1020623132cad60bdacf437e6f85f68999322937c833e0ffe77ff7a35147eafb76fe72410ae08
7
- data.tar.gz: 73f57511a7651bb6d3fbb05ed9d643d22af65aff458ed570534681fe6f3b27235d54528c6361a39109eade3eea301ca0178d110eb14dfc5f948230826ed95979
6
+ metadata.gz: 1ee3bb90f0c601107ecdfc33ce2fe5e0850264c0d81d9ef6e23f796c25eed7a6d333f1e68eee3cf34558627680a1e6f4dfafa19f6685e2608c9276e969aead79
7
+ data.tar.gz: 39fc74d1826fde0be3159762104d97ee7537682bfb4f6655dc5058fd583b167e7344262d4bd2c679dcf9dd2f295e1923d1aaf9c08f32a47899c81413500c0262
@@ -1,6 +1,9 @@
1
1
  require 'action_view/helpers/translation_helper'
2
2
  require 'action_view/helpers'
3
3
 
4
+ require 'vesr/reference_builder'
5
+ require 'vesr/validation_digit_calculator'
6
+
4
7
  module Prawn
5
8
  module EsrRecipe
6
9
 
@@ -110,31 +113,17 @@ module Prawn
110
113
  end
111
114
 
112
115
  def esr9_reference(invoice, esr_account)
113
- esr9_format(esr9_add_validation_digit(esr_number(esr_account.esr_id, invoice.customer.id, invoice.id)))
116
+ esr_number = VESR::ReferenceBuilder.call(invoice.customer.id, invoice.id, esr_account.esr_id)
117
+ esr9_format VESR::ValidationDigitCalculator.call(esr_number)
114
118
  end
115
119
 
116
120
  def esr9_build(esr_amount, invoice, biller_id, esr_id)
117
121
  # 01 is type 'Einzahlung in CHF'
118
122
  amount_string = "01#{sprintf('%011.2f', esr_amount).delete('.')}"
119
- id_string = esr_number(esr_id, invoice.customer.id, invoice.id)
123
+ id_string = VESR::ReferenceBuilder.call(invoice.customer.id, invoice.id, esr_id)
120
124
  biller_string = esr9_format_account_id(biller_id)
121
125
 
122
- "#{esr9_add_validation_digit(amount_string)}>#{esr9_add_validation_digit(id_string)}+ #{biller_string}>"
123
- end
124
-
125
- def esr_number(esr_id, customer_id, invoice_id)
126
- customer_id_length = 19 - esr_id.to_s.length
127
- esr_id.to_s + sprintf("%0#{customer_id_length}i", customer_id).delete(' ') + sprintf('%07i', invoice_id).delete(' ')
128
- end
129
-
130
- def esr9_add_validation_digit(value)
131
- # Defined at http://www.pruefziffernberechnung.de/E/Einzahlungsschein-CH.shtml
132
- esr9_table = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5]
133
- digit = 0
134
- value.split('').map{|c| digit = esr9_table[(digit + c.to_i) % 10]}
135
- digit = (10 - digit) % 10
136
-
137
- "#{value}#{digit}"
126
+ "#{VESR::ValidationDigitCalculator.call(amount_string)}>#{VESR::ValidationDigitCalculator.call(id_string)}+ #{biller_string}>"
138
127
  end
139
128
 
140
129
  def esr9_format(reference_code)
@@ -0,0 +1,35 @@
1
+ require 'vesr/validation_digit_calculator'
2
+
3
+ module VESR
4
+ class ReferenceBuilder
5
+ def self.call(customer_id, invoice_id, esr_id)
6
+ new(customer_id, invoice_id, esr_id).call
7
+ end
8
+
9
+ attr_reader :customer_id, :invoice_id, :esr_id
10
+
11
+ def initialize(customer_id, invoice_id, esr_id)
12
+ @customer_id = customer_id
13
+ @invoice_id = invoice_id
14
+ @esr_id = esr_id
15
+ end
16
+
17
+ def call
18
+ "#{esr_id}#{formatted_customer_id}#{formatted_invoice_id}"
19
+ end
20
+
21
+ private
22
+
23
+ def formatted_customer_id
24
+ format "%0#{customer_id_length}i", customer_id
25
+ end
26
+
27
+ def customer_id_length
28
+ 19 - esr_id.to_s.length
29
+ end
30
+
31
+ def formatted_invoice_id
32
+ format '%07i', invoice_id
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ module VESR
2
+ class ValidationDigitCalculator
3
+ # Defined at http://www.pruefziffernberechnung.de/E/Einzahlungsschein-CH.shtml
4
+
5
+ ESR9_TABLE = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5]
6
+
7
+ def self.call(value)
8
+ value = value.to_s
9
+
10
+ digit = 0
11
+ value.split('').each do |char|
12
+ current_digit = digit + char.to_i
13
+ digit = ESR9_TABLE[current_digit % 10]
14
+ end
15
+ digit = (10 - digit) % 10
16
+
17
+ "#{value}#{digit}"
18
+ end
19
+ end
20
+ end
data/lib/vesr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vesr
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vesr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Simecek
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-01-04 00:00:00.000000000 Z
12
+ date: 2016-02-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aasm
@@ -81,6 +81,8 @@ files:
81
81
  - lib/vesr/prawn.rb
82
82
  - lib/vesr/prawn/esr_recipe.rb
83
83
  - lib/vesr/railtie.rb
84
+ - lib/vesr/reference_builder.rb
85
+ - lib/vesr/validation_digit_calculator.rb
84
86
  - lib/vesr/version.rb
85
87
  homepage: https://github.com/raskhadafi/vesr
86
88
  licenses: