From a4865bc348cdc990b3f415bca390d901fe68a238 Mon Sep 17 00:00:00 2001 From: Nicolas Hiillos Date: Thu, 5 Oct 2023 19:06:17 +0300 Subject: [PATCH] Working implementation --- README.md | 0 reference.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 README.md create mode 100755 reference.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/reference.py b/reference.py new file mode 100755 index 0000000..29cc06a --- /dev/null +++ b/reference.py @@ -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)