【ABAP系列】SAP 一个完整的SAP的Abap例子(idoc,edi文件的相互转换)

公众号:SAP Technical

本文作者:matinal

原文出处:http://www.cnblogs.com/SAPmatinal/

原文链接:【ABAP系列】SAP 一个完整的SAP的Abap例子(idoc,edi文件的相互转换)

前言部分

大家可以关注我的公众号,公众号里的排版更好,阅读更舒适。

正文部分

?
*&---------------------------------------------------------------------*
*& 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的代码如下:

?
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.

?

原文地址:https://www.cnblogs.com/SAPmatinal/p/11183785.html

时间: 2024-11-10 10:40:07

【ABAP系列】SAP 一个完整的SAP的Abap例子(idoc,edi文件的相互转换)的相关文章

一个完整的SAP RFC调用接口封装

因为经常需要访问sap操作数据,就封装了一个类方便调用,运行条件需要安装sap客户端,在sap客户端安装之后会带有一个com接口,本接口就通过这个com访问sap,因为com的后期绑定问题故使用了vb.net开发,分享给大家. 特色之处:把SAP的传入.传出内表直接映射成dotNet的DataTable方便操作,并提供了从字段列表到DataTable的转换函数. 1 SAP 操作 1.1 创建结构 ZMM_MATNR ( tcode: se11) 1.2 创建 RFC 函数 ZTEST2 ( t

【ABAP系列】SAP ABAP MIR7预制凭证BAPI

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP MIR7预制凭证BAPI 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 做预制凭证有两个BAPI: BAPI_INCOMINGINVOICE_PARK 和 BAPI_INCOMINGINVOICE_CREATE 连个的区别是:第一个只做发票预制,第二个是发票和过账一块做. 下面简单

【ABAP系列】SAP ABAP 开发中的SMARTFORMS 参数

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 开发中的SMARTFORMS 参数 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 &symbol& (括号中,小写字母为变量)&symbol&  屏蔽从第一位开始的N位&symbol (n)&   只显示前N位&symbol (S)&a

【ABAP系列】SAP ABAP SY-SUBRC的含义解析

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP SY-SUBRC的含义解析 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 使用SELECT语句选择查询:SY-SUBRC = 0: 至少有一行数据,当ENDSELECT语句执行完,SY-DBCNT中保存着记录的个数.SY-SUBRC = 4: 没有数据.SY-SUBRC = 8:

【ABAP系列】SAP ABAP 动态指针

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 动态指针 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 有收有放,转在说明,后面举例,给初学者(因为有很多人问到我这个) 关于abap指针的一些资料.    1.什么是abap指针:    在abap里面,fieldsymbol就相当于c语言的指针.如果你定义并且分配了相应的结构或

【ABAP系列】SAP ABAP7.40新语法简介第一篇

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP7.40新语法简介第一篇 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 SAP的系统不断的更新 7.40已经出来很久了,一直忙着没有更新7.40新语法内容 慢慢写点新语法的相关内容 首先说一下7.40的特点及简介 1.ABAP 7.40是AS ABAP 7.31(也称为ABAP 7.

【ABAP系列】SAP ABAP 关于FUNCTION-POOL的理解

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 关于FUNCTION-POOL的理解 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 很多初学者在学到函数这一块,下面的解释送给初学者 当创建的时候,都会遇到关于FUNCTION-POOL的疑问 下面解释一下,同时,感谢nancy的讲解 FUNCTION-POOL的作用 其实,就是声

【ABAP系列】SAP Web Dynpro 技术简介

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP Web Dynpro 技术简介 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 AP webdynpro是为SAP NetWeaver战略UI编程模型. 它是用于创建平台并独立于语言,基于WEB的用户界面. Web Dynpro可用于SAP NetWeaver Application Serv

【ABAP系列】SAP ABAP 如何控制Dialog中的键盘(回车)功能

公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 如何控制Dialog中的键盘(回车)功能 前言部分 大家可以关注我的公众号,公众号里的排版更好,阅读更舒适. 正文部分 MODULE USER_COMMAND_2100 INPUT. CLEAR SAVE_OK. SAVE_OK = OK_CODE. CLEAR OK_CODE. CASE SAVE_OK. WH