Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NicolasZanotti/9448291 to your computer and use it in GitHub Desktop.
Save NicolasZanotti/9448291 to your computer and use it in GitHub Desktop.
/**
* Function for generating ESR-numbers for orange swiss payment slips (so called "Oranger Einzahlungsschein").
*
* @author Jason Rubichi, Nicolas Zanotti
* @param
* bc: fix, 01 or 04
* chf: dynamic, amount in chf without rappen, must have eight chars minimum (if less than eight chars, insert zeros before)
* rappen: dynamic, amount in rappen
* help1, help2, help3: fix, "+" or ">", no editing required
* referenceNumber: dynamic, contains matag number, zeros, client number and job number
* participantNumber: dynamic, bankaccount number
*
* @usage generateCodeline("01", "4378", "85", ">", "94476300000000128001105152", "+", "01200027", ">");
*/
function generateCodeline(bc, chf, rappen, help1, referenceNumber, help2, participantNumber, help3) {
function moduloTenRecursive(number) {
var table = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5];
var carryover = 0;
var numbers = number.split("");
for (var i = 0, j = 0; i < numbers.length; i++) {
j = parseInt(carryover, 10) + parseInt(numbers[i], 10);
carryover = table[j % 10];
}
var result = (10 - carryover) % 10;
return result;
}
var editedChf;
var chfLength = chf.length;
var zeros = "";
if (chfLength < 8) { //check if amount has less than eight chars
var zerosAmount = 8 - chfLength;
for (var i = 0; i < zerosAmount; i++) {
zeros += "0"; //generate String with zeros
}
editedChf = zeros.concat(chf); //insert zeros before amount to have eight chars
} else {
editedChf = chf;
}
// dynamic, check digit for bc and value (calculated with modulo 10 recursive)
var p1 = moduloTenRecursive(bc + editedChf + rappen);
// dynamic, check digit for referenceNumber (calculated with modulo 10 recursive)
var p2 = moduloTenRecursive(referenceNumber);
// dynamic, check digit for participantNumber (calculated with modulo 10 recursive)
var p3 = moduloTenRecursive(participantNumber);
return bc + editedChf + rappen + p1 + help1 + referenceNumber + p2 + help2 + " " + participantNumber + p3 + help3;
}
@NicolasZanotti
Copy link
Author

NicolasZanotti commented Dec 14, 2020

This logic has now been tested and documentend and put into its own package at npmjs.com/package/esr-code-line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment