cfita 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +6 -0
- data/cfita.gemspec +1 -1
- data/lib/cfita/codice_fiscale.rb +83 -13
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6000d09a1d72bf1f7138f063310857187f8ff248a2d3b753336e03952dcd4dc5
|
4
|
+
data.tar.gz: 3a0dfb634d4c8654eb22cd2f5b121bed83f71ff540e2ed0d13ec7137db165f95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6635c4f248bdece2be0c10cdb3f20ee3635b161c2a9ee4daafd51c0d08b7b7d9a6b195e0c4681d7ca3bdd99279dfab944aa97a22ccb67b9c8bd3f430753bb28f
|
7
|
+
data.tar.gz: 6af7e7120a4a70ce72737ed3139e1a7ce28fd97d3922b2b759095a30effbd5e5f122f64f505fdb957042e305d409b0efaed42d29b72161a677d071d16ebbc6e2
|
data/README.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
1
|
# Cfita
|
2
2
|
|
3
3
|
Check italian fiscal code
|
4
|
+
|
5
|
+
see:
|
6
|
+
https://www.agenziaentrate.gov.it/wps/content/Nsilib/Nsi/Schede/Istanze/Richiesta+TS_CF/Informazioni+codificazione+pf
|
7
|
+
|
8
|
+
|
9
|
+
today_year = 1965 ; (0..99).each {|year| p [year, [1800,1900,2000].map {|mill| today_year - (mill + year)}.reject{|x| x < 14 }.min ].flatten }
|
data/cfita.gemspec
CHANGED
data/lib/cfita/codice_fiscale.rb
CHANGED
@@ -1,21 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'active_support/all'
|
4
|
+
# require './belfiore.rb'
|
4
5
|
|
5
6
|
module Cfita
|
6
7
|
# Controllo codice fiscale italiano
|
7
8
|
class CodiceFiscale
|
8
|
-
attr_reader :
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
attr_reader :fiscal_code,
|
10
|
+
:sex,
|
11
|
+
:birth_place,
|
12
|
+
:birth_date,
|
13
|
+
:data,
|
14
|
+
:errors
|
15
|
+
|
16
|
+
def initialize(fiscal_code)
|
17
|
+
@fiscal_code = fiscal_code.upcase.strip
|
12
18
|
@data = {}
|
13
19
|
@errors = []
|
14
20
|
parse
|
15
21
|
end
|
16
22
|
|
17
23
|
def to_s
|
18
|
-
|
24
|
+
fiscal_code
|
19
25
|
end
|
20
26
|
|
21
27
|
def valid?
|
@@ -32,34 +38,97 @@ module Cfita
|
|
32
38
|
check_checksum
|
33
39
|
return if errors.any?
|
34
40
|
|
35
|
-
|
41
|
+
check_birth_date
|
42
|
+
# check_birth_place
|
36
43
|
end
|
37
44
|
|
38
45
|
def check_size
|
39
|
-
size = @
|
46
|
+
size = @fiscal_code.size
|
40
47
|
errors << "Lunghezza errata (#{size})" unless size == 16
|
41
48
|
end
|
42
49
|
|
43
50
|
def check_chars
|
44
|
-
test = @
|
51
|
+
test = @fiscal_code == @fiscal_code[/^[A-Z0-9]*$/]
|
45
52
|
errors << 'Caratteri non ammessi' unless test
|
46
53
|
end
|
47
54
|
|
48
55
|
def check_checksum
|
49
|
-
errors << 'Checksum errato' if checksum != @
|
56
|
+
errors << 'Checksum errato' if checksum != @fiscal_code.last
|
50
57
|
end
|
51
58
|
|
52
59
|
def check_sex
|
53
|
-
case @
|
60
|
+
case @fiscal_code[9]
|
54
61
|
when /[0-3LMNP]/
|
55
|
-
@data[:
|
62
|
+
@data[:sex] = 'M'
|
56
63
|
when /[4-7QRST]/
|
57
|
-
@data[:
|
64
|
+
@data[:sex] = 'F'
|
58
65
|
else
|
59
66
|
@errors << 'Cifra decina giorno di nascita errata'
|
60
67
|
end
|
61
68
|
end
|
62
69
|
|
70
|
+
def check_birth_place
|
71
|
+
letter = @fiscal_code[11]
|
72
|
+
numbers =
|
73
|
+
@fiscal_code[12..14]
|
74
|
+
.split(//)
|
75
|
+
.map do |c|
|
76
|
+
i = OMOCODICI.index(c)
|
77
|
+
i ? i.to_s : c
|
78
|
+
end
|
79
|
+
.join
|
80
|
+
belfiore = letter + numbers
|
81
|
+
|
82
|
+
@birth_place = BELFIORE[belfiore]
|
83
|
+
@errors << "Codice istat #{belfiore} non trovato" unless @birth_place
|
84
|
+
end
|
85
|
+
|
86
|
+
MESI = 'ABCDEHLMPRST'.freeze
|
87
|
+
|
88
|
+
def check_birth_date
|
89
|
+
yy = cifre(6..7)
|
90
|
+
return if @errors.any?
|
91
|
+
|
92
|
+
day = cifre(9..10)
|
93
|
+
return if @errors.any?
|
94
|
+
|
95
|
+
@errors << 'Cifra decina giorno di nascita errata' if day > 71
|
96
|
+
return if @errors.any?
|
97
|
+
|
98
|
+
@data[:sex] = day > 40 ? 'F' : 'M'
|
99
|
+
|
100
|
+
month = MESI.index(@fiscal_code[8])
|
101
|
+
@errors << 'Mese errato' unless month
|
102
|
+
return if @errors.any?
|
103
|
+
|
104
|
+
@birth_date =
|
105
|
+
Date.new(yy2yyyy(yy), month + 1, day % 40) rescue nil
|
106
|
+
|
107
|
+
@errors << 'Data di nascita errata' unless @birth_date
|
108
|
+
return if @errors.any?
|
109
|
+
|
110
|
+
@data[:birth_date] = @birth_date
|
111
|
+
end
|
112
|
+
|
113
|
+
def yy2yyyy(yy)
|
114
|
+
Date.today.year -
|
115
|
+
(Date.today.year % 100 + 100 - yy ) % 100
|
116
|
+
end
|
117
|
+
|
118
|
+
def cifre(range)
|
119
|
+
result = 0
|
120
|
+
range.each do |position|
|
121
|
+
char = @fiscal_code[position]
|
122
|
+
value = CIFRE.index(char)
|
123
|
+
@errors << "Carattere '#{char}' errato in posizione #{position}" unless value
|
124
|
+
return nil unless value
|
125
|
+
|
126
|
+
result *= 10
|
127
|
+
result += value % 10
|
128
|
+
end
|
129
|
+
result
|
130
|
+
end
|
131
|
+
|
63
132
|
DISPARI = [
|
64
133
|
1, 0, 5, 7, 9,
|
65
134
|
13, 15, 17, 19, 21,
|
@@ -69,10 +138,11 @@ module Cfita
|
|
69
138
|
].freeze
|
70
139
|
|
71
140
|
OMOCODICI = 'LMNPQRSTUV'
|
141
|
+
CIFRE = ('0123456789' + OMOCODICI).freeze
|
72
142
|
|
73
143
|
def checksum
|
74
144
|
tot = 0
|
75
|
-
@
|
145
|
+
@fiscal_code[0..14].bytes.first(15).each.with_index do |byte, i|
|
76
146
|
next unless byte
|
77
147
|
|
78
148
|
byte -= byte < 65 ? 48 : 65
|