Created
September 2, 2021 16:23
-
-
Save vsnikkil/957d984c84382c1b57959bd7f26298b7 to your computer and use it in GitHub Desktop.
Create reference numbers for invoicing. International or local (Finnish) RF format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const createReferenceNumbers = ( | |
baseStr: string = "000", | |
qty = 10, | |
international = false | |
): string[] => { | |
const baseNumber = BigInt(baseStr); | |
return Array.from({ length: qty }, (_value, idx) => { | |
const base = String(baseNumber + BigInt(idx)); | |
const checksum: number = | |
10 - | |
(Array.from(base).reduceRight( | |
(acc: number, current: string, idx, arr) => { | |
return ( | |
acc + | |
Number.parseInt(current) * | |
prodCoefficients[ | |
(arr.length - 1 - idx) % lenOfCoefficients | |
] | |
); | |
}, | |
0 | |
) % | |
10); | |
const finnishRefNumber = `${base}${checksum === 10 ? 0 : checksum}`; | |
if (international) { | |
const internationalChecksum = String( | |
98n - (BigInt(finnishRefNumber + rfSequence) % 97n) | |
).padStart(2, "0"); | |
return `RF${internationalChecksum}${finnishRefNumber}`; | |
} else { | |
return finnishRefNumber; | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment