Skip to content

Instantly share code, notes, and snippets.

@shivergard
Created March 4, 2015 14:30
Show Gist options
  • Select an option

  • Save shivergard/287fcc806c86214d35a2 to your computer and use it in GitHub Desktop.

Select an option

Save shivergard/287fcc806c86214d35a2 to your computer and use it in GitHub Desktop.
LV Personal Code generator
window.genPk = function (argument) {
if (typeof argument == 'undefined'){
argument = {};
}
if (typeof argument.date == 'undefined'){
startTime =new Date.today().add(-18).years();
endTime =new Date.today().add(-70).years();
//Random First part
sint= window.mt_rand(endTime.getTime() , startTime.getTime());
}else{
sint = Date.parse(argument.date).getTime();
}
dateGen = new Date(sint);
totalDate = ' ' + dateGen.getFullYear();
diff = totalDate.slice(2);
century = totalDate.replace(diff, "").replace(" " , "");
yr = ' '+ dateGen.getFullYear();
day = dateGen.getUTCDate();
if (day < 10){
day = '0' + (day + 1);
}
month = dateGen.getMonth();
if (month < 10){
month = '0' + (month + 1);
}
Sstring = day + '' + month + '' + yr.slice(-2) + '-'; // getFullYear()
//generate X1 X2 X3 X4 X5 X6 - X7 X8 X9 X10 X11
codeExploded = Sstring.split('-');
dateData = window.str_split(codeExploded[0] , 1);
secondHalf = [century];
x = [];
x[1] = dateData[0] * 1;
x[2] = dateData[1] * 6;
x[3] = dateData[2] * 3;
x[4] = dateData[3] * 7;
x[5] = dateData[4] * 9;
x[6] = dateData[5] * 10;
x[7] = secondHalf[0] * 5;
//generate random number
secondHalf[1] = mt_rand(1 , 9);
secondHalf[2] = mt_rand(1 , 9);
secondHalf[3] = mt_rand(1 , 9);
if (typeof argument.second != 'undefined'){
if (typeof argument.second[0] != 'undefined'){
secondHalf[1] = argument.second[0];
}
if (typeof argument.second[0] != 'undefined'){
secondHalf[2] = argument.second[1];
}
if (typeof argument.second[0] != 'undefined'){
secondHalf[3] = argument.second[2];
}
}
//build X
x[7] = secondHalf[0] * 5;
x[8] = secondHalf[1] * 8;
x[9] = secondHalf[2] * 4;
x[10] = secondHalf[3] * 2;
total=0;
for (var i=x.length; i--;) {
if (typeof x[i] != 'undefined'){
total+=parseInt(x[i]);
}
}
subVal = 1101 - total;
secSubVal = subVal / 11 ;
if (parseInt(secSubVal) > secSubVal){
roundedSecSubVal = parseInt(secSubVal) - 1;
}else{
roundedSecSubVal = parseInt(secSubVal);
}
thirfSubVal = roundedSecSubVal * 11;
checkSumm = (subVal - thirfSubVal);
secondHalf[4] = checkSumm;
//Validate
genedPk = Sstring + secondHalf.join('');
return genedPk;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment