`

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

    博客分类:
  • SAP
阅读更多
*&---------------------------------------------------------------------*
*& 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.

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    SAP ABAP IDOC Interface

    根据文档标题“BC-IDocInterface:EDI Application Scenarios”以及描述“SAP ABAP IDOC Interface”,我们可以推测此文档主要介绍了如何利用SAP ABAP中的IDOC接口来进行EDI相关的应用开发。文档中可能包含以下内容:...

    sap abap 创建IDOC

    在WE30中,你可以定义一个新IDOC类型,为它分配一个唯一的编号和描述,以便标识不同的数据交换场景。 3) **创建消息类型(WE81)** 消息类型定义了IDOC的上下文,即它代表的业务事件,如订单确认或发货通知。在WE...

    SAP-ABAP+IDOC+Interface.pdf

    SAP-ABAP+IDOC+Interface.pdf 文档是关于SAP系统中ABAP编程语言、IDOC技术以及BC-IDocInterface模块在EDI应用中的详细介绍。通过学习这份文档,读者可以深入了解如何利用这些技术实现企业间的高效数据交换和集成,...

    SAP-ABAP-IDOC使用材料资料.pdf

    SAP-ABAP-IDOC 使用材料资料 SAP-ABAP-IDOC 是一种系统间通用的数据交换格式,通过 IDoc 接口可以实现 SAP 系统之间以及 SAP 系统与其他系统之间的数据交换。基于 IDoc 的应用技术有:ALE、EDI 等。 IDoc 的基本...

    sapabap开发从入门到精通

    资源名称:sap abap开发从入门到精通 内容简介: 《SAP ABAP开发从入门到精通》以应用实例的形式,讲解了SAPABAP开发的相关知识点,详细介绍了SAPABAP开发者必须掌握的包与变更传输系统(CTS)、数据类型、数据库...

    通过 EDI IDOC 处理销售订单

    在这个过程中,IDOC (Intermediate Document) 是关键的数据载体,它是一个中间文档格式,用于在不同的应用程序和系统之间交换信息。在SAP系统中,IDOCs扮演着桥梁的角色,连接了SAP系统与外部业务伙伴的系统。 1. *...

    SAP ABAP 调用HTTPS ; 上传文件并调用接口

    SAP ABAP 调用HTTPS ; 上传文件并调用接口

    SAP ABAP 电子书

    SAP ABAP(Advanced Business Application Programming)是SAP公司推出的一种高级业务应用编程语言,用于开发在SAP R/3和SAP NetWeaver平台上的应用程序。这些电子书提供了全面的资源,帮助学习者深入理解并掌握SAP ...

    ABAP开发从入门到精通-高清自学版 SAP+ABAP开发从入门到精通 SAP开发自学必读 SAP SAP开发自学入门到精通

    "SAP SAP开发自学入门到精通完整版HANA开发"提及了SAP HANA,这是一个高性能的数据分析平台。在SAP HANA环境中,ABAP开发者需要学习如何利用HANA的特性,比如列式存储、实时分析和计算视图,来优化业务应用的性能。 ...

    sap abap query高级功能

    关于程序的生成,ABAP Query 还提供了一个独特的功能,即可以直接将查询转换为一个完整的 ABAP 报表程序。这样做可以让你进一步定制和优化生成的程序,包括调整性能、增加额外功能或修复潜在问题。这为那些需要更...

    SAP ABAP 1.4.4代码下载

    首先,SAP ABAP不仅是一种编程语言,还是一个完整的开发环境,它包含了编写、测试和调试代码的所有工具。在1.4.4这样的版本中,通常会包含一系列的改进和修复,以提高性能、稳定性和用户体验。这可能包括新的语法...

    SAP ABAP 实用程序开发攻略

    文档的后半部分深入到ABAP开发平台ABAP Workbench,它是SAP系统中一个关键的开发工具。开发者可以通过ABAP Workbench进行各种开发活动,包括程序设计、调试、测试和维护等。文档还介绍了ABAP数据字典,它是存储了...

    SAP ABAP开发从入门到精通

    《SAP ABAP开发从入门到精通》以应用实例的形式,讲解了SAP ABAP开发的相关知识点,详细介绍了SAP ABAP开发者必须掌握的包与变更传输系统(CTS)、数据类型、数据库、模块化程序、内表、调试、ABAP数据字典、锁对象...

    实战SAP程序开发——从实例学SAP ABAP编程.rar

    SAP是一种全球领先的业务软件系统,它用于企业管理各种复杂流程,包括财务、供应链、人力资源等。...实践是学习编程的最佳途径,这个资源提供了一个良好的起点,帮助你在SAP ABAP的世界里稳步前行。

    sap IDOC 错误处理

    - **BD87**:这是一个用于查看和修改 IDOC 状态的事务码。通过这个工具,你可以检查所有相关的错误信息,包括错误代码、错误文本和错误发生的步骤。同时,你可以在这里对 IDOC 进行重新发送,尝试解决错误。 - **...

    SAP ABAP 代码备份下载到本地程序代码

    SAP ABAP 代码备份下载到本地程序代码

    SAP ABAP开发快捷键

    然而,在ABAP开发过程中,效率是一个非常重要的因素。为了提高开发效率,熟悉ABAP开发快捷键是非常必要的。在本文中,我们将详细介绍SAP ABAP开发快捷键,帮助开发者快速掌握ABAP开发技巧。 注释快捷键 在ABAP开发...

    ABAP 外部链接下载文件

    根据给定文件的信息,本文将围绕“ABAP外部链接下载文件”的主题展开,详细解析如何在SAP ABAP环境中利用URL实现文件下载的功能,并对代码片段进行深入分析。 ### 核心知识点概述 1. **ABAP环境中的HTTP客户端操作...

    sapjbidoc10P_5-20007299-Patch 5 for SAP Java Base IDoc Library 1.0.zip

    标题 "sapjbidoc10P_5-20007299-Patch 5 for SAP Java Base IDoc Library 1.0.zip" 暗示了这是一个针对SAP Java Base IDoc Library的更新补丁,版本号为1.0,并且是第五个补丁。IDoc(Intermediate Document)是SAP...

Global site tag (gtag.js) - Google Analytics