1. 程式人生 > >一個完整的SAP的Abap例子(idoc,edi檔案的相互轉換)

一個完整的SAP的Abap例子(idoc,edi檔案的相互轉換)

Sql程式碼

*&---------------------------------------------------------------------*
*& Report  Z_IDOC_EDI_CONVERT                                          *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  Z_IDOC_EDI_CONVERT                   .
TABLES:ZEDITAB,EDISDEF,EDSAPPL.
*&---------------------------------------------------------------------*
*& Author: ZHT.
*& paramenters region.(parameters)
*&---------------------------------------------------------------------*
PARAMETERS:
           P_FILE LIKE FILEPATH-PATHINTERN OBLIGATORY ,                  "read file path.
           W_FILE LIKE FILEPATH-PATHINTERN OBLIGATORY ,                  "write file path
           CRITERIA TYPE C LENGTH 100 DEFAULT '*' OBLIGATORY,
           IDOC_EDI RADIOBUTTON GROUP RDO,                                      "radio button. idoc convert edi.
           EDI_IDOC RADIOBUTTON GROUP RDO.                                      "radio button edi convert idoc.

IF IDOC_EDI  = 'X'.
   " Choose idoc convert to edi.
    CALL  FUNCTION 'ZIDOC_CONVERT_EDI'
         EXPORTING
                    P_FILE = P_FILE
                    W_FILE = W_FILE
                    CRITERIA = CRITERIA.
ELSEIF EDI_IDOC = 'X'.
    " Choose edi convert to idoc..
    CALL  FUNCTION 'ZEDI_CONVERT_IDOC'
          EXPORTING
                      P_FILE = P_FILE
                      W_FILE = W_FILE
                      CRITERIA = CRITERIA.
ENDIF.
idoc convert edi的程式碼如下:

sql程式碼

FUNCTION ZIDOC_CONVERT_EDI.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(P_FILE) TYPE  FILENAME-FILEINTERN
*"     REFERENCE(W_FILE) TYPE  FILENAME-FILEINTERN
*"     REFERENCE(CRITERIA) TYPE  ZCRITERIA
*"----------------------------------------------------------------------
  DATA:
           readfielname LIKE filepath-pathintern  ,writefielname LIKE filepath-pathintern ,
           mess TYPE string,segtype LIKE edsappl-segtyp,str_format TYPE c LENGTH 7,
           str_filedvalue TYPE c LENGTH 10,readresult TYPE c LENGTH 2000,
           curr_field_length TYPE i,_index TYPE i VALUE 1,INX TYPE i VALUE 1,
           len TYPE i,
           substring_result TYPE c LENGTH 2000,
           count TYPE i,
           str TYPE c LENGTH 500 VALUE '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&',
           read_line_index TYPE i VALUE 0,write_str TYPE c LENGTH 3000,strlenth TYPE i ,
           write_result TYPE c LENGTH 3000,itab_lines TYPE i,
           gen_filename TYPE STRING ,               " generate fileName.
           read_filename TYPE STRING,               " read file name.
           ms_type TYPE c LENGTH 100,
           prefix_path TYPE c LENGTH 50,            "prefix path,
           max_length TYPE i,                              "last  a record .
           sigent_filename TYPE Z_FILE_NAME,
           document_number TYPE EDI4DOCNUC,
           idoc_number TYPE EDI4DOCNUC,

           wname TYPE string,                             "temp write file name.
           fname_write TYPE String,
           fname_read(260),
           many_record TYPE i,
           mess_type TYPE zmestype.                 "message type.
           readfielname = p_file.                            "logical path.
           writefielname = w_file.                           "physical file.
*&Structure region:(work area)Author: ZHT.
  DATA:BEGIN OF idoc_doment_wa,
                format_str TYPE c LENGTH 7 ,
                read_result TYPE c LENGTH 3000,
                fieldvalue TYPE c LENGTH 10,
  END OF idoc_doment_wa.
  DATA:BEGIN OF writestr,                               "declare a structure ,
                  str TYPE string,
                  strlength TYPE i,
  END OF writestr.
  DATA:str_itab LIKE TABLE OF writestr.
*&Internal Tables  Region.*& Author: ZHT.
  DATA:zeditab_itab LIKE STANDARD TABLE OF zeditab WITH HEADER LINE,
            zeditab_itab_two LIKE STANDARD TABLE OF zeditab WITH HEADER LINE,
            itab_edsappl LIKE STANDARD TABLE OF edsappl WITH HEADER LINE ,
            idoc_doment_itab LIKE idoc_doment_wa OCCURS 0 WITH HEADER LINE,
            tb_list_of_file LIKE TABLE OF rsfillst,
            wa_list_of_file LIKE LINE OF tb_list_of_file.
*&file process .parameters: readfielname and writefielname return a writefielname internal table.
  CALL FUNCTION 'ZIDOC_EDI_FILEPROCESS_PATH_FUN'
    EXPORTING
         CRITERIA = CRITERIA
         READFIELNAME     = readfielname
         READ_FILENAME   = read_filename
         WRITEFIELNAME   = writefielname
         GEN_FILENAME     = gen_filename
   IMPORTING
         FNAME_WRITE      = fname_write
   TABLES
         TB_LIST_OF_FILE  = tb_list_of_file.
          prefix_path = fname_write.
"&loop all files.
  LOOP AT tb_list_of_file INTO wa_list_of_file.
    CONCATENATE  wa_list_of_file-dirname wa_list_of_file-name INTO fname_read .
    IF wa_list_of_file-name <> '.'  AND wa_list_of_file-name <> '..'.
*&open dataset.(read file).
      OPEN DATASET fname_read FOR INPUT  IN TEXT MODE ENCODING DEFAULT MESSAGE mess.
      DO.
        READ DATASET  fname_read INTO readresult.
        IF sy-subrc <> 0.
          EXIT.                                                   "if sy-subrc <> 0.
        ENDIF.
        IF sy-index = 1.
                mess_type = readresult+99(6).             "get Message type.
                idoc_number = readresult+13(16).         "get idoc number.
                CLEAR: zeditab_itab,idoc_doment_itab.
                SELECT * INTO CORRESPONDING FIELDS OF TABLE zeditab_itab FROM zeditab WHERE messagetype = mess_type AND zidefaultidoc = ''.     "search  data,. return zditab internal table.
                itab_lines = LINES( zeditab_itab ).
                wname = wa_list_of_file-name.
                CALL  FUNCTION 'ZGENERATE_FILENAME_FUNCTION'
                    EXPORTING
                              MESS_TYPE = MESS_TYPE
                              READ_FILE_NAME = WNAME
                              ZTYPE = 'IDOC_EDI'
                    IMPORTING
                              WRITE_FINENAME = gen_filename.
                               sigent_filename = gen_filename.
                               fname_write = gen_filename.
                               CLEAR gen_filename.
        ENDIF.
"&if is first line.
        IF sy-index <> 1.
                idoc_doment_itab-read_result = readresult.
                str_format = idoc_doment_itab-read_result+0(7).                                                              "Reading the document. Get top 7 as a format.
                IF str_format = 'E2EDL24'.
                     many_record = many_record + 1.
                ENDIF.
                         CALL FUNCTION 'ZHT_SEARCH_EDISDEF'                                                          "Call function,import format. return a SEGTYPE.
                                    EXPORTING
                                           strformat = str_format
                                    IMPORTING
                                           segtype   = segtype.
                         idoc_doment_itab-format_str = segtype.
                         idoc_doment_itab-fieldvalue = readresult+63(3).
                         APPEND idoc_doment_itab.
                         CLEAR str_format.
         ENDIF.
      ENDDO.
      CLOSE DATASET fname_read.   "Close DataSet
"&Automatic single data processing
     CALL FUNCTION 'ZSINGLE_RECORD_FUNCTION'
             TABLES
                   ZEDITAB_ITAB_TWO = ZEDITAB_ITAB_TWO
                   ZEDITAB_ITAB = ZEDITAB_ITAB
                   IDOC_DOMENT_ITAB = IDOC_DOMENT_ITAB.
"&Automatic many data processing
    CALL FUNCTION 'ZMANY_RECORD_FUNCTION'
                    EXPORTING
                         many_record = many_record
                   TABLES
                         ZEDITAB_ITAB_TWO = ZEDITAB_ITAB_TWO
                         ZEDITAB_ITAB = ZEDITAB_ITAB
                         IDOC_DOMENT_ITAB = IDOC_DOMENT_ITAB.
      CLEAR: many_record.
      DATA:read_index TYPE i VALUE 1.
      DATA:sum_num TYPE i.
      LOOP AT zeditab_itab_two.
            sum_num = zeditab_itab_two-edi_index.
      ENDLOOP.
      REFRESH: zeditab_itab,IDOC_DOMENT_ITAB,str_itab.
"&loop all line.
      DO sum_num TIMES.
                    IF sy-subrc <> 0.
                          EXIT.
                    ENDIF.
                    LOOP AT zeditab_itab_two WHERE edi_index = _index.
                             "Append space.
                             IF read_index = 1.
                                    write_str = zeditab_itab_two-eancomsegment.
                                    IF STRLEN( write_str ) < 6.
                                      len = 6 - STRLEN( write_str ).
                                      strlenth = STRLEN( str ) - len.
                                      CONCATENATE write_str str+strlenth(len) INTO write_str.
                                    ENDIF.
                             ENDIF.
                            IF zeditab_itab_two-qualifiervalue = space.
                                       strlenth = STRLEN( str ) - zeditab_itab_two-length.
                                       CONCATENATE write_str str+strlenth(zeditab_itab_two-length) INTO write_str.
                            ELSE.
                                     CALL FUNCTION 'ZIDOC_EDI_REPLACE_FUNCTION'  "CALL  FUNCTION . replace str. return  a resullt.
                                             EXPORTING
                                               qualifiervalue  = zeditab_itab_two-qualifiervalue
                                               format          = zeditab_itab_two-z_format
                                               param_num       = 5
                                               IMPORTING
                                               reploace_result = zeditab_itab_two-qualifiervalue.
                                     CONCATENATE write_str zeditab_itab_two-qualifiervalue INTO write_str.
                          ENDIF.
                            read_index = read_index + 1.
                            writestr-str = write_str.
                   ENDLOOP.
                   APPEND writestr TO str_itab.
                   read_index = 1.
                   _index  = _index + 1.
      ENDDO.
      _index = 1.

      IF itab_lines <> 0.
        "OPEN DATASET .
        CONCATENATE prefix_path fname_write INTO fname_write.
        OPEN DATASET fname_write FOR OUTPUT IN TEXT MODE ENCODING DEFAULT MESSAGE mess.
               LOOP AT str_itab INTO writestr.
                 count = STRLEN( writestr-str ).
                 DO count TIMES.
                   REPLACE '&' WITH ' ' INTO writestr-str LENGTH 1.   "replace #&# with  ' '.
                 ENDDO.
                 SELECT COUNT(*) INTO document_number FROM ZEDITAB WHERE EDI_INDEX = INX  AND MESSAGETYPE = MESS_TYPE.
                 INX = INX + 1.
                 CALL FUNCTION 'IDOC_EDI_LOGDATA_FUN'
                               EXPORTING
                                      DOCUMENT_NUMBER = idoc_number                     "idoc number.
                                      IDOC_MESSAGETYPE = MESS_TYPE
                                      IDOC_NUMBER = document_number                        "document number.
                                      FILE_NAME = sigent_filename.
                                      WRITE: / writestr-str.
                                      TRANSFER writestr-str TO fname_write.
               ENDLOOP.
               INX = 1.
        CLOSE DATASET fname_write.  "close dataset.
        IF itab_lines = 0.
            MESSAGE e000(ZIDOC_EDI_MSG).
        ELSE.
          MESSAGE s001(ZIDOC_EDI_MSG) WITH fname_write.
        ENDIF.
          WRITE: / sy-uline.
      ENDIF.
    CLEAR fname_write.
    REFRESH: zeditab_itab,str_itab,zeditab_itab_two.
*           DATA:filename TYPE filepath-pathintern.
*            filename = wa_list_of_file-name.
*            IF MESS_TYPE <> 'ORDRSP'.
*                      CALL FUNCTION 'ZMOVEOUTEDI_IDOCFILEFUNCITON'
*                EXPORTING
*                     resourefilepath = fname_read
*                     messagetype     = mess_type
*                     filename        = filename.
*            ENDIF.
    ENDIF.
  ENDLOOP.
ENDFUNCTION.