Tuesday 12 March 2013

PLSQL Modulo 11 check...

What is Modulo 11 check?

MSI was developed by the MSI Data Corporation, based on the original Plessey Code. MSI, also known as Modified Plessey, is used primarily to mark retail shelves for inventory control. MSI is a continuous, non-self-checking symbology. While the length of an MSI bar code can be of any length, a given application usually implements a fixed-length code. 
A typical MSI bar code is


MSI, and other symbologies based on Pulse-Width Modulation, offer no significant benefit over more modern symbologies. While it is not a bad idea to support MSI for legacy bar codes, most new applications do not choose MSI as their symbology of choice.


COMPUTING THE CHECKSUM DIGIT MSI 

uses one or two check digits, which may be calculated in a number of different ways. As such, it is really up to the software application to implement and check the check digit(s).

The most common methods of calculating MSI check digits are: Modulo 10 and Modulo 11


Modulo 11 is what we want which is developed by IBM company based on Lhun algorithm...

A check digit is a number that is used to validate a series of numbers whose accuracy you want to insure. Frequently the last digit of a number string such as identification number is a check digit. Lets say the identification number starts out at 6 digits. A calculation is done using the six digits and a seventh digit is produced as a result of the calculation. This number is the check digit. There are many calculations that can be used - this example illustrates the logic of the MOD11 check digit.
Steps to calculate the MOD11 check digit for a number such as an id #:

    • Assign weights to each digit of the id #. The weights in MOD11 are from 2 through a maximum of 10 beginning with the low order position in the field.
    • Each digit in the id # is multiplied by its weight
    • The results of the multiplication are added together
    • This product is divided by the modulus number 11
    • The remainder is subtracted from the modulus number 11 giving the check digit
Example: find the check digit for the number 036532

0
3
6
5
3
2
x7
x6
x5
x4
x3
x2
0
18
30
20
9
4
0 + 18 + 30 + 20 + 9 + 4 = 81
81/11 = 7 remainder 4
11 - 4 = 7
7 is therefore the check digit.
PROBLEMS: If the remainder from the division is 0 or 1, then the subtraction will yield a two digit number of either 10 or 11. This won't work, so if the check digit is 10, then X is frequently used as the check digit and if the check digit is 11 then 0 is used as the check digit. If X is used, then the field for the check digit has to be defined as character (PIC X) or there will be a numeric problem.
Steps to verify if the check digit is included as part of the number:

    • The entire number is multiplied by the same weights that were used to calculate and the check digit itself is multiplied by 1.
    • The results of the multiplication are added together.
    • The sum is divided by 11 and if the remainder is 0, the number is correct.
PROBLEM: Note that if the check digit is X then 10 is used in the multiplication. Code for this occurrence must be included.
Example of verifying the number 0365327 where 7 is the calculated MOD11 check digit:
0
3
6
5
3
2
7
x7
x6
x5
x4
x3
x2
x1
0
18
30
20
9
4
7
0 + 18 + 30 + 20 + 9 + 4 + 7 = 88
88/11 is 8 remainder 0
Since the remainder from this calculation is 0, the check digit 7 is valid.

So without any other complications here is the PL code...

procedure modulo_11 (l_input in varchar2) as
l_cntrl_num_sum number(1):= 0;
l_pom varchar2(30):=' ';
l_length number(30):= 0;
l_length_pom number(30):= 0;
l_sum number(30):= 0;
l_error number(2) := 0;
----
l_counter number(30);
l_sum1 number(30):=0;
l_sum_uk number(30):=0;
l_reminder number(30) := 0;
l_control_number number(30) := 0;
l_counter1 number(2) := 0;
l_errmsg varchar2(200) := ' ';
e_exit exception;
begin
-- test input
--l_input  := '102305789016';
   -- determine length of input num
   l_length := length(l_input);
   -- take last digit for control
   l_cntrl_num_sum := substr(l_input,l_length,1);
   -- set length -1 sign
   -- take length in some local variable
   l_length_pom := l_length;
   -- set variable counters to zero
   l_counter := 0;
   l_reminder := 0;
   l_control_number := 0;
   l_counter :=0;
   -- counter to 1
   l_counter1 := 1;
   l_pom := null;
   -- reversing string input
   select reverse(l_input) into l_pom from dual;
   -- taking second place till last in local var which is used for calculating
   l_pom := substr(l_pom,2,l_length_pom - 1);
   l_length := length(l_pom);
   if l_length is null then
      l_error := 2;
      raise e_exit;
   end if;
   -- starting algorithm
   for k in 1..l_length
      loop
         l_sum := 0;
         l_sum1 :=0;
         l_counter := l_counter + 1;
         l_counter1 :=  l_counter1 + 1;
         l_sum := l_counter1 * substr(l_pom,l_counter,1);
         l_sum1 := l_sum;        
         l_sum_uk := l_sum_uk + l_sum1;
         l_length_pom := l_length_pom - 1;
      end loop;
   l_length := length(l_input) - 1;
   l_reminder := mod(l_sum_uk,11);
   l_control_number := 11 - l_reminder;
   if l_reminder in (0, 1) then
      l_control_number := 0;
   end if;
   if l_control_number != l_cntrl_num_sum then
      l_error := 2;
      l_errmsg := ('error in MOD_11_INI :');
      raise e_exit;
   end if;
exception
   when e_exit then
      raise_application_error(-20010,l_errmsg||' '||sqlcode);
  END modulo_11;

enjoy.....
text sources: 
http://www.pgrocer.net/Cis51/mod11.html
http://en.wikipedia.org/wiki/Luhn_algorithm
http://en.wikipedia.org/wiki/MSI_Barcode

0 comments:

Post a Comment