ABAP: BAPI_SALESORDER_CREATEFROMDAT2 创建销售订单

问题:第三方系统提供一些参数,sap通过BAPI_SALESORDER_CREATEFROMDAT2 自动创建销售订单。

DATA: ls_bapisdhd   TYPE bapisdhd1,
      ls_bapisdhdx  TYPE bapisdhd1x,
      lt_bapiparnr  TYPE TABLE OF bapiparnr WITH HEADER LINE,
      lt_bapisditm  TYPE TABLE OF bapisditm WITH HEADER LINE,
      lt_bapisditmx TYPE TABLE OF bapisditmx WITH HEADER LINE,
      lt_bapischdl  TYPE TABLE OF bapischdl WITH HEADER LINE,
      lt_bapischdlx TYPE TABLE OF bapischdlx WITH HEADER LINE,
      lt_bapicond   TYPE TABLE OF bapicond WITH HEADER LINE,
      lt_bapicondx  TYPE TABLE OF bapicondx WITH HEADER LINE,
      lt_return     TYPE TABLE OF bapiret2 WITH HEADER LINE,
      ls_return     TYPE bapiret2,
      lt_so         TYPE TABLE OF ztsd_so WITH HEADER LINE,

      lt_item_txt   TYPE TABLE OF bapisdtext WITH HEADER LINE.
DATA:         lt_order TYPE TABLE OF ztsd_dms_order WITH HEADER LINE.
DATA: c_werks TYPE werks_d,
      l_kunag TYPE kunnr.
DATA: l_system(10),
      l_vkorg       TYPE vkorg,
      l_kunnr       TYPE kunnr,
      l_kunnr_sp    TYPE kunnr, "特殊客户
      l_kunwe       TYPE kunnr, "
      l_meins       TYPE meins,
      l_matnr       TYPE matnr,
      l_lgort       TYPE lgort_d,
      l_temp(10),
      l_created,
      l_edatu(8),
      l_line(2),
      l_menge(2),
      l_vbeln       TYPE vbeln,
      l_kbetr       TYPE bapikbetr1,
      l_kbetr_l(50) TYPE c,
      l_kbetr_s(50) TYPE c,
      l_kbetr_p(16) TYPE p DECIMALS 9,
      l_kbetr1      TYPE bapikbetr1,
      vkorg_v       TYPE vkorg.

"假设下面是第三方系统传来的参数
l_kunag = '100001'.     "售达方
l_edatu = '20230527'.   "计划行日期
l_system = 'DMS'.       "系统类型
l_matnr = '20000001'.   "物料编号
l_line = 10.            "行号
l_vbeln = '1001450000'. "采购订单编号
l_kunwe = '100001'.     "客户编号
l_lgort = '2102'.       "库存地点
l_menge = '5'.          "订单数量
l_kbetr1 = '120'.       "价格
l_kbetr = '10'.         "价格
c_werks = '1000'.       "工厂

CLEAR: lt_bapisditm,lt_bapisditmx.

lt_bapisditm-itm_number = l_line.   "销售凭证项目
lt_bapisditm-plant = c_werks.       "工厂
lt_bapisditmx-itm_number = l_line.  "销售凭证项目
lt_bapisditmx-updateflag = 'I'.
lt_bapisditmx-plant = 'X'.
lt_bapiparnr-partn_role = 'WE'.     "合作伙伴功能


CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input  = l_kunwe
  IMPORTING
    output = lt_bapiparnr-partn_numb.

SELECT SINGLE kunnr INTO   lt_bapiparnr-partn_numb  "客户编号
FROM kna1
WHERE kunnr = lt_bapiparnr-partn_numb.
APPEND lt_bapiparnr.

"
SELECT SINGLE a~vkorg a~vtweg a~spart
INTO (ls_bapisdhd-sales_org,ls_bapisdhd-distr_chan,ls_bapisdhd-division)
FROM knvv AS a
WHERE a~kunnr = lt_bapiparnr-partn_numb
AND a~vtweg = '10'
AND a~loevm <> 'X'
AND a~aufsd <> 'X'
AND a~erdat = ( SELECT MAX( k~erdat ) FROM knvv AS k WHERE k~kunnr = a~kunnr
AND k~vtweg = a~vtweg AND k~loevm = a~loevm AND k~aufsd = a~aufsd )
AND EXISTS ( SELECT * FROM tvkwz AS b WHERE b~vkorg EQ a~vkorg AND b~werks EQ c_werks ).
ls_bapisdhdx-sales_org = 'X'. "销售组织
ls_bapisdhdx-distr_chan = 'X'."分销渠道
ls_bapisdhdx-division = 'X'.  "产品组

ls_bapisdhd-doc_type = 'ZXS1'.      "销售凭证类型 
ls_bapisdhd-purch_no_c = l_vbeln.   "客户采购订单编号


ls_bapisdhd-created_by = 'TEST00001'. "创建对象的人员名称
ls_bapisdhd-ord_reason = ''.        "订购原因( 业务原因 )
ls_bapisdhdx-ord_reason = 'X'.


lt_bapischdl-itm_number = l_line. "销售凭证项目
lt_bapischdlx-itm_number = l_line. "
lt_bapischdl-sched_line = 1. "计划行
lt_bapischdlx-sched_line = 1. "
lt_bapischdlx-updateflag = 'I'.
lt_bapischdl-req_date = l_edatu. "计划行日期
lt_bapischdlx-req_date = 'X'.


CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
  EXPORTING
    input        = l_matnr
  IMPORTING
    output       = lt_bapisditm-material
  EXCEPTIONS
    length_error = 1
    OTHERS       = 2.
IF sy-subrc <> 0.

ENDIF.
*lt_order-matnr = lt_bapisditm-material.
lt_bapisditm-store_loc = l_lgort.  "库存地点
lt_bapisditmx-store_loc = 'X'.
lt_bapisditmx-material = 'X'.
lt_bapisditm-target_qty = l_menge. "销售单位目标数量
lt_bapisditmx-target_qty = 'X'.
lt_bapischdl-req_qty = l_menge.     "以销售单位计的订单数量
lt_bapischdlx-req_qty = 'X'.

IF ls_bapisdhd-doc_type = 'ZTH'. "退货订单类型
  lt_bapisditm-item_categ = 'KAN'.
  lt_bapisditmx-item_categ = 'X'.

ENDIF.

"销售单位目标数量 and 销售单位转换成基本单位的因子 ( 目标数量 )
SELECT SINGLE umrez umren INTO (lt_bapisditm-trg_qty_no,lt_bapisditm-trgqty_den)
FROM marm
WHERE matnr = lt_bapisditm-material
AND meinh = 'EA'.

lt_bapisditmx-trg_qty_no = 'X'.
lt_bapisditmx-trgqty_den = 'X'.

APPEND: lt_bapisditm,
lt_bapisditmx,
lt_bapischdl,
lt_bapischdlx.


lt_bapicond-cond_value = l_kbetr1. "定价比率
lt_bapicondx-cond_value = 'X'.
lt_bapicond-cond_type = 'PR00'.    "条件类型
lt_bapicondx-cond_type = 'PR00'.
lt_bapicond-itm_number = l_line.  "条件项目号
lt_bapicondx-itm_number = l_line.

*lt_bapicond-cond_value = l_kbetr.
lt_bapicond-cond_p_unt = l_menge.


*lt_bapicondx-cond_value = 'X'.
lt_bapicondx-cond_p_unt = 'X'.
*lt_bapicond-cond_type = 'PRZ0'.
*lt_bapicondx-cond_type = 'PRZ0'.

*lt_bapicond-cond_st_no = '001'.
*lt_bapicondx-cond_st_no = '001'.
lt_bapicond-cond_count = '01'. "条件计数器
lt_bapicondx-cond_count = '01'.
lt_bapicondx-updateflag = 'U'.
lt_bapicond-currency = 'CNY'.  "货币码
lt_bapicondx-currency = 'X'.  "货币码
lt_bapicond-cond_p_unt = 1.  "条件定价单位
lt_bapicondx-cond_p_unt = 'X'.
lt_bapicond-cond_unit = lt_bapisditm-target_qu. "条件单位
lt_bapicondx-cond_unit = 'X'.
APPEND lt_bapicond.
APPEND lt_bapicondx.

lt_item_txt-doc_number = l_vbeln.  "销售凭证
lt_item_txt-text_id = '0001'.      "文本 ID
lt_item_txt-langu = sy-langu.      "语言代码
lt_item_txt-format_col = '01'.     "标记列
lt_item_txt-text_line = '测试抬头文本1'.  "销售订单抬头文本
APPEND lt_item_txt.

lt_item_txt-itm_number = l_line.
lt_item_txt-text_id = '0001'.
lt_item_txt-langu = sy-langu.
lt_item_txt-format_col = '01'.
lt_item_txt-text_line = '测试文本1'.  "销售订单行项目文本
APPEND lt_item_txt.


  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
    EXPORTING
      order_header_in      = ls_bapisdhd
      order_header_inx     = ls_bapisdhdx
*     logic_switch         = wa_bapisdls
    IMPORTING
      salesdocument        = l_vbeln
    TABLES
      return               = lt_return
      order_items_in       = lt_bapisditm
      order_items_inx      = lt_bapisditmx
      order_partners       = lt_bapiparnr
      order_schedules_in   = lt_bapischdl
      order_schedules_inx  = lt_bapischdlx
      order_conditions_in  = lt_bapicond
      order_conditions_inx = lt_bapicondx
      order_text           = lt_item_txt.


READ TABLE lt_return TRANSPORTING NO FIELDS WITH KEY type = 'E'.
IF sy-subrc EQ 0 OR l_vbeln IS INITIAL.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
ELSE.
  CLEAR ls_return.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait   = 'X'
    IMPORTING
      return = ls_return.
  APPEND ls_return TO lt_return.
  IF ls_return-type EQ 'E'.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
  ENDIF.
ENDIF.

CLEAR ls_return.

自动写入的抬头文本示例:

ABAP: BAPI_SALESORDER_CREATEFROMDAT2 创建销售订单_第1张图片

 

你可能感兴趣的:(SAP学习,SAP,ABAP)