公众号:SAP Technical
本文作者:matinal
 

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

前言部分

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

正文部分


*&---------------------------------------------------------------------*
*& 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 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 ,
str_filedvalue TYPE c LENGTH ,readresult TYPE c LENGTH ,
curr_field_length TYPE i,_index TYPE i VALUE ,INX TYPE i VALUE ,
len TYPE i,
substring_result TYPE c LENGTH ,
count TYPE i,
str TYPE c LENGTH VALUE '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&',
read_line_index TYPE i VALUE ,write_str TYPE c LENGTH ,strlenth TYPE i ,
write_result TYPE c LENGTH ,itab_lines TYPE i,
gen_filename TYPE STRING , " generate fileName.
read_filename TYPE STRING, " read file name.
ms_type TYPE c LENGTH ,
prefix_path TYPE c LENGTH , "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(),
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 ,
read_result TYPE c LENGTH ,
fieldvalue TYPE c LENGTH ,
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 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 <> .
EXIT. "if sy-subrc <> 0.
ENDIF.
IF sy-index = .
mess_type = readresult+(). "get Message type.
idoc_number = readresult+(). "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 <> .
idoc_doment_itab-read_result = readresult.
str_format = idoc_doment_itab-read_result+(). "Reading the document. Get top 7 as a format.
IF str_format = 'E2EDL24'.
many_record = many_record + .
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+().
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 .
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 <> .
EXIT.
ENDIF.
LOOP AT zeditab_itab_two WHERE edi_index = _index.
"Append space.
IF read_index = .
write_str = zeditab_itab_two-eancomsegment.
IF STRLEN( write_str ) < .
len = - 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 =
IMPORTING
reploace_result = zeditab_itab_two-qualifiervalue.
CONCATENATE write_str zeditab_itab_two-qualifiervalue INTO write_str.
ENDIF.
read_index = read_index + .
writestr-str = write_str.
ENDLOOP.
APPEND writestr TO str_itab.
read_index = .
_index = _index + .
ENDDO.
_index = . IF itab_lines <> .
"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 . "replace #&# with ' '.
ENDDO.
SELECT COUNT(*) INTO document_number FROM ZEDITAB WHERE EDI_INDEX = INX AND MESSAGETYPE = MESS_TYPE.
INX = INX + .
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 = .
CLOSE DATASET fname_write. "close dataset.
IF itab_lines = .
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. ​
04-25 17:23