-
Notifications
You must be signed in to change notification settings - Fork 1
/
ird_generator.py
120 lines (90 loc) · 4.01 KB
/
ird_generator.py
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
# -*- coding: utf-8 -*-
'''
This script generates a 9 digit IRD number
https://www.ird.govt.nz/-/media/project/ir/home/documents/income-tax/withholding-taxes/rwt-nrwt-withholding-tax-certificate/2020-rwt-and-nrwt-certificate-filing-specification.pdf
Check digit validation
The following steps are to be performed:
1. Check the valid range
- If the IRD number is < 10 - 000 - 000 or > 150 - 000 - 000 then the number is
invalid.This step ensures that the IRD number is in the already issued
range, or is in the range expected to be issued in the next 10 years.
2. Form the eight digit base number:
- Remove the trailing check digit.
- If the resulting number is seven digits long, pad to eight digits by adding a
leading zero.
3. Calculate the check digit:
- To each of the base number's eight digits a weight factor is assigned. From
left to right these are: 3, 2, 7, 6, 5, 4, 3, 2.
- Sum together the products of the weight factors and their associated
digits.
- Divide the sum by 11. If the remainder is 0 then the calculated check digit
is 0.
- If the remainder is not 0 then subtract the remainder from 11, giving the
calculated check digit.
- If the calculated check digit is in the range 0 to 9, go to step 5.
- If the calculated check digit is 10, continue with step 4.
4. Re - calculate the check digit:
- To each of the base number's eight digits a secondary weight factor is
assigned.From left to right these are: 7, 4, 3, 2, 5, 2, 7, 6.
- Sum together the products of the weight factors and their associated
digits.
- Divide the sum by 11. If the remainder is 0 then the calculated check digit
is 0.
- If the remainder is not 0 then subtract the remainder from 11, giving the
00 calculated check digit.
- If the calculated check digit is 10, the IRD number is invalid.
5. Compare the check digit:
- Compare the calculated check digit to the last digit of the original IRD
number.
- If they match, the IRD number is valid.
NOTE: Step 4 is not required for the data generated by this script because the code only outputs
the ird numbers that match either checksum = 0 or if it is in a range 0 to 9.
'''
from random import randint
import csv
IRD_NUMBERS_TO_GENERATE = 10
def generate_ird(ird_num_to_generate):
ird =[]
ird_list=[]
pri_num_weight = [3, 2, 7, 6, 5, 4, 3, 2]
i = 1
ird_list.append('ird_numbers')
while i <= ird_num_to_generate: #How many IRD numbers to generate
total_sum = 0
ird.append(1) # The first digit in IRD starts with 1
ird.append(randint(0,4)) # The second digit in IRD starts with 4
for num in range(0,6): # Append 6 random digits to IRD number
ird.append(randint(0,9))
for num in range(0,8): # Get total sum of 8 digit based on their weight
total_sum = total_sum + (ird[num] * pri_num_weight[num])
quotient, remainder = divmod(total_sum, 11)
if remainder == 0:
ird.append(0)
i = i + 1
ird = int("".join(map(str, ird)))
ird_list.append(ird)
#print("Loop 1 %s" %ird)
elif (0 < remainder <=9):
if ((11 - remainder) == 10):
print("Excluding this functionality")
else:
ird.append(11 - remainder)
i = i + 1
ird = int("".join(map(str, ird)))
ird_list.append(ird)
#print("Loop 2 %s" %ird)
elif remainder == 10:
print("Excluding this functionality")
#print(ird)
#print(quotient)
#print(remainder)
ird=[]
write_to_file(ird_list)
def write_to_file(ird_list):
with open('./ird.csv', 'w') as ird_file:
file_writer = csv.writer(ird_file,quoting=csv.QUOTE_ALL,delimiter='\n')
file_writer.writerow(ird_list)
def main():
generate_ird(IRD_NUMBERS_TO_GENERATE)
if __name__ == "__main__":
main()