项目中,期初采购申请批导也是必须的,下面代码是利用BAPI BAPI_PR_CREATE开发的采购申请批导函数,批导结果会保存在txt文本文件中,分享一下,希望对用到的兄弟有帮助。
*&---------------------------------------------------------------------*
*& Report ZMM_PR_BATCH_INPUT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZMM_PR_BATCH_INPUT .
TYPES:BEGIN OF TY_ITAB,
WBS_ELEMENT(24),
MATERIAL(18),
QUANTITY(13),
UNIT(3),
DELIV_DATE(8),
PURCH_ORG(4),
* KNTTP TYPE EBAN-KNTTP,
END OF TY_ITAB.
DATA:ITAB TYPE TABLE OF TY_ITAB WITH HEADER LINE.
DATA:WA TYPE TY_ITAB.
TYPES:BEGIN OF TY_RESULT,
WBS_ELEMENT(24),
MATERIAL(18),
QUANTITY(13),
UNIT(3),
DELIV_DATE(8),
PURCH_ORG(4),
* KNTTP TYPE EBAN-KNTTP,
FLAG(1),
NUMBER TYPE BAPIMEREQHEADER-PREQ_NO,
MESSAGE(1200),
END OF TY_RESULT.
DATA:IT_RESULT TYPE TABLE OF TY_RESULT with header line.
data: g_info type string.
DATA:PRHEADER TYPE BAPIMEREQHEADER,
PRHEADERX TYPE BAPIMEREQHEADERX,
NUMBER TYPE BAPIMEREQHEADER-PREQ_NO,
PRITEM TYPE TABLE OF BAPIMEREQITEMIMP WITH HEADER LINE,
PRITEMX TYPE TABLE OF BAPIMEREQITEMX WITH HEADER LINE,
PRITEMTEXT TYPE TABLE OF BAPIMEREQITEMTEXT WITH HEADER LINE,
PRACCOUNT TYPE TABLE OF BAPIMEREQACCOUNT WITH HEADER LINE,
PRACCOUNTX TYPE TABLE OF BAPIMEREQACCOUNTX WITH HEADER LINE,
RETURN TYPE TABLE OF BAPIRET2 WITH HEADER LINE.
DATA:FLAG(1).
selection-screen begin of block shi with frame title text-001.
parameters:p_file like rlgrap-filename memory id zy1 obligatory.
PARAMETERS:P_WERKS LIKE MARC-WERKS OBLIGATORY.
parameters:P_CHECK RADIOBUTTON GROUP SHI,
P_CHECK1 RADIOBUTTON GROUP SHI.
selection-screen end of block shi.
at selection-screen on value-request for p_file.
CALL FUNCTION ‘F4_FILENAME‘
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
IMPORTING
file_name = p_file.
*at selection-screen.
*****加权限对象-----
* IF P_WERKS IS NOT INITIAL.
* AUTHORITY-CHECK OBJECT ‘M_BANF_WRK‘
* ID ‘ACTVT‘ FIELD ‘01‘
* ID ‘WERKS‘ FIELD P_WERKS.
* IF SY-SUBRC <> 0.
* MESSAGE E019(ZMM001) WITH P_WERKS.
* ENDIF.
* ENDIF.
START-OF-SELECTION.
PERFORM FRM_GETDATA.
PERFORM FRM_MVT_BAPI.
PERFORM FRM_DOWNLOAD.
*&---------------------------------------------------------------------*
*& Form FRM_GETDATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GETDATA .
CALL FUNCTION ‘WS_UPLOAD‘
EXPORTING
* CODEPAGE = ‘ ‘
FILENAME = P_FILE
FILETYPE = ‘DAT‘
* HEADLEN = ‘ ‘
* LINE_EXIT = ‘ ‘
* TRUNCLEN = ‘ ‘
* USER_FORM = ‘ ‘
* USER_PROG = ‘ ‘
* DAT_D_FORMAT = ‘ ‘
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = ITAB
* EXCEPTIONS
* CONVERSION_ERROR = 1
* FILE_OPEN_ERROR = 2
* FILE_READ_ERROR = 3
* INVALID_TYPE = 4
* NO_BATCH = 5
* UNKNOWN_ERROR = 6
* INVALID_TABLE_WIDTH = 7
* GUI_REFUSE_FILETRANSFER = 8
* CUSTOMER_ERROR = 9
* OTHERS = 10
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " FRM_GETDATA
*&---------------------------------------------------------------------*
*& Form FRM_MVT_BAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_MVT_BAPI .
DATA:m_SBDKZ TYPE MARC-SBDKZ.
LOOP AT ITAB.
MOVE-CORRESPONDING ITAB TO IT_RESULT.
CLEAR m_SBDKZ.
SELECT SINGLE
SBDKZ
INTO m_SBDKZ
FROM
MARC
WHERE MATNR = ITAB-MATERIAL AND WERKS = P_WERKS.
* IF ITAB-KNTTP = ‘‘.
* IT_RESULT-FLAG = ‘E‘.
* IT_RESULT-MESSAGE = ‘科目分配为空‘.
* APPEND IT_RESULT.
* CLEAR IT_RESULT.
* ELSEIF ITAB-KNTTP = ‘1‘ AND m_SBDKZ = ‘2‘.
* IT_RESULT-FLAG = ‘E‘.
* IT_RESULT-MESSAGE = ‘系统库存按非项目管理‘.
* APPEND IT_RESULT.
* CLEAR IT_RESULT.
* ELSEIF ITAB-KNTTP = ‘2‘ AND m_SBDKZ = ‘1‘.
* IT_RESULT-FLAG = ‘E‘.
* IT_RESULT-MESSAGE = ‘系统库存按项目管理‘.
* APPEND IT_RESULT.
* CLEAR IT_RESULT.
* ELSE.
IF P_CHECK = ‘X‘.
PRHEADER-PR_TYPE = ‘NB‘.
ELSE.
PRHEADER-PR_TYPE = ‘ZNB‘.
ENDIF.
PRHEADERX-PR_TYPE = ‘X‘.
PRITEM-PREQ_ITEM = ‘00010‘.
PRITEM-MATERIAL = ITAB-MATERIAL.
PRITEM-PLANT = P_WERKS.
PRITEM-QUANTITY = ITAB-QUANTITY.
PRITEM-UNIT = ITAB-UNIT.
PRITEM-DELIV_DATE = ITAB-DELIV_DATE.
* Modified by liuxy2010-01-25
IF m_SBDKZ = ‘1‘.
PRITEM-ACCTASSCAT = ‘Q‘.
ELSE.
PRITEM-ACCTASSCAT = ‘‘.
ENDIF.
PRITEM-PURCH_ORG = ITAB-PURCH_ORG.
APPEND PRITEM.
CLEAR PRITEM.
PRITEMX-PREQ_ITEM = ‘00010‘.
PRITEMX-MATERIAL = ‘X‘.
PRITEMX-PLANT = ‘X‘.
PRITEMX-QUANTITY = ‘X‘.
PRITEMX-UNIT = ‘X‘.
PRITEMX-DELIV_DATE = ‘X‘.
PRITEMX-ACCTASSCAT = ‘X‘.
PRITEMX-PURCH_ORG = ‘X‘.
APPEND PRITEMX.
CLEAR PRITEMX.
PRITEMTEXT-PREQ_ITEM = ‘00010‘.
PRITEMTEXT-TEXT_ID = ‘B01‘.
PRITEMTEXT-TEXT_LINE = ITAB-WBS_ELEMENT.
APPEND PRITEMTEXT.
CLEAR PRITEMTEXT.
PRACCOUNT-PREQ_ITEM = ‘00010‘.
PRACCOUNT-SERIAL_NO = ‘01‘.
PRACCOUNT-WBS_ELEMENT = ITAB-WBS_ELEMENT.
APPEND PRACCOUNT.
CLEAR PRACCOUNT.
PRACCOUNTX-PREQ_ITEM = ‘00010‘.
PRACCOUNTX-SERIAL_NO = ‘01‘.
PRACCOUNTX-WBS_ELEMENT = ‘X‘.
PRACCOUNTX-PREQ_ITEMX = ‘X‘.
APPEND PRACCOUNTX.
CLEAR PRACCOUNTX.
CALL FUNCTION ‘BAPI_PR_CREATE‘
EXPORTING
PRHEADER = PRHEADER
PRHEADERX = PRHEADERX
* TESTRUN =
IMPORTING
NUMBER = NUMBER
* PRHEADEREXP =
TABLES
RETURN = RETURN
PRITEM = PRITEM
PRITEMX = PRITEMX
PRITEMTEXT = PRITEMTEXT
* PRITEMEXP =
* PRITEMSOURCE =
PRACCOUNT = PRACCOUNT
* PRACCOUNTPROITSEGMENT =
PRACCOUNTX = PRACCOUNTX
* PRADDRDELIVERY =
* PRITEMTEXT =
* PRHEADERTEXT =
* EXTENSIONIN =
* EXTENSIONOUT =
* PRVERSION =
* PRVERSIONX =
* ALLVERSIONS =
.
CLEAR RETURN.
LOOP AT RETURN WHERE TYPE = ‘E‘ OR TYPE = ‘A‘.
FLAG = ‘X‘.
IT_RESULT-FLAG = ‘E‘.
CONCATENATE IT_RESULT-MESSAGE ‘&‘ RETURN-MESSAGE INTO IT_RESULT-MESSAGE.
ENDLOOP.
IF FLAG = ‘X‘.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK‘.
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT‘.
wait up to 1 seconds.
IT_RESULT-FLAG = ‘S‘.
IT_RESULT-MESSAGE = ‘成功创建!‘.
IT_RESULT-NUMBER = NUMBER.
ENDIF.
APPEND IT_RESULT.
CLEAR IT_RESULT.
CLEAR PRHEADER.
CLEAR PRHEADERX.
CLEAR NUMBER.
REFRESH PRITEM.
CLEAR PRITEM.
REFRESH PRITEMX.
CLEAR PRITEMX.
REFRESH PRITEMTEXT.
CLEAR PRITEMTEXT.
REFRESH PRACCOUNT.
CLEAR PRACCOUNT.
REFRESH PRACCOUNTX.
CLEAR PRACCOUNTX.
CLEAR FLAG.
REFRESH RETURN.
CLEAR RETURN.
* ENDIF.
ENDLOOP.
ENDFORM. " FRM_MVT_BAPI
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DOWNLOAD .
CALL FUNCTION ‘GUI_DOWNLOAD‘
EXPORTING
* BIN_FILESIZE =
FILENAME = ‘C:/PR批导结果表.TXT‘
FILETYPE = ‘ASC‘
APPEND = ‘ ‘
* WRITE_FIELD_SEPARATOR = ‘ ‘
* HEADER = ‘00‘
* TRUNC_TRAILING_BLANKS = ‘ ‘
* WRITE_LF = ‘X‘
* COL_SELECT = ‘ ‘
* COL_SELECT_MASK = ‘ ‘
* DAT_MODE = ‘ ‘
* CONFIRM_OVERWRITE = ‘ ‘
* NO_AUTH_CHECK = ‘ ‘
* CODEPAGE = ‘ ‘
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = ‘#‘
* WRITE_BOM = ‘ ‘
* TRUNC_TRAILING_BLANKS_EOL = ‘X‘
* WK1_N_FORMAT = ‘ ‘
* WK1_N_SIZE = ‘ ‘
* WK1_T_FORMAT = ‘ ‘
* WK1_T_SIZE = ‘ ‘
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = IT_RESULT
* FIELDNAMES =
* EXCEPTIONS
* FILE_WRITE_ERROR = 1
* NO_BATCH = 2
* GUI_REFUSE_FILETRANSFER = 3
* INVALID_TYPE = 4
* NO_AUTHORITY = 5
* UNKNOWN_ERROR = 6
* HEADER_NOT_ALLOWED = 7
* SEPARATOR_NOT_ALLOWED = 8
* FILESIZE_NOT_ALLOWED = 9
* HEADER_TOO_LONG = 10
* DP_ERROR_CREATE = 11
* DP_ERROR_SEND = 12
* DP_ERROR_WRITE = 13
* UNKNOWN_DP_ERROR = 14
* ACCESS_DENIED = 15
* DP_OUT_OF_MEMORY = 16
* DISK_FULL = 17
* DP_TIMEOUT = 18
* FILE_NOT_FOUND = 19
* DATAPROVIDER_EXCEPTION = 20
* CONTROL_FLUSH_ERROR = 21
* OTHERS = 22
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
WRITE:‘数据成功保存到C盘根目录下,请查看!‘.
ENDFORM. " FRM_DOWNLOAD
原文地址:http://blog.csdn.net/champaignwolf/article/details/45689187