-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organisationsnummer.go
209 lines (169 loc) · 4.33 KB
/
organisationsnummer.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package organisationsnummer
import (
"errors"
"fmt"
personnummer "github.com/personnummer/go/v3"
)
var (
ErrInvalidOrganizationNumber = errors.New("Invalid Swedish organization number")
rule3 = [...]int{0, 2, 4, 6, 8, 1, 3, 5, 7, 9}
unknown = "Okänt"
types = map[byte]string{
'1': "Dödsbon",
'2': "Stat, landsting, kommun eller församling",
'3': "Utländska företag som bedriver näringsverksamhet eller äger fastigheter i Sverige",
'5': "Aktiebolag",
'6': "Enkelt bolag",
'7': "Ekonomisk förening eller bostadsrättsförening",
'8': "Ideella förening och stiftelse",
'9': "Handelsbolag, kommanditbolag och enkelt bolag",
}
)
// charsToDigit converts char bytes to a digit
// example: ['1", '1'] => 11
func charsToDigit(chars []byte) int {
l := len(chars)
r := 0
for i, c := range chars {
p := int((c - '0'))
for j := 0; j < l-i-1; j++ {
p *= 10
}
r += p
}
return r
}
// getCleanNumber will return clean numbers.
func getCleanNumber(in string) []byte {
cleanNumber := make([]byte, 0, len(in))
for _, c := range in {
if c == '+' {
continue
}
if c == '-' {
continue
}
if c > '9' {
return nil
}
if c < '0' {
return nil
}
cleanNumber = append(cleanNumber, byte(c))
}
return cleanNumber
}
// luhn will test if the given string is a valid luhn string.
func luhn(s []byte) bool {
odd := len(s) & 1
var sum int
for i, c := range s {
if i&1 == odd {
sum += rule3[c-'0']
} else {
sum += int(c - '0')
}
}
return sum%10 == 0
}
// Organisationsnummer represents the organisationsnummer struct.
type Organisationsnummer struct {
number string
personnummer *personnummer.Personnummer
}
// New parse a Swedish organization numbers and returns a new struct or a error.
func New(input string) (*Organisationsnummer, error) {
o := &Organisationsnummer{}
if err := o.parse(input); err != nil {
return nil, err
}
return o, nil
}
// parse Swedish organization numbers and set struct properpties or return a error.
func (o *Organisationsnummer) parse(input string) error {
if len(input) < 10 || len(input) > 13 {
return ErrInvalidOrganizationNumber
}
number := getCleanNumber(input)
p, err := personnummer.Parse(input)
if err == nil {
o.personnummer = p
o.number = string(number)
return nil
} else if len(number) == 12 {
// May only be prefixed with 16.
if charsToDigit(number[0:2]) != 16 {
return ErrInvalidOrganizationNumber
}
number = number[2:]
}
if len(number) == 10 {
// Third digit bust be more than 20.
if charsToDigit(number[2:4]) < 20 {
return ErrInvalidOrganizationNumber
}
// May not start with leading 0.
if charsToDigit(number[0:2]) < 10 {
return ErrInvalidOrganizationNumber
}
if !luhn(number) {
return ErrInvalidOrganizationNumber
}
o.number = string(number)
} else {
return err
}
return nil
}
// Get Personnummer instance
func (o *Organisationsnummer) Personnummer() personnummer.Personnummer {
return *o.personnummer
}
// Determine if personnummer or not.
func (o *Organisationsnummer) IsPersonnummer() bool {
return o.personnummer != nil
}
// Format a Swedish organization number as one of the official formats,
// a long format or a short format.
func (o *Organisationsnummer) Format(separator ...bool) string {
var number = o.number
if o.IsPersonnummer() {
if len(separator) > 0 && separator[0] {
f, _ := o.personnummer.Format(false)
return f
}
f, _ := o.personnummer.Format(true)
return f[2:]
}
if len(separator) > 0 && separator[0] {
return fmt.Sprintf("%s-%s", number[0:6], number[6:])
}
return number
}
// Get the organization type.
func (o *Organisationsnummer) GetType() string {
if o.IsPersonnummer() {
return "Enskild firma"
}
if types[o.number[0]] != "" {
return types[o.number[0]]
}
return unknown
}
// Get the organization type.
func (o *Organisationsnummer) String() string {
return o.GetType()
}
// Get vat number for a organization number.
func (o *Organisationsnummer) VatNumber() string {
return fmt.Sprintf("SE%s01", o.Format(false))
}
// Valid will validate Swedish organization numbers
func Valid(input string) bool {
_, err := Parse(input)
return err == nil
}
// Parse Swedish organization numbers and return a new struct.
func Parse(input string) (*Organisationsnummer, error) {
return New(input)
}