Sha256: bf46bf93eeef3bc13770d71cf6044e1ab692ba5fd41236b5f916c14fe51ad76e

Contents?: true

Size: 924 Bytes

Versions: 16

Compression:

Stored size: 924 Bytes

Contents

# frozen_string_literal: true

class Valvat
  module Checksum
    class IE < Base
      def check_digit
        total = sum_figures_by do |fig, i|
          fig * (i + 2)
        end
        total += ((CHARS.index(str_wo_country[8]) || 0) * 9) if str_wo_country.size == 9
        total.modulo(23)
      end

      CHARS = 'WABCDEFGHIJKLMNOPQRSTUV'.split('')

      def given_check_digit
        if str_wo_country.size == 9
          CHARS.index(str_wo_country[7])
        else
          CHARS.index(given_check_digit_str)
        end
      end

      def str_wo_country
        str = super
        # Convert old irish vat format to new one
        if str =~ /\A[0-9][A-Z][0-9]{5}[A-Z]\Z/
          "0#{str[2..6]}#{str[0]}#{str[7]}"
        else
          str
        end
      end

      def figures_str
        if super.size == 8
          super[0...-1]
        else
          super
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
valvat-2.0.1 lib/valvat/checksum/ie.rb
valvat-2.0.0 lib/valvat/checksum/ie.rb
valvat-1.4.4 lib/valvat/checksum/ie.rb
valvat-1.4.3 lib/valvat/checksum/ie.rb
valvat-1.4.2 lib/valvat/checksum/ie.rb
valvat-1.4.1 lib/valvat/checksum/ie.rb
valvat-1.4.0 lib/valvat/checksum/ie.rb
valvat-1.3.0 lib/valvat/checksum/ie.rb
valvat-1.2.1 lib/valvat/checksum/ie.rb
valvat-1.2.0 lib/valvat/checksum/ie.rb
valvat-1.1.5 lib/valvat/checksum/ie.rb
valvat-1.1.4 lib/valvat/checksum/ie.rb
valvat-1.1.3 lib/valvat/checksum/ie.rb
valvat-1.1.2 lib/valvat/checksum/ie.rb
valvat-1.1.1 lib/valvat/checksum/ie.rb
valvat-1.1.0 lib/valvat/checksum/ie.rb