Working implementation
This commit is contained in:
50
reference.py
Executable file
50
reference.py
Executable file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
|
||||
# Creates an iterator that loops a list indefinitely
|
||||
class LoopList:
|
||||
def __init__(self, ls):
|
||||
self.ls = ls
|
||||
def __iter__(self):
|
||||
self.i = 0
|
||||
return self
|
||||
def __next__(self):
|
||||
v = self.ls[self.i]
|
||||
self.i = (self.i + 1) % len(self.ls)
|
||||
return v
|
||||
|
||||
def splitAt(w,n):
|
||||
for i in range(0,len(w),n):
|
||||
yield w[i:i+n]
|
||||
|
||||
# computes checkdigit for invoice number
|
||||
# https://www.pangaliit.ee/settlements-and-standards/reference-number-of-the-invoice
|
||||
def checkdigit(ref):
|
||||
ref = str(ref)
|
||||
weights = [7,3,1]
|
||||
wref = [int(x[0])*int(x[1]) for x in zip(ref[::-1], LoopList(weights))]
|
||||
wsum = sum(wref)
|
||||
checkdigit = 10 - (wsum % 10)
|
||||
return str(checkdigit)[-1]
|
||||
|
||||
# https://github.com/terokallio/reference-numbers/blob/master/src/main/java/com/terokallio/referencenumbers/RFCreditorReference.java
|
||||
def isocheckdigits(ref):
|
||||
# ref + "RF" + "00"
|
||||
# | | |
|
||||
# v v v
|
||||
magic = ref + '2715' + '00'
|
||||
mod = int(magic) % 97
|
||||
check = 98 - mod
|
||||
return '0' + str(check) if check < 10 else str(check)
|
||||
|
||||
def convert(ref):
|
||||
refc = ref + checkdigit(ref)
|
||||
print('Reference with checkdigit: \n\t', refc)
|
||||
|
||||
refiso = 'RF' + isocheckdigits(refc) + refc
|
||||
refiso = " ".join(splitAt(refiso,4))
|
||||
print('ISO 11649 converted reference: \n\t', refiso)
|
||||
|
||||
if __name__ == '__main__':
|
||||
ref = sys.argv[1]
|
||||
convert(ref)
|
||||
Reference in New Issue
Block a user