# Check Digits 101: How do you code a Check Digit algorithm for generating PRO numbers?

## Question

Check Digits 101: How do you code a Check Digit algorithm for generating PRO numbers?

What is a Check Digit?

A check digit is a digit that is appended onto an identifier using a set algorithm so a vendor can quickly verify whether the identifier you have given is a valid one. For example, if you have a credit card with a 16 digit account number, the number is generated as a 15 digit number. Then an algorithm is applied to the number to get the check digit. This check digit is then appended to the end of the 15 digit number giving you the final account number. Other vendors or software that know this algorithm can quickly verify whether you have given a valid account number by taking the first 15 digits, applying the algorithm and verifying that the 16th digit matches the result.

A Carrier gives you a new check digit algorithm you need to implement. How do you do this in DCS?

DCS provides a userexit called XWMSUE_GETEXTERNALCHECKDIGIT. There is a .sql script provided out of the box in \$DCS_BASE/database/xwmspkg/xwmsue_checkdigitcalc.sql which contains the definition for a PL/SQL procedure that defines this userexit.

According to this code, if the Check Digit field for the SCAC code in 02016 is set to 'XUSF', it will take the next PRO number in the sequence, and do the following:

1) Sum the digits 1, 3, 5, 7, and 9. Multiply this sum by 3.
2) Sum the digits 2, 4, 6, 8, and 10 and add this to the total.
3) Do a MOD 10 on this new total (divide by 10 and take the remainder.)
4) Store this value as 'OUT_CHECK_DIGIT'

After this procedure runs, the value of OUT_CHECK_DIGIT will be returned back to the system and will be appended to the next 9-digit PRO sequence number as the check digit .

This is the part of the code that does what is explained in the pseudocode above.

IF INPUT_CHECK_TYPE = 'XUSF' THEN
SELECT
RTRIM(INPUT_PREFIX1) || RTRIM(INPUT_CURRENT_PRO_NO)
INTO
TRACKING_NO
FROM
DUAL;
SELECT TO_CHAR ( MOD (
( 3 * (
to_number(NVL( SUBSTR( TRACKING_NO, 1 , 1), 0)) +
to_number(NVL( SUBSTR( TRACKING_NO, 3 , 1), 0)) +
to_number(NVL( SUBSTR( TRACKING_NO, 5 , 1), 0)) +
to_number(NVL( SUBSTR( TRACKING_NO, 7 , 1), 0)) +
to_number(NVL( SUBSTR( TRACKING_NO, 9 , 1), 0))
) ) +
(
to_number(NVL( SUBSTR( TRACKING_NO, 2 , 1), 0)) +
to_number(NVL( SUBSTR( TRACKING_NO, 4 , 1), 0)) +
to_number(NVL( SUBSTR( TRACKING_NO, 6 , 1), 0)) +
to_number(NVL( SUBSTR( TRACKING_NO, 8 , 1), 0)) +
to_number(NVL( SUBSTR( TRACKING_NO, 10, 1), 0))
)
, 10 ) )
INTO
OUT_CHECK_DIGIT
FROM
DUAL;
END IF;
SQLCD := 0;

To add a checkdigit algorithm to be used in 02016, you will need to basically:

1) Copy/paste just this code block so it's there twice.
2) In the second block, change "XUSF" to the new userexit code you want to use for the new algorithm.
3) The value of the PRO sequence is stored as TRACKING_NO (thanks to the first SELECT query). Rewrite ONLY the portion of the code where the comment is below to calculate the check digit according to the new algorithm. If you need help with PL/SQL syntax, there are plenty of free online tutorials you can use as a reference.

The rest of the code will store the value in OUT_CHECK_DIGIT for you. You do not need to modify anything else.

IF INPUT_CHECK_TYPE = 'MOD7' THEN
SELECT
RTRIM(INPUT_PREFIX1) || RTRIM(INPUT_CURRENT_PRO_NO)
INTO
TRACKING_NO
FROM
DUAL;
SELECT TO_CHAR (

--INSERT CODE TO CALCULATE CHECK DIGIT HERE!

)
INTO
OUT_CHECK_DIGIT
FROM
DUAL;
END IF;
SQLCD := 0;

4) Once finished, save a copy of the new file, log into PL/SQL and compile it using the @ operator. For help with this step, or to confirm whether the new file has been compiled correctly, check with your DBA or consult any PL/SQL tutorial, forum or programming guide.

5) You need to add the check digit value to the list of Check Digit codes by creating a new Common Codes record in 09280.

For Code Type, enter "PCD", Code Val will be your new check digit value ("XUSF", for example)

Important points to remember:

1) You can use any Chk Dgt code you wish for your custom algorithm, up to 5 characters, as long as it begins with X.

2) Your new check digit algorithm can be used with any SCAC code that has "Use Tracking No" set to Y.