Thursday, March 26, 2009

NPI check digit validation - PeopleCode Sample

/* NPI check digit Validation PeopleCode
:::::::::::::::: How to do NPI validation ::::::::::::::::::

Assume the 9-position identifier part of the NPI is 123456789.
Using the Luhn formula on the identifier portion, the check digit is calculated as follows:
NPI without check digit: 1 2 3 4 5 6 7 8 9

Step 1: Double the value of alternate digits, beginning with the rightmost digit.
2 6 10 14 18

Step 2: Add constant 24, to account for the 80840 prefix that would be present on a card issuer identifier, plus the individual digits of products of doubling, plus unaffected digits.
24 + 2 + 2 + 6 + 4 + 1 + 0 + 6 + 1 + 4 + 8 + 1 + 8 = 67

Step 3: Subtract from next higher number ending in zero.
70 – 67 = 3

Check digit = 3

NPI with check digit = 1234567893

**********************************************************************/


If %Component = Component.LICENSES_CERTIF Then
If All(LIC_CERT_NBR) And
ACCOMPLISHMENT = "US-NPI" Then

For &I = 1 To Len(RTrim(LIC_CERT_NBR))
&CHAR = Substring(LIC_CERT_NBR, &I, 1);
&ASCII = Code(Substring(LIC_CERT_NBR, &I, 1));
If (&ASCII > 57 or &ASCII <> 10 Then
Error MsgGet(22000, 3, "NPI number should contain 10 digit numeric data only");
End-If;

/* We should always see 10 digit NPI code in Application */
&I = 10;
/* mysum initialized to 24 since it accounts for the first 5 digits 80840 prefix */
&mysum = 24;
&r = 0;
&bflag = 1;


While &I > 0
If &bflag = 0 Then
&r = Value(Substring(LIC_CERT_NBR, &I, 1)) * 2;

If &r > 9 Then
&r = &r - 9;
End-If;
&mysum = &mysum + &r;
&bflag = 1;
Else
&mysum = &mysum + Value(Substring(LIC_CERT_NBR, &I, 1));
&bflag = 0;
End-If;

&I = &I - 1;
/* WinMessage("Mysum is" &mysum) */
End-While;
If ((Mod(&mysum, 10) = 0) And
&mysum <> 0) Then
&a = "Good NPI code"
Else
Error MsgGet(22000, 4, "The NPI number entered is invalid. Please verify.");

End-If;

End-If;
End-If;

No comments:

Post a Comment