SAP Function to validate Shipping container number.



FUNCTION zcontainernumber_validation.
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     VALUE(I_CONTAINERNO) TYPE  CHAR11
*”  EXPORTING
*”     VALUE(E_SUBRC) TYPE  SY-SUBRC
*”     VALUE(E_MESSAGE) TYPE  BAPI_MSG
*”     VALUE(E_CHECKDIGIT) TYPE  INT1
*”———————————————————————-

   TYPES: BEGIN OF typ_alphabet,
           char,
           value TYPE i,
          END OF typ_alphabet.
   TYPES: typ_it_alphabet TYPE STANDARD TABLE OF typ_alphabet.

   DATA: _it_alphabet TYPE typ_it_alphabet,
         _alphabet    TYPE typ_alphabet.

   DATA: _len   TYPE i,
         _index TYPE i,
         _sum   TYPE menge_d,
         _check TYPE menge_d.

   CLEAR: e_subrc,
          e_message.

   CHECK i_containerno IS NOT INITIAL.

   CONDENSE i_containerno NOGAPS.
   TRANSLATE i_containerno TO UPPER CASE.

* Check input string has enough characters
   _len = STRLEN( i_containerno ).
   IF _len <> 11.
     e_subrc = 1.
     e_message = ‘Container number must be 11 characters’.
     RETURN.
   ENDIF.

* Initialize ISO Validation Alphabet <<<
   _alphabetchar = ‘0’.
   _alphabetvalue = 0.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘1’.
   _alphabetvalue = 1.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘2’.
   _alphabetvalue = 2.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘3’.
   _alphabetvalue = 3.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘4’.
   _alphabetvalue = 4.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘5’.
   _alphabetvalue = 5.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘6’.
   _alphabetvalue = 6.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘7’.
   _alphabetvalue = 7.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘8’.
   _alphabetvalue = 8.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘9’.
   _alphabetvalue = 9.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘A’.
   _alphabetvalue = 10.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘B’.
   _alphabetvalue = 12.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘B’.
   _alphabetvalue = 12.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘C’.
   _alphabetvalue = 13.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘D’.
   _alphabetvalue = 14.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘E’.
   _alphabetvalue = 15.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘F’.
   _alphabetvalue = 16.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘G’.
   _alphabetvalue = 17.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘H’.
   _alphabetvalue = 18.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘I’.
   _alphabetvalue = 19.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘J’.
   _alphabetvalue = 20.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘K’.
   _alphabetvalue = 21.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘L’.
   _alphabetvalue = 23.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘M’.
   _alphabetvalue = 24.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘N’.
   _alphabetvalue = 25.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘O’.
   _alphabetvalue = 26.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘P’.
   _alphabetvalue = 27.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘Q’.
   _alphabetvalue = 28.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘R’.
   _alphabetvalue = 29.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘S’.
   _alphabetvalue = 30.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘T’.
   _alphabetvalue = 31.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘U’.
   _alphabetvalue = 32.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘V’.
   _alphabetvalue = 34.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘W’.
   _alphabetvalue = 35.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘X’.
   _alphabetvalue = 36.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘Y’.
   _alphabetvalue = 37.
   APPEND _alphabet TO _it_alphabet.
   _alphabetchar = ‘Z’.
   _alphabetvalue = 38.
   APPEND _alphabet TO _it_alphabet.
* >>> Initialize ISO Validation Alphabet

* Remove illegal characters
   CLEAR _index.
   WHILE _index < 11.
     READ TABLE _it_alphabet INTO _alphabet
                             WITH KEY char = i_containerno+_index(1).
     IF sysubrc = 0.
       IF _index < 3 AND
          _alphabetvalue < 10.“it is numeric
         e_subrc = 3.
         e_message = ‘First 3 chars of container no must be letter’.
         EXIT.
       ELSEIF _index = 3 AND
              _alphabetchar <> ‘U’ AND
              _alphabetchar <> ‘J’ AND
              _alphabetchar <> ‘Z’.
         e_subrc = 4.
         CONCATENATE ‘4th char of container no can be’
                     ‘U(All freight containers) or’
                     ‘J(Detachable freight container-related equipment)’
                     ‘or Z(Trailers and Chassis)’
                INTO e_message
         SEPARATED BY space.
         EXIT.             

       ELSEIF _index > 3 AND

                    _alphabetvalue >= 10. “it is alphabetic

         e_subrc = 5.

         e_message = ‘Last 7 digits of container no must be numeric’.

         EXIT.

       ENDIF.
       _index = _index + 1.
     ELSE.
       e_subrc = 2.
       e_message = ‘Container no must contain only letters and numbers’.
       EXIT.
     ENDIF.
   ENDWHILE.
   CHECK e_subrc IS INITIAL.

* Calculate the sum by the ISO Formula
   CLEAR _index.
   WHILE _index < 10.“except check digit
     READ TABLE _it_alphabet INTO _alphabet
                             WITH KEY char = i_containerno+_index(1).
     _sum = _sum + ( _alphabetvalue * ( 2 ** _index ) ).
     _index = _index + 1.
   ENDWHILE.

* Calculate the Check number with the ISO Formula
   _check = _sum ( FLOOR( _sum / 11 ) * 11 ).

* Set CheckNumber 0 if it is 10 – In some cases this is needed
   IF _check = 10.
     CLEAR _check.
   ENDIF.

   IF _check = i_containerno+10(1).
     CLEAR e_subrc.
     e_message = ‘Container no is valid’.
   ELSE.
     e_subrc   = 8.
     e_message = ‘Container no is not valid’.
   ENDIF.

   e_checkdigit = _check.

ENDFUNCTION.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply