SD_SALESDOCUMENT_CREATE
用于创建销售订单。
创建销售订单的BAPI,比较常用的是BAPI_SALESORDER_CREATEFROMDAT2,但是 BUSINESS OBJECT限制为 BUS2032。
使用以下函数可以得到BUSINESS OBJECT:
DATA:LV_OBJTYPE TYPE OJ_NAME .
LS_HEADER-SD_DOC_CAT为销售订单类型,即类型为:VBAK-VBTYP
CALL FUNCTION 'SD_OBJECT_TYPE_DETERMINE'
EXPORTING
I_DOCUMENT_TYPE = LS_HEADER-SD_DOC_CAT
IMPORTING
E_BUSINESS_OBJECT = LV_OBJTYPE
EXCEPTIONS
OTHERS = 1.
如果LV_OBJTYPE = ‘BUS2032’.
则可以使用BAPI_SALESORDER_CREATEFROMDAT2
如果其他的业务对象则使用FM: SD_SALESDOCUMENT_CREATE创建销售订单
创建借贷项订单相关代码:
*&----------调用BAPI参数定义--------(S)DATA: GV_VBELN TYPE BAPIVBELN-VBELN, GS_BAPISDHD1 TYPE BAPISDHD1, GS_BAPISDHD1X TYPE BAPISDHD1X, GS_BAPISDLS TYPE BAPISDLS, GV_TESTRUN TYPE TESTRUN, GT_RETURN TYPE STANDARD TABLE OF BAPIRET2 WITH HEADER LINE, GS_RETURN LIKE LINE OF GT_RETURN, GT_BAPISDITM TYPE TABLE OF BAPISDITM WITH HEADER LINE, GT_BAPISDITMX TYPE TABLE OF BAPISDITMX WITH HEADER LINE, GT_BAPIPARNR TYPE TABLE OF BAPIPARNR WITH HEADER LINE, GT_BAPISCHDL TYPE TABLE OF BAPISCHDL WITH HEADER LINE, GT_BAPISCHDLX TYPE TABLE OF BAPISCHDLX WITH HEADER LINE, GT_BAPICOND TYPE TABLE OF BAPICOND WITH HEADER LINE, GT_BAPICONDX TYPE TABLE OF BAPICONDX WITH HEADER LINE, GS_BAPISDTEXT TYPE BAPISDTEXT, GT_BAPISDTEXT TYPE TABLE OF BAPISDTEXT, GS_BAPIPAREX TYPE BAPIPAREX, GT_BAPIPAREX TYPE TABLE OF BAPIPAREX, GS_BAPIPAREXX TYPE BAPIPAREXX, GT_BAPIPAREXX TYPE TABLE OF BAPIPAREX.* GS_BAPISDLS TYPE BAPISDLS.
CLEAR:GS_BAPISDHD1,GS_BAPISDHD1X,GT_BAPISDITM[],GT_BAPISDITMX[],GT_BAPISCHDL[],GT_BAPISCHDLX[],GS_ZBAPE_VBAK,GS_ZBAPE_VBAKX, GT_BAPIPAREX[],GT_BAPIPAREXX[]. LOOP AT GT_DATA1 INTO GS_DATA1. IF GS_DATA1-STATUS = ICON_LED_RED. MESSAGE '不能选中错误数据导入' TYPE 'E'. ENDIF. GS_BAPISDHD1-DOC_TYPE = GS_DATA1-AUART."订单类型 1 GS_BAPISDHD1-SALES_ORG = GS_DATA1-VKORG."销售组织 2 GS_BAPISDHD1-DISTR_CHAN = GS_DATA1-VTWEG."分销渠道 3 GS_BAPISDHD1-DIVISION = GS_DATA1-SPART."产品组 4 GS_BAPISDHD1-SALES_OFF = GS_DATA1-VKBUR."销售办事处 5 GS_BAPISDHD1-SALES_GRP = GS_DATA1-VKGRP."销售组 6 GS_BAPISDHD1-PURCH_NO_C = GS_DATA1-BSTKD."客户参考 11 GS_BAPISDHD1-ORD_REASON = GS_DATA1-AUGRU."订单原因 24 GS_BAPISDHD1X-DOC_TYPE = 'X'. GS_BAPISDHD1X-SALES_ORG = 'X'. GS_BAPISDHD1X-DISTR_CHAN = 'X'. GS_BAPISDHD1X-DIVISION = 'X'. GS_BAPISDHD1X-SALES_OFF = 'X'. GS_BAPISDHD1X-SALES_GRP = 'X'. GS_BAPISDHD1X-PURCH_NO_C = 'X'."客户参考 GS_BAPISDHD1X-ORD_REASON = 'X'. LOOP AT GT_DATA INTO GS_DATA WHERE ZNUM = GS_DATA1-ZNUM. IF GS_DATA-STATUS <> ICON_LED_RED AND GS_DATA-VBELN IS INITIAL. CLEAR GT_BAPISDITM. GT_BAPISDITM-MATERIAL = GS_DATA-MATNR."物料编码 13 GT_BAPISDITM-TARGET_VAL = GS_DATA-KZWI3. GT_BAPISDITM-CURRENCY = GS_DATA-WAERK. " 17 GT_BAPISDITM-TARGET_QTY = GS_DATA-KWMENG."下单数量 14 GT_BAPISDITM-PLANT = GS_DATA-WERKS."发运工厂 19 GT_BAPISDITM-PURCH_NO_C = GS_DATA-BSTKD."客户参考 11 GT_BAPISDITM-STORE_LOC = GS_DATA-LGORT."发运库位 27 GT_BAPISDITM-CUST_MAT35 = GS_DATA-KDMAT."备注信息:旧订单号+旧订单行号 18 GT_BAPISDITM-DIVISION = GS_DATA-SPART."产品组 GT_BAPISDITM-ITM_NUMBER = GS_DATA-POSNR."销售订单行项目 GT_BAPISDITM-PURCH_DATE = GS_DATA-BSTDK. "扣款单日期 12 GT_BAPISDITM-BATCH = GS_DATA-CHARG. " 25 APPEND GT_BAPISDITM. CLEAR GT_BAPISDITMX. GT_BAPISDITMX-UPDATEFLAG = '1'. GT_BAPISDITMX-MATERIAL = 'X'. GT_BAPISDITMX-TARGET_VAL = 'X'. GT_BAPISDITMX-TARGET_QTY = 'X'. GT_BAPISDITMX-PLANT = 'X'. GT_BAPISDITMX-PURCH_NO_C = 'X'. GT_BAPISDITMX-STORE_LOC = 'X'. GT_BAPISDITMX-CUST_MAT35 = 'X'. GT_BAPISDITMX-DIVISION = 'X'. GT_BAPISDITMX-PURCH_DATE = 'X'. GT_BAPISDITMX-BATCH = 'X'. APPEND GT_BAPISDITMX. CLEAR GT_BAPISCHDL. GT_BAPISCHDL-ITM_NUMBER = GS_DATA-POSNR."销售订单行项目 GT_BAPISCHDL-REQ_QTY = GS_DATA-KWMENG."下单数量 GT_BAPISCHDL-REQ_DATE = SY-DATUM."日期 APPEND GT_BAPISCHDL. CLEAR GT_BAPISCHDLX. GT_BAPISCHDLX-UPDATEFLAG = 'X'. GT_BAPISCHDLX-ITM_NUMBER = GS_DATA-POSNR."销售订单行项目 GT_BAPISCHDLX-REQ_QTY = 'X'. GT_BAPISCHDLX-REQ_DATE = 'X'. APPEND GT_BAPISCHDLX. IF GS_DATA-ZGRN IS NOT INITIAL. GS_ZBAPE_VBAK-ZGRN = GS_DATA-ZGRN. " GS_ZBAPE_VBAKX-ZGRN = 'X'. " ENDIF. GS_BAPIPAREX-STRUCTURE = 'BAPE_VBAK'. GS_BAPIPAREX-VALUEPART1 = GS_ZBAPE_VBAK. APPEND GS_BAPIPAREX TO GT_BAPIPAREX. CLEAR GS_BAPIPAREX. GS_BAPIPAREXX-STRUCTURE = 'BAPE_VBAKX'. GS_BAPIPAREXX-VALUEPART1 = GS_ZBAPE_VBAKX. APPEND GS_BAPIPAREXX TO GT_BAPIPAREXX. CLEAR GS_BAPIPAREXX. CLEAR GT_BAPIPARNR. GT_BAPIPARNR-PARTN_ROLE = 'AG'. GT_BAPIPARNR-PARTN_NUMB = GS_DATA-KUNNR."售达方编码 APPEND GT_BAPIPARNR. IF GS_DATA-KUNNR1 IS NOT INITIAL. GT_BAPIPARNR-PARTN_ROLE = 'WE'. GT_BAPIPARNR-PARTN_NUMB = GS_DATA-KUNNR1."送达方编码 APPEND GT_BAPIPARNR. ENDIF. IF GS_DATA-LIFNR1 IS NOT INITIAL. GT_BAPIPARNR-PARTN_ROLE = 'Z1'. GT_BAPIPARNR-PARTN_NUMB = GS_DATA-LIFNR1."销售team APPEND GT_BAPIPARNR. ENDIF. IF GS_DATA-LIFNR2 IS NOT INITIAL. GT_BAPIPARNR-PARTN_ROLE = 'Z2'. GT_BAPIPARNR-PARTN_NUMB = GS_DATA-LIFNR2."销售leader APPEND GT_BAPIPARNR. ENDIF. IF GS_DATA-LIFNR3 IS NOT INITIAL. GT_BAPIPARNR-PARTN_ROLE = 'Z3'. GT_BAPIPARNR-PARTN_NUMB = GS_DATA-LIFNR3."销售员 APPEND GT_BAPIPARNR. ENDIF. IF GS_DATA-LIFNR4 IS NOT INITIAL. GT_BAPIPARNR-PARTN_ROLE = 'Z4'. GT_BAPIPARNR-PARTN_NUMB = GS_DATA-LIFNR4."销售员 APPEND GT_BAPIPARNR. ENDIF. CLEAR GT_BAPICOND. GT_BAPICOND-ITM_NUMBER = GS_DATA-POSNR."销售订单行项目 GT_BAPICOND-CURRENCY = GS_DATA-WAERK. GT_BAPICOND-COND_P_UNT = GS_DATA-KPEIN. GT_BAPICOND-COND_VALUE = GS_DATA-KZWI3.* GT_BAPICOND-COND_UPDAT = 'X'. APPEND GT_BAPICOND. CLEAR GT_BAPICONDX. GT_BAPICONDX-ITM_NUMBER = GS_DATA-POSNR."销售订单行项目* GT_BAPICONDX-COND_TYPE = 'X'. GT_BAPICONDX-UPDATEFLAG = 'U'. GT_BAPICONDX-CURRENCY = 'X'. GT_BAPICONDX-COND_P_UNT = 'X'. GT_BAPICONDX-COND_VALUE = 'X'. APPEND GT_BAPICONDX. ENDIF. ENDLOOP.* AT END OF ZNUM. SORT GT_BAPIPARNR. DELETE ADJACENT DUPLICATES FROM GT_BAPIPARNR COMPARING ALL FIELDS. GS_BAPISDLS-PRICING = 'C'. CLEAR:GS_RETURN,GT_RETURN[].
CALL FUNCTION 'SD_SALESDOCUMENT_CREATE' EXPORTING* SALESDOCUMENT = SALES_HEADER_IN = GS_BAPISDHD1* SALES_HEADER_INX = GS_BAPISDHD1X* SENDER =* BINARY_RELATIONSHIPTYPE = ' '* INT_NUMBER_ASSIGNMENT = ' '* BEHAVE_WHEN_ERROR = ' ' LOGIC_SWITCH = GS_BAPISDLS* BUSINESS_OBJECT = ' '* TESTRUN =* CONVERT_PARVW_AUART = ' '* STATUS_BUFFER_REFRESH = 'X'* CALL_ACTIVE = ' '* I_WITHOUT_INIT = ' ' IMPORTING SALESDOCUMENT_EX = GV_VBELN* SALES_HEADER_OUT =* SALES_HEADER_STATUS = TABLES RETURN = GT_RETURN SALES_ITEMS_IN = GT_BAPISDITM SALES_ITEMS_INX = GT_BAPISDITMX SALES_PARTNERS = GT_BAPIPARNR SALES_SCHEDULES_IN = GT_BAPISCHDL SALES_SCHEDULES_INX = GT_BAPISCHDLX SALES_CONDITIONS_IN = GT_BAPICOND SALES_CONDITIONS_INX = GT_BAPICONDX
SALES_TEXT = GT_BAPISDTEXT
EXTENSIONIN = GT_BAPIPAREX
EXTENSIONEX = GT_BAPIPAREXX . CLEAR:GT_BAPISDITM[],GT_BAPISDITMX[],GT_BAPIPARNR[],GT_BAPIPARNR[],GT_BAPISCHDL[],GT_BAPISCHDLX[],GT_BAPICOND[]. CLEAR:GT_BAPICOND[],GT_BAPICOND[],GT_BAPISDTEXT[],GT_BAPIPAREX. LOOP AT GT_RETURN INTO GS_RETURN. IF GS_RETURN-TYPE = 'E'. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. GS_DATA-STATUS = ICON_RED_LIGHT. GS_DATA-ZMESG = GS_RETURN-MESSAGE. DATA : LV_NUM1 TYPE I. LV_NUM1 = GS_DATA-ZNUM. MODIFY GT_DATA FROM GS_DATA TRANSPORTING STATUS ZMESG WHERE ZNUM = LV_NUM1. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING WAIT = 'X'. GS_DATA-STATUS = ICON_GREEN_LIGHT. GS_DATA-ZMESG = '成功创建借贷项订单'. GS_DATA-VBELN = GV_VBELN. DATA : LV_NUM2 TYPE I. LV_NUM2 = GS_DATA-ZNUM. MODIFY GT_DATA FROM GS_DATA TRANSPORTING STATUS ZMESG VBELN WHERE ZNUM = LV_NUM2. ENDIF. ENDLOOP.* ENDAT. CLEAR GS_DATA. ENDLOOP.