TYPES: BEGIN OF TY_DATA,
ZLINE TYPE NUMC4,
AUART TYPE VBAK-AUART, "订单类型
VKORG TYPE VBAK-VKORG, "销售组织
VTWEG TYPE VBAK-VTWEG, "分销渠道
SPART TYPE VBAK-SPART, "产品组
VKBUR TYPE VBAK-VKBUR, "销售办事处
VKGRP TYPE VBAK-VKGRP, "销售组
KUNNR TYPE VBAK-KUNNR, "售达方
KUNWE TYPE VBAK-KUNNR, "送达方
BSTNK TYPE VBAK-BSTNK, "客户参考
ZYWY TYPE VBAK-ZYWY, "业务员
PRSDT TYPE VBKD-PRSDT, "定价日期
ZTERM TYPE VBKD-ZTERM, "付款条件
CMTD TYPE VBAP-CMTD_DELIV_DATE, "交货日期
MATNR TYPE VBAP-MATNR, "物料编码
KWMENG TYPE VBAP-KWMENG, "数量
PSTYV TYPE VBAP-PSTYV, "项目类别
NETWR TYPE VBAP-NETWR, "价格
KPEIN TYPE VBAP-KPEIN, "价格单位
WERKS TYPE VBAP-WERKS, "工厂
WAERK TYPE VBAP-WAERK, "货币
KSCHA TYPE KSCHA, "条件类型
MEINS TYPE MARA-MEINS, "计量单位
STATUS TYPE ICON_D, "返回状态
MSG TYPE MSGTXT_LONG, "报错信息
VBELN TYPE VBELN, "生成的销售订单
LGORT TYPE LIPS-LGORT, "库存地点
END OF TY_DATA,
BEGIN OF TY_MSG,
VBELN TYPE VBELN,
ZLINE TYPE NUMC4,
LGORT TYPE LIPS-LGORT,
STATUS TYPE ICON_D,
MSG TYPE MSGTXT_LONG,
CMTD TYPE VBAP-CMTD_DELIV_DATE, "交货日期
END OF TY_MSG.
DATA: GT_DATA TYPE TABLE OF TY_DATA.
DATA: GS_DATA TYPE TY_DATA.
DATA: LT_RETURN TYPE TABLE OF BAPIRET2 WITH HEADER LINE,
LT_PARTNERS TYPE TABLE OF BAPIPARNR WITH HEADER LINE,
LT_ITEMS TYPE TABLE OF BAPISDITM WITH HEADER LINE,
LT_ITEMSX TYPE TABLE OF BAPISDITMX WITH HEADER LINE,
LT_SCHDL TYPE TABLE OF BAPISCHDL WITH HEADER LINE,
LT_SCHDLX TYPE TABLE OF BAPISCHDLX WITH HEADER LINE,
LT_COND TYPE TABLE OF BAPICOND WITH HEADER LINE,
LT_CONDX TYPE TABLE OF BAPICONDX WITH HEADER LINE.
DATA: LS_HEADER TYPE BAPISDHD1, "销售订单抬头
LS_HEADERX TYPE BAPISDHD1X.
DATA: BAPE_VBAK TYPE BAPE_VBAK,
BAPE_VBAKX TYPE BAPE_VBAKX.
DATA: LT_EXTENSIONIN TYPE TABLE OF BAPIPAREX,
LS_EXTENSIONIN TYPE BAPIPAREX.
FIELD-SYMBOLS:
DATA LT_VBAP TYPE TABLE OF VBAP WITH HEADER LINE.
DATA: L_VBELN TYPE VBELN_VL,
L_VBNUM TYPE VBNUM,
L_RFLAG TYPE C,
L_STR TYPE STRING.
DATA BAPIRET2 TYPE TABLE OF BAPIRET2 WITH HEADER LINE.
DATA LV_VBELN TYPE BAPIVBELN-VBELN.
DATA LV_POSNR TYPE POSNR.
DATA LV_FLAG TYPE ABAP_BOOL.
DATA LV_MSG TYPE STRING.
DATA LT_MSG TYPE TABLE OF TY_MSG.
DATA GS_MSG TYPE TY_MSG.
LOOP AT GT_DATA ASSIGNING
CLEAR GS_DATA.
MOVE
LV_POSNR = LV_POSNR + 10.
LT_ITEMS = VALUE #( ITM_NUMBER = LV_POSNR
MATERIAL = GS_DATA-MATNR
PO_ITM_NO = '000010'
TARGET_QTY = GS_DATA-KWMENG
PLANT = GS_DATA-WERKS
ITEM_CATEG = GS_DATA-PSTYV
STORE_LOC = GS_DATA-LGORT ).
APPEND LT_ITEMS.
LT_ITEMSX = VALUE #( ITM_NUMBER = LV_POSNR
MATERIAL = 'X'
PO_ITM_NO = 'X'
TARGET_QTY = 'X'
PLANT = 'X'
ITEM_CATEG = 'X' ).
APPEND LT_ITEMSX.
LT_SCHDL = VALUE #( ITM_NUMBER = LV_POSNR
SCHED_LINE = '0001'
REQ_QTY = GS_DATA-KWMENG
REQ_DATE = GS_DATA-CMTD
DATE_TYPE = '1' ).
APPEND LT_SCHDL.
LT_SCHDLX = VALUE #( ITM_NUMBER = LV_POSNR
SCHED_LINE = '0001'
REQ_QTY = 'X'
REQ_DATE = 'X'
DATE_TYPE = 'X' ).
APPEND LT_SCHDLX.
LT_COND = VALUE #( ITM_NUMBER = LV_POSNR
COND_ST_NO = '010'
COND_COUNT = '01'
COND_TYPE = GS_DATA-KSCHA
COND_VALUE = GS_DATA-NETWR
COND_P_UNT = GS_DATA-KPEIN
CURRENCY = GS_DATA-WAERK ).
APPEND LT_COND.
LT_CONDX = VALUE #( ITM_NUMBER = LV_POSNR
COND_ST_NO = '010'
COND_COUNT = '01'
UPDATEFLAG = 'I'
COND_VALUE = 'X'
COND_P_UNT = 'X'
CURRENCY = 'X' ).
LT_CONDX-COND_TYPE = LT_CONDX-COND_TYPE .
APPEND LT_CONDX.
AT END OF ZLINE.
"根据不同的序列号生成销售订单
CLEAR GS_MSG.
LS_HEADER = VALUE #( DOC_TYPE = GS_DATA-AUART "销售订单类型
SALES_ORG = GS_DATA-VKORG "销售组织
DISTR_CHAN = GS_DATA-VTWEG "分销渠道
DIVISION = GS_DATA-SPART "产品组
SALES_OFF = GS_DATA-VKBUR "销售办事处
SALES_GRP = GS_DATA-VKGRP "销售组
PURCH_NO_C = GS_DATA-BSTNK "客户参考
PRICE_DATE = GS_DATA-PRSDT "定价日期
PMNTTRMS = GS_DATA-ZTERM ). "付款条件
LS_HEADERX = VALUE #( DOC_TYPE = 'X'
SALES_ORG = 'X'
DISTR_CHAN = 'X'
DIVISION = 'X'
SALES_OFF = 'X'
SALES_GRP = 'X'
PURCH_NO_C = 'X'
PRICE_DATE = 'X'
PMNTTRMS = 'X' ).
LT_PARTNERS[] = VALUE #( ( PARTN_ROLE = 'AG' PARTN_NUMB = GS_DATA-KUNNR )
( PARTN_ROLE = 'RE' PARTN_NUMB = GS_DATA-KUNWE ) ).
BAPE_VBAK = VALUE #( ZYWY = GS_DATA-ZYWY ).
LS_EXTENSIONIN = VALUE #( STRUCTURE = 'BAPE_VBAK' ).
CALL METHOD CL_ABAP_CONTAINER_UTILITIES=>FILL_CONTAINER_C
EXPORTING
IM_VALUE = BAPE_VBAK
IMPORTING
EX_CONTAINER = LS_EXTENSIONIN-VALUEPART1
EXCEPTIONS
ILLEGAL_PARAMETER_TYPE = 1
OTHERS = 2.
APPEND LS_EXTENSIONIN TO LT_EXTENSIONIN.
BAPE_VBAKX = VALUE #( ZYWY = 'X' ).
LS_EXTENSIONIN = VALUE #( STRUCTURE = 'BAPE_VBAKX' ).
CALL METHOD CL_ABAP_CONTAINER_UTILITIES=>FILL_CONTAINER_C
EXPORTING
IM_VALUE = BAPE_VBAKX
IMPORTING
EX_CONTAINER = LS_EXTENSIONIN-VALUEPART1
EXCEPTIONS
ILLEGAL_PARAMETER_TYPE = 1
OTHERS = 2.
APPEND LS_EXTENSIONIN TO LT_EXTENSIONIN.
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
ORDER_HEADER_IN = LS_HEADER
ORDER_HEADER_INX = LS_HEADERX
IMPORTING
SALESDOCUMENT = LV_VBELN
TABLES
RETURN = LT_RETURN
ORDER_ITEMS_IN = LT_ITEMS
ORDER_ITEMS_INX = LT_ITEMSX
ORDER_PARTNERS = LT_PARTNERS
ORDER_SCHEDULES_IN = LT_SCHDL
ORDER_SCHEDULES_INX = LT_SCHDLX
ORDER_CONDITIONS_IN = LT_COND
ORDER_CONDITIONS_INX = LT_CONDX
EXTENSIONIN = LT_EXTENSIONIN.
LOOP AT LT_RETURN WHERE TYPE CA 'EAXI'.
LV_FLAG = 'X'.
ENDLOOP.
IF LV_FLAG IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
"创建成功后获取成功消息
LOOP AT LT_RETURN WHERE TYPE EQ 'S' AND ID EQ 'V1' AND NUMBER EQ '311'.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
MSGID = LT_RETURN-ID
MSGNR = LT_RETURN-NUMBER
MSGV1 = LT_RETURN-MESSAGE_V1
MSGV2 = LT_RETURN-MESSAGE_V2
MSGV3 = LT_RETURN-MESSAGE_V3
MSGV4 = LT_RETURN-MESSAGE_V4
IMPORTING
MESSAGE_TEXT_OUTPUT = LV_MSG.
CONCATENATE LV_MSG GS_MSG-MSG INTO GS_MSG-MSG.
ENDLOOP.
GS_MSG-STATUS = '@5B@'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
"回滚后获取报错原因
LOOP AT LT_RETURN WHERE TYPE CA 'EAXI'.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
MSGID = LT_RETURN-ID
MSGNR = LT_RETURN-NUMBER
MSGV1 = LT_RETURN-MESSAGE_V1
MSGV2 = LT_RETURN-MESSAGE_V2
MSGV3 = LT_RETURN-MESSAGE_V3
MSGV4 = LT_RETURN-MESSAGE_V4
IMPORTING
MESSAGE_TEXT_OUTPUT = LV_MSG.
CONCATENATE LV_MSG GS_MSG-MSG INTO GS_MSG-MSG.
ENDLOOP.
GS_MSG-STATUS = '@5C@'.
ENDIF.
"会写销售订单号
GS_MSG-VBELN = LV_VBELN.
GS_MSG-ZLINE = GS_DATA-ZLINE.
GS_MSG-LGORT = GS_DATA-LGORT.
GS_MSG-CMTD = GS_DATA-CMTD.
APPEND GS_MSG TO LT_MSG.
CLEAR: LV_POSNR,LV_FLAG,LV_VBELN,LV_MSG,LS_HEADER,LS_HEADERX,LT_ITEMS[],LT_ITEMSX[],
LT_PARTNERS[],LT_SCHDL[],LT_SCHDLX[],LT_COND[],LT_CONDX[],LT_EXTENSIONIN[],
GS_MSG.
ENDAT.
ENDLOOP.