@@ -19,8 +19,8 @@ const (
1919)
2020
2121const (
22- legalSerialNumberLength = 4
23- physicalSerialNumberLength = 5
22+ legalSerialNumberLength = 5
23+ physicalSerialNumberLength = 6
2424)
2525
2626type INNType uint
@@ -43,17 +43,44 @@ const (
4343 ForeignLegal
4444)
4545
46- type SerialNumber int
46+ type SerialNumber struct {
47+ val int
48+ size int
49+ }
4750
4851func (sn SerialNumber ) String () string {
49- return strconv .Itoa (int (sn ))
52+ return utils .StrCode (sn .val , sn .size )
53+ }
54+
55+ func (sn * SerialNumber ) Ints () []int {
56+ if sn == nil {
57+ return nil
58+ }
59+
60+ res := make ([]int , sn .size )
61+ nums := utils .CodeToInts (sn .val )
62+
63+ idx := len (res ) - 1
64+ for i := len (nums ) - 1 ; i >= 0 ; i -- {
65+ res [idx ] = nums [i ]
66+ idx --
67+ }
68+
69+ return res
5070}
5171
5272func GenerateSerailNumber (innType INNType ) SerialNumber {
5373 if innType == Physical {
54- return SerialNumber (utils .RandomDigits (physicalSerialNumberLength ))
74+ return SerialNumber {
75+ val : int (utils .RandomDigits (physicalSerialNumberLength )),
76+ size : physicalSerialNumberLength ,
77+ }
78+ }
79+
80+ return SerialNumber {
81+ val : int (utils .RandomDigits (legalSerialNumberLength )),
82+ size : legalSerialNumberLength ,
5583 }
56- return SerialNumber (utils .RandomDigits (legalSerialNumberLength ))
5784}
5885
5986type CheckSums []int
@@ -67,6 +94,27 @@ func (cs CheckSums) String() string {
6794 return res .String ()
6895}
6996
97+ func GenerateCheckSums (innType INNType , nums []int ) CheckSums {
98+ checkFuncs := []checkSumFuncType {
99+ hash10 ,
100+ }
101+
102+ shiftIdx := - 1
103+ if innType == Physical {
104+ shiftIdx = - 2
105+ checkFuncs = []checkSumFuncType {
106+ hash11 , hash12 ,
107+ }
108+ }
109+
110+ for _ , f := range checkFuncs {
111+ nums = append (nums , f (nums ))
112+ }
113+
114+ fmt .Println (nums [len (nums )+ shiftIdx :])
115+ return nums [len (nums )+ shiftIdx :]
116+ }
117+
70118type INNStruct struct {
71119 taxRegionCode * fts.TaxRegionCode
72120 serialNumber SerialNumber
@@ -82,6 +130,7 @@ func NewINN(innType INNType) *INNStruct {
82130 return & INNStruct {
83131 taxRegionCode : taxRegionCode ,
84132 serialNumber : serialNumber ,
133+ checkSums : GenerateCheckSums (innType , append (taxRegionCode .Ints (), serialNumber .Ints ()... )),
85134 }
86135}
87136
@@ -99,17 +148,19 @@ func ParseINN(inn string) (*INNStruct, error) {
99148 }
100149
101150 t := Physical
102- parseIdx := len (inn ) - 3
151+ snSize := physicalSerialNumberLength
152+ parseIdx := len (inn ) - 2
103153 if len (inn ) == legalLength {
154+ snSize = legalSerialNumberLength
104155 t = Legal
105- parseIdx = len (inn ) - 2
156+ parseIdx = len (inn ) - 1
106157 const foreignLegalStartWith = "9909"
107158 if inn [0 :4 ] == foreignLegalStartWith {
108159 t = ForeignLegal
109160 }
110161 }
111162
112- seraclNumberArr , err := utils .StrToArr (inn [4 :parseIdx ])
163+ serialNumberArr , err := utils .StrToArr (inn [4 :parseIdx ])
113164 if err != nil {
114165 return nil , fmt .Errorf ("parse raw serial number %s: %w" , packageName , err )
115166 }
@@ -121,9 +172,12 @@ func ParseINN(inn string) (*INNStruct, error) {
121172
122173 return & INNStruct {
123174 taxRegionCode : taxRegionCode ,
124- serialNumber : SerialNumber (utils .SliceToInt (seraclNumberArr )),
125- checkSums : checkSums ,
126- t : t ,
175+ serialNumber : SerialNumber {
176+ val : utils .SliceToInt (serialNumberArr ),
177+ size : snSize ,
178+ },
179+ checkSums : checkSums ,
180+ t : t ,
127181 }, nil
128182}
129183
@@ -134,14 +188,14 @@ func (inn *INNStruct) IsValid() (bool, error) {
134188 return false , ErrNilINN
135189 }
136190
137- nums := append (inn .taxRegionCode .Ints (), utils . CodeToInts ( int ( inn .serialNumber ) )... )
191+ nums := append (inn .taxRegionCode .Ints (), inn .serialNumber . Ints ( )... )
138192
139193 checkFuncs := []checkSumFuncType {
140194 hash10 ,
141195 }
142196
143197 if inn .IsPhysical () {
144- if len (nums ) != physicalLength {
198+ if len (nums ) != physicalLength - 2 {
145199 return false , fmt .Errorf ("invalid nums length for %s type" , inn .t )
146200 }
147201
@@ -155,7 +209,7 @@ func (inn *INNStruct) IsValid() (bool, error) {
155209 }
156210
157211 if inn .IsLegal () {
158- if len (nums ) != legalLength {
212+ if len (nums ) != legalLength - 1 {
159213 return false , fmt .Errorf ("invalid nums length for %s type" , inn .t )
160214 }
161215
@@ -176,23 +230,15 @@ func (inn *INNStruct) IsValid() (bool, error) {
176230
177231func (inn * INNStruct ) String () string {
178232 n := physicalLength
179- snRequired := physicalSerialNumberLength
180233 if inn .IsLegal () {
181234 n = legalLength
182- snRequired = legalSerialNumberLength
183235 }
184236
185237 var res strings.Builder
186238 res .Grow (n )
187239
188240 res .WriteString (inn .taxRegionCode .String ())
189-
190- sn := inn .serialNumber .String ()
191- if len (sn ) < snRequired {
192- sn = strings .Repeat ("0" , snRequired - len (sn )) + sn
193- }
194-
195- res .WriteString (sn )
241+ res .WriteString (inn .serialNumber .String ())
196242 res .WriteString (inn .checkSums .String ())
197243
198244 return res .String ()
0 commit comments