使用BAPI_REQUISITION_CREATE批量创建PR(采购申请)

原文地址: http://blog.chinaunix.net/u1/33519/showart.php?id=1674662

*&---------------------------------------------------------------------*
*& Report ZMM_UPLOAD_PR
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZMM_UPLOAD_PR.

DATA: BEGIN OF GT_DATA1 OCCURS 0,
BSART TYPE STRING, "凭证类型
BNFPO TYPE STRING, "项目
KNTTP TYPE STRING, "科目分配类别
TXZ01 TYPE STRING, "短文本
MENGE TYPE STRING, "数量
MEINS TYPE STRING, "单位
EEIND TYPE STRING, "交货日期
MATKL TYPE STRING, "物料组
WERKS TYPE STRING, "工厂
EKGRP TYPE STRING, "采购组
AFNAM TYPE STRING, "申请者
BEDNR TYPE STRING, "需求跟踪号
SAKTO TYPE STRING, "总帐科目
KOSTL TYPE STRING, "成本中心
ANLN1 TYPE STRING, "资产
AUFNR TYPE STRING, "订单
PREIS TYPE STRING, "评估价格
WAERS TYPE STRING, "币种
PEINH TYPE STRING, "价格单位
DISPO TYPE STRING, "MRP控制者
STR1 TYPE STRING,"行项目文本-传送文本
STR2 TYPE STRING,"行项目文本-预算年度
STR3 TYPE STRING,"行项目文本-资产类别
END OF GT_DATA1.

DATA: BEGIN OF GT_DATA OCCURS 0,
BEDNR TYPE STRING, "需求跟踪号
BSART TYPE STRING, "凭证类型
BNFPO TYPE STRING, "项目
KNTTP TYPE STRING, "科目分配类别
TXZ01 TYPE STRING, "短文本
MENGE TYPE STRING, "数量
MEINS TYPE STRING, "单位
EEIND TYPE STRING, "交货日期
MATKL TYPE STRING, "物料组
WERKS TYPE STRING, "工厂
EKGRP TYPE STRING, "采购组
AFNAM TYPE STRING, "申请者
SAKTO TYPE STRING, "总帐科目
KOSTL TYPE STRING, "成本中心
ANLN1 TYPE STRING, "资产
AUFNR TYPE STRING, "订单
PREIS TYPE STRING, "评估价格
WAERS TYPE STRING, "币种
PEINH TYPE STRING, "价格单位
DISPO TYPE STRING, "MRP控制者
STR1 TYPE STRING,"行项目文本-传送文本
STR2 TYPE STRING,"行项目文本-预算年度
STR3 TYPE STRING,"行项目文本-资产类别
END OF GT_DATA.


DATA: BEGIN OF GT_OUT OCCURS 0,
TEXT(255),
END OF GT_OUT.

DATA: PR_ITEM LIKE TABLE OF BAPIEBANC WITH HEADER LINE,
PR_ACCOUNT LIKE TABLE OF BAPIEBKN WITH HEADER LINE,
PR_ITEM_ID LIKE TABLE OF BAPIEBANTX WITH HEADER LINE,
PR_RETURN LIKE TABLE OF BAPIRETURN WITH HEADER LINE.
DATA: L_RETURN LIKE PR_RETURN.
DATA: LV_MESSAGE(255).

DATA: PR_NO TYPE BAPIEBANC-PREQ_NO.
DATA: BNFPO TYPE BNFPO.
DATA: PP_FILE TYPE STRING.

PARAMETERS:P_FILE(128) .

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
PERFORM GET_FILE.

START-OF-SELECTION.
PERFORM GET_DATA.
PERFORM CREATE_PR.
PERFORM WRITE_OUT.
*&---------------------------------------------------------------------*
*& Form GET_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_FILE .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
DEF_FILENAME = P_FILE
MASK = ',*.txt,*.TXT.'
*'',*.xls,*.XLS.'如果读入txt文件,在后边就需要用函数
* CALL FUNCTION 'WS_UPLOAD'
MODE = 'O'
TITLE = 'File Name'
IMPORTING
FILENAME = P_FILE
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4
OTHERS = 5.
PP_FILE = P_FILE.

ENDFORM. " GET_FILE
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_DATA .
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = PP_FILE
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'X'
* HEADER_LENGTH = 0
READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
DATA_TAB = GT_DATA1
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_READ_ERROR = 2
NO_BATCH = 3
GUI_REFUSE_FILETRANSFER = 4
INVALID_TYPE = 5
NO_AUTHORITY = 6
UNKNOWN_ERROR = 7
BAD_DATA_FORMAT = 8
HEADER_NOT_ALLOWED = 9
SEPARATOR_NOT_ALLOWED = 10
HEADER_TOO_LONG = 11
UNKNOWN_DP_ERROR = 12
ACCESS_DENIED = 13
DP_OUT_OF_MEMORY = 14
DISK_FULL = 15
DP_TIMEOUT = 16
OTHERS = 17
.
IF SY-SUBRC <> 0.
CASE SY-SUBRC.
WHEN 1.MESSAGE 'FILE_OPEN_ERROR ' TYPE 'E'.
WHEN 2.MESSAGE 'FILE_READ_ERROR ' TYPE 'E'.
WHEN 3.MESSAGE 'NO_BATCH' TYPE 'E'.
WHEN 4.MESSAGE 'GUI_REFUSE_FILETRANSFER ' TYPE 'E'.
WHEN 5.MESSAGE 'INVALID_TYPE ' TYPE 'E'.
WHEN 6.MESSAGE ' NO_AUTHORITY' TYPE 'E'.
WHEN 7.MESSAGE 'UNKNOWN_ERROR' TYPE 'E'.
WHEN 8.MESSAGE 'BAD_DATA_FORMAT ' TYPE 'E'.
WHEN 9.MESSAGE 'HEADER_NOT_ALLOWED' TYPE 'E'.
WHEN 10.MESSAGE 'SEPARATOR_NOT_ALLOWED' TYPE 'E'.
WHEN 11.MESSAGE 'HEADER_TOO_LONG ' TYPE 'E'.
WHEN 12.MESSAGE 'UNKNOWN_DP_ERROR' TYPE 'E'.
WHEN 13.MESSAGE ' ACCESS_DENIED ' TYPE 'E'.
WHEN 14.MESSAGE 'DP_OUT_OF_MEMORY ' TYPE 'E'.
WHEN 15.MESSAGE 'DISK_FULL ' TYPE 'E'.
WHEN 16.MESSAGE 'DP_TIMEOUT' TYPE 'E'.
WHEN 17.MESSAGE ' OTHERS ' TYPE 'E'.
WHEN OTHERS.
ENDCASE.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
LOOP AT GT_DATA1.
MOVE-CORRESPONDING GT_DATA1 TO GT_DATA.
APPEND GT_DATA.
CLEAR: GT_DATA,GT_DATA1.
ENDLOOP.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form CREATE_PR
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM CREATE_PR .
LOOP AT GT_DATA.
BNFPO = GT_DATA-BNFPO.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = BNFPO
IMPORTING
OUTPUT = BNFPO.

PR_ITEM-DOC_TYPE = GT_DATA-BSART. "凭证类型
PR_ITEM-PREQ_ITEM = BNFPO. "项目
PR_ITEM-ACCTASSCAT = GT_DATA-KNTTP."科目分配类别
PR_ITEM-SHORT_TEXT = GT_DATA-TXZ01."短文本
PR_ITEM-QUANTITY = GT_DATA-MENGE. "数量
PR_ITEM-UNIT = GT_DATA-MEINS."单位
PR_ITEM-DELIV_DATE = GT_DATA-EEIND. "交货日期
PR_ITEM-MAT_GRP = GT_DATA-MATKL. "物料组
PR_ITEM-PLANT = GT_DATA-WERKS."工厂
PR_ITEM-PUR_GROUP = GT_DATA-EKGRP."采购组
PR_ITEM-PREQ_NAME = GT_DATA-AFNAM."申请者
PR_ITEM-TRACKINGNO = GT_DATA-BEDNR."需求跟踪号
PR_ITEM-C_AMT_BAPI = GT_DATA-PREIS."评估价格
PR_ITEM-CURRENCY = GT_DATA-WAERS."货币码
PR_ITEM-PRICE_UNIT = GT_DATA-PEINH."价格单位
PR_ITEM-MRP_CONTR = GT_DATA-DISPO."MRP控制者
APPEND PR_ITEM.
CLEAR PR_ITEM.

PR_ACCOUNT-PREQ_ITEM = BNFPO. "项目
PR_ACCOUNT-G_L_ACCT = GT_DATA-SAKTO. "总帐科目
PR_ACCOUNT-COST_CTR = GT_DATA-KOSTL. "成本中心
PR_ACCOUNT-ASSET_NO = GT_DATA-ANLN1. "资产
PR_ACCOUNT-ORDER_NO = GT_DATA-AUFNR. "订单
PR_ACCOUNT-CO_AREA = 'BELL'.
APPEND PR_ACCOUNT.
CLEAR PR_ACCOUNT.

PR_ITEM_ID-PREQ_ITEM = BNFPO."项目
PR_ITEM_ID-TEXT_ID = 'B03'.
PR_ITEM_ID-TEXT_LINE = GT_DATA-STR1."行项目文本-传送文本
APPEND PR_ITEM_ID.
CLEAR PR_ITEM_ID.

PR_ITEM_ID-PREQ_ITEM = BNFPO."项目
PR_ITEM_ID-TEXT_ID = 'B07'.
PR_ITEM_ID-TEXT_LINE = GT_DATA-STR2."行项目文本-预算年度
APPEND PR_ITEM_ID.
CLEAR PR_ITEM_ID.

PR_ITEM_ID-PREQ_ITEM = BNFPO."项目
PR_ITEM_ID-TEXT_ID = 'B08'.
PR_ITEM_ID-TEXT_LINE = GT_DATA-STR3."行项目文本-资产类别
APPEND PR_ITEM_ID.
CLEAR PR_ITEM_ID.

AT END OF BEDNR.

CALL FUNCTION 'BAPI_REQUISITION_CREATE'
* EXPORTING
* SKIP_ITEMS_WITH_ERROR =
* AUTOMATIC_SOURCE = 'X'
IMPORTING
NUMBER = PR_NO
TABLES
REQUISITION_ITEMS = PR_ITEM
REQUISITION_ACCOUNT_ASSIGNMENT = PR_ACCOUNT
REQUISITION_ITEM_TEXT = PR_ITEM_ID
* REQUISITION_LIMITS =
* REQUISITION_CONTRACT_LIMITS =
* REQUISITION_SERVICES =
* REQUISITION_SRV_ACCASS_VALUES =
RETURN = PR_RETURN
* REQUISITION_SERVICES_TEXT =
* REQUISITION_ADDRDELIVERY =
* EXTENSIONIN =
.
LOOP AT PR_RETURN WHERE TYPE = 'E' OR TYPE = 'A'.
ENDLOOP.
IF SY-SUBRC = 0.
CLEAR: LV_MESSAGE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT PR_RETURN INTO L_RETURN WHERE TYPE = 'E' .
CONCATENATE LV_MESSAGE L_RETURN-MESSAGE ';'
INTO LV_MESSAGE.
ENDLOOP.
CONCATENATE GT_DATA-BEDNR LV_MESSAGE INTO GT_OUT-TEXT.
APPEND GT_OUT.
CLEAR GT_OUT.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
CONCATENATE PR_NO '创建成功' INTO GT_OUT-TEXT.
APPEND GT_OUT.
CLEAR GT_OUT.
ENDIF.
FREE PR_ITEM.
FREE PR_ACCOUNT.
FREE PR_ITEM_ID.
FREE PR_RETURN.

ENDAT.
ENDLOOP.

ENDFORM. " CREATE_PR
*&---------------------------------------------------------------------*
*& Form WRITE_OUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM WRITE_OUT .
LOOP AT GT_OUT.
WRITE: / GT_OUT-TEXT.
ENDLOOP.
ENDFORM. " WRITE_OUT

BAPI_REQUISITION_CREATE



你可能感兴趣的:(pr,bapi,创建采购申请)