File: company_it.rb

package info (click to toggle)
ruby-ffaker 2.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,776 kB
  • sloc: ruby: 12,788; makefile: 8; sh: 1
file content (45 lines) | stat: -rw-r--r-- 1,230 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module FFaker
  module CompanyIT
    extend ModuleUtils
    extend self

    PREFIXES = ['Studio Legale', 'Studio Tecnico', 'Laboratorio'].freeze
    SUFFIXES = %w[S.p.a. s.r.l. s.n.c. Avvocati Architetti Ingegneri].freeze

    def name
      case rand(0..2)
      when 0 then "#{NameIT.last_name} #{suffix}"
      when 1 then "#{NameIT.last_name} e #{NameIT.last_name} #{suffix}"
      when 2 then "#{prefix} #{NameIT.last_name}"
      end
    end

    def suffix
      fetch_sample(SUFFIXES)
    end

    def prefix
      fetch_sample(PREFIXES)
    end

    # Calculated using the algorithm at https://it.wikipedia.org/wiki/Partita_IVA
    # to return a valid Partita IVA (Italian VAT number)
    # @return a valid Italian VAT number (Partita IVA)
    def partita_iva
      matricola = Array.new(7) { rand(0..9) }
      office_code = fetch_sample(PIVA_OFFICE_CODES)
      base = matricola + office_code.chars.map(&:to_i)

      # Thanks https://stackoverflow.com/a/9189731/1627766
      sum = base.reverse.each_slice(2).flat_map do |x, y = 0|
        [(x * 2).divmod(10), y]
      end.flatten.sum

      control_digit = (10 - (sum % 10)) % 10

      (base << control_digit).join
    end
  end
end