Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
basarozgur_kahraman
Contributor
3,726

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 NO-GAPS.
   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 <<<
   _alphabet-char = '0'.
   _alphabet-value = 0.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '1'.
   _alphabet-value = 1.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '2'.
   _alphabet-value = 2.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '3'.
   _alphabet-value = 3.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '4'.
   _alphabet-value = 4.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '5'.
   _alphabet-value = 5.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '6'.
   _alphabet-value = 6.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '7'.
   _alphabet-value = 7.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '8'.
   _alphabet-value = 8.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = '9'.
   _alphabet-value = 9.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'A'.
   _alphabet-value = 10.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'B'.
   _alphabet-value = 12.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'B'.
   _alphabet-value = 12.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'C'.
   _alphabet-value = 13.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'D'.
   _alphabet-value = 14.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'E'.
   _alphabet-value = 15.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'F'.
   _alphabet-value = 16.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'G'.
   _alphabet-value = 17.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'H'.
   _alphabet-value = 18.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'I'.
   _alphabet-value = 19.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'J'.
   _alphabet-value = 20.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'K'.
   _alphabet-value = 21.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'L'.
   _alphabet-value = 23.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'M'.
   _alphabet-value = 24.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'N'.
   _alphabet-value = 25.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'O'.
   _alphabet-value = 26.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'P'.
   _alphabet-value = 27.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'Q'.
   _alphabet-value = 28.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'R'.
   _alphabet-value = 29.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'S'.
   _alphabet-value = 30.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'T'.
   _alphabet-value = 31.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'U'.
   _alphabet-value = 32.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'V'.
   _alphabet-value = 34.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'W'.
   _alphabet-value = 35.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'X'.
   _alphabet-value = 36.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'Y'.
   _alphabet-value = 37.
   APPEND _alphabet TO _it_alphabet.
   _alphabet-char = 'Z'.
   _alphabet-value = 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 sy-subrc = 0.
       IF _index < 3 AND
          _alphabet-value < 10."it is numeric
         e_subrc = 3.
         e_message = 'First 3 chars of container no must be letter'.
         EXIT.
       ELSEIF _index = 3 AND
              _alphabet-char <> 'U' AND
              _alphabet-char <> 'J' AND
              _alphabet-char <> '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

                    _alphabet-value >= 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 + ( _alphabet-value * ( 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.

Labels in this area