ABAP创建销售订单BAPI示例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ABAP创建销售订单BAPI⽰例
ABAP中创建销售订单
1.BAPI
1. BAPI_SALESORDER_CREATEFROMDAT2
2. BAPI_CUSTOMERRETURN_CREATE
3. SD_SALESDOCUMENT_CREATE
2.BAPI
BAPI_SALESORDER_CREATEFROMDAT2 业务对象限制为BUS2032
BAPI_CUSTOMERRETURN_CREATE 业务对象限制为BUS2102
SD_SALESDOCUMENT_CREATE 未限制业务对象类型
BUS2030 客户询价单
BUS2031 客户报价
BUS2032 销售订单
BUS2034 合同
BUS2094 贷项订单
BUS2096 借项订单
BUS2102 退货订单
BUS2103 后续免费送货订单
BAPI_SALESORDER_CREATEFROMDAT2和BAPI_CUSTOMERRETURN_CREATE都在内部调⽤SD_SALESDOCUMENT_CREATE 对于不同订单类型(Z001,Z002…),配置了的SD凭证类型;可在TVAK表中VBTYP字段查看
业务对象和SD凭证类型有绑定关系;⽐如Z008 Z009凭证,SD凭证类型为H(退货)所以需要使⽤业务对象类型BUS2102的BAPI;或者使⽤SD_SALESDOCUMENT_CREATE并设置参数BUSINESS_OBJECT = ‘BUS2102’
3.
4.
5.
此⽅法save_text切割的字符串结尾为空格时;会造成空格丢失,需要⾃⾏写逻辑处理
处理逻辑:缩短切割长度(例如:120);然后判断前⼀⾏是否末尾为空格,如果空则将空格写⼊本⾏开头FUNCTION ZSD_IF0012.
*"----------------------------------------------------------------------
*"*"本地接⼝:
*" IMPORTING
*" VALUE(I_HEAD) TYPE ZSDS_CRM_IF007
*" EXPORTING
*" VALUE(RESULTCODE) TYPE CHAR1
*" VALUE(RESULTMSG) TYPE BAPI_MSG
*" VALUE(O_VBELN) TYPE VBELN
*" TABLES
*" T_ITEM STRUCTURE ZSDS_CRM_IF008
*"----------------------------------------------------------------------
DATA:
WA_ORDER_HEADER_IN LIKE BAPISDHD1, "抬头
WA_ORDER_HEADER_INX LIKE BAPISDHD1X, "抬头控制
WA_LOGIC_SWITCH LIKE BAPISDLS, "内部控制参数
GV_SALESDOCUMENT_EX LIKE BAPIVBELN-VBELN, "返回SO凭证号
WA_ORDER_ITEMS_IN LIKE BAPISDITM, "⾏项⽬
ORDER_ITEMS_IN LIKE TABLE OF BAPISDITM, "⾏项⽬
WA_ORDER_ITEMS_INX LIKE BAPISDITMX, "⾏项⽬控制
ORDER_ITEMS_INX LIKE TABLE OF BAPISDITMX, "⾏项⽬控制
WA_ORDER_PARTNERS LIKE BAPIPARNR, "业务伙伴
ORDER_PARTNERS LIKE TABLE OF BAPIPARNR, "业务伙伴
WA_ORDER_SCHEDULES_IN LIKE BAPISCHDL, "计划⾏
ORDER_SCHEDULES_IN LIKE TABLE OF BAPISCHDL, "计划⾏
WA_ORDER_SCHEDULES_INX LIKE BAPISCHDLX, "计划⾏控制
ORDER_SCHEDULES_INX LIKE TABLE OF BAPISCHDLX, "计划⾏控制
WA_ORDER_CONDITIONS_IN LIKE BAPICOND, "定价条件
ORDER_CONDITIONS_IN LIKE TABLE OF BAPICOND, "定价条件
WA_ORDER_CONDITIONS_INX LIKE BAPICONDX, "定价条件控制
ORDER_CONDITIONS_INX LIKE TABLE OF BAPICONDX, "定价条件控制
WA_ORDER_TEXT LIKE BAPISDTEXT, "⽂本
ORDER_TEXT LIKE TABLE OF BAPISDTEXT, "⽂本
WA_EXTENSIONIN TYPE BAPIPAREX,
ORDER_EXTENSIONIN TYPE TABLE OF BAPIPAREX,
WA_RETURN LIKE BAPIRET2, "BAPI返回
RETURN LIKE TABLE OF BAPIRET2. "BAPI返回
DATA: WA_BAPE_VBAP TYPE BAPE_VBAP,
WA_BAPE_VBAPX TYPE BAPE_VBAPX,
WA_BAPE_VBAK TYPE BAPE_VBAK,
WA_BAPE_VBAKX TYPE BAPE_VBAKX.
DATA: WA_ITEM TYPE ZSDS_CRM_IF008,
GS_LOGS TYPE ZALL_LOGS. "⽇志
DATA: GT_DATA TYPE TABLE OF ZSDT_CRM_IF012, "业务数据
WA_DATA TYPE ZSDT_CRM_IF012.
DATA: GV_STRING TYPE STRING,
GT_STRING TYPE TABLE OF CHAR_132,
WA_STRING TYPE CHAR_132,
GV_LENGTH TYPE I.
SELECT *
FROM ZMPPZB AS A
FOR ALL ENTRIES IN @T_ITEM
WHERE ZMPXX = @T_ITEM-ZMPXX
INTO TABLE @DATA(GT_ZMPPZB).
* 获取⽇志表流⽔号
CALL FUNCTION 'NUMBER_RANGE_ENQUEUE'
EXPORTING
OBJECT = 'ZLOG001'
EXCEPTIONS
FOREIGN_LOCK = 1
OBJECT_NOT_FOUND = 2
SYSTEM_FAILURE = 3
OTHERS = 4.
IF SY-SUBRC EQ 0.
* Implement suitable error handling here
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
NR_RANGE_NR = '01'
OBJECT = 'ZLOG001'
IMPORTING
NUMBER = GS_LOGS-SEQNR
* QUANTITY =
* RETURNCODE =
EXCEPTIONS
INTERVAL_NOT_FOUND = 1
NUMBER_RANGE_NOT_INTERN = 2
OBJECT_NOT_FOUND = 3
QUANTITY_IS_0 = 4
QUANTITY_IS_NOT_1 = 5
INTERVAL_OVERFLOW = 6
BUFFER_OVERFLOW = 7
OTHERS = 8.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
CALL FUNCTION 'NUMBER_RANGE_DEQUEUE'
EXPORTING
OBJECT = 'ZLOG001'
EXCEPTIONS
OBJECT_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ELSE.
RAISE NUM_RANGE_ERROR .
ENDIF.
IF I_HEAD-SESSION_ID IS INITIAL.
RESULTCODE = 'E'.
RESULTMSG = |[SESSION_ID]不允许为空|.
O_VBELN = ''.
RETURN.
ENDIF.
"Head extenntion
SELECT SINGLE *
FROM ZTXTGG AS A
WHERE ZXTGG = @I_HEAD-ZXTGG
INTO @DATA(WA_ZTXTGG).
IF SY-SUBRC = 0.
CLEAR WA_EXTENSIONIN.
WA_BAPE_VBAK-ZXTGG = I_HEAD-ZXTGG.
MOVE 'BAPE_VBAK' TO WA_EXTENSIONIN-STRUCTURE.
CALL METHOD CL_ABAP_CONTAINER_UTILITIES=>FILL_CONTAINER_C
EXPORTING
IM_VALUE = WA_BAPE_VBAK
IMPORTING
EX_CONTAINER = WA_EXTENSIONIN-VALUEPART1.
APPEND WA_EXTENSIONIN TO ORDER_EXTENSIONIN.
CLEAR WA_EXTENSIONIN.
WA_BAPE_VBAKX-ZXTGG = ABAP_TRUE.
MOVE 'BAPE_VBAKX' TO WA_EXTENSIONIN-STRUCTURE.
MOVE WA_BAPE_VBAPX TO WA_EXTENSIONIN-VALUEPART1.
APPEND WA_EXTENSIONIN TO ORDER_EXTENSIONIN.
ELSE.
RESULTCODE = 'E'.
RESULTMSG = '箱贴规格未维护,请联系SD顾问'.
O_VBELN = ''.
RETURN.
ENDIF.
"SO Header Text
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = I_HEAD-ZTEXTP. "整单包装要求
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
* WA_ORDER_TEXT-doc_number = ''. "凭证号 = ⽂本名,创建时为空
WA_ORDER_TEXT-ITM_NUMBER = SPACE. "space = 000000 代表抬头⽂本 WA_ORDER_TEXT-TEXT_ID = 'Z001'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = I_HEAD-ZTEXTD. "整单送货地址
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
WA_ORDER_TEXT-ITM_NUMBER = SPACE. "space = 000000 代表抬头⽂本
WA_ORDER_TEXT-TEXT_ID = 'Z002'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = I_HEAD-ZTEXTI. "整单开票要求
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
WA_ORDER_TEXT-ITM_NUMBER = SPACE. "space = 000000 代表抬头⽂本
WA_ORDER_TEXT-TEXT_ID = 'Z003'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = I_HEAD-ZTEXTO. "其他->整单参数特性
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
WA_ORDER_TEXT-ITM_NUMBER = SPACE. "space = 000000 代表抬头⽂本
WA_ORDER_TEXT-TEXT_ID = 'Z004'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
"SO Header
CLEAR: WA_ORDER_HEADER_IN,WA_ORDER_HEADER_INX.
WA_ORDER_HEADER_IN-DOC_TYPE = I_HEAD-AUART. "销售凭证类型
WA_ORDER_HEADER_IN-CREATED_BY = I_HEAD-ERNAM. "创建⼈
WA_ORDER_HEADER_IN-SALES_ORG = I_HEAD-VKORG. "销售组织
WA_ORDER_HEADER_IN-SALES_GRP = I_HEAD-VKGRP. "销售组
WA_ORDER_HEADER_IN-DISTR_CHAN = I_HEAD-VTWEG. "分销渠道
WA_ORDER_HEADER_IN-DIVISION = I_HEAD-SPART. "产品组
WA_ORDER_HEADER_IN-REQ_DATE_H = I_HEAD-VDATU. "请求交货⽇期
WA_ORDER_HEADER_IN-DOC_DATE = SY-DATUM. "凭证时间
WA_ORDER_HEADER_IN-ORD_REASON = I_HEAD-AUGRU. "订单原因
WA_ORDER_HEADER_IN-CURRENCY = I_HEAD-WAERK. "销售和分销凭证货币
WA_ORDER_HEADER_IN-PMNTTRMS = I_HEAD-ZTERM. "付款条件代码
WA_ORDER_HEADER_IN-INCOTERMS1 = I_HEAD-INCO1. "国际贸易条款(第 1 部分)
WA_ORDER_HEADER_IN-H_CURR = I_HEAD-WAERS. "货币码
WA_ORDER_HEADER_INX-DOC_TYPE = ABAP_TRUE..
WA_ORDER_HEADER_INX-SALES_ORG = ABAP_TRUE.
WA_ORDER_HEADER_INX-DISTR_CHAN = ABAP_TRUE.
WA_ORDER_HEADER_INX-DIVISION = ABAP_TRUE.
WA_ORDER_HEADER_INX-REQ_DATE_H = ABAP_TRUE.
WA_ORDER_HEADER_INX-DOC_DATE = ABAP_TRUE.
WA_ORDER_HEADER_INX-ORD_REASON = ABAP_TRUE.
WA_ORDER_HEADER_INX-CURRENCY = ABAP_TRUE.
WA_ORDER_HEADER_INX-PMNTTRMS = ABAP_TRUE.
WA_ORDER_HEADER_INX-INCOTERMS1 = ABAP_TRUE.
WA_ORDER_HEADER_INX-H_CURR = ABAP_TRUE.
WA_ORDER_HEADER_INX-H_CURR = ABAP_TRUE.
"SO Partner
I_HEAD-ZKUNNRSP = |{ I_HEAD-ZKUNNRSP ALPHA = IN }|.
I_HEAD-ZKUNNRSH = |{ I_HEAD-ZKUNNRSH ALPHA = IN }|.
SELECT KUNNR,ANRED,NAME1,LAND1,PSTLZ,ORT01,STRAS
FROM KNA1
WHERE KUNNR = @I_HEAD-ZKUNNRSP
* ,@I_HEAD-ZKUNNRSH
INTO TABLE @DATA(LT_PARTNERS).
CLEAR WA_ORDER_PARTNERS.
READ TABLE LT_PARTNERS INTO DATA(WA_PARTNERS) WITH KEY KUNNR = I_HEAD-ZKUNNRSP. IF SY-SUBRC = 0.
WA_ORDER_PARTNERS-PARTN_ROLE = 'AG'. "售达⽅
WA_ORDER_PARTNERS-PARTN_NUMB = I_HEAD-ZKUNNRSP. "售达⽅代码
WA_ORDER_PARTNERS-TITLE = WA_PARTNERS-ANRED. "抬头->公司
WA_ORDER_PARTNERS-NAME = WA_PARTNERS-NAME1. "名称1
WA_ORDER_PARTNERS-COUNTRY = WA_PARTNERS-LAND1. "国家代码
WA_ORDER_PARTNERS-POSTL_CODE = WA_PARTNERS-PSTLZ. "邮政编码
WA_ORDER_PARTNERS-CITY = WA_PARTNERS-ORT01. "城市
WA_ORDER_PARTNERS-STREET = WA_PARTNERS-STRAS. "街道
APPEND WA_ORDER_PARTNERS TO ORDER_PARTNERS.
ELSE.
RESULTCODE = 'E'.
RESULTMSG = '售达⽅不存在'.
O_VBELN = ''.
RETURN.
ENDIF.
CLEAR: WA_ORDER_PARTNERS,WA_PARTNERS.
READ TABLE LT_PARTNERS INTO WA_PARTNERS WITH KEY KUNNR = I_HEAD-ZKUNNRSH. IF SY-SUBRC = 0.
WA_ORDER_PARTNERS-PARTN_ROLE = 'WE'. "送达⽅
WA_ORDER_PARTNERS-PARTN_NUMB = I_HEAD-ZKUNNRSH. "送达⽅代码
WA_ORDER_PARTNERS-TITLE = WA_PARTNERS-ANRED. "抬头->公司
WA_ORDER_PARTNERS-NAME = WA_PARTNERS-NAME1. "名称1
WA_ORDER_PARTNERS-COUNTRY = WA_PARTNERS-LAND1. "国家代码
WA_ORDER_PARTNERS-POSTL_CODE = WA_PARTNERS-PSTLZ. "邮政编码
WA_ORDER_PARTNERS-CITY = WA_PARTNERS-ORT01. "城市
WA_ORDER_PARTNERS-STREET = WA_PARTNERS-STRAS. "街道
APPEND WA_ORDER_PARTNERS TO ORDER_PARTNERS.
ELSE.
RESULTCODE = 'E'.
RESULTMSG = '送达⽅不存在'.
O_VBELN = ''.
RETURN.
ENDIF.
"SO Condition Header
CLEAR: WA_ORDER_CONDITIONS_IN,WA_ORDER_CONDITIONS_INX.
WA_ORDER_CONDITIONS_IN-ITM_NUMBER = SPACE. "⾏项⽬ space = 抬头
WA_ORDER_CONDITIONS_IN-COND_TYPE = 'ZF01'. "运费
WA_ORDER_CONDITIONS_IN-CURRENCY = I_HEAD-WAERS. "货币码/%
WA_ORDER_CONDITIONS_IN-COND_VALUE = I_HEAD-ZYSJG. "运费价格
WA_ORDER_CONDITIONS_INX-ITM_NUMBER = SPACE.
WA_ORDER_CONDITIONS_INX-COND_TYPE = 'ZF01'.
WA_ORDER_CONDITIONS_INX-CURRENCY = ABAP_TRUE.
WA_ORDER_CONDITIONS_INX-COND_VALUE = ABAP_TRUE.
APPEND WA_ORDER_CONDITIONS_IN TO ORDER_CONDITIONS_IN.
APPEND WA_ORDER_CONDITIONS_INX TO ORDER_CONDITIONS_INX.
CLEAR: WA_ORDER_CONDITIONS_IN,WA_ORDER_CONDITIONS_INX.
WA_ORDER_CONDITIONS_IN-ITM_NUMBER = SPACE. "⾏项⽬ space = 抬头
WA_ORDER_CONDITIONS_IN-COND_TYPE = 'ZF02'. "保险费
WA_ORDER_CONDITIONS_IN-CURRENCY = I_HEAD-WAERS. "货币码
WA_ORDER_CONDITIONS_IN-COND_VALUE = I_HEAD-ZBXJG. "保险价格
WA_ORDER_CONDITIONS_INX-ITM_NUMBER = SPACE.
WA_ORDER_CONDITIONS_INX-COND_TYPE = 'ZF02'.
WA_ORDER_CONDITIONS_INX-CURRENCY = ABAP_TRUE.
WA_ORDER_CONDITIONS_INX-COND_VALUE = ABAP_TRUE.
APPEND WA_ORDER_CONDITIONS_IN TO ORDER_CONDITIONS_IN.
APPEND WA_ORDER_CONDITIONS_INX TO ORDER_CONDITIONS_INX.
"SO ITEM
LOOP AT T_ITEM INTO WA_ITEM.
IF WA_ITEM-POSNR IS INITIAL.
RESULTCODE = 'E'.
RESULTMSG = |[LINE:{ SY-TABIX }]⾏项⽬号不允许为空|.
O_VBELN = ''.
RETURN.
ENDIF.
"Item extenntion
READ TABLE GT_ZMPPZB INTO DATA(WA_ZMPPZB) WITH KEY ZMPXX = WA_ITEM-ZMPXX. IF SY-SUBRC = 0.
CLEAR WA_EXTENSIONIN.
WA_BAPE_VBAP-POSNR = WA_ITEM-POSNR.
WA_BAPE_VBAP-ZMPXX = WA_ZMPPZB-ZMPXX.
WA_BAPE_VBAP-ZMPGG = WA_ZMPPZB-ZMPGG.
WA_BAPE_VBAP-ZMPDX = WA_ZMPPZB-ZMPDX.
WA_BAPE_VBAP-ZMPBM = WA_ZMPPZB-ZMPBM.
MOVE 'BAPE_VBAP' TO WA_EXTENSIONIN-STRUCTURE.
CALL METHOD CL_ABAP_CONTAINER_UTILITIES=>FILL_CONTAINER_C
EXPORTING
IM_VALUE = WA_BAPE_VBAP
IMPORTING
EX_CONTAINER = WA_EXTENSIONIN-VALUEPART1.
APPEND WA_EXTENSIONIN TO ORDER_EXTENSIONIN.
CLEAR WA_EXTENSIONIN.
WA_BAPE_VBAPX-POSNR = WA_ITEM-POSNR.
WA_BAPE_VBAPX-ZMPXX = ABAP_TRUE.
WA_BAPE_VBAPX-ZMPGG = ABAP_TRUE.
WA_BAPE_VBAPX-ZMPDX = ABAP_TRUE.
WA_BAPE_VBAPX-ZMPBM = ABAP_TRUE.
MOVE 'BAPE_VBAPX' TO WA_EXTENSIONIN-STRUCTURE.
MOVE WA_BAPE_VBAPX TO WA_EXTENSIONIN-VALUEPART1.
APPEND WA_EXTENSIONIN TO ORDER_EXTENSIONIN.
ELSE.
RESULTCODE = 'E'.
RESULTMSG = '铭牌选项未维护,请联系SD顾问'.
O_VBELN = ''.
RETURN.
ENDIF.
"保存业务数据
WA_DATA-SEQNR = GS_LOGS-SEQNR.
WA_DATA-ZINDEX = WA_ITEM-POSNR.
MOVE-CORRESPONDING I_HEAD TO WA_DATA.
MOVE-CORRESPONDING WA_ITEM TO WA_DATA.
WA_DATA-ZDATE = SY-DATUM.
WA_DATA-ZTIME = SY-UZEIT.
WA_DATA-ZUSER = SY-UNAME.
APPEND WA_DATA TO GT_DATA.
CLEAR WA_DATA.
WA_ITEM-MATNR = |{ WA_ITEM-MATNR ALPHA = IN }|.
WA_ORDER_ITEMS_IN-ITM_NUMBER = WA_ITEM-POSNR. "⾏项⽬
WA_ORDER_ITEMS_IN-MATERIAL = WA_ITEM-MATNR. "物料编号
WA_ORDER_ITEMS_IN-TARGET_QTY = WA_ITEM-DZMENG. "⽬标数量(采⽤销售单位)
WA_ORDER_ITEMS_IN-TARGET_QU = WA_ITEM-VRKME. "数量单位
WA_ORDER_ITEMS_IN-SALES_UNIT = WA_ITEM-VRKME. "销售单位
WA_ORDER_ITEMS_IN-INCOTERMS2L = I_HEAD-INCO2. "国际贸易条款位置 1
WA_ORDER_ITEMS_IN-PLANT = '1100'. "⼯⼚
* WA_ORDER_ITEMS_IN-STORE_LOC = WA_ITEM-LGORT. "库存地
* WA_ORDER_ITEMS_IN-BATCH = WA_ITEM-CHARG. "批次
WA_ORDER_ITEMS_INX-ITM_NUMBER = ABAP_TRUE.
WA_ORDER_ITEMS_INX-MATERIAL = ABAP_TRUE.
WA_ORDER_ITEMS_INX-TARGET_QTY = ABAP_TRUE.
WA_ORDER_ITEMS_INX-TARGET_QU = ABAP_TRUE.
WA_ORDER_ITEMS_INX-SALES_UNIT = ABAP_TRUE.
WA_ORDER_ITEMS_INX-INCOTERMS2L = ABAP_TRUE.
WA_ORDER_ITEMS_INX-PLANT = ABAP_TRUE.
APPEND WA_ORDER_ITEMS_IN TO ORDER_ITEMS_IN.
APPEND WA_ORDER_ITEMS_INX TO ORDER_ITEMS_INX.
"SCHEDULE Line
CLEAR WA_ORDER_SCHEDULES_IN.
WA_ORDER_SCHEDULES_IN-ITM_NUMBER = WA_ITEM-POSNR. "⾏项⽬
WA_ORDER_SCHEDULES_IN-REQ_QTY = WA_ITEM-DZMENG. "数量
APPEND WA_ORDER_SCHEDULES_IN TO ORDER_SCHEDULES_IN.
CLEAR WA_ORDER_SCHEDULES_INX.
WA_ORDER_SCHEDULES_INX-ITM_NUMBER = ABAP_TRUE.
WA_ORDER_SCHEDULES_INX-REQ_QTY = ABAP_TRUE.
APPEND WA_ORDER_SCHEDULES_INX TO ORDER_SCHEDULES_INX.
"SO Condition Item
CLEAR: WA_ORDER_CONDITIONS_IN,WA_ORDER_CONDITIONS_INX.
WA_ORDER_CONDITIONS_IN-ITM_NUMBER = WA_ITEM-POSNR. "⾏项⽬
WA_ORDER_CONDITIONS_IN-COND_TYPE = 'ZR01'. "含税价格
WA_ORDER_CONDITIONS_IN-CURRENCY = I_HEAD-WAERK. "货币码
WA_ORDER_CONDITIONS_IN-COND_VALUE = WA_ITEM-KBETR. "含税单价
* WA_ORDER_CONDITIONS_IN-cond_unit = ''. "条件单位
* WA_ORDER_CONDITIONS_IN-cond_p_unt = ''. "条件定价单位
WA_ORDER_CONDITIONS_INX-ITM_NUMBER = WA_ITEM-POSNR.
WA_ORDER_CONDITIONS_INX-COND_TYPE = 'ZR01'.
WA_ORDER_CONDITIONS_INX-CURRENCY = ABAP_TRUE.
WA_ORDER_CONDITIONS_INX-COND_VALUE = ABAP_TRUE.
APPEND WA_ORDER_CONDITIONS_IN TO ORDER_CONDITIONS_IN.
APPEND WA_ORDER_CONDITIONS_INX TO ORDER_CONDITIONS_INX.
CLEAR: WA_ORDER_CONDITIONS_IN,WA_ORDER_CONDITIONS_INX.
WA_ORDER_CONDITIONS_IN-ITM_NUMBER = WA_ITEM-POSNR. "⾏项⽬
WA_ORDER_CONDITIONS_IN-COND_TYPE = 'ZK07'. "折扣
WA_ORDER_CONDITIONS_IN-CURRENCY = ''. "货币码/% space = %
WA_ORDER_CONDITIONS_IN-COND_VALUE = WA_ITEM-ZPERCNT."折扣⽐例 WA_ORDER_CONDITIONS_INX-ITM_NUMBER = WA_ITEM-POSNR.
WA_ORDER_CONDITIONS_INX-COND_TYPE = 'ZK07'.
WA_ORDER_CONDITIONS_INX-CURRENCY = ABAP_TRUE.
WA_ORDER_CONDITIONS_INX-COND_VALUE = ABAP_TRUE.
APPEND WA_ORDER_CONDITIONS_IN TO ORDER_CONDITIONS_IN.
APPEND WA_ORDER_CONDITIONS_INX TO ORDER_CONDITIONS_INX.
CLEAR: WA_ORDER_CONDITIONS_IN,WA_ORDER_CONDITIONS_INX.
WA_ORDER_CONDITIONS_IN-ITM_NUMBER = WA_ITEM-POSNR. "⾏项⽬
WA_ORDER_CONDITIONS_IN-COND_TYPE = 'ZDPG'. "价格体系
WA_ORDER_CONDITIONS_IN-CURRENCY = ''. "货币码/% space = %
WA_ORDER_CONDITIONS_IN-COND_VALUE = WA_ITEM-ZZDPG. "客户折扣⽐例 WA_ORDER_CONDITIONS_INX-ITM_NUMBER = WA_ITEM-POSNR.
WA_ORDER_CONDITIONS_INX-COND_TYPE = 'ZDPG'.
WA_ORDER_CONDITIONS_INX-CURRENCY = ABAP_TRUE.
WA_ORDER_CONDITIONS_INX-COND_VALUE = ABAP_TRUE.
APPEND WA_ORDER_CONDITIONS_IN TO ORDER_CONDITIONS_IN.
APPEND WA_ORDER_CONDITIONS_INX TO ORDER_CONDITIONS_INX.
"SO Item Text
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = WA_ITEM-ZTEXTBD. "物料基本描述
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
WA_ORDER_TEXT-ITM_NUMBER = WA_ITEM-POSNR. "⽂本⾏项⽬
WA_ORDER_TEXT-TEXT_ID = 'GRUN'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = WA_ITEM-ZTEXTAB. "⾏参数特性要求
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
WA_ORDER_TEXT-ITM_NUMBER = WA_ITEM-POSNR. "⽂本⾏项⽬
WA_ORDER_TEXT-TEXT_ID = 'Z001'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = WA_ITEM-ZTEXTAD. "⾏送货地址
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
WA_ORDER_TEXT-ITM_NUMBER = WA_ITEM-POSNR. "⽂本⾏项⽬ WA_ORDER_TEXT-TEXT_ID = 'Z002'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = WA_ITEM-ZTEXTW. "受托加⼯备注
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
WA_ORDER_TEXT-ITM_NUMBER = WA_ITEM-POSNR. "⽂本⾏项⽬ WA_ORDER_TEXT-TEXT_ID = 'Z003'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
CLEAR: WA_ORDER_TEXT,GV_LENGTH,GT_STRING,WA_STRING.
LV_STRING = WA_ITEM-ZTEXTX. "⾏维修信息备注
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_STRING
IMPORTING
LENGTH = LV_LENGTH
TABLES
FTEXT_TAB = LT_STRING. "132长度切割⼀次
LOOP AT LT_STRING INTO WA_STRING.
CLEAR WA_ORDER_TEXT.
WA_ORDER_TEXT-ITM_NUMBER = WA_ITEM-POSNR. "⽂本⾏项⽬ WA_ORDER_TEXT-TEXT_ID = 'Z007'. "⽂本ID
WA_ORDER_TEXT-LANGU = 1. "语⾔代码1->中⽂
WA_ORDER_TEXT-LANGU_ISO = 'ZH'. "语⾔ISO代码->zh
IF SY-TABIX = 1.
WA_ORDER_TEXT-FORMAT_COL = '*'. "标记列
ELSE.
WA_ORDER_TEXT-FORMAT_COL = '/'.
ENDIF.
WA_ORDER_TEXT-TEXT_LINE = WA_STRING.
APPEND WA_ORDER_TEXT TO ORDER_TEXT.
ENDLOOP.
CLEAR WA_ITEM.
ENDLOOP.
CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
EXPORTINGa
SALES_HEADER_IN = WA_ORDER_HEADER_IN
SALES_HEADER_INX = WA_ORDER_HEADER_INX
CONVERT_PARVW_AUART = ABAP_ON
LOGIC_SWITCH = WA_LOGIC_SWITCH
IMPORTING
SALESDOCUMENT_EX = GV_SALESDOCUMENT_EX
TABLES
RETURN = RETURN
SALES_ITEMS_IN = ORDER_ITEMS_IN
SALES_ITEMS_INX = ORDER_ITEMS_INX
SALES_SCHEDULES_IN = ORDER_SCHEDULES_IN
SALES_SCHEDULES_INX = ORDER_SCHEDULES_INX
SALES_CONDITIONS_IN = ORDER_CONDITIONS_IN
SALES_CONDITIONS_INX = ORDER_CONDITIONS_INX
EXTENSIONIN = ORDER_EXTENSIONIN
SALES_PARTNERS = ORDER_PARTNERS
SALES_TEXT = ORDER_TEXT.
IF SY-SUBRC = 0 AND GV_SALESDOCUMENT_EX IS NOT INITIAL.
RESULTCODE = 'S'.
RESULTMSG = '销售订单创建成功'.
O_VBELN = GV_SALESDOCUMENT_EX.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
ELSE.
SELECT SINGLE *
FROM @RETURN AS A
WHERE TYPE = 'E'
INTO @WA_RETURN.
RESULTCODE = 'E'.
RESULTMSG = |[MESSAGE NO.{ WA_RETURN-ID }{ WA_RETURN-NUMBER }]{ WA_RETURN-MESSAGE }|. O_VBELN = GV_SALESDOCUMENT_EX.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.
* 添加⽇志表记录
GS_LOGS-SEQNR = GS_LOGS-SEQNR.
GS_LOGS-ZIFNAME = 'ZSD_IF0012'.
GS_LOGS-ZSYSTEM = 'CRM/SAP'.
* 1代表调⽤其他系统接⼝,2代表被其他系统调⽤
GS_LOGS-ZCALL = '2'.
GS_LOGS-ZTYPE = RESULTCODE.
GS_LOGS-ZMSG = RESULTMSG.
GS_LOGS-ZDATE = SY-DATUM.
GS_LOGS-ZTIME = SY-UZEIT.
GS_LOGS-ZUSER = SY-UNAME.
GS_LOGS-ZVALUE1 = 'CRM->SAP 销售订单创建接⼝'.
GS_LOGS-ZVALUE2 = O_VBELN. " 销售订单号
* 对应业务数据表
GS_LOGS-ZTBNAME = 'ZSDT_CRM_IF012'.
MODIFY ZALL_LOGS FROM GS_LOGS.
MODIFY ZSDT_CRM_IF012 FROM TABLE GT_DATA.
ENDFUNCTION.。